ATY_LIB V2_102_230218
ATY_LIB for general devices or ALGO
 
Loading...
Searching...
No Matches
ds18b20.c File Reference
#include "ds18b20.h"

Go to the source code of this file.

Functions

void DS18B20_Init (void)
 Initialize DS18B20.
 
__BIT DS18B20_Reset (void)
 Reset DS18B20.
 
__BIT DS18B20_ReadBit (void)
 Read one bit from DS18B20.
 
uint8_t DS18B20_ReadByte (void)
 Read one byte from DS18B20.
 
void DS18B20_WriteBit (__BIT b)
 Write one bit to DS18B20.
 
void DS18B20_WriteByte (uint8_t byte)
 Write one byte to DS18B20.
 
void DS18B20_ReadScratchpad (uint8_t *buf)
 Read SRAM scratchpad.
 
uint8_t DS18B20_Crc (uint8_t *addr, uint8_t len)
 8-bit CRC calculation
 
void DS18B20_StartAll (void)
 Start conversion on all slaves.
 
__BIT DS18B20_AllDone (void)
 If read bit is low, then device is not finished yet with calculation temperature.
 
void DS18B20_ReadRom (uint8_t *buf)
 Read 64-bit ROM: 8-bit family code "0x28", unique 48-bit serial number, 8-bit CRC.
 
void DS18B20_Select (const uint8_t *addr)
 Select a slave on the bus.
 
void DS18B20_Start (const uint8_t *addr)
 Start conversion on selected slave.
 
void DS18B20_ReadScratchpadFromAddr (const uint8_t *addr, uint8_t *buf)
 Read SRAM scratchpad from selected slave.
 
uint8_t DS18B20_Search (uint8_t *buff, uint8_t *stack, uint8_t split_point)
 Perform one ROM search.
 

Function Documentation

◆ DS18B20_AllDone()

__BIT DS18B20_AllDone ( void  )

If read bit is low, then device is not finished yet with calculation temperature.

Returns
bit value

Definition at line 183 of file ds18b20.c.

184{
185 /* If read bit is low, then device is not finished yet with calculation temperature */
186 return DS18B20_ReadBit();
187}
__BIT DS18B20_ReadBit(void)
Read one bit from DS18B20.
Definition: ds18b20.c:49

◆ DS18B20_Crc()

uint8_t DS18B20_Crc ( uint8_t addr,
uint8_t  len 
)

8-bit CRC calculation

Parameters
addr
len
Returns
crc result

Definition at line 151 of file ds18b20.c.

152{
153 uint8_t crc = 0, inbyte, i, mix;
154
155 while (len--)
156 {
157 inbyte = *addr++;
158 for (i = 8; i; i--)
159 {
160 mix = (crc ^ inbyte) & 0x01;
161 crc >>= 1;
162 if (mix)
163 {
164 crc ^= 0x8C;
165 }
166 inbyte >>= 1;
167 }
168 }
169 /* Return calculated CRC */
170 return crc;
171}
unsigned char uint8_t
Definition: fw_types.h:18
uint8_t addr[8]
Definition: main.c:28
uint8_t __XDATA i

◆ DS18B20_Init()

void DS18B20_Init ( void  )

Initialize DS18B20.

Definition at line 17 of file ds18b20.c.

18{
19 /* Pull up 2 seconds for possible capacitor charging */
23 SYS_Delay(1000);
25 SYS_Delay(1);
27 SYS_Delay(1000);
28}
void SYS_Delay(uint16_t t)
Definition: fw_sys.c:65
@ RESET
Definition: fw_types.h:84
@ SET
Definition: fw_types.h:85
#define DS18B20_DQ
Definition: ds18b20.h:28
#define DS18B20_DQ_PULLUP()
Definition: ds18b20.h:29
#define DS18B20_DQ_OUTPUT()
Definition: ds18b20.h:31

◆ DS18B20_ReadBit()

__BIT DS18B20_ReadBit ( void  )

Read one bit from DS18B20.

Returns
bit value

Definition at line 49 of file ds18b20.c.

50{
51 __BIT b = RESET;
52
53 /* Line low */
56 SYS_DelayUs(2);
57
58 /* Release line */
60 SYS_DelayUs(10);
61
62 /* Read line value */
63 if (DS18B20_DQ) {
64 /* Bit is HIGH */
65 b = SET;
66 }
67
68 /* Wait 50us to complete 60us period */
69 SYS_DelayUs(50);
70
71 /* Return bit value */
72 return b;
73}
void SYS_DelayUs(uint16_t t)
Definition: fw_sys.c:75
#define DS18B20_DQ_INPUT()
Definition: ds18b20.h:30

