cevrero jeremy / Mbed 2 deprecated 1_Programme_principal

Dependencies:   mbed

Committer:
jeremycevrero
Date:
Tue May 01 21:29:55 2012 +0000
Revision:
0:40a613e7ba83

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jeremycevrero 0:40a613e7ba83 1 /*** D'apres : *********************************************************
jeremycevrero 0:40a613e7ba83 2 *
jeremycevrero 0:40a613e7ba83 3 * FILENAME: ds1820.h
jeremycevrero 0:40a613e7ba83 4 * DATE: 25.02.2005
jeremycevrero 0:40a613e7ba83 5 * AUTHOR: Christian Stadler
jeremycevrero 0:40a613e7ba83 6 *
jeremycevrero 0:40a613e7ba83 7 * DESCRIPTION: Driver for DS1820 1-Wire Temperature sensor (Dallas)
jeremycevrero 0:40a613e7ba83 8 *
jeremycevrero 0:40a613e7ba83 9 ******************************************************************************/
jeremycevrero 0:40a613e7ba83 10 #include "mbed.h"
jeremycevrero 0:40a613e7ba83 11 DigitalInOut DQ (p18); // broche DQ relie a la broche p15 du MBED
jeremycevrero 0:40a613e7ba83 12 DigitalOut G__MOS_P (p17);
jeremycevrero 0:40a613e7ba83 13 Serial pc(USBTX, USBRX); // tx, rx
jeremycevrero 0:40a613e7ba83 14 /* -------------------------------------------------------------------------- */
jeremycevrero 0:40a613e7ba83 15 /* DS1820 Timing Parameters */
jeremycevrero 0:40a613e7ba83 16 /* -------------------------------------------------------------------------- */
jeremycevrero 0:40a613e7ba83 17 #define DS1820_RST_PULSE 480 /* master reset pulse time in [us] */
jeremycevrero 0:40a613e7ba83 18 #define DS1820_MSTR_BITSTART 2 /* delay time for bit start by master */
jeremycevrero 0:40a613e7ba83 19 #define DS1820_PRESENCE_WAIT 40 /* delay after master reset pulse in [us] */
jeremycevrero 0:40a613e7ba83 20 #define DS1820_PRESENCE_FIN 480 /* dealy after reading of presence pulse [us] */
jeremycevrero 0:40a613e7ba83 21 #define DS1820_BITREAD_DLY 5 /* bit read delay */
jeremycevrero 0:40a613e7ba83 22 #define DS1820_BITWRITE_DLY 100 /* bit write delay */
jeremycevrero 0:40a613e7ba83 23
jeremycevrero 0:40a613e7ba83 24
jeremycevrero 0:40a613e7ba83 25 /* -------------------------------------------------------------------------- */
jeremycevrero 0:40a613e7ba83 26 /* DS1820 Registers */
jeremycevrero 0:40a613e7ba83 27 /* -------------------------------------------------------------------------- */
jeremycevrero 0:40a613e7ba83 28
jeremycevrero 0:40a613e7ba83 29 #define DS1820_REG_TEMPLSB 0
jeremycevrero 0:40a613e7ba83 30 #define DS1820_REG_TEMPMSB 1
jeremycevrero 0:40a613e7ba83 31 #define DS1820_REG_CNTREMAIN 6
jeremycevrero 0:40a613e7ba83 32 #define DS1820_REG_CNTPERSEC 7
jeremycevrero 0:40a613e7ba83 33 #define DS1820_SCRPADMEM_LEN 9 // length of scratchpad memory
jeremycevrero 0:40a613e7ba83 34
jeremycevrero 0:40a613e7ba83 35 #define DS1820_ADDR_LEN 8
jeremycevrero 0:40a613e7ba83 36
jeremycevrero 0:40a613e7ba83 37
jeremycevrero 0:40a613e7ba83 38 /* -------------------------------------------------------------------------- */
jeremycevrero 0:40a613e7ba83 39 /* DS1820 Commands */
jeremycevrero 0:40a613e7ba83 40 /* -------------------------------------------------------------------------- */
jeremycevrero 0:40a613e7ba83 41
jeremycevrero 0:40a613e7ba83 42 #define DS1820_CMD_SEARCHROM 0xF0 // recherche des differents DS1820 et de leur numROMs
jeremycevrero 0:40a613e7ba83 43 #define DS1820_CMD_READROM 0x33 // idem que SEARCHROM mais utilise pour 1 seul DS1820
jeremycevrero 0:40a613e7ba83 44 #define DS1820_CMD_MATCHROM 0x55 // permet de communiquer avec un DS1820 en particulier grace a son numROM
jeremycevrero 0:40a613e7ba83 45 #define DS1820_CMD_SKIPROM 0xCC // permet de communiquer avec tous les DS1820 en meme temps
jeremycevrero 0:40a613e7ba83 46 #define DS1820_CMD_ALARMSEARCH 0xEC // permet de dialoguer seulement avec les DS1820 qui ont un flag
jeremycevrero 0:40a613e7ba83 47 #define DS1820_CMD_CONVERTTEMP 0x44 // permet de lancer un convertion de la temperature, le resultat sera stocke dans le scratchpad
jeremycevrero 0:40a613e7ba83 48 #define DS1820_CMD_WRITESCRPAD 0x4E // permet au MBED d'ecrire deux octets dans le scratchpad dont un dans le registre TH et un dans le registre TL
jeremycevrero 0:40a613e7ba83 49 #define DS1820_CMD_READSCRPAD 0xBE // permet au MBED de lire le scratchpad
jeremycevrero 0:40a613e7ba83 50 #define DS1820_CMD_COPYSCRPAD 0x48 // permet de copier le scratchpad et les registres TH, TL stocke dans EEPROM
jeremycevrero 0:40a613e7ba83 51 #define DS1820_CMD_RECALLEE 0xB8 // rappel les valeur de declenchement de l'alarme
jeremycevrero 0:40a613e7ba83 52
jeremycevrero 0:40a613e7ba83 53
jeremycevrero 0:40a613e7ba83 54 #define DS1820_FAMILY_CODE_DS18B20 0x28
jeremycevrero 0:40a613e7ba83 55 #define DS1820_FAMILY_CODE_DS18S20 0x10
jeremycevrero 0:40a613e7ba83 56
jeremycevrero 0:40a613e7ba83 57 char dowcrc=0; // crc is accumulated in this variable
jeremycevrero 0:40a613e7ba83 58 // crc lookup table
jeremycevrero 0:40a613e7ba83 59 char const dscrc_table[] = {
jeremycevrero 0:40a613e7ba83 60 0, 94,188,226, 97, 63,221,131,194,156,126, 32,163,253, 31, 65,
jeremycevrero 0:40a613e7ba83 61 157,195, 33,127,252,162, 64, 30, 95, 1,227,189, 62, 96,130,220,
jeremycevrero 0:40a613e7ba83 62 35,125,159,193, 66, 28,254,160,225,191, 93, 3,128,222, 60, 98,
jeremycevrero 0:40a613e7ba83 63 190,224, 2, 92,223,129, 99, 61,124, 34,192,158, 29, 67,161,255,
jeremycevrero 0:40a613e7ba83 64 70, 24,250,164, 39,121,155,197,132,218, 56,102,229,187, 89, 7,
jeremycevrero 0:40a613e7ba83 65 219,133,103, 57,186,228, 6, 88, 25, 71,165,251,120, 38,196,154,
jeremycevrero 0:40a613e7ba83 66 101, 59,217,135, 4, 90,184,230,167,249, 27, 69,198,152,122, 36,
jeremycevrero 0:40a613e7ba83 67 248,166, 68, 26,153,199, 37,123, 58,100,134,216, 91, 5,231,185,
jeremycevrero 0:40a613e7ba83 68 140,210, 48,110,237,179, 81, 15, 78, 16,242,172, 47,113,147,205,
jeremycevrero 0:40a613e7ba83 69 17, 79,173,243,112, 46,204,146,211,141,111, 49,178,236, 14, 80,
jeremycevrero 0:40a613e7ba83 70 175,241, 19, 77,206,144,114, 44,109, 51,209,143, 12, 82,176,238,
jeremycevrero 0:40a613e7ba83 71 50,108,142,208, 83, 13,239,177,240,174, 76, 18,145,207, 45,115,
jeremycevrero 0:40a613e7ba83 72 202,148,118, 40,171,245, 23, 73, 8, 86,180,234,105, 55,213,139,
jeremycevrero 0:40a613e7ba83 73 87, 9,235,181, 54,104,138,212,149,203, 41,119,244,170, 72, 22,
jeremycevrero 0:40a613e7ba83 74 233,183, 85, 11,136,214, 52,106, 43,117,151,201, 74, 20,246,168,
jeremycevrero 0:40a613e7ba83 75 116, 42,200,150, 21, 75,169,247,182,232, 10, 84,215,137,107, 53
jeremycevrero 0:40a613e7ba83 76 };
jeremycevrero 0:40a613e7ba83 77
jeremycevrero 0:40a613e7ba83 78 /* -------------------------------------------------------------------------- */
jeremycevrero 0:40a613e7ba83 79 bool doneFlag; // declaration d'une variable booleenne "doneFlag"
jeremycevrero 0:40a613e7ba83 80 char lastDiscrep,numROMs; // declaration des variables lastDiscrep et numROMs
jeremycevrero 0:40a613e7ba83 81 char RomBytes[DS1820_ADDR_LEN]; // declaration de la variable RomBytes[DS1820_ADDR_LEN]
jeremycevrero 0:40a613e7ba83 82 char FoundROM[9][8]; // Table of found ROM codes, 8 bytes for each
jeremycevrero 0:40a613e7ba83 83 float temperature;// declaration de la variable
jeremycevrero 0:40a613e7ba83 84
jeremycevrero 0:40a613e7ba83 85 /* -------------------------------------------------------------------------- */
jeremycevrero 0:40a613e7ba83 86 /* Low-Level Functions */
jeremycevrero 0:40a613e7ba83 87 /* -------------------------------------------------------------------------- */
jeremycevrero 0:40a613e7ba83 88 /*******************************************************************************
jeremycevrero 0:40a613e7ba83 89 * FUNCTION: ow_reset
jeremycevrero 0:40a613e7ba83 90 * PURPOSE: Initializes the DS1820 device.
jeremycevrero 0:40a613e7ba83 91 *
jeremycevrero 0:40a613e7ba83 92 * INPUT: -
jeremycevrero 0:40a613e7ba83 93 * OUTPUT: -
jeremycevrero 0:40a613e7ba83 94 * RETURN: FALSE if at least one device is on the 1-wire bus, TRUE otherwise
jeremycevrero 0:40a613e7ba83 95 ******************************************************************************/
jeremycevrero 0:40a613e7ba83 96 bool ow_reset(void)
jeremycevrero 0:40a613e7ba83 97 {
jeremycevrero 0:40a613e7ba83 98 bool presence;
jeremycevrero 0:40a613e7ba83 99
jeremycevrero 0:40a613e7ba83 100 /* reset pulse */
jeremycevrero 0:40a613e7ba83 101 DQ.output();
jeremycevrero 0:40a613e7ba83 102 DQ=0;
jeremycevrero 0:40a613e7ba83 103 wait_us(DS1820_RST_PULSE);
jeremycevrero 0:40a613e7ba83 104 DQ=1;
jeremycevrero 0:40a613e7ba83 105
jeremycevrero 0:40a613e7ba83 106 /* wait until pullup pull 1-wire bus to high */
jeremycevrero 0:40a613e7ba83 107 wait_us(DS1820_PRESENCE_WAIT);
jeremycevrero 0:40a613e7ba83 108
jeremycevrero 0:40a613e7ba83 109 /* get presence pulse */
jeremycevrero 0:40a613e7ba83 110 DQ.input();
jeremycevrero 0:40a613e7ba83 111 presence=DQ.read();
jeremycevrero 0:40a613e7ba83 112
jeremycevrero 0:40a613e7ba83 113 wait_us(424);
jeremycevrero 0:40a613e7ba83 114
jeremycevrero 0:40a613e7ba83 115 return presence;
jeremycevrero 0:40a613e7ba83 116 }
jeremycevrero 0:40a613e7ba83 117
jeremycevrero 0:40a613e7ba83 118 /*******************************************************************************
jeremycevrero 0:40a613e7ba83 119 * FUNCTION: read_bit
jeremycevrero 0:40a613e7ba83 120 * PURPOSE: Reads a single bit from the DS1820 device.
jeremycevrero 0:40a613e7ba83 121 *
jeremycevrero 0:40a613e7ba83 122 * INPUT: -
jeremycevrero 0:40a613e7ba83 123 * OUTPUT: -
jeremycevrero 0:40a613e7ba83 124 * RETURN: bool value of the bit which as been read form the DS1820
jeremycevrero 0:40a613e7ba83 125 ******************************************************************************/
jeremycevrero 0:40a613e7ba83 126 bool read_bit(void)
jeremycevrero 0:40a613e7ba83 127 {
jeremycevrero 0:40a613e7ba83 128 DQ.output();
jeremycevrero 0:40a613e7ba83 129 DQ=0;
jeremycevrero 0:40a613e7ba83 130 wait_us(DS1820_MSTR_BITSTART);
jeremycevrero 0:40a613e7ba83 131 DQ.input();
jeremycevrero 0:40a613e7ba83 132 DQ.read();
jeremycevrero 0:40a613e7ba83 133 wait_us(DS1820_BITREAD_DLY);
jeremycevrero 0:40a613e7ba83 134
jeremycevrero 0:40a613e7ba83 135 return (DQ);
jeremycevrero 0:40a613e7ba83 136 }
jeremycevrero 0:40a613e7ba83 137
jeremycevrero 0:40a613e7ba83 138 /*******************************************************************************
jeremycevrero 0:40a613e7ba83 139 * FUNCTION: write_bit
jeremycevrero 0:40a613e7ba83 140 * PURPOSE: Writes a single bit to the DS1820 device.
jeremycevrero 0:40a613e7ba83 141 *
jeremycevrero 0:40a613e7ba83 142 * INPUT: bBit value of bit to be written
jeremycevrero 0:40a613e7ba83 143 * OUTPUT: -
jeremycevrero 0:40a613e7ba83 144 * RETURN: -
jeremycevrero 0:40a613e7ba83 145 ******************************************************************************/
jeremycevrero 0:40a613e7ba83 146 void write_bit(bool bBit)
jeremycevrero 0:40a613e7ba83 147 {
jeremycevrero 0:40a613e7ba83 148 DQ.output();
jeremycevrero 0:40a613e7ba83 149 DQ=0;
jeremycevrero 0:40a613e7ba83 150 wait_us(DS1820_MSTR_BITSTART);
jeremycevrero 0:40a613e7ba83 151
jeremycevrero 0:40a613e7ba83 152 if (bBit != false) DQ=1;
jeremycevrero 0:40a613e7ba83 153
jeremycevrero 0:40a613e7ba83 154 wait_us(DS1820_BITWRITE_DLY);
jeremycevrero 0:40a613e7ba83 155 DQ=1;
jeremycevrero 0:40a613e7ba83 156
jeremycevrero 0:40a613e7ba83 157 }
jeremycevrero 0:40a613e7ba83 158
jeremycevrero 0:40a613e7ba83 159 /*******************************************************************************
jeremycevrero 0:40a613e7ba83 160 * FUNCTION: read_byte
jeremycevrero 0:40a613e7ba83 161 * PURPOSE: Reads a single byte from the DS1820 device.
jeremycevrero 0:40a613e7ba83 162 *
jeremycevrero 0:40a613e7ba83 163 * INPUT: -
jeremycevrero 0:40a613e7ba83 164 * OUTPUT: -
jeremycevrero 0:40a613e7ba83 165 * RETURN: uint8 byte which has been read from the DS1820
jeremycevrero 0:40a613e7ba83 166 ******************************************************************************/
jeremycevrero 0:40a613e7ba83 167 char read_byte(void)
jeremycevrero 0:40a613e7ba83 168 {
jeremycevrero 0:40a613e7ba83 169 char i;
jeremycevrero 0:40a613e7ba83 170 char value = 0;
jeremycevrero 0:40a613e7ba83 171
jeremycevrero 0:40a613e7ba83 172 for (i=0 ; i < 8; i++)
jeremycevrero 0:40a613e7ba83 173 {
jeremycevrero 0:40a613e7ba83 174 if ( read_bit() ) value |= (1 << i);
jeremycevrero 0:40a613e7ba83 175 wait_us(120);
jeremycevrero 0:40a613e7ba83 176 }
jeremycevrero 0:40a613e7ba83 177 return(value);
jeremycevrero 0:40a613e7ba83 178 }
jeremycevrero 0:40a613e7ba83 179
jeremycevrero 0:40a613e7ba83 180 /*******************************************************************************
jeremycevrero 0:40a613e7ba83 181 * FUNCTION: write_byte
jeremycevrero 0:40a613e7ba83 182 * PURPOSE: Writes a single byte to the DS1820 device.
jeremycevrero 0:40a613e7ba83 183 *
jeremycevrero 0:40a613e7ba83 184 * INPUT: val_u8 byte to be written
jeremycevrero 0:40a613e7ba83 185 * OUTPUT: -
jeremycevrero 0:40a613e7ba83 186 * RETURN: -
jeremycevrero 0:40a613e7ba83 187 ******************************************************************************/
jeremycevrero 0:40a613e7ba83 188 void write_byte(char val_u8)
jeremycevrero 0:40a613e7ba83 189 {
jeremycevrero 0:40a613e7ba83 190 char i;
jeremycevrero 0:40a613e7ba83 191 char temp;
jeremycevrero 0:40a613e7ba83 192
jeremycevrero 0:40a613e7ba83 193 for (i=0; i < 8; i++) /* writes byte, one bit at a time */
jeremycevrero 0:40a613e7ba83 194 {
jeremycevrero 0:40a613e7ba83 195 temp = val_u8 >> i; /* shifts val right 'i' spaces */
jeremycevrero 0:40a613e7ba83 196 temp &= 0x01; /* copy that bit to temp */
jeremycevrero 0:40a613e7ba83 197 write_bit(temp); /* write bit in temp into */
jeremycevrero 0:40a613e7ba83 198 }
jeremycevrero 0:40a613e7ba83 199
jeremycevrero 0:40a613e7ba83 200 wait_us(105);
jeremycevrero 0:40a613e7ba83 201 }
jeremycevrero 0:40a613e7ba83 202 /* -------------------------------------------------------------------------- */
jeremycevrero 0:40a613e7ba83 203 // One wire crc
jeremycevrero 0:40a613e7ba83 204 char ow_crc(char x)
jeremycevrero 0:40a613e7ba83 205 {
jeremycevrero 0:40a613e7ba83 206 dowcrc = dscrc_table[dowcrc^x];
jeremycevrero 0:40a613e7ba83 207 return dowcrc;
jeremycevrero 0:40a613e7ba83 208 }
jeremycevrero 0:40a613e7ba83 209 /* -------------------------------------------------------------------------- */
jeremycevrero 0:40a613e7ba83 210 /* API Interface */
jeremycevrero 0:40a613e7ba83 211 /* -------------------------------------------------------------------------- */
jeremycevrero 0:40a613e7ba83 212
jeremycevrero 0:40a613e7ba83 213 /*******************************************************************************
jeremycevrero 0:40a613e7ba83 214 * FUNCTION: DS1820_AddrDevice
jeremycevrero 0:40a613e7ba83 215 * PURPOSE: Addresses a single or all devices on the 1-wire bus.
jeremycevrero 0:40a613e7ba83 216 *
jeremycevrero 0:40a613e7ba83 217 * INPUT: nAddrMethod use DS1820_CMD_MATCHROM to select a single
jeremycevrero 0:40a613e7ba83 218 * device or DS1820_CMD_SKIPROM to select all
jeremycevrero 0:40a613e7ba83 219 * OUTPUT: -
jeremycevrero 0:40a613e7ba83 220 * RETURN: -
jeremycevrero 0:40a613e7ba83 221 ******************************************************************************/
jeremycevrero 0:40a613e7ba83 222 void DS1820_AddrDevice(char nAddrMethod)
jeremycevrero 0:40a613e7ba83 223 {
jeremycevrero 0:40a613e7ba83 224 char i;
jeremycevrero 0:40a613e7ba83 225
jeremycevrero 0:40a613e7ba83 226 if (nAddrMethod == DS1820_CMD_MATCHROM)
jeremycevrero 0:40a613e7ba83 227 {
jeremycevrero 0:40a613e7ba83 228 write_byte(DS1820_CMD_MATCHROM); /* address single devices on bus */
jeremycevrero 0:40a613e7ba83 229 for (i = 0; i < DS1820_ADDR_LEN; i ++)
jeremycevrero 0:40a613e7ba83 230 write_byte(RomBytes[i]);
jeremycevrero 0:40a613e7ba83 231 }
jeremycevrero 0:40a613e7ba83 232 else
jeremycevrero 0:40a613e7ba83 233 write_byte(DS1820_CMD_SKIPROM); /* address all devices on bus */
jeremycevrero 0:40a613e7ba83 234 }
jeremycevrero 0:40a613e7ba83 235
jeremycevrero 0:40a613e7ba83 236 /*******************************************************************************
jeremycevrero 0:40a613e7ba83 237 * FUNCTION: Next
jeremycevrero 0:40a613e7ba83 238 * PURPOSE: Finds next device connected to the 1-wire bus.
jeremycevrero 0:40a613e7ba83 239 *
jeremycevrero 0:40a613e7ba83 240 * INPUT: -
jeremycevrero 0:40a613e7ba83 241 * OUTPUT: RomBytes[] ROM code of the next device
jeremycevrero 0:40a613e7ba83 242 * RETURN: bool TRUE if there are more devices on the 1-wire
jeremycevrero 0:40a613e7ba83 243 * bus, FALSE otherwise
jeremycevrero 0:40a613e7ba83 244 ******************************************************************************/
jeremycevrero 0:40a613e7ba83 245 bool Next(void)
jeremycevrero 0:40a613e7ba83 246 {
jeremycevrero 0:40a613e7ba83 247 char x;
jeremycevrero 0:40a613e7ba83 248 char n;
jeremycevrero 0:40a613e7ba83 249 char k = 1;
jeremycevrero 0:40a613e7ba83 250 char m = 1;
jeremycevrero 0:40a613e7ba83 251 char discrepMarker = 0;
jeremycevrero 0:40a613e7ba83 252 bool g;
jeremycevrero 0:40a613e7ba83 253 bool flag;
jeremycevrero 0:40a613e7ba83 254 bool nxt = false;
jeremycevrero 0:40a613e7ba83 255
jeremycevrero 0:40a613e7ba83 256 /* init ROM address */
jeremycevrero 0:40a613e7ba83 257 for (n=0; n < 8; n ++)
jeremycevrero 0:40a613e7ba83 258 RomBytes[n] = 0x00;
jeremycevrero 0:40a613e7ba83 259
jeremycevrero 0:40a613e7ba83 260 flag = ow_reset(); /* reset the 1-wire */
jeremycevrero 0:40a613e7ba83 261
jeremycevrero 0:40a613e7ba83 262 if (flag || doneFlag) /* no device found */
jeremycevrero 0:40a613e7ba83 263 {
jeremycevrero 0:40a613e7ba83 264 lastDiscrep = 0; /* reset the search */
jeremycevrero 0:40a613e7ba83 265 return false;
jeremycevrero 0:40a613e7ba83 266 }
jeremycevrero 0:40a613e7ba83 267
jeremycevrero 0:40a613e7ba83 268 /* send search rom command */
jeremycevrero 0:40a613e7ba83 269 write_byte(DS1820_CMD_SEARCHROM);
jeremycevrero 0:40a613e7ba83 270
jeremycevrero 0:40a613e7ba83 271 n = 0;
jeremycevrero 0:40a613e7ba83 272 do
jeremycevrero 0:40a613e7ba83 273 {
jeremycevrero 0:40a613e7ba83 274 x = 0;
jeremycevrero 0:40a613e7ba83 275
jeremycevrero 0:40a613e7ba83 276 /* read bit */
jeremycevrero 0:40a613e7ba83 277 if ( read_bit() == 1 ) x = 2;
jeremycevrero 0:40a613e7ba83 278 wait_us(120);
jeremycevrero 0:40a613e7ba83 279
jeremycevrero 0:40a613e7ba83 280 /* read bit complement */
jeremycevrero 0:40a613e7ba83 281 if ( read_bit() == 1 ) x |= 1;
jeremycevrero 0:40a613e7ba83 282 wait_us(120);
jeremycevrero 0:40a613e7ba83 283
jeremycevrero 0:40a613e7ba83 284 /* description for values of x: */
jeremycevrero 0:40a613e7ba83 285 /* 00 There are devices connected to the bus which have conflicting */
jeremycevrero 0:40a613e7ba83 286 /* bits in the current ROM code bit position. */
jeremycevrero 0:40a613e7ba83 287 /* 01 All devices connected to the bus have a 0 in this bit position. */
jeremycevrero 0:40a613e7ba83 288 /* 10 All devices connected to the bus have a 1 in this bit position. */
jeremycevrero 0:40a613e7ba83 289 /* 11 There are no devices connected to the 1-wire bus. */
jeremycevrero 0:40a613e7ba83 290
jeremycevrero 0:40a613e7ba83 291 /* if there are no devices on the bus */
jeremycevrero 0:40a613e7ba83 292 if (x == 3) break;
jeremycevrero 0:40a613e7ba83 293 else
jeremycevrero 0:40a613e7ba83 294 {
jeremycevrero 0:40a613e7ba83 295 /* devices have the same logical value at this position */
jeremycevrero 0:40a613e7ba83 296 if (x > 0) g = (bool)(x >> 1);// get bit value
jeremycevrero 0:40a613e7ba83 297 /* devices have confilcting bits in the current ROM code */
jeremycevrero 0:40a613e7ba83 298 else
jeremycevrero 0:40a613e7ba83 299 {
jeremycevrero 0:40a613e7ba83 300 /* if there was a conflict on the last iteration */
jeremycevrero 0:40a613e7ba83 301 if (m < lastDiscrep)
jeremycevrero 0:40a613e7ba83 302 /* take same bit as in last iteration */
jeremycevrero 0:40a613e7ba83 303 g = ( (RomBytes[n] & k) > 0 );
jeremycevrero 0:40a613e7ba83 304 else
jeremycevrero 0:40a613e7ba83 305 g = (m == lastDiscrep);
jeremycevrero 0:40a613e7ba83 306
jeremycevrero 0:40a613e7ba83 307 if (g == 0)
jeremycevrero 0:40a613e7ba83 308 discrepMarker = m;
jeremycevrero 0:40a613e7ba83 309 }
jeremycevrero 0:40a613e7ba83 310
jeremycevrero 0:40a613e7ba83 311 /* store bit in ROM address */
jeremycevrero 0:40a613e7ba83 312 if (g ==1)
jeremycevrero 0:40a613e7ba83 313 RomBytes[n] |= k;
jeremycevrero 0:40a613e7ba83 314 else
jeremycevrero 0:40a613e7ba83 315 RomBytes[n] &= ~k;
jeremycevrero 0:40a613e7ba83 316
jeremycevrero 0:40a613e7ba83 317 write_bit(g);
jeremycevrero 0:40a613e7ba83 318
jeremycevrero 0:40a613e7ba83 319 /* increment bit position */
jeremycevrero 0:40a613e7ba83 320 m ++;
jeremycevrero 0:40a613e7ba83 321
jeremycevrero 0:40a613e7ba83 322 /* calculate next mask value */
jeremycevrero 0:40a613e7ba83 323 k = k << 1;
jeremycevrero 0:40a613e7ba83 324
jeremycevrero 0:40a613e7ba83 325 /* check if this byte has finished */
jeremycevrero 0:40a613e7ba83 326 if (k == 0)
jeremycevrero 0:40a613e7ba83 327 {
jeremycevrero 0:40a613e7ba83 328 ow_crc(RomBytes[n]); // Accumulate the crc
jeremycevrero 0:40a613e7ba83 329 n ++; /* advance to next byte of ROM mask */
jeremycevrero 0:40a613e7ba83 330 k = 1; /* update mask */
jeremycevrero 0:40a613e7ba83 331 }
jeremycevrero 0:40a613e7ba83 332 }
jeremycevrero 0:40a613e7ba83 333 } while (n < DS1820_ADDR_LEN);
jeremycevrero 0:40a613e7ba83 334
jeremycevrero 0:40a613e7ba83 335
jeremycevrero 0:40a613e7ba83 336 /* if search was unsuccessful then */
jeremycevrero 0:40a613e7ba83 337 if ((m < 65) ||dowcrc)
jeremycevrero 0:40a613e7ba83 338 // if (m < 65 )
jeremycevrero 0:40a613e7ba83 339 /* reset the last discrepancy to 0 */
jeremycevrero 0:40a613e7ba83 340 lastDiscrep = 0;
jeremycevrero 0:40a613e7ba83 341 else
jeremycevrero 0:40a613e7ba83 342 {
jeremycevrero 0:40a613e7ba83 343 /* search was successful */
jeremycevrero 0:40a613e7ba83 344 lastDiscrep = discrepMarker;
jeremycevrero 0:40a613e7ba83 345 doneFlag = (lastDiscrep == 0);
jeremycevrero 0:40a613e7ba83 346
jeremycevrero 0:40a613e7ba83 347 /* indicates search is not complete yet, more parts remain */
jeremycevrero 0:40a613e7ba83 348 nxt = true;
jeremycevrero 0:40a613e7ba83 349 }
jeremycevrero 0:40a613e7ba83 350 return nxt;
jeremycevrero 0:40a613e7ba83 351 }
jeremycevrero 0:40a613e7ba83 352
jeremycevrero 0:40a613e7ba83 353 /*******************************************************************************
jeremycevrero 0:40a613e7ba83 354 * FUNCTION: First
jeremycevrero 0:40a613e7ba83 355 * PURPOSE: Starts the device search on the 1-wire bus.
jeremycevrero 0:40a613e7ba83 356 *
jeremycevrero 0:40a613e7ba83 357 * INPUT: -
jeremycevrero 0:40a613e7ba83 358 * OUTPUT: RomBytes[] ROM code of the first device
jeremycevrero 0:40a613e7ba83 359 * RETURN: bool TRUE if there are more devices on the 1-wire
jeremycevrero 0:40a613e7ba83 360 * bus, FALSE otherwise
jeremycevrero 0:40a613e7ba83 361 ******************************************************************************/
jeremycevrero 0:40a613e7ba83 362 bool First(void)
jeremycevrero 0:40a613e7ba83 363 {
jeremycevrero 0:40a613e7ba83 364 lastDiscrep = 0;
jeremycevrero 0:40a613e7ba83 365 doneFlag = false;
jeremycevrero 0:40a613e7ba83 366
jeremycevrero 0:40a613e7ba83 367 return ( Next() );
jeremycevrero 0:40a613e7ba83 368 }
jeremycevrero 0:40a613e7ba83 369 /* -------------------------------------------------------------------------- */
jeremycevrero 0:40a613e7ba83 370 void FindDevices(void)
jeremycevrero 0:40a613e7ba83 371 {
jeremycevrero 0:40a613e7ba83 372 char m;
jeremycevrero 0:40a613e7ba83 373 if(!ow_reset())
jeremycevrero 0:40a613e7ba83 374 {
jeremycevrero 0:40a613e7ba83 375 if(First()) // Begins when at least one part found
jeremycevrero 0:40a613e7ba83 376 {
jeremycevrero 0:40a613e7ba83 377 numROMs = 0;
jeremycevrero 0:40a613e7ba83 378 do
jeremycevrero 0:40a613e7ba83 379 {
jeremycevrero 0:40a613e7ba83 380 numROMs++;
jeremycevrero 0:40a613e7ba83 381 for (m=0;m<8;m++)
jeremycevrero 0:40a613e7ba83 382 {
jeremycevrero 0:40a613e7ba83 383 FoundROM[numROMs-1][m] = RomBytes[m];
jeremycevrero 0:40a613e7ba83 384 }
jeremycevrero 0:40a613e7ba83 385 } while (Next()&&(numROMs<10)); // Continues until no additional
jeremycevrero 0:40a613e7ba83 386 }
jeremycevrero 0:40a613e7ba83 387 }
jeremycevrero 0:40a613e7ba83 388
jeremycevrero 0:40a613e7ba83 389 }
jeremycevrero 0:40a613e7ba83 390 //******************************************************************************
jeremycevrero 0:40a613e7ba83 391 // Sends Match ROM command to bus then device address
jeremycevrero 0:40a613e7ba83 392 char Send_MatchRom(char actNumROM)
jeremycevrero 0:40a613e7ba83 393 {
jeremycevrero 0:40a613e7ba83 394 char i;
jeremycevrero 0:40a613e7ba83 395 if (ow_reset()) return false; // 0 if device present
jeremycevrero 0:40a613e7ba83 396 write_byte(0x55); // Match ROM
jeremycevrero 0:40a613e7ba83 397
jeremycevrero 0:40a613e7ba83 398 for (i=0;i<8;i++)
jeremycevrero 0:40a613e7ba83 399 {
jeremycevrero 0:40a613e7ba83 400 write_byte(FoundROM[actNumROM][i]); // Send ROM code
jeremycevrero 0:40a613e7ba83 401 }
jeremycevrero 0:40a613e7ba83 402
jeremycevrero 0:40a613e7ba83 403 return true;
jeremycevrero 0:40a613e7ba83 404 }
jeremycevrero 0:40a613e7ba83 405 //******************************************************************************
jeremycevrero 0:40a613e7ba83 406 void Read_ROMCode(void)
jeremycevrero 0:40a613e7ba83 407 {
jeremycevrero 0:40a613e7ba83 408 char n;
jeremycevrero 0:40a613e7ba83 409 char dat[9];
jeremycevrero 0:40a613e7ba83 410
jeremycevrero 0:40a613e7ba83 411 ow_reset();
jeremycevrero 0:40a613e7ba83 412 write_byte(0x33);
jeremycevrero 0:40a613e7ba83 413 for (n=0;n<8;n++){dat[n]=read_byte();}
jeremycevrero 0:40a613e7ba83 414
jeremycevrero 0:40a613e7ba83 415 pc.printf("\f%X%X%X%X\n%X%X%X%X",dat[0],dat[1],dat[2],dat[3],dat[4],dat[5],
jeremycevrero 0:40a613e7ba83 416 dat[6],dat[7]);
jeremycevrero 0:40a613e7ba83 417
jeremycevrero 0:40a613e7ba83 418 wait_ms(5000);
jeremycevrero 0:40a613e7ba83 419 }
jeremycevrero 0:40a613e7ba83 420 /*******************************************************************************
jeremycevrero 0:40a613e7ba83 421 * FUNCTION: DS1820_WriteEEPROM
jeremycevrero 0:40a613e7ba83 422 * PURPOSE: Writes to the DS1820 EEPROM memory (2 bytes available).
jeremycevrero 0:40a613e7ba83 423 *
jeremycevrero 0:40a613e7ba83 424 * INPUT: nTHigh high byte of EEPROM
jeremycevrero 0:40a613e7ba83 425 * nTLow low byte of EEPROM
jeremycevrero 0:40a613e7ba83 426 * OUTPUT: -
jeremycevrero 0:40a613e7ba83 427 * RETURN: -
jeremycevrero 0:40a613e7ba83 428 ******************************************************************************/
jeremycevrero 0:40a613e7ba83 429 void DS1820_WriteEEPROM(char nTHigh, char nTLow)
jeremycevrero 0:40a613e7ba83 430 {
jeremycevrero 0:40a613e7ba83 431 /* --- write to scratchpad ----------------------------------------------- */
jeremycevrero 0:40a613e7ba83 432 ow_reset(); // appel de la fonction ow_reset
jeremycevrero 0:40a613e7ba83 433 DS1820_AddrDevice(DS1820_CMD_MATCHROM);
jeremycevrero 0:40a613e7ba83 434 write_byte(DS1820_CMD_WRITESCRPAD); // debut conversion
jeremycevrero 0:40a613e7ba83 435 write_byte(nTHigh);
jeremycevrero 0:40a613e7ba83 436 write_byte(nTLow);
jeremycevrero 0:40a613e7ba83 437 wait_us(10); // attendre 10ms
jeremycevrero 0:40a613e7ba83 438 ow_reset(); // appel de la fonction ow_reset
jeremycevrero 0:40a613e7ba83 439 DS1820_AddrDevice(DS1820_CMD_MATCHROM);
jeremycevrero 0:40a613e7ba83 440 write_byte(DS1820_CMD_COPYSCRPAD); // start conversion
jeremycevrero 0:40a613e7ba83 441 G__MOS_P=0; // "strong pullup"
jeremycevrero 0:40a613e7ba83 442 wait_ms(10); // attendre 10 ms
jeremycevrero 0:40a613e7ba83 443 G__MOS_P=1; // "strong pullup"
jeremycevrero 0:40a613e7ba83 444 }
jeremycevrero 0:40a613e7ba83 445
jeremycevrero 0:40a613e7ba83 446 /*******************************************************************************
jeremycevrero 0:40a613e7ba83 447 * FUNCTION: DS1820_GetTemp
jeremycevrero 0:40a613e7ba83 448 * PURPOSE: Get temperature value from single DS1820 device.
jeremycevrero 0:40a613e7ba83 449 *
jeremycevrero 0:40a613e7ba83 450 * Scratchpad Memory Layout
jeremycevrero 0:40a613e7ba83 451 * Byte Register
jeremycevrero 0:40a613e7ba83 452 * 0 Temperature_LSB
jeremycevrero 0:40a613e7ba83 453 * 1 Temperature_MSB
jeremycevrero 0:40a613e7ba83 454 * 2 Temp Alarm High / User Byte 1
jeremycevrero 0:40a613e7ba83 455 * 3 Temp Alarm Low / User Byte 2
jeremycevrero 0:40a613e7ba83 456 * 4 Reserved
jeremycevrero 0:40a613e7ba83 457 * 5 Reserved
jeremycevrero 0:40a613e7ba83 458 * 6 Count_Remain
jeremycevrero 0:40a613e7ba83 459 * 7 Count_per_C
jeremycevrero 0:40a613e7ba83 460 * 8 CRC
jeremycevrero 0:40a613e7ba83 461 *
jeremycevrero 0:40a613e7ba83 462 * Temperature calculation for DS18S20 (Family Code 0x10):
jeremycevrero 0:40a613e7ba83 463 * =======================================================
jeremycevrero 0:40a613e7ba83 464 * (Count_per_C - Count_Remain)
jeremycevrero 0:40a613e7ba83 465 * Temperature = temp_read - 0.25 + ----------------------------
jeremycevrero 0:40a613e7ba83 466 * Count_per_C
jeremycevrero 0:40a613e7ba83 467 *
jeremycevrero 0:40a613e7ba83 468 * Where temp_read is the value from the temp_MSB and temp_LSB with
jeremycevrero 0:40a613e7ba83 469 * the least significant bit removed (the 0.5C bit).
jeremycevrero 0:40a613e7ba83 470 *
jeremycevrero 0:40a613e7ba83 471 *
jeremycevrero 0:40a613e7ba83 472 * Temperature calculation for DS18B20 (Family Code 0x28):
jeremycevrero 0:40a613e7ba83 473 * =======================================================
jeremycevrero 0:40a613e7ba83 474 * bit7 bit6 bit5 bit4 bit3 bit2 bit1 bit0
jeremycevrero 0:40a613e7ba83 475 * LSB 2^3 2^2 2^1 2^0 2^-1 2^-2 2^-3 2^-4
jeremycevrero 0:40a613e7ba83 476 * bit15 bit14 bit13 bit12 bit3 bit2 bit1 bit0
jeremycevrero 0:40a613e7ba83 477 * MSB S S S S S 2^6 2^5 2^4
jeremycevrero 0:40a613e7ba83 478 *
jeremycevrero 0:40a613e7ba83 479 * The temperature data is stored as a 16-bit sign-extended two&#65533;s
jeremycevrero 0:40a613e7ba83 480 * complement number in the temperature register. The sign bits (S)
jeremycevrero 0:40a613e7ba83 481 * indicate if the temperature is positive or negative: for
jeremycevrero 0:40a613e7ba83 482 * positive numbers S = 0 and for negative numbers S = 1.
jeremycevrero 0:40a613e7ba83 483 *
jeremycevrero 0:40a613e7ba83 484 * RETURN: float
jeremycevrero 0:40a613e7ba83 485 ******************************************************************************/
jeremycevrero 0:40a613e7ba83 486 float DS1820_GetTemp(void) //fonction capture de la temperature
jeremycevrero 0:40a613e7ba83 487 {
jeremycevrero 0:40a613e7ba83 488 char i; //declaration de la variable i
jeremycevrero 0:40a613e7ba83 489 char scrpad[DS1820_SCRPADMEM_LEN]; //declaration de la variable scrpad[DS1820_SCRPADMEM_LEN]
jeremycevrero 0:40a613e7ba83 490 signed char temp_read; //declaration de la variable signe temp_read
jeremycevrero 0:40a613e7ba83 491 float temperature; //declaration de la variable flotante temperature
jeremycevrero 0:40a613e7ba83 492
jeremycevrero 0:40a613e7ba83 493 /* --- start temperature conversion -------------------------------------- */
jeremycevrero 0:40a613e7ba83 494 ow_reset();
jeremycevrero 0:40a613e7ba83 495 DS1820_AddrDevice(DS1820_CMD_MATCHROM); /* address the device */
jeremycevrero 0:40a613e7ba83 496 DQ.output(); //DQ en sortie
jeremycevrero 0:40a613e7ba83 497 DQ=1; //DQ &#65533; 1
jeremycevrero 0:40a613e7ba83 498 write_byte(DS1820_CMD_CONVERTTEMP); /* start conversion */
jeremycevrero 0:40a613e7ba83 499 G__MOS_P=0; //"strong pullup"
jeremycevrero 0:40a613e7ba83 500 /* wait for temperature conversion */
jeremycevrero 0:40a613e7ba83 501 wait_ms(750); // attendre 0.75s
jeremycevrero 0:40a613e7ba83 502 G__MOS_P=1; //"strong pullup"
jeremycevrero 0:40a613e7ba83 503
jeremycevrero 0:40a613e7ba83 504 /* --- read sratchpad ---------------------------------------------------- */
jeremycevrero 0:40a613e7ba83 505 ow_reset();
jeremycevrero 0:40a613e7ba83 506 DS1820_AddrDevice(DS1820_CMD_MATCHROM); /* address the device */
jeremycevrero 0:40a613e7ba83 507 write_byte(DS1820_CMD_READSCRPAD); /* read scratch pad */
jeremycevrero 0:40a613e7ba83 508
jeremycevrero 0:40a613e7ba83 509 /* read scratch pad data */
jeremycevrero 0:40a613e7ba83 510 for (i=0; i < DS1820_SCRPADMEM_LEN; i++) // boucle for : i=0 puis on l'incremante jusqu'a i < DS1820_SCRPADMEM_LEN
jeremycevrero 0:40a613e7ba83 511 scrpad[i] = read_byte();
jeremycevrero 0:40a613e7ba83 512
jeremycevrero 0:40a613e7ba83 513 /* --- calculate temperature --------------------------------------------- */
jeremycevrero 0:40a613e7ba83 514
jeremycevrero 0:40a613e7ba83 515 if (RomBytes[0] == DS1820_FAMILY_CODE_DS18S20)
jeremycevrero 0:40a613e7ba83 516 {
jeremycevrero 0:40a613e7ba83 517 /////////////////////// precision O,5 degC //////////////////////////////////////
jeremycevrero 0:40a613e7ba83 518 /*// temp = (signed int) make16(scrpad[1],scrpad[0]);
jeremycevrero 0:40a613e7ba83 519 temp = (signed int) ((int)scrpad[1]<<8 | (int)scrpad[0]);
jeremycevrero 0:40a613e7ba83 520 temperature =(float)temp/2;*/
jeremycevrero 0:40a613e7ba83 521 /////////////////////////////////////////////////////////////////////////////////
jeremycevrero 0:40a613e7ba83 522
jeremycevrero 0:40a613e7ba83 523 ////////////////////// precision O,1 deg ////////////////////////////////////////
jeremycevrero 0:40a613e7ba83 524 //...calcul....
jeremycevrero 0:40a613e7ba83 525 temp_read=(signed char)(((int)scrpad[1]<<8 | (int)scrpad[0])>>1);
jeremycevrero 0:40a613e7ba83 526 temperature=(float)(temp_read);
jeremycevrero 0:40a613e7ba83 527 temperature = temperature + 0.75 - (float)(scrpad[6]/(float)scrpad[7]);
jeremycevrero 0:40a613e7ba83 528 ////////////////////////////////////////////////////////////////////////////////
jeremycevrero 0:40a613e7ba83 529 }
jeremycevrero 0:40a613e7ba83 530 else//ds18b20
jeremycevrero 0:40a613e7ba83 531 temperature =((float) (signed int)((int)scrpad[1]<<8 | (int)scrpad[0]))/16;
jeremycevrero 0:40a613e7ba83 532
jeremycevrero 0:40a613e7ba83 533 return (temperature);
jeremycevrero 0:40a613e7ba83 534 }
jeremycevrero 0:40a613e7ba83 535 //******************************************************************************
jeremycevrero 0:40a613e7ba83 536 void read_temp()
jeremycevrero 0:40a613e7ba83 537 {
jeremycevrero 0:40a613e7ba83 538 G__MOS_P =1 ; /* initialisation du P-MOS */
jeremycevrero 0:40a613e7ba83 539 FindDevices(); /* fonction cherchant le nombre de ds1820
jeremycevrero 0:40a613e7ba83 540 et leur numero de serie */
jeremycevrero 0:40a613e7ba83 541 if ( First() ) /* fonction if */
jeremycevrero 0:40a613e7ba83 542 {
jeremycevrero 0:40a613e7ba83 543 temperature = DS1820_GetTemp(); /* affectation du resultat de la temperature
jeremycevrero 0:40a613e7ba83 544 a la variable temperature */
jeremycevrero 0:40a613e7ba83 545 pc.printf("\t temp = % +.3g""\xb0""C \r",temperature); /* affichage de la temperature */
jeremycevrero 0:40a613e7ba83 546 }
jeremycevrero 0:40a613e7ba83 547
jeremycevrero 0:40a613e7ba83 548 }