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

Go to the source code of this file.

Macros

#define DS18B20_DQ   P35
 
#define DS18B20_DQ_PULLUP()   GPIO_SetPullUp(GPIO_Port_3, GPIO_Pin_5, HAL_State_ON)
 
#define DS18B20_DQ_INPUT()   GPIO_P3_SetMode(GPIO_Pin_5, GPIO_Mode_Input_HIP)
 
#define DS18B20_DQ_OUTPUT()   GPIO_P3_SetMode(GPIO_Pin_5, GPIO_Mode_InOut_OD)
 
#define DS18B20_DQ_LOW()   DS18B20_DQ=RESET
 
#define DS18B20_DQ_HIGH()   DS18B20_DQ=SET
 
#define ONEWIRE_CMD_RSCRATCHPAD   0xBE
 
#define ONEWIRE_CMD_WSCRATCHPAD   0x4E
 
#define ONEWIRE_CMD_CPYSCRATCHPAD   0x48
 
#define ONEWIRE_CMD_RECEEPROM   0xB8
 
#define ONEWIRE_CMD_RPWRSUPPLY   0xB4
 
#define ONEWIRE_CMD_SEARCHROM   0xF0
 
#define ONEWIRE_CMD_READROM   0x33
 
#define ONEWIRE_CMD_MATCHROM   0x55
 
#define ONEWIRE_CMD_SKIPROM   0xCC
 
#define DS18B20_FAMILY_CODE   0x28
 
#define DS18B20_CMD_ALARMSEARCH   0xEC
 
#define DS18B20_CMD_CONVERTTEMP   0x44 /* Convert temperature */
 
#define DS18B20_DECIMAL_STEPS_12BIT   0.0625
 
#define DS18B20_DECIMAL_STEPS_11BIT   0.125
 
#define DS18B20_DECIMAL_STEPS_10BIT   0.25
 
#define DS18B20_DECIMAL_STEPS_9BIT   0.5
 
#define DS18B20_RESOLUTION_R1   6
 
#define DS18B20_RESOLUTION_R0   5
 

Enumerations

enum  DS18B20_Resolution_t {
  DS18B20_Resolution_9bits = 9 , DS18B20_Resolution_10bits = 10 , DS18B20_Resolution_11bits = 11 , DS18B20_Resolution_12bits = 12 ,
  DS18B20_Resolution_9bits = 9 , DS18B20_Resolution_10bits = 10 , DS18B20_Resolution_11bits = 11 , DS18B20_Resolution_12bits = 12
}
 

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.
 
uint8_t DS18B20_Crc (uint8_t *addr, uint8_t len)
 8-bit CRC calculation
 
void DS18B20_ReadScratchpad (uint8_t *buf)
 Read SRAM scratchpad.
 
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.
 

Macro Definition Documentation

◆ DS18B20_CMD_ALARMSEARCH

#define DS18B20_CMD_ALARMSEARCH   0xEC

Definition at line 47 of file ds18b20.h.

◆ DS18B20_CMD_CONVERTTEMP

#define DS18B20_CMD_CONVERTTEMP   0x44 /* Convert temperature */

Definition at line 50 of file ds18b20.h.

◆ DS18B20_DECIMAL_STEPS_10BIT

#define DS18B20_DECIMAL_STEPS_10BIT   0.25

Definition at line 53 of file ds18b20.h.

◆ DS18B20_DECIMAL_STEPS_11BIT

#define DS18B20_DECIMAL_STEPS_11BIT   0.125

Definition at line 52 of file ds18b20.h.

◆ DS18B20_DECIMAL_STEPS_12BIT

#define DS18B20_DECIMAL_STEPS_12BIT   0.0625

Definition at line 51 of file ds18b20.h.

◆ DS18B20_DECIMAL_STEPS_9BIT

#define DS18B20_DECIMAL_STEPS_9BIT   0.5

Definition at line 54 of file ds18b20.h.

◆ DS18B20_DQ

#define DS18B20_DQ   P35

DS18B20 - Programmable Resolution 1-Wire Digital Thermometer

To-92 Pins: With the flat side facing you and with the leads pointing down, they are GND, DQ and Vdd

Definition at line 28 of file ds18b20.h.

◆ DS18B20_DQ_HIGH

#define DS18B20_DQ_HIGH ( )    DS18B20_DQ=SET

Definition at line 33 of file ds18b20.h.

◆ DS18B20_DQ_INPUT

#define DS18B20_DQ_INPUT ( )    GPIO_P3_SetMode(GPIO_Pin_5, GPIO_Mode_Input_HIP)

Definition at line 30 of file ds18b20.h.

◆ DS18B20_DQ_LOW

#define DS18B20_DQ_LOW ( )    DS18B20_DQ=RESET

Definition at line 32 of file ds18b20.h.

◆ DS18B20_DQ_OUTPUT

#define DS18B20_DQ_OUTPUT ( )    GPIO_P3_SetMode(GPIO_Pin_5, GPIO_Mode_InOut_OD)

Definition at line 31 of file ds18b20.h.

◆ DS18B20_DQ_PULLUP

#define DS18B20_DQ_PULLUP ( )    GPIO_SetPullUp(GPIO_Port_3, GPIO_Pin_5, HAL_State_ON)

Definition at line 29 of file ds18b20.h.

◆ DS18B20_FAMILY_CODE

#define DS18B20_FAMILY_CODE   0x28

Definition at line 46 of file ds18b20.h.

◆ DS18B20_RESOLUTION_R0