◆ DS18B20_ReadByte()

uint8_t DS18B20_ReadByte ( void  )

Read one byte from DS18B20.

Returns
byte value

Definition at line 75 of file ds18b20.c.

76{
77 uint8_t i = 8, byte = 0;
78 while (i--)
79 {
80 byte >>= 1;
81 if (DS18B20_ReadBit())
82 {
83 byte |= 0x80;
84 }
85 }
86 return byte;
87}

◆ DS18B20_ReadRom()

void DS18B20_ReadRom ( uint8_t buf)

Read 64-bit ROM: 8-bit family code "0x28", unique 48-bit serial number, 8-bit CRC.

Note
This command can only be used if there is a single DS18B20 on the bus. If multiple slaves are present, a data collision will occur(a wired AND result).
Parameters
bufa 8-byte buffer

Definition at line 189 of file ds18b20.c.

190{
191 uint8_t i = 0;
192 /* Reset pulse */
194 /* Read rom */
196 /* Get data */
197 for (i = 0; i < 8; i++)
198 {
199 /* Read byte by byte */
200 *buf++ = DS18B20_ReadByte();
201 }
202}
void DS18B20_WriteByte(uint8_t byte)
Write one byte to DS18B20.
Definition: ds18b20.c:121
uint8_t DS18B20_ReadByte(void)
Read one byte from DS18B20.
Definition: ds18b20.c:75
__BIT DS18B20_Reset(void)
Reset DS18B20.
Definition: ds18b20.c:30
#define ONEWIRE_CMD_READROM
Definition: ds18b20.h:42

◆ DS18B20_ReadScratchpad()

void DS18B20_ReadScratchpad ( uint8_t buf)

Read SRAM scratchpad.

Parameters
bufa 9-byte buffer, Byte 8 contains the CRC code for bytes 0 through 7

Definition at line 133 of file ds18b20.c.

134{
135 uint8_t i = 0;
136 /* Reset line */
138 /* Skip ROM */
140 /* Read scratchpad command by onewire protocol */
142
143 /* Get data */
144 for (i = 0; i < 9; i++)
145 {
146 /* Read byte by byte */
147 *buf++ = DS18B20_ReadByte();
148 }
149}
#define ONEWIRE_CMD_SKIPROM
Definition: ds18b20.h:44
#define ONEWIRE_CMD_RSCRATCHPAD
Definition: ds18b20.h:36

◆ DS18B20_ReadScratchpadFromAddr()

void DS18B20_ReadScratchpadFromAddr ( const uint8_t addr,
uint8_t buf 
)

Read SRAM scratchpad from selected slave.

Parameters
addr64-bit ROM code
bufa 9-byte buffer, Byte 8 contains the CRC code for bytes 0 through 7

Definition at line 224 of file ds18b20.c.

225{
226 uint8_t i = 0;
227 /* Reset line */
229 /* Select ROM number */
231 /* Read scratchpad command by onewire protocol */
233
234 /* Get data */
235 for (i = 0; i < 9; i++)
236 {
237 /* Read byte by byte */
238 *buf++ = DS18B20_ReadByte();
239 }
240}
void DS18B20_Select(const uint8_t *addr)
Select a slave on the bus.
Definition: ds18b20.c:204

◆ DS18B20_Reset()

__BIT DS18B20_Reset ( void  )

Reset DS18B20.

Returns
bit value

Definition at line 30 of file ds18b20.c.

31{
32 __BIT b;
33
34 /* Line low, and wait 480us */
37 SYS_DelayUs(500);
38 /* Release line and wait for 70us */
40 SYS_DelayUs(70);
41 /* Check bit value, success if low */
42 b = DS18B20_DQ;
43 /* Delay for 410 us */
44 SYS_DelayUs(410);
45 /* Return value of presence pulse, 0 = OK, 1 = ERROR */
46 return b;
47}

◆ DS18B20_Search()

uint8_t DS18B20_Search ( uint8_t buff,
uint8_t stack,
uint8_t  split_point 
)

Perform one ROM search.

Parameters
buff8-byte array for ROM bytes
stack8-byte array for search stack
split_pointdeepest split point of last search
Returns
new split point

Definition at line 242 of file ds18b20.c.

