| #include "hal_ds18b20.h" |
| #include "hal_delay.h" |
| #include "cc2530_ioctl.h" |
| #include "hal_lcd.h" |
| #include <stdio.h> |
| |
| |
| #define HAL_DS18B20_FALSE 0 |
| #define HAL_DS18B20_TRUE 1 |
| |
| |
| #define HAL_DS18B20_DELAY_US(x) delayUsIn32Mhz((x)) |
| #define HAL_DS18B20_DELAY_MS(x) delayMs(SYSCLK_32MHZ ,(x)) |
| |
| |
| #define HAL_DS18B20_IO_OUTPUT() CC2530_IOCTL(HAL_DS18B20_PORT, HAL_DS18B20_PIN, CC2530_OUTPUT) |
| #define HAL_DS18B20_IO_PULLDOWN() CC2530_IOCTL(HAL_DS18B20_PORT, HAL_DS18B20_PIN, CC2530_INPUT_PULLDOWN) |
| #define HAL_DS18B20_IO_PULLUP() CC2530_IOCTL(HAL_DS18B20_PORT, HAL_DS18B20_PIN, CC2530_INPUT_PULLUP) |
| |
| |
| #define HAL_DS18B20_IO_SET(port, pin, level) do { \ |
| if(level) CC2530_GPIO_SET(port, pin); \ |
| else CC2530_GPIO_CLEAR(port, pin); \ |
| } while(0) |
| |
| #define HAL_DS18B20_IO_SET_LO() HAL_DS18B20_IO_SET(HAL_DS18B20_PORT, HAL_DS18B20_PIN, 0) |
| #define HAL_DS18B20_IO_SET_HI() HAL_DS18B20_IO_SET(HAL_DS18B20_PORT, HAL_DS18B20_PIN, 1) |
| |
| |
| #define HAL_DS18B20_IO_GET(port, pin) CC2530_GPIO_GET(port, pin) |
| #define HAL_DS18B20_IO() HAL_DS18B20_IO_GET(HAL_DS18B20_PORT, HAL_DS18B20_PIN) |
| |
| static uint8_t ReadOneChar(void); |
| static void WriteOneChar(uint8_t dat); |
| |
| unsigned char halDS18B20Init(void) |
| { |
| unsigned char x = 0; |
| HAL_DS18B20_IO_OUTPUT(); |
| |
| |
| HAL_DS18B20_IO_SET_HI(); |
| HAL_DS18B20_DELAY_US(74); |
| |
| |
| HAL_DS18B20_IO_SET_LO(); |
| HAL_DS18B20_DELAY_US(750); |
| |
| |
| HAL_DS18B20_IO_SET_HI(); |
| HAL_DS18B20_DELAY_US(50); |
| |
| HAL_DS18B20_IO_PULLUP(); |
| |
| x = HAL_DS18B20_IO(); |
| |
| |
| HAL_DS18B20_DELAY_US(100); |
| |
| return x; |
| } |
| |
| |
| halDS18B20Data_t halDS18B20GetData(void) |
| { |
| uint8_t tempL, tempH, xiaoshu1, xiaoshu2; |
| halDS18B20Data_t ds18b20Dat = { .ok = HAL_DS18B20_FALSE, .fg = 1 }; |
| |
| halDS18B20Init(); |
| HAL_DS18B20_DELAY_US(15); |
| WriteOneChar(0xcc); |
| WriteOneChar(0x44); |
| HAL_DS18B20_DELAY_US(1000); |
| |
| halDS18B20Init(); |
| HAL_DS18B20_DELAY_US(15); |
| WriteOneChar(0xcc); |
| WriteOneChar(0xbe); |
| |
| tempL = ReadOneChar(); |
| tempH = ReadOneChar(); |
| |
| if(tempH & 0xf8) |
| { |
| tempL = ~tempL; |
| tempH = ~tempH+1; |
| ds18b20Dat.fg = 0; |
| } |
| |
| |
| ds18b20Dat.zs = tempL/16+tempH*16; |
| xiaoshu1 = (tempL&0x0f)*10/16; |
| xiaoshu2 = (tempL&0x0f)*100/16%10; |
| ds18b20Dat.xs = xiaoshu1*10+xiaoshu2; |
| |
| |
| if ((!ds18b20Dat.fg && ds18b20Dat.zs <= 55) || (ds18b20Dat.fg && ds18b20Dat.zs <= 125)) |
| { |
| ds18b20Dat.ok = HAL_DS18B20_TRUE; |
| } |
| |
| halDS18B20Init(); |
| return ds18b20Dat; |
| } |
| |
| |
| static uint8_t ReadOneChar(void) |
| { |
| uint8_t i = 0; |
| uint8_t dat = 0; |
| |
| for(i = 8; i>0; i--) |
| { |
| HAL_DS18B20_IO_OUTPUT(); |
| |
| dat>>=1; |
| HAL_DS18B20_IO_SET_LO(); |
| HAL_DS18B20_DELAY_US(2); |
| HAL_DS18B20_IO_SET_HI(); |
| |
| HAL_DS18B20_IO_PULLUP(); |
| HAL_DS18B20_DELAY_US(12); |
| if(HAL_DS18B20_IO()) |
| dat |= 0x80; |
| |
| HAL_DS18B20_DELAY_US(50); |
| } |
| return dat; |
| } |
| |
| |
| static void WriteOneChar(uint8_t dat) |
| { |
| HAL_DS18B20_IO_OUTPUT(); |
| |
| uint8_t i = 0; |
| for(i = 8; i > 0; i--) |
| { |
| HAL_DS18B20_IO_SET_LO(); |
| HAL_DS18B20_DELAY_US(1); |
| |
| if (dat&0x01) HAL_DS18B20_IO_SET_HI(); |
| else HAL_DS18B20_IO_SET_LO(); |
| |
| HAL_DS18B20_DELAY_US(68); |
| |
| HAL_DS18B20_IO_SET_HI(); |
| dat >>= 1; |
| } |
| HAL_DS18B20_DELAY_US(40); |
| } |