#define DS18B20_RESOLUTION_R0   5

Definition at line 58 of file ds18b20.h.

◆ DS18B20_RESOLUTION_R1

#define DS18B20_RESOLUTION_R1   6

Definition at line 57 of file ds18b20.h.

◆ ONEWIRE_CMD_CPYSCRATCHPAD

#define ONEWIRE_CMD_CPYSCRATCHPAD   0x48

Definition at line 38 of file ds18b20.h.

◆ ONEWIRE_CMD_MATCHROM

#define ONEWIRE_CMD_MATCHROM   0x55

Definition at line 43 of file ds18b20.h.

◆ ONEWIRE_CMD_READROM

#define ONEWIRE_CMD_READROM   0x33

Definition at line 42 of file ds18b20.h.

◆ ONEWIRE_CMD_RECEEPROM

#define ONEWIRE_CMD_RECEEPROM   0xB8

Definition at line 39 of file ds18b20.h.

◆ ONEWIRE_CMD_RPWRSUPPLY

#define ONEWIRE_CMD_RPWRSUPPLY   0xB4

Definition at line 40 of file ds18b20.h.

◆ ONEWIRE_CMD_RSCRATCHPAD

#define ONEWIRE_CMD_RSCRATCHPAD   0xBE

Definition at line 36 of file ds18b20.h.

◆ ONEWIRE_CMD_SEARCHROM

#define ONEWIRE_CMD_SEARCHROM   0xF0

Definition at line 41 of file ds18b20.h.

◆ ONEWIRE_CMD_SKIPROM

#define ONEWIRE_CMD_SKIPROM   0xCC

Definition at line 44 of file ds18b20.h.

◆ ONEWIRE_CMD_WSCRATCHPAD

#define ONEWIRE_CMD_WSCRATCHPAD   0x4E

Definition at line 37 of file ds18b20.h.

Enumeration Type Documentation

◆ DS18B20_Resolution_t

Enumerator
DS18B20_Resolution_9bits 

DS18B20 9 bits resolution

DS18B20_Resolution_10bits 

DS18B20 10 bits resolution

DS18B20_Resolution_11bits 

DS18B20 11 bits resolution

DS18B20_Resolution_12bits 

DS18B20 12 bits resolution

DS18B20_Resolution_9bits 

DS18B20 9 bits resolution

DS18B20_Resolution_10bits 

DS18B20 10 bits resolution

DS18B20_Resolution_11bits 

DS18B20 11 bits resolution

DS18B20_Resolution_12bits 

DS18B20 12 bits resolution

Definition at line 60 of file ds18b20.h.

60 {
DS18B20_Resolution_t
Definition: ds18b20.h:60
@ DS18B20_Resolution_11bits
Definition: ds18b20.h:63
@ DS18B20_Resolution_9bits
Definition: ds18b20.h:61
@ DS18B20_Resolution_10bits
Definition: ds18b20.h:62
@ DS18B20_Resolution_12bits
Definition: ds18b20.h:64

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 53 of file DS18B20_ATY.c.

54{
55 /*1.GPIOC口初始化*/
56 RCC->APB2ENR |= 1 << 3;
57 GPIOB->CRL &= 0x0FFFFFFF;
58 GPIOB->CRL |= 0x30000000;
59 GPIOB->ODR |= 1 << 7;
60
61 /*2.检测DS18B20设备是否正常*/
62 switch(DS18B20_CheckDevice())
63 {
64 case 0: printf("DS18B20_Init OK!\n"); break;
65 case 1: printf("DS18B20设备响应复位信号失败!\n"); break;
66 case 2: printf("DS18B20设备释放总线失败!\n"); break;
67 }
68}
u8 DS18B20_CheckDevice(void)
Definition: DS18B20_ATY.c:118
__WEAK_ATY void printf(uint8_t *,...)

◆ 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
@ RESET
Definition: fw_types.h:84
@ SET
Definition: fw_types.h:85
#define DS18B20_DQ_INPUT()
Definition: ds18b20.h:30
#define DS18B20_DQ
Definition: ds18b20.h:28
#define DS18B20_DQ_OUTPUT()
Definition: ds18b20.h:31

◆ DS18B20_ReadByte()

uint8_t DS18B20_ReadByte ( void  )

Read one byte from DS18B20.

Returns
byte value

Definition at line 143 of file DS18B20_ATY.c.

144{
145 u8 i, data = 0;
146 for(i = 0; i < 8; i++)
147 {
148 DS18B20_OutPut_Mode();//初始化为输出模式
149 DS18B20_OUT = 0;
150 DelayUs(2); //主机拉低总线读数据时间隙2us
151 DS18B20_OUT = 1; //释放总线,准备读取位数据
152 DS18B20_InPut_Mode(); //初始化为输入模式
153 DelayUs(10); //等待DS18B20的数据输出
154 data >>= 1; //高位补0,默认以0为准
155 if(DS18B20_IN) data |= 0x80;
156 DelayUs(60); //延时确保DS18B20采样周期已经过去(非常重要)
157 DS18B20_OUT = 1; //释放总线准备读取下一位位数据
158 }
159 return data;
160}
#define DS18B20_InPut_Mode()
Definition: DS18B20_ATY.h:50
#define DS18B20_OUT
Definition: DS18B20_ATY.h:53
#define DS18B20_OutPut_Mode()
Definition: DS18B20_ATY.h:49
#define DS18B20_IN
Definition: DS18B20_ATY.h:52
void DelayUs(uint32_t us)

◆ 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}