MAX31850, DS18B20, DS2450, thermocouple
onewire.cpp@0:5d39f2521173, 2015-03-09 (annotated)
- Committer:
- fblanc
- Date:
- Mon Mar 09 11:55:54 2015 +0000
- Revision:
- 0:5d39f2521173
MAX31850
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
fblanc | 0:5d39f2521173 | 1 | /** |
fblanc | 0:5d39f2521173 | 2 | * @file onewire.c |
fblanc | 0:5d39f2521173 | 3 | * @brief library 1-Wire(www.maxim-ic.com) |
fblanc | 0:5d39f2521173 | 4 | * @author Maciej Rajtar (Published 10 May 2010 www.mbed.org) |
fblanc | 0:5d39f2521173 | 5 | * @author Frederic BLANC (Published 01/03/2012 www.mbed.org) |
fblanc | 0:5d39f2521173 | 6 | */ |
fblanc | 0:5d39f2521173 | 7 | |
fblanc | 0:5d39f2521173 | 8 | #include "mbed.h" |
fblanc | 0:5d39f2521173 | 9 | #include "onewire.h" |
fblanc | 0:5d39f2521173 | 10 | |
fblanc | 0:5d39f2521173 | 11 | DigitalInOut ow_pin(p21); |
fblanc | 0:5d39f2521173 | 12 | DigitalInOut ow_0(p21); |
fblanc | 0:5d39f2521173 | 13 | DigitalInOut ow_1(p22); |
fblanc | 0:5d39f2521173 | 14 | DigitalInOut ow_2(p23); |
fblanc | 0:5d39f2521173 | 15 | DigitalInOut ow_3(p24); |
fblanc | 0:5d39f2521173 | 16 | DigitalInOut* t_ow[4]={&ow_0,&ow_1,&ow_2,&ow_3}; |
fblanc | 0:5d39f2521173 | 17 | |
fblanc | 0:5d39f2521173 | 18 | //********************************************************************************************************** |
fblanc | 0:5d39f2521173 | 19 | //* show_id |
fblanc | 0:5d39f2521173 | 20 | //********************************************************************************************************** |
fblanc | 0:5d39f2521173 | 21 | |
fblanc | 0:5d39f2521173 | 22 | |
fblanc | 0:5d39f2521173 | 23 | /** |
fblanc | 0:5d39f2521173 | 24 | * @brief show_id |
fblanc | 0:5d39f2521173 | 25 | * @param [in] id[] = rom_code |
fblanc | 0:5d39f2521173 | 26 | * @param [out] text id |
fblanc | 0:5d39f2521173 | 27 | * @date 02/12/2013 |
fblanc | 0:5d39f2521173 | 28 | */ |
fblanc | 0:5d39f2521173 | 29 | char* ow_show_id( uint8_t id[],char *text) { |
fblanc | 0:5d39f2521173 | 30 | |
fblanc | 0:5d39f2521173 | 31 | char hex[4]; |
fblanc | 0:5d39f2521173 | 32 | sprintf(text,""); |
fblanc | 0:5d39f2521173 | 33 | sprintf(hex,"%2.2X",id[0]); |
fblanc | 0:5d39f2521173 | 34 | strcat(text,hex); |
fblanc | 0:5d39f2521173 | 35 | for (int i = OW_ROMCODE_SIZE-2; i >=1 ; --i ) { |
fblanc | 0:5d39f2521173 | 36 | sprintf(hex,"%2.2X",id[i]); |
fblanc | 0:5d39f2521173 | 37 | strcat(text,hex); |
fblanc | 0:5d39f2521173 | 38 | } |
fblanc | 0:5d39f2521173 | 39 | sprintf(hex,"%2.2X",id[OW_ROMCODE_SIZE-1]); |
fblanc | 0:5d39f2521173 | 40 | strcat(text,hex); |
fblanc | 0:5d39f2521173 | 41 | return text; |
fblanc | 0:5d39f2521173 | 42 | |
fblanc | 0:5d39f2521173 | 43 | } |
fblanc | 0:5d39f2521173 | 44 | //********************************************************************************************************** |
fblanc | 0:5d39f2521173 | 45 | //* show_id |
fblanc | 0:5d39f2521173 | 46 | //********************************************************************************************************** |
fblanc | 0:5d39f2521173 | 47 | |
fblanc | 0:5d39f2521173 | 48 | |
fblanc | 0:5d39f2521173 | 49 | /** |
fblanc | 0:5d39f2521173 | 50 | * @brief uint64_id |
fblanc | 0:5d39f2521173 | 51 | * @param [in] id[] = rom_code |
fblanc | 0:5d39f2521173 | 52 | * @return [out] uint64_t id |
fblanc | 0:5d39f2521173 | 53 | * @date 28/03/2011 |
fblanc | 0:5d39f2521173 | 54 | */ |
fblanc | 0:5d39f2521173 | 55 | uint64_t uint64_id( uint8_t id[]) { |
fblanc | 0:5d39f2521173 | 56 | |
fblanc | 0:5d39f2521173 | 57 | uint64_t *ptr; |
fblanc | 0:5d39f2521173 | 58 | |
fblanc | 0:5d39f2521173 | 59 | ptr=(uint64_t *) &id[0]; |
fblanc | 0:5d39f2521173 | 60 | |
fblanc | 0:5d39f2521173 | 61 | return *ptr; |
fblanc | 0:5d39f2521173 | 62 | } |
fblanc | 0:5d39f2521173 | 63 | //********************************************************************************************************** |
fblanc | 0:5d39f2521173 | 64 | //* search_sensors |
fblanc | 0:5d39f2521173 | 65 | //********************************************************************************************************** |
fblanc | 0:5d39f2521173 | 66 | |
fblanc | 0:5d39f2521173 | 67 | /** |
fblanc | 0:5d39f2521173 | 68 | * @brief search_sensors |
fblanc | 0:5d39f2521173 | 69 | * @param [out] nSensors number of device onewire |
fblanc | 0:5d39f2521173 | 70 | * @param [out] gSensorIDs[][] array of id romcode |
fblanc | 0:5d39f2521173 | 71 | * @return OW_OK or OW_PRESENCE_ERR or OW_DATA_ERR |
fblanc | 0:5d39f2521173 | 72 | * @date 20/06/2011 |
fblanc | 0:5d39f2521173 | 73 | */ |
fblanc | 0:5d39f2521173 | 74 | uint8_t search_sensors(uint8_t *nSensors,uint8_t gSensorIDs[][OW_ROMCODE_SIZE] ) { |
fblanc | 0:5d39f2521173 | 75 | |
fblanc | 0:5d39f2521173 | 76 | |
fblanc | 0:5d39f2521173 | 77 | uint8_t i; |
fblanc | 0:5d39f2521173 | 78 | uint8_t id[OW_ROMCODE_SIZE]; |
fblanc | 0:5d39f2521173 | 79 | uint8_t diff; |
fblanc | 0:5d39f2521173 | 80 | printf( "Scanning Bus\r\n" ); |
fblanc | 0:5d39f2521173 | 81 | diff = OW_SEARCH_FIRST; |
fblanc | 0:5d39f2521173 | 82 | for (*nSensors = 0 ; (diff != OW_LAST_DEVICE) && (*nSensors < MAXSENSORS) ;++(*nSensors) ) { |
fblanc | 0:5d39f2521173 | 83 | ow_find_sensor( &diff, &id[0] ); |
fblanc | 0:5d39f2521173 | 84 | if ( diff == OW_PRESENCE_ERR ) { |
fblanc | 0:5d39f2521173 | 85 | printf( "No Sensor found\r\n" ); |
fblanc | 0:5d39f2521173 | 86 | return diff; |
fblanc | 0:5d39f2521173 | 87 | } |
fblanc | 0:5d39f2521173 | 88 | if ( diff == OW_DATA_ERR ) { |
fblanc | 0:5d39f2521173 | 89 | printf( "Bus Error\r\n" ); |
fblanc | 0:5d39f2521173 | 90 | return diff; |
fblanc | 0:5d39f2521173 | 91 | } |
fblanc | 0:5d39f2521173 | 92 | for (i=0;i<OW_ROMCODE_SIZE;i++) |
fblanc | 0:5d39f2521173 | 93 | gSensorIDs[*nSensors][i]=id[i]; |
fblanc | 0:5d39f2521173 | 94 | |
fblanc | 0:5d39f2521173 | 95 | } |
fblanc | 0:5d39f2521173 | 96 | return OW_OK; |
fblanc | 0:5d39f2521173 | 97 | } |
fblanc | 0:5d39f2521173 | 98 | /** |
fblanc | 0:5d39f2521173 | 99 | * @brief search_sensors |
fblanc | 0:5d39f2521173 | 100 | * @param [in] n num bus onewire |
fblanc | 0:5d39f2521173 | 101 | * @param [out] nSensors number of device onewire |
fblanc | 0:5d39f2521173 | 102 | * @param [out] gSensorIDs[][][] array of id romcode |
fblanc | 0:5d39f2521173 | 103 | * @return OW_OK or OW_PRESENCE_ERR or OW_DATA_ERR |
fblanc | 0:5d39f2521173 | 104 | * @date 02/09/2011 |
fblanc | 0:5d39f2521173 | 105 | */ |
fblanc | 0:5d39f2521173 | 106 | uint8_t search_sensors(uint8_t n,uint8_t *nSensors,uint8_t gSensorIDs[][MAXSENSORS][OW_ROMCODE_SIZE] ) { |
fblanc | 0:5d39f2521173 | 107 | |
fblanc | 0:5d39f2521173 | 108 | |
fblanc | 0:5d39f2521173 | 109 | uint8_t i; |
fblanc | 0:5d39f2521173 | 110 | uint8_t id[OW_ROMCODE_SIZE]; |
fblanc | 0:5d39f2521173 | 111 | uint8_t diff; |
fblanc | 0:5d39f2521173 | 112 | printf( "Scanning Bus %d\r\n",n ); |
fblanc | 0:5d39f2521173 | 113 | diff = OW_SEARCH_FIRST; |
fblanc | 0:5d39f2521173 | 114 | for (*nSensors = 0 ; (diff != OW_LAST_DEVICE) && (*nSensors < MAXSENSORS) ;++(*nSensors) ) { |
fblanc | 0:5d39f2521173 | 115 | ow_find_sensor(n, &diff, &id[0] ); |
fblanc | 0:5d39f2521173 | 116 | if ( diff == OW_PRESENCE_ERR ) { |
fblanc | 0:5d39f2521173 | 117 | printf( "No Sensor found\r\n" ); |
fblanc | 0:5d39f2521173 | 118 | return diff; |
fblanc | 0:5d39f2521173 | 119 | } |
fblanc | 0:5d39f2521173 | 120 | if ( diff == OW_DATA_ERR ) { |
fblanc | 0:5d39f2521173 | 121 | printf( "Bus Error\r\n" ); |
fblanc | 0:5d39f2521173 | 122 | return diff; |
fblanc | 0:5d39f2521173 | 123 | } |
fblanc | 0:5d39f2521173 | 124 | for (i=0;i<OW_ROMCODE_SIZE;i++){ |
fblanc | 0:5d39f2521173 | 125 | gSensorIDs[n][*nSensors][i]=id[i]; |
fblanc | 0:5d39f2521173 | 126 | //printf( "id[%d]=%d\r\n" ,i,id[i]); |
fblanc | 0:5d39f2521173 | 127 | } |
fblanc | 0:5d39f2521173 | 128 | |
fblanc | 0:5d39f2521173 | 129 | } |
fblanc | 0:5d39f2521173 | 130 | return OW_OK; |
fblanc | 0:5d39f2521173 | 131 | } |
fblanc | 0:5d39f2521173 | 132 | //********************************************************************************************************** |
fblanc | 0:5d39f2521173 | 133 | //* find Sensors on 1-Wire-Bus |
fblanc | 0:5d39f2521173 | 134 | //********************************************************************************************************** |
fblanc | 0:5d39f2521173 | 135 | |
fblanc | 0:5d39f2521173 | 136 | /** |
fblanc | 0:5d39f2521173 | 137 | * @brief find Sensors on 1-Wire-Bus |
fblanc | 0:5d39f2521173 | 138 | * @param [in/out] diff is the result of the last rom-search |
fblanc | 0:5d39f2521173 | 139 | * @param [out] is the rom-code of the sensor found |
fblanc | 0:5d39f2521173 | 140 | * @return OW_OK or OW_ERROR |
fblanc | 0:5d39f2521173 | 141 | * @date 20/06/2011 |
fblanc | 0:5d39f2521173 | 142 | */ |
fblanc | 0:5d39f2521173 | 143 | uint8_t ow_find_sensor(uint8_t *diff, uint8_t id[]) { |
fblanc | 0:5d39f2521173 | 144 | for (;;) |
fblanc | 0:5d39f2521173 | 145 | { |
fblanc | 0:5d39f2521173 | 146 | *diff = ow_rom_search( *diff, &id[0] ); |
fblanc | 0:5d39f2521173 | 147 | if ( *diff==OW_PRESENCE_ERR) |
fblanc | 0:5d39f2521173 | 148 | return OW_ERROR; |
fblanc | 0:5d39f2521173 | 149 | if ( *diff==OW_DATA_ERR ) |
fblanc | 0:5d39f2521173 | 150 | return OW_ERROR; |
fblanc | 0:5d39f2521173 | 151 | if ( *diff == OW_LAST_DEVICE ) |
fblanc | 0:5d39f2521173 | 152 | return OW_OK ; |
fblanc | 0:5d39f2521173 | 153 | if ( id[0] == DS18B20_ID || id[0] == DS18S20_ID ) |
fblanc | 0:5d39f2521173 | 154 | return OW_OK ; |
fblanc | 0:5d39f2521173 | 155 | if ( id[0] == DS2450_ID ) |
fblanc | 0:5d39f2521173 | 156 | return OW_OK ; |
fblanc | 0:5d39f2521173 | 157 | if ( id[0] == MAX31850_ID ) |
fblanc | 0:5d39f2521173 | 158 | return OW_OK ; |
fblanc | 0:5d39f2521173 | 159 | } |
fblanc | 0:5d39f2521173 | 160 | } |
fblanc | 0:5d39f2521173 | 161 | |
fblanc | 0:5d39f2521173 | 162 | /** |
fblanc | 0:5d39f2521173 | 163 | * @brief find Sensors on 1-Wire-Bus |
fblanc | 0:5d39f2521173 | 164 | * @param [in] num bus onewire |
fblanc | 0:5d39f2521173 | 165 | * @param [in/out] diff is the result of the last rom-search |
fblanc | 0:5d39f2521173 | 166 | * @param [out] is the rom-code of the sensor found |
fblanc | 0:5d39f2521173 | 167 | * @return OW_OK or OW_ERROR |
fblanc | 0:5d39f2521173 | 168 | * @date 30/08/2011 |
fblanc | 0:5d39f2521173 | 169 | */ |
fblanc | 0:5d39f2521173 | 170 | uint8_t ow_find_sensor(uint8_t n,uint8_t *diff, uint8_t id[]) { |
fblanc | 0:5d39f2521173 | 171 | for (;;) |
fblanc | 0:5d39f2521173 | 172 | { |
fblanc | 0:5d39f2521173 | 173 | *diff = ow_rom_search(n, *diff, &id[0] ); |
fblanc | 0:5d39f2521173 | 174 | if ( *diff==OW_PRESENCE_ERR) |
fblanc | 0:5d39f2521173 | 175 | return OW_ERROR; |
fblanc | 0:5d39f2521173 | 176 | if ( *diff==OW_DATA_ERR ) |
fblanc | 0:5d39f2521173 | 177 | return OW_ERROR; |
fblanc | 0:5d39f2521173 | 178 | if ( *diff == OW_LAST_DEVICE ) |
fblanc | 0:5d39f2521173 | 179 | return OW_OK ; |
fblanc | 0:5d39f2521173 | 180 | if ( id[0] == DS18B20_ID || id[0] == DS18S20_ID ) |
fblanc | 0:5d39f2521173 | 181 | return OW_OK ; |
fblanc | 0:5d39f2521173 | 182 | if ( id[0] == DS2450_ID ) |
fblanc | 0:5d39f2521173 | 183 | return OW_OK ; |
fblanc | 0:5d39f2521173 | 184 | if ( id[0] == MAX31850_ID ) |
fblanc | 0:5d39f2521173 | 185 | return OW_OK ; |
fblanc | 0:5d39f2521173 | 186 | } |
fblanc | 0:5d39f2521173 | 187 | } |
fblanc | 0:5d39f2521173 | 188 | //********************************************************************************************************** |
fblanc | 0:5d39f2521173 | 189 | //* search romcode |
fblanc | 0:5d39f2521173 | 190 | //********************************************************************************************************** |
fblanc | 0:5d39f2521173 | 191 | |
fblanc | 0:5d39f2521173 | 192 | /** |
fblanc | 0:5d39f2521173 | 193 | * @brief search romcode |
fblanc | 0:5d39f2521173 | 194 | * @param [in] uint8_t diff |
fblanc | 0:5d39f2521173 | 195 | * @param [out] id romcode |
fblanc | 0:5d39f2521173 | 196 | * @return next_diff or OW_LAST_DEVICE or OW_DATA_ERR or OW_PRESENCE_ERR |
fblanc | 0:5d39f2521173 | 197 | * @date 20/06/2011 |
fblanc | 0:5d39f2521173 | 198 | */ |
fblanc | 0:5d39f2521173 | 199 | uint8_t ow_rom_search( uint8_t diff, uint8_t id[] ) { |
fblanc | 0:5d39f2521173 | 200 | uint8_t i, j, next_diff; |
fblanc | 0:5d39f2521173 | 201 | uint8_t b; |
fblanc | 0:5d39f2521173 | 202 | |
fblanc | 0:5d39f2521173 | 203 | if ( ow_reset() ) |
fblanc | 0:5d39f2521173 | 204 | return OW_PRESENCE_ERR; // error, no device found |
fblanc | 0:5d39f2521173 | 205 | ow_byte_wr( OW_SEARCH_ROM ); // ROM search command |
fblanc | 0:5d39f2521173 | 206 | next_diff = OW_LAST_DEVICE; // unchanged on last device |
fblanc | 0:5d39f2521173 | 207 | i = OW_ROMCODE_SIZE * 8; // 8 bytes |
fblanc | 0:5d39f2521173 | 208 | do { |
fblanc | 0:5d39f2521173 | 209 | j = 8; // 8 bits |
fblanc | 0:5d39f2521173 | 210 | do { |
fblanc | 0:5d39f2521173 | 211 | b = ow_bit_io( 1 ); // read bit |
fblanc | 0:5d39f2521173 | 212 | if ( ow_bit_io( 1 ) ) { // read complement bit |
fblanc | 0:5d39f2521173 | 213 | if ( b ) // 11 |
fblanc | 0:5d39f2521173 | 214 | return OW_DATA_ERR; // data error |
fblanc | 0:5d39f2521173 | 215 | } else { |
fblanc | 0:5d39f2521173 | 216 | if ( !b ) { // 00 = 2 devices |
fblanc | 0:5d39f2521173 | 217 | if ( diff > i || ((*id & 1) && diff != i) ) { |
fblanc | 0:5d39f2521173 | 218 | b = 1; // now 1 |
fblanc | 0:5d39f2521173 | 219 | next_diff = i; // next pass 0 |
fblanc | 0:5d39f2521173 | 220 | } |
fblanc | 0:5d39f2521173 | 221 | } |
fblanc | 0:5d39f2521173 | 222 | } |
fblanc | 0:5d39f2521173 | 223 | ow_bit_io( b ); // write bit |
fblanc | 0:5d39f2521173 | 224 | *id >>= 1; |
fblanc | 0:5d39f2521173 | 225 | if ( b ) |
fblanc | 0:5d39f2521173 | 226 | *id |= 0x80; // store bit |
fblanc | 0:5d39f2521173 | 227 | --i; |
fblanc | 0:5d39f2521173 | 228 | } while ( --j ); |
fblanc | 0:5d39f2521173 | 229 | id++; // next byte |
fblanc | 0:5d39f2521173 | 230 | } while ( i ); |
fblanc | 0:5d39f2521173 | 231 | return next_diff; // to continue search |
fblanc | 0:5d39f2521173 | 232 | } |
fblanc | 0:5d39f2521173 | 233 | /** |
fblanc | 0:5d39f2521173 | 234 | * @brief search romcode |
fblanc | 0:5d39f2521173 | 235 | * @param [in]n num bus onewire |
fblanc | 0:5d39f2521173 | 236 | * @param [in] uint8_t diff |
fblanc | 0:5d39f2521173 | 237 | * @param [out] id romcode |
fblanc | 0:5d39f2521173 | 238 | * @return next_diff or OW_LAST_DEVICE or OW_DATA_ERR or OW_PRESENCE_ERR |
fblanc | 0:5d39f2521173 | 239 | * @date 30/08/2011 |
fblanc | 0:5d39f2521173 | 240 | */ |
fblanc | 0:5d39f2521173 | 241 | uint8_t ow_rom_search(uint8_t n, uint8_t diff, uint8_t id[] ) { |
fblanc | 0:5d39f2521173 | 242 | uint8_t i, j, next_diff; |
fblanc | 0:5d39f2521173 | 243 | uint8_t b; |
fblanc | 0:5d39f2521173 | 244 | if ( ow_reset(n) ) |
fblanc | 0:5d39f2521173 | 245 | return OW_PRESENCE_ERR; // error, no device found |
fblanc | 0:5d39f2521173 | 246 | ow_byte_wr(n, OW_SEARCH_ROM ); // ROM search command |
fblanc | 0:5d39f2521173 | 247 | next_diff = OW_LAST_DEVICE; // unchanged on last device |
fblanc | 0:5d39f2521173 | 248 | i = OW_ROMCODE_SIZE * 8; // 8 bytes |
fblanc | 0:5d39f2521173 | 249 | do { |
fblanc | 0:5d39f2521173 | 250 | j = 8; // 8 bits |
fblanc | 0:5d39f2521173 | 251 | do { |
fblanc | 0:5d39f2521173 | 252 | b = ow_bit_io(n, 1 ); // read bit |
fblanc | 0:5d39f2521173 | 253 | if ( ow_bit_io(n, 1 ) ) { // read complement bit |
fblanc | 0:5d39f2521173 | 254 | if ( b ) // 11 |
fblanc | 0:5d39f2521173 | 255 | { |
fblanc | 0:5d39f2521173 | 256 | |
fblanc | 0:5d39f2521173 | 257 | return OW_DATA_ERR; // data error |
fblanc | 0:5d39f2521173 | 258 | } |
fblanc | 0:5d39f2521173 | 259 | } else { |
fblanc | 0:5d39f2521173 | 260 | if ( !b ) { // 00 = 2 devices |
fblanc | 0:5d39f2521173 | 261 | if ( diff > i || ((*id & 1) && diff != i) ) { |
fblanc | 0:5d39f2521173 | 262 | b = 1; // now 1 |
fblanc | 0:5d39f2521173 | 263 | next_diff = i; // next pass 0 |
fblanc | 0:5d39f2521173 | 264 | } |
fblanc | 0:5d39f2521173 | 265 | } |
fblanc | 0:5d39f2521173 | 266 | } |
fblanc | 0:5d39f2521173 | 267 | ow_bit_io(n, b ); // write bit |
fblanc | 0:5d39f2521173 | 268 | |
fblanc | 0:5d39f2521173 | 269 | *id >>= 1; |
fblanc | 0:5d39f2521173 | 270 | if ( b ) |
fblanc | 0:5d39f2521173 | 271 | *id |= 0x80; // store bit |
fblanc | 0:5d39f2521173 | 272 | --i; |
fblanc | 0:5d39f2521173 | 273 | } while ( --j ); |
fblanc | 0:5d39f2521173 | 274 | id++; // next byte |
fblanc | 0:5d39f2521173 | 275 | |
fblanc | 0:5d39f2521173 | 276 | } while ( i ); |
fblanc | 0:5d39f2521173 | 277 | |
fblanc | 0:5d39f2521173 | 278 | |
fblanc | 0:5d39f2521173 | 279 | return next_diff; // to continue search |
fblanc | 0:5d39f2521173 | 280 | } |
fblanc | 0:5d39f2521173 | 281 | |
fblanc | 0:5d39f2521173 | 282 | |
fblanc | 0:5d39f2521173 | 283 | //********************************************************************************************************** |
fblanc | 0:5d39f2521173 | 284 | //* test pin onewire bus |
fblanc | 0:5d39f2521173 | 285 | //********************************************************************************************************** |
fblanc | 0:5d39f2521173 | 286 | /** |
fblanc | 0:5d39f2521173 | 287 | * @brief test pin onewire bus |
fblanc | 0:5d39f2521173 | 288 | * @return etat pin ow |
fblanc | 0:5d39f2521173 | 289 | * @date 20/06/2011 |
fblanc | 0:5d39f2521173 | 290 | */ |
fblanc | 0:5d39f2521173 | 291 | uint8_t ow_test_pin (void){ |
fblanc | 0:5d39f2521173 | 292 | if (ow_pin) |
fblanc | 0:5d39f2521173 | 293 | return 1; |
fblanc | 0:5d39f2521173 | 294 | return 0; |
fblanc | 0:5d39f2521173 | 295 | } |
fblanc | 0:5d39f2521173 | 296 | |
fblanc | 0:5d39f2521173 | 297 | /** |
fblanc | 0:5d39f2521173 | 298 | * @brief test pin onewire bus |
fblanc | 0:5d39f2521173 | 299 | * @param [in] num bus one wire |
fblanc | 0:5d39f2521173 | 300 | * @return etat pin ow |
fblanc | 0:5d39f2521173 | 301 | * @date 30/08/2011 |
fblanc | 0:5d39f2521173 | 302 | */ |
fblanc | 0:5d39f2521173 | 303 | uint8_t ow_test_pin (uint8_t n){ |
fblanc | 0:5d39f2521173 | 304 | |
fblanc | 0:5d39f2521173 | 305 | if (*t_ow[n]) |
fblanc | 0:5d39f2521173 | 306 | return 1; |
fblanc | 0:5d39f2521173 | 307 | return 0; |
fblanc | 0:5d39f2521173 | 308 | } |
fblanc | 0:5d39f2521173 | 309 | |
fblanc | 0:5d39f2521173 | 310 | //********************************************************************************************************** |
fblanc | 0:5d39f2521173 | 311 | //* onewire reset bus |
fblanc | 0:5d39f2521173 | 312 | //********************************************************************************************************** |
fblanc | 0:5d39f2521173 | 313 | /** |
fblanc | 0:5d39f2521173 | 314 | * @brief onewire reset bus |
fblanc | 0:5d39f2521173 | 315 | * @return pin ow or OW_SHORT_CIRCUIT |
fblanc | 0:5d39f2521173 | 316 | * @date 20/06/2011 |
fblanc | 0:5d39f2521173 | 317 | */ |
fblanc | 0:5d39f2521173 | 318 | uint8_t ow_reset(void) { // reset. Should improve to act as a presence pulse |
fblanc | 0:5d39f2521173 | 319 | uint8_t err; |
fblanc | 0:5d39f2521173 | 320 | |
fblanc | 0:5d39f2521173 | 321 | ow_pin.output(); |
fblanc | 0:5d39f2521173 | 322 | ow_pin = 0; // bring low for 500 us |
fblanc | 0:5d39f2521173 | 323 | wait_us(500); |
fblanc | 0:5d39f2521173 | 324 | ow_pin.input(); |
fblanc | 0:5d39f2521173 | 325 | wait_us(60); |
fblanc | 0:5d39f2521173 | 326 | err = ow_pin; |
fblanc | 0:5d39f2521173 | 327 | wait_us(240); |
fblanc | 0:5d39f2521173 | 328 | if ( ow_pin == 0 ) { // short circuit |
fblanc | 0:5d39f2521173 | 329 | err = OW_SHORT_CIRCUIT; |
fblanc | 0:5d39f2521173 | 330 | } |
fblanc | 0:5d39f2521173 | 331 | return err; |
fblanc | 0:5d39f2521173 | 332 | } |
fblanc | 0:5d39f2521173 | 333 | |
fblanc | 0:5d39f2521173 | 334 | /** |
fblanc | 0:5d39f2521173 | 335 | * @brief onewire reset bus |
fblanc | 0:5d39f2521173 | 336 | * @param [in] num bus onewire |
fblanc | 0:5d39f2521173 | 337 | * @return pin ow or OW_SHORT_CIRCUIT |
fblanc | 0:5d39f2521173 | 338 | * @date 30/08/2011 |
fblanc | 0:5d39f2521173 | 339 | */ |
fblanc | 0:5d39f2521173 | 340 | uint8_t ow_reset(uint8_t n) { // reset. Should improve to act as a presence pulse |
fblanc | 0:5d39f2521173 | 341 | uint8_t err; |
fblanc | 0:5d39f2521173 | 342 | |
fblanc | 0:5d39f2521173 | 343 | t_ow[n]->output(); |
fblanc | 0:5d39f2521173 | 344 | *t_ow[n] = 0; // bring low for 500 us |
fblanc | 0:5d39f2521173 | 345 | wait_us(500); |
fblanc | 0:5d39f2521173 | 346 | t_ow[n]->input(); |
fblanc | 0:5d39f2521173 | 347 | wait_us(60); |
fblanc | 0:5d39f2521173 | 348 | err = *t_ow[n]; |
fblanc | 0:5d39f2521173 | 349 | wait_us(240); |
fblanc | 0:5d39f2521173 | 350 | if ( *t_ow[n] == 0 ) { // short circuit |
fblanc | 0:5d39f2521173 | 351 | err = OW_SHORT_CIRCUIT; |
fblanc | 0:5d39f2521173 | 352 | |
fblanc | 0:5d39f2521173 | 353 | } |
fblanc | 0:5d39f2521173 | 354 | return err; |
fblanc | 0:5d39f2521173 | 355 | } |
fblanc | 0:5d39f2521173 | 356 | //********************************************************************************************************** |
fblanc | 0:5d39f2521173 | 357 | //* read write onewire |
fblanc | 0:5d39f2521173 | 358 | //********************************************************************************************************** |
fblanc | 0:5d39f2521173 | 359 | /** |
fblanc | 0:5d39f2521173 | 360 | * @brief read write onewire |
fblanc | 0:5d39f2521173 | 361 | * @param [in/out] b data |
fblanc | 0:5d39f2521173 | 362 | * @return data |
fblanc | 0:5d39f2521173 | 363 | * @date 20/06/2011 |
fblanc | 0:5d39f2521173 | 364 | */ |
fblanc | 0:5d39f2521173 | 365 | uint8_t ow_bit_io( uint8_t b ) { |
fblanc | 0:5d39f2521173 | 366 | |
fblanc | 0:5d39f2521173 | 367 | ow_pin.output(); // drive bus low |
fblanc | 0:5d39f2521173 | 368 | ow_pin = 0; |
fblanc | 0:5d39f2521173 | 369 | wait_us(1); // Recovery-Time wuffwuff was 1 |
fblanc | 0:5d39f2521173 | 370 | |
fblanc | 0:5d39f2521173 | 371 | if ( b ) |
fblanc | 0:5d39f2521173 | 372 | ow_pin.input(); // if bit is 1 set bus high (by ext. pull-up) |
fblanc | 0:5d39f2521173 | 373 | // delay was 15uS-1 see comment above |
fblanc | 0:5d39f2521173 | 374 | wait_us(15-1); |
fblanc | 0:5d39f2521173 | 375 | if ( ow_pin == 0 ) b = 0; // sample at end of read-timeslot |
fblanc | 0:5d39f2521173 | 376 | wait_us(60-15); |
fblanc | 0:5d39f2521173 | 377 | ow_pin.input(); |
fblanc | 0:5d39f2521173 | 378 | return b; |
fblanc | 0:5d39f2521173 | 379 | } |
fblanc | 0:5d39f2521173 | 380 | /** |
fblanc | 0:5d39f2521173 | 381 | * @brief read write onewire |
fblanc | 0:5d39f2521173 | 382 | * @param [in] n num bus onewire |
fblanc | 0:5d39f2521173 | 383 | * @param [in/out] b data |
fblanc | 0:5d39f2521173 | 384 | * @return data |
fblanc | 0:5d39f2521173 | 385 | * @date 30/08/2011 |
fblanc | 0:5d39f2521173 | 386 | */ |
fblanc | 0:5d39f2521173 | 387 | uint8_t ow_bit_io(uint8_t n, uint8_t b ) { |
fblanc | 0:5d39f2521173 | 388 | |
fblanc | 0:5d39f2521173 | 389 | t_ow[n]->output(); // drive bus low |
fblanc | 0:5d39f2521173 | 390 | *t_ow[n] = 0; |
fblanc | 0:5d39f2521173 | 391 | wait_us(1); // Recovery-Time wuffwuff was 1 |
fblanc | 0:5d39f2521173 | 392 | |
fblanc | 0:5d39f2521173 | 393 | if ( b ) |
fblanc | 0:5d39f2521173 | 394 | t_ow[n]->input(); // if bit is 1 set bus high (by ext. pull-up) |
fblanc | 0:5d39f2521173 | 395 | // delay was 15uS-1 see comment above |
fblanc | 0:5d39f2521173 | 396 | wait_us(15-1); |
fblanc | 0:5d39f2521173 | 397 | if ( *t_ow[n] == 0 ) b = 0; // sample at end of read-timeslot |
fblanc | 0:5d39f2521173 | 398 | wait_us(60-15); |
fblanc | 0:5d39f2521173 | 399 | t_ow[n]->input(); |
fblanc | 0:5d39f2521173 | 400 | // printf("ow_bit_io n=%d b=%X\n",n,b); |
fblanc | 0:5d39f2521173 | 401 | return b; |
fblanc | 0:5d39f2521173 | 402 | } |
fblanc | 0:5d39f2521173 | 403 | //********************************************************************************************************** |
fblanc | 0:5d39f2521173 | 404 | //* byte write on onewire |
fblanc | 0:5d39f2521173 | 405 | //********************************************************************************************************** |
fblanc | 0:5d39f2521173 | 406 | /** |
fblanc | 0:5d39f2521173 | 407 | * @brief byte write on onewire |
fblanc | 0:5d39f2521173 | 408 | * @param [in] b data |
fblanc | 0:5d39f2521173 | 409 | * @return data |
fblanc | 0:5d39f2521173 | 410 | * @date 20/06/2011 |
fblanc | 0:5d39f2521173 | 411 | */ |
fblanc | 0:5d39f2521173 | 412 | uint8_t ow_byte_wr( uint8_t b ) { |
fblanc | 0:5d39f2521173 | 413 | uint8_t i = 8, j; |
fblanc | 0:5d39f2521173 | 414 | |
fblanc | 0:5d39f2521173 | 415 | do { |
fblanc | 0:5d39f2521173 | 416 | j = ow_bit_io( b & 1 ); |
fblanc | 0:5d39f2521173 | 417 | b >>= 1; |
fblanc | 0:5d39f2521173 | 418 | if ( j ) |
fblanc | 0:5d39f2521173 | 419 | b |= 0x80; |
fblanc | 0:5d39f2521173 | 420 | } while ( --i ); |
fblanc | 0:5d39f2521173 | 421 | return b; |
fblanc | 0:5d39f2521173 | 422 | } |
fblanc | 0:5d39f2521173 | 423 | /** |
fblanc | 0:5d39f2521173 | 424 | * @brief byte write on onewire |
fblanc | 0:5d39f2521173 | 425 | * @param [in] n num bus onewire |
fblanc | 0:5d39f2521173 | 426 | * @param [in] b data |
fblanc | 0:5d39f2521173 | 427 | * @return data |
fblanc | 0:5d39f2521173 | 428 | * @date 30/08/2011 |
fblanc | 0:5d39f2521173 | 429 | */ |
fblanc | 0:5d39f2521173 | 430 | uint8_t ow_byte_wr(uint8_t n, uint8_t b ) { |
fblanc | 0:5d39f2521173 | 431 | uint8_t i = 8, j; |
fblanc | 0:5d39f2521173 | 432 | |
fblanc | 0:5d39f2521173 | 433 | do { |
fblanc | 0:5d39f2521173 | 434 | j = ow_bit_io(n, b & 1 ); |
fblanc | 0:5d39f2521173 | 435 | b >>= 1; |
fblanc | 0:5d39f2521173 | 436 | if ( j ) |
fblanc | 0:5d39f2521173 | 437 | b |= 0x80; |
fblanc | 0:5d39f2521173 | 438 | } while ( --i ); |
fblanc | 0:5d39f2521173 | 439 | return b; |
fblanc | 0:5d39f2521173 | 440 | } |
fblanc | 0:5d39f2521173 | 441 | //********************************************************************************************************** |
fblanc | 0:5d39f2521173 | 442 | //* byte write on onewire |
fblanc | 0:5d39f2521173 | 443 | //********************************************************************************************************** |
fblanc | 0:5d39f2521173 | 444 | /** |
fblanc | 0:5d39f2521173 | 445 | * @brief byte read on onewire |
fblanc | 0:5d39f2521173 | 446 | * @return data |
fblanc | 0:5d39f2521173 | 447 | * @date 20/06/2011 |
fblanc | 0:5d39f2521173 | 448 | */ |
fblanc | 0:5d39f2521173 | 449 | uint8_t ow_byte_rd( void ) { |
fblanc | 0:5d39f2521173 | 450 | // read by sending 0xff (a dontcare?) |
fblanc | 0:5d39f2521173 | 451 | return ow_byte_wr( 0xFF ); |
fblanc | 0:5d39f2521173 | 452 | } |
fblanc | 0:5d39f2521173 | 453 | |
fblanc | 0:5d39f2521173 | 454 | /** |
fblanc | 0:5d39f2521173 | 455 | * @brief byte read on onewire |
fblanc | 0:5d39f2521173 | 456 | * @param [in] n num onewire |
fblanc | 0:5d39f2521173 | 457 | * @return data |
fblanc | 0:5d39f2521173 | 458 | * @date 30/08/2011 |
fblanc | 0:5d39f2521173 | 459 | */ |
fblanc | 0:5d39f2521173 | 460 | uint8_t ow_byte_rd( uint8_t n) { |
fblanc | 0:5d39f2521173 | 461 | // read by sending 0xff (a dontcare?) |
fblanc | 0:5d39f2521173 | 462 | return ow_byte_wr(n, 0xFF ); |
fblanc | 0:5d39f2521173 | 463 | } |
fblanc | 0:5d39f2521173 | 464 | //********************************************************************************************************** |
fblanc | 0:5d39f2521173 | 465 | //* byte write on onewire |
fblanc | 0:5d39f2521173 | 466 | //********************************************************************************************************** |
fblanc | 0:5d39f2521173 | 467 | /** |
fblanc | 0:5d39f2521173 | 468 | * @brief write command |
fblanc | 0:5d39f2521173 | 469 | * @param [in] command |
fblanc | 0:5d39f2521173 | 470 | * @param [in] id romcode |
fblanc | 0:5d39f2521173 | 471 | * @date 20/06/2011 |
fblanc | 0:5d39f2521173 | 472 | */ |
fblanc | 0:5d39f2521173 | 473 | uint8_t ow_command( uint8_t command, uint8_t id[] ) { |
fblanc | 0:5d39f2521173 | 474 | uint8_t i; |
fblanc | 0:5d39f2521173 | 475 | |
fblanc | 0:5d39f2521173 | 476 | ow_reset(); |
fblanc | 0:5d39f2521173 | 477 | if ( id ) { |
fblanc | 0:5d39f2521173 | 478 | ow_byte_wr( OW_MATCH_ROM ); // to a single device |
fblanc | 0:5d39f2521173 | 479 | i = OW_ROMCODE_SIZE; |
fblanc | 0:5d39f2521173 | 480 | do { |
fblanc | 0:5d39f2521173 | 481 | ow_byte_wr( *id ); |
fblanc | 0:5d39f2521173 | 482 | ++id; |
fblanc | 0:5d39f2521173 | 483 | } while ( --i ); |
fblanc | 0:5d39f2521173 | 484 | } else { |
fblanc | 0:5d39f2521173 | 485 | ow_byte_wr( OW_SKIP_ROM ); // to all devices |
fblanc | 0:5d39f2521173 | 486 | } |
fblanc | 0:5d39f2521173 | 487 | ow_byte_wr( command ); |
fblanc | 0:5d39f2521173 | 488 | return 0; |
fblanc | 0:5d39f2521173 | 489 | } |
fblanc | 0:5d39f2521173 | 490 | /** |
fblanc | 0:5d39f2521173 | 491 | * @brief write command |
fblanc | 0:5d39f2521173 | 492 | * @param [in] n num bus onewire |
fblanc | 0:5d39f2521173 | 493 | * @param [in] command |
fblanc | 0:5d39f2521173 | 494 | * @param [in] id romcode |
fblanc | 0:5d39f2521173 | 495 | * @date 30/08/2011 |
fblanc | 0:5d39f2521173 | 496 | */ |
fblanc | 0:5d39f2521173 | 497 | uint8_t ow_command(uint8_t n, uint8_t command, uint8_t id[] ) { |
fblanc | 0:5d39f2521173 | 498 | uint8_t i; |
fblanc | 0:5d39f2521173 | 499 | |
fblanc | 0:5d39f2521173 | 500 | ow_reset(n); |
fblanc | 0:5d39f2521173 | 501 | if ( id ) { |
fblanc | 0:5d39f2521173 | 502 | ow_byte_wr( n,OW_MATCH_ROM ); // to a single device |
fblanc | 0:5d39f2521173 | 503 | i = OW_ROMCODE_SIZE; |
fblanc | 0:5d39f2521173 | 504 | do { |
fblanc | 0:5d39f2521173 | 505 | ow_byte_wr(n, *id ); |
fblanc | 0:5d39f2521173 | 506 | ++id; |
fblanc | 0:5d39f2521173 | 507 | } while ( --i ); |
fblanc | 0:5d39f2521173 | 508 | } else { |
fblanc | 0:5d39f2521173 | 509 | ow_byte_wr(n, OW_SKIP_ROM ); // to all devices |
fblanc | 0:5d39f2521173 | 510 | } |
fblanc | 0:5d39f2521173 | 511 | ow_byte_wr(n, command ); |
fblanc | 0:5d39f2521173 | 512 | return 0; |
fblanc | 0:5d39f2521173 | 513 | } |
fblanc | 0:5d39f2521173 | 514 | //********************************************************************************************************** |
fblanc | 0:5d39f2521173 | 515 | //* ow mode |
fblanc | 0:5d39f2521173 | 516 | //********************************************************************************************************** |
fblanc | 0:5d39f2521173 | 517 | /** |
fblanc | 0:5d39f2521173 | 518 | * @brief parasite enable |
fblanc | 0:5d39f2521173 | 519 | * @date 20/06/2011 |
fblanc | 0:5d39f2521173 | 520 | */ |
fblanc | 0:5d39f2521173 | 521 | uint8_t ow_parasite_enable(void) { |
fblanc | 0:5d39f2521173 | 522 | ow_pin.output(); |
fblanc | 0:5d39f2521173 | 523 | ow_pin = 1; |
fblanc | 0:5d39f2521173 | 524 | return 0; |
fblanc | 0:5d39f2521173 | 525 | } |
fblanc | 0:5d39f2521173 | 526 | /** |
fblanc | 0:5d39f2521173 | 527 | * @brief parasite disable |
fblanc | 0:5d39f2521173 | 528 | * @date 20/06/2011 |
fblanc | 0:5d39f2521173 | 529 | */ |
fblanc | 0:5d39f2521173 | 530 | uint8_t ow_parasite_disable(void) { |
fblanc | 0:5d39f2521173 | 531 | |
fblanc | 0:5d39f2521173 | 532 | ow_pin.input(); |
fblanc | 0:5d39f2521173 | 533 | return 0; |
fblanc | 0:5d39f2521173 | 534 | } |
fblanc | 0:5d39f2521173 | 535 | |
fblanc | 0:5d39f2521173 | 536 | /** |
fblanc | 0:5d39f2521173 | 537 | * @brief parasite enable |
fblanc | 0:5d39f2521173 | 538 | * @param [in] n num bus onewire |
fblanc | 0:5d39f2521173 | 539 | * @date 30/08/2011 |
fblanc | 0:5d39f2521173 | 540 | */ |
fblanc | 0:5d39f2521173 | 541 | uint8_t ow_parasite_enable(uint8_t n) { |
fblanc | 0:5d39f2521173 | 542 | t_ow[n]->output(); |
fblanc | 0:5d39f2521173 | 543 | *t_ow[n] = 1; |
fblanc | 0:5d39f2521173 | 544 | return 0; |
fblanc | 0:5d39f2521173 | 545 | } |
fblanc | 0:5d39f2521173 | 546 | /** |
fblanc | 0:5d39f2521173 | 547 | * @brief parasite disable |
fblanc | 0:5d39f2521173 | 548 | * @param [in] n num bus onewire |
fblanc | 0:5d39f2521173 | 549 | * @date 30/08/2011 |
fblanc | 0:5d39f2521173 | 550 | */ |
fblanc | 0:5d39f2521173 | 551 | uint8_t ow_parasite_disable(uint8_t n) { |
fblanc | 0:5d39f2521173 | 552 | t_ow[n]->input(); |
fblanc | 0:5d39f2521173 | 553 | return 0; |
fblanc | 0:5d39f2521173 | 554 | } |
fblanc | 0:5d39f2521173 | 555 | /** |
fblanc | 0:5d39f2521173 | 556 | * @brief PUL-UP bus OW |
fblanc | 0:5d39f2521173 | 557 | * @return OW_OK |
fblanc | 0:5d39f2521173 | 558 | * @date 20/06/2011 |
fblanc | 0:5d39f2521173 | 559 | */ |
fblanc | 0:5d39f2521173 | 560 | uint8_t ow_PullUp(void) |
fblanc | 0:5d39f2521173 | 561 | { |
fblanc | 0:5d39f2521173 | 562 | ow_pin.mode(PullUp); //PULL-UP bus OW |
fblanc | 0:5d39f2521173 | 563 | return OW_OK; |
fblanc | 0:5d39f2521173 | 564 | } |
fblanc | 0:5d39f2521173 | 565 | /** |
fblanc | 0:5d39f2521173 | 566 | * @brief PUL-UP bus OW |
fblanc | 0:5d39f2521173 | 567 | * @param [in] n num bus onewire |
fblanc | 0:5d39f2521173 | 568 | * @return OW_OK |
fblanc | 0:5d39f2521173 | 569 | * @date 30/08/2011 |
fblanc | 0:5d39f2521173 | 570 | */ |
fblanc | 0:5d39f2521173 | 571 | uint8_t ow_PullUp(uint8_t n) |
fblanc | 0:5d39f2521173 | 572 | { |
fblanc | 0:5d39f2521173 | 573 | t_ow[n]->mode(PullUp); //PULL-UP bus OW |
fblanc | 0:5d39f2521173 | 574 | return OW_OK; |
fblanc | 0:5d39f2521173 | 575 | } |