1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
| #include "uart.h" #include "sys.h"
sbit DQ =P3^5;
typedef enum { SEARCH_ROM = 0xf0, READ_ROM = 0x33, MATH_ROM = 0x55, SKIP_ROM = 0xcc, ALARM_SEARCH = 0xec, CONVERT_T = 0x44, WRITE_SCRATCHPAD = 0x4e, READ_SCRATCHPAD = 0xbe, COPY_SCRATCHPAD = 0x48, RECALL_E2 = 0xb8, READ_POWER_SUPPLY = 0xb4, } DS18B20_CMD;
void DS18B20_Reset() { DQ = 0; Delay500us(); DQ = 1;
while(1==DQ); while(0==DQ); }
unsigned char DS18B20_ReadByte() { unsigned char i = 0; unsigned char dat = 0; for (i=0; i<8; i++) { dat >>= 1; DQ = 0; Delay1us(); DQ = 1; Delay1us(); if (DQ) { dat |= 0x80; } Delay60us(); } return dat; }
void DS18B20_WriteByte(unsigned char dat) { char i; for (i=0; i<8; i++) { DQ = 0; Delay1us(); DQ = 0X01 & dat; Delay60us(); DQ = 1; Delay1us(); dat >>= 1; } }
bit DS18B20_read_bit() { bit dat = 0;
DQ = 0; Delay1us(); DQ = 1; Delay1us();; if (DQ) { dat = 1; } Delay60us(); return dat; }
bit DS18B20_write_bit(bit dat) { DQ = 0; Delay1us(); DQ = dat; Delay60us();; DQ = 1; Delay1us(); return dat; }
char DS18B20_ReadTemperature(char *pData, int Size) { unsigned char TPH = 0; unsigned char TPL = 0;
DS18B20_Reset(); DS18B20_WriteByte(SKIP_ROM); DS18B20_WriteByte(CONVERT_T); while (!DQ);
DS18B20_Reset(); DS18B20_WriteByte(SKIP_ROM); DS18B20_WriteByte(READ_SCRATCHPAD); TPL = DS18B20_ReadByte(); TPH = DS18B20_ReadByte(); pData[0] = (TPL >> 4) + (TPH << 4); pData[1] = ((TPL&0X08)>>3)*50+ ((TPL&0X04)>>2)*25 ; return pData[0]; }
char DS18B20_Read_ROM_Temperature(char *pID, int IDSize, char *pData, int Size) { unsigned char TPH = 0; unsigned char TPL = 0; unsigned char i = 0;
DS18B20_Reset(); DS18B20_WriteByte(SKIP_ROM); DS18B20_WriteByte(CONVERT_T); while (!DQ);
DS18B20_Reset(); DS18B20_WriteByte(MATH_ROM); for(i = 0; i < IDSize; i++) { DS18B20_WriteByte(pID[i]); }
DS18B20_WriteByte(READ_SCRATCHPAD); TPL = DS18B20_ReadByte(); TPH = DS18B20_ReadByte(); pData[0] = (TPL >> 4) + (TPH << 4); pData[1] = ((TPL&0X08)>>3)*50+ ((TPL&0X04)>>2)*25 ; return pData[0]; }
unsigned char DS18B20_Search_Rom() { unsigned char i = 0; unsigned char aByte = 0; unsigned char num = 0; unsigned char Stack[10]; unsigned char StackTop; unsigned char Way; unsigned char MarkTheWay[8];
bit FirstBit; bit SecondBit;
StackTop = 0; Stack[StackTop] = 0; num = 0; do { DS18B20_Reset(); DS18B20_WriteByte(SEARCH_ROM); i=0; while(++i) { aByte >>= 1; FirstBit = DS18B20_read_bit(); SecondBit = DS18B20_read_bit();
if(1 == FirstBit && 1 == SecondBit) { num++; break ; } if(0 == FirstBit && 1 == SecondBit) { aByte |= 0X00; DS18B20_write_bit(0); } if(1 == FirstBit && 0 == SecondBit) { aByte |= 0X80; DS18B20_write_bit(1); } if(0 == FirstBit && 0 == SecondBit) { if( i > Stack[StackTop]) { Stack[++StackTop] = i; aByte |= 0X00; DS18B20_write_bit(0); SetBit(MarkTheWay, sizeof(MarkTheWay), i, 0); } else if( i == Stack[StackTop]) { StackTop--; aByte |= 0X80; DS18B20_write_bit(1); SetBit(MarkTheWay, sizeof(MarkTheWay), i, 1); }
else if( i < Stack[StackTop]) { Way = GetBit(MarkTheWay, sizeof(MarkTheWay), i); aByte |= (Way << 7); DS18B20_write_bit(Way); } } if(i%8==0) { SendHEX(aByte); } if(i >= 64) { SendLR(); } } }while(Stack[StackTop] != 0); return num; }
|