243{
244 uint8_t len = 64, pos = 0;
245 /* Start from deepest point */
246 split_point = (split_point == 0x00)? 0xFF : split_point;
247 /* Reset line */
249 /* Start searching */
251
252 while (len--)
253 {
254 // Read the value and its complement value of this bit
255 __BIT pb = DS18B20_ReadBit();
256 __BIT cb = DS18B20_ReadBit();
257 if (pb && cb) // no device
258 {
259 return 0;
260 }
261 else if (pb) // bit = 1
262 {
263 *(buff + pos / 8) |= 0x01 << (pos % 8);
265 // confirm: set this bit to 1
266 *(stack + pos / 8) |= 0x01 << (pos % 8);
267 }
268 else if (cb) // bit = 0
269 {
270 *(buff + pos / 8) &= ~(0x01 << (pos % 8));
272 // confirm: set this bit to 1
273 *(stack + pos / 8) |= 0x01 << (pos % 8);
274 }
275 else // bit can be 0 or 1, possible split point
276 {
277 if (split_point == 0xFF || pos > split_point)
278 {
279 // new split point, try 0
280 *(buff + pos / 8) &= ~(0x01 << (pos % 8));
282 // unconfirm: set this bit to 0
283 *(stack + pos / 8) &= ~(0x01 << (pos % 8));
284 // record this new split point
285 split_point = pos;
286 }
287 else if (pos == split_point)
288 {
289 // reach split point, try 1
290 *(buff + pos / 8) |= 0x01 << (pos % 8);
292 // confirm: set this bit to 1
293 *(stack + pos / 8) |= 0x01 << (pos % 8);
294 }
295 else // middle point, use existing bit
296 {
297 DS18B20_WriteBit(*(buff + pos / 8) >> (pos % 8) & 0x01);
298 }
299 }
300 pos++;
301 }
302 // Relocate split point, move it to the last *unconfirmed* bit of stack
303 while (split_point > 0 && *(stack + split_point / 8) >> (split_point % 8) & 0x01 == 0x01) split_point--;
304 return split_point;
305}
uint8_t pos
__XDATA uint8_t buff[7]
Definition: ds3231.c:17
void DS18B20_WriteBit(__BIT b)
Write one bit to DS18B20.
Definition: ds18b20.c:89
#define ONEWIRE_CMD_SEARCHROM
Definition: ds18b20.h:41

◆ DS18B20_Select()

void DS18B20_Select ( const uint8_t addr)

Select a slave on the bus.

Note
Only the slave that exactly matches the 64-bit ROM code sequence will respond to the function command issued by the master; all other slaves on the bus will wait for a reset pulse.
Parameters
addr64-bit ROM code

Definition at line 204 of file ds18b20.c.

205{
206 uint8_t len = 8;
208 while (len--)
209 {
211 }
212}
#define ONEWIRE_CMD_MATCHROM
Definition: ds18b20.h:43

◆ DS18B20_Start()

void DS18B20_Start ( const uint8_t addr)

Start conversion on selected slave.

Parameters
addr64-bit ROM code

Definition at line 214 of file ds18b20.c.

215{
216 /* Reset pulse */
218 /* Select ROM number */
220 /* Start conversion on selected device */
222}
#define DS18B20_CMD_CONVERTTEMP
Definition: ds18b20.h:50

◆ DS18B20_StartAll()

void DS18B20_StartAll ( void  )

Start conversion on all slaves.

Definition at line 173 of file ds18b20.c.

174{
175 /* Reset pulse */
177 /* Skip rom */
179 /* Start conversion on all connected devices */
181}

◆ DS18B20_WriteBit()

void DS18B20_WriteBit ( __BIT  b)

Write one bit to DS18B20.

Parameters
bbit value

Definition at line 89 of file ds18b20.c.

90{
91 if (b)
92 {
93 /* Set line low */
96 SYS_DelayUs(10);
97
98 /* Bit high */
100
101 /* Wait for 55 us and release the line */
102 SYS_DelayUs(55);
104 }
105 else
106 {
107 /* Set line low */
110 SYS_DelayUs(65);
111
112 /* Bit high */
114
115 /* Wait for 5 us and release the line */
116 SYS_DelayUs(5);
118 }
119}

◆ DS18B20_WriteByte()

void DS18B20_WriteByte ( uint8_t  byte)

Write one byte to DS18B20.

Parameters
bytebyte value

Definition at line 121 of file ds18b20.c.

122{
123 uint8_t i = 8;
124 /* Write 8 bits */
125 while (i--)
126 {
127 /* LSB bit is first */
128 DS18B20_WriteBit(byte & 0x01);
129 byte >>= 1;
130 }
131}