![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Test 1-wire , working wtih parasite power and few sensors with mixed power supply.
main.cpp@0:1197076b78f4, 2010-05-10 (annotated)
- Committer:
- macraj
- Date:
- Mon May 10 08:14:32 2010 +0000
- Revision:
- 0:1197076b78f4
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
macraj | 0:1197076b78f4 | 1 | #include "mbed.h" |
macraj | 0:1197076b78f4 | 2 | #include "onewire.h" |
macraj | 0:1197076b78f4 | 3 | |
macraj | 0:1197076b78f4 | 4 | |
macraj | 0:1197076b78f4 | 5 | |
macraj | 0:1197076b78f4 | 6 | BYTE gSensorIDs[MAXSENSORS][OW_ROMCODE_SIZE]; |
macraj | 0:1197076b78f4 | 7 | |
macraj | 0:1197076b78f4 | 8 | Serial pc(USBTX, USBRX); // tx, rx |
macraj | 0:1197076b78f4 | 9 | |
macraj | 0:1197076b78f4 | 10 | BYTE search_sensors(void) { |
macraj | 0:1197076b78f4 | 11 | BYTE i; |
macraj | 0:1197076b78f4 | 12 | BYTE id[OW_ROMCODE_SIZE]; |
macraj | 0:1197076b78f4 | 13 | BYTE diff, nSensors; |
macraj | 0:1197076b78f4 | 14 | |
macraj | 0:1197076b78f4 | 15 | printf( "Scanning Bus for DS18X20\n" ); |
macraj | 0:1197076b78f4 | 16 | |
macraj | 0:1197076b78f4 | 17 | nSensors = 0; |
macraj | 0:1197076b78f4 | 18 | |
macraj | 0:1197076b78f4 | 19 | for ( diff = OW_SEARCH_FIRST; |
macraj | 0:1197076b78f4 | 20 | diff != OW_LAST_DEVICE && nSensors < MAXSENSORS ; ) { |
macraj | 0:1197076b78f4 | 21 | DS18X20_find_sensor( &diff, &id[0] ); |
macraj | 0:1197076b78f4 | 22 | |
macraj | 0:1197076b78f4 | 23 | if ( diff == OW_PRESENCE_ERR ) { |
macraj | 0:1197076b78f4 | 24 | printf( "No Sensor found\n" ); |
macraj | 0:1197076b78f4 | 25 | break; |
macraj | 0:1197076b78f4 | 26 | } |
macraj | 0:1197076b78f4 | 27 | |
macraj | 0:1197076b78f4 | 28 | if ( diff == OW_DATA_ERR ) { |
macraj | 0:1197076b78f4 | 29 | printf( "Bus Error\n" ); |
macraj | 0:1197076b78f4 | 30 | break; |
macraj | 0:1197076b78f4 | 31 | } |
macraj | 0:1197076b78f4 | 32 | |
macraj | 0:1197076b78f4 | 33 | for (i=0;i<OW_ROMCODE_SIZE;i++) |
macraj | 0:1197076b78f4 | 34 | gSensorIDs[nSensors][i]=id[i]; |
macraj | 0:1197076b78f4 | 35 | |
macraj | 0:1197076b78f4 | 36 | nSensors++; |
macraj | 0:1197076b78f4 | 37 | } |
macraj | 0:1197076b78f4 | 38 | |
macraj | 0:1197076b78f4 | 39 | return nSensors; |
macraj | 0:1197076b78f4 | 40 | } |
macraj | 0:1197076b78f4 | 41 | |
macraj | 0:1197076b78f4 | 42 | void uart_put_temp(const uint8_t subzero, uint8_t cel, uint8_t cel_frac_bits) { |
macraj | 0:1197076b78f4 | 43 | // uint8_t buffer[sizeof(int)*8+1]; |
macraj | 0:1197076b78f4 | 44 | uint16_t decicelsius; |
macraj | 0:1197076b78f4 | 45 | |
macraj | 0:1197076b78f4 | 46 | float temperature; |
macraj | 0:1197076b78f4 | 47 | pc.printf("[%s", (subzero)?"-":"+"); |
macraj | 0:1197076b78f4 | 48 | // pc.printf("%d.",cel); |
macraj | 0:1197076b78f4 | 49 | //itoa((cel_frac_bits*DS18X20_FRACCONV),buffer,10); |
macraj | 0:1197076b78f4 | 50 | //sprintf(buffer, "%d",(cel_frac_bits*DS18X20_FRACCONV)); |
macraj | 0:1197076b78f4 | 51 | //j=4-strlen(buffer); |
macraj | 0:1197076b78f4 | 52 | //for (i=0;i<j;i++) pc.printf("0"); |
macraj | 0:1197076b78f4 | 53 | //pc.printf("%s °C [",buffer); |
macraj | 0:1197076b78f4 | 54 | // pc.printf(" freak %d ::", (cel_frac_bits*DS18X20_FRACCONV)); |
macraj | 0:1197076b78f4 | 55 | // "rounding" |
macraj | 0:1197076b78f4 | 56 | //pc.printf((subzero)?'-':'+'); |
macraj | 0:1197076b78f4 | 57 | decicelsius = DS18X20_temp_to_decicel(subzero, cel, cel_frac_bits); |
macraj | 0:1197076b78f4 | 58 | temperature = decicelsius; |
macraj | 0:1197076b78f4 | 59 | temperature = temperature/10; |
macraj | 0:1197076b78f4 | 60 | pc.printf("%4.1f C]\n", temperature); |
macraj | 0:1197076b78f4 | 61 | |
macraj | 0:1197076b78f4 | 62 | //pc.printf( "%d 0°C]\n",decicelsius); |
macraj | 0:1197076b78f4 | 63 | |
macraj | 0:1197076b78f4 | 64 | } |
macraj | 0:1197076b78f4 | 65 | |
macraj | 0:1197076b78f4 | 66 | |
macraj | 0:1197076b78f4 | 67 | |
macraj | 0:1197076b78f4 | 68 | |
macraj | 0:1197076b78f4 | 69 | int main(void) { |
macraj | 0:1197076b78f4 | 70 | |
macraj | 0:1197076b78f4 | 71 | pc.printf("Qantum 1 wire interface test!\n"); |
macraj | 0:1197076b78f4 | 72 | uint8_t nSensors, i; |
macraj | 0:1197076b78f4 | 73 | uint8_t subzero, cel, cel_frac_bits; |
macraj | 0:1197076b78f4 | 74 | |
macraj | 0:1197076b78f4 | 75 | nSensors = search_sensors(); |
macraj | 0:1197076b78f4 | 76 | |
macraj | 0:1197076b78f4 | 77 | ow_command( DS18X20_CONVERT_T, 0); |
macraj | 0:1197076b78f4 | 78 | |
macraj | 0:1197076b78f4 | 79 | pc.printf("Sensors found : %d\n", nSensors); |
macraj | 0:1197076b78f4 | 80 | if (nSensors > 0) { |
macraj | 0:1197076b78f4 | 81 | for (i=0; i<nSensors; i++) { |
macraj | 0:1197076b78f4 | 82 | pc.printf("Sensor # %d is a ", i+1); |
macraj | 0:1197076b78f4 | 83 | if ( gSensorIDs[i][0] == DS18S20_ID) { |
macraj | 0:1197076b78f4 | 84 | pc.printf("DS18S20/DS1820 "); |
macraj | 0:1197076b78f4 | 85 | } else pc.printf("DS18B20 "); |
macraj | 0:1197076b78f4 | 86 | if ( DS18X20_get_power_status( &gSensorIDs[i][0] ) == |
macraj | 0:1197076b78f4 | 87 | DS18X20_POWER_PARASITE ) |
macraj | 0:1197076b78f4 | 88 | pc.printf( "parasite" ); |
macraj | 0:1197076b78f4 | 89 | else pc.printf( "externally" ); |
macraj | 0:1197076b78f4 | 90 | pc.printf( " powered\n" ); |
macraj | 0:1197076b78f4 | 91 | DS18X20_show_id_uart( &gSensorIDs[i][0], OW_ROMCODE_SIZE ); |
macraj | 0:1197076b78f4 | 92 | } |
macraj | 0:1197076b78f4 | 93 | } |
macraj | 0:1197076b78f4 | 94 | |
macraj | 0:1197076b78f4 | 95 | pc.printf( "Convert_T and Read Sensor by Sensor (reverse order)\n" ); |
macraj | 0:1197076b78f4 | 96 | for ( i=nSensors; i>0; i-- ) { |
macraj | 0:1197076b78f4 | 97 | if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, &gSensorIDs[i-1][0] ) == DS18X20_OK ) { |
macraj | 0:1197076b78f4 | 98 | wait_ms(DS18B20_TCONV_12BIT); |
macraj | 0:1197076b78f4 | 99 | pc.printf("Sensor# %d = ", i); |
macraj | 0:1197076b78f4 | 100 | if ( DS18X20_read_meas( &gSensorIDs[i-1][0], &subzero, &cel, &cel_frac_bits) == DS18X20_OK ) { |
macraj | 0:1197076b78f4 | 101 | uart_put_temp(subzero, cel, cel_frac_bits); |
macraj | 0:1197076b78f4 | 102 | } else pc.printf("CRC Error (lost connection?)\n"); |
macraj | 0:1197076b78f4 | 103 | |
macraj | 0:1197076b78f4 | 104 | } else pc.printf("Start meas. failed (short circuit?)\n"); |
macraj | 0:1197076b78f4 | 105 | } |
macraj | 0:1197076b78f4 | 106 | |
macraj | 0:1197076b78f4 | 107 | while (1) { |
macraj | 0:1197076b78f4 | 108 | for ( i=nSensors; i>0; i-- ) { |
macraj | 0:1197076b78f4 | 109 | // if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, &gSensorIDs[i-1][0] ) == DS18X20_OK ) { |
macraj | 0:1197076b78f4 | 110 | pc.printf("Sensor# %d = ", i); |
macraj | 0:1197076b78f4 | 111 | if ( DS18X20_read_meas( &gSensorIDs[i-1][0], &subzero, &cel, &cel_frac_bits) == DS18X20_OK ) { |
macraj | 0:1197076b78f4 | 112 | uart_put_temp(subzero, cel, cel_frac_bits); |
macraj | 0:1197076b78f4 | 113 | } else pc.printf("CRC Error (lost connection?)\n"); |
macraj | 0:1197076b78f4 | 114 | |
macraj | 0:1197076b78f4 | 115 | //if ( DS18X20_start_meas( DS18X20_POWER_PARASITE, &gSensorIDs[i-1][0] ) == DS18X20_OK ) { |
macraj | 0:1197076b78f4 | 116 | //} else pc.printf("Start meas. failed (short circuit?)\n"); |
macraj | 0:1197076b78f4 | 117 | } |
macraj | 0:1197076b78f4 | 118 | ow_command( DS18X20_CONVERT_T, 0); |
macraj | 0:1197076b78f4 | 119 | ow_parasite_enable(); |
macraj | 0:1197076b78f4 | 120 | |
macraj | 0:1197076b78f4 | 121 | |
macraj | 0:1197076b78f4 | 122 | // ow_reset(); |
macraj | 0:1197076b78f4 | 123 | // ow_command( DS18X20_CONVERT_T, 0); |
macraj | 0:1197076b78f4 | 124 | // ow_parasite_enable(); |
macraj | 0:1197076b78f4 | 125 | wait(10); |
macraj | 0:1197076b78f4 | 126 | // ow_parasite_disable(); |
macraj | 0:1197076b78f4 | 127 | |
macraj | 0:1197076b78f4 | 128 | } |
macraj | 0:1197076b78f4 | 129 | |
macraj | 0:1197076b78f4 | 130 | } |
macraj | 0:1197076b78f4 | 131 |