基於W80x實現DS18B20驅動。代碼如下;
#include "wm_include.h"
#include "temper.h"
#define DS18B20_SKIP_ROM 0xCC
#define DS18B20_CONVERT_T 0x44
#define DS18B20_READ_SCRATCHPAD 0xBE
static void t_Delay_us(uint16_t num)
{
uint32_t i;
for(i=0;i<(80*num);i++)
{
__NOP();
}
}
static uint8_t DS18B20_IN(void)
{
tls_gpio_cfg(WM_IO_PB_00, WM_GPIO_DIR_INPUT, WM_GPIO_ATTR_FLOATING);
return tls_bitband_read(HR_GPIOB_DATA,WM_IO_PB_00-16); //讀取引腳電平
}
static uint8_t DS18B20_LOW(void)
{
// tls_gpio_cfg(WM_IO_PB_00, WM_GPIO_DIR_OUTPUT, WM_GPIO_ATTR_FLOATING);
tls_bitband_write(HR_GPIOB_DATA, WM_IO_PB_00-16, 0);
}
static uint8_t DS18B20_HIGH(void)
{
// tls_gpio_cfg(WM_IO_PB_00, WM_GPIO_DIR_OUTPUT, WM_GPIO_ATTR_FLOATING);
tls_bitband_write(HR_GPIOB_DATA, WM_IO_PB_00-16, 1);
}
static void set_gpio(void)
{
tls_gpio_cfg(WM_IO_PB_00, WM_GPIO_DIR_OUTPUT, WM_GPIO_ATTR_FLOATING);
}
static uint8_t DS18B20_Init(void)
{
uint8_t AckBit;
DS18B20_LOW();
t_Delay_us(480);
DS18B20_HIGH();
t_Delay_us(70);
AckBit = DS18B20_IN();
t_Delay_us(240);
if(AckBit)
{
/*打印錯誤信息*/
return 1;
}
set_gpio();
return 0;
}
static void DS18B20_WriteBit(uint8_t bit)
{
DS18B20_LOW();
t_Delay_us(10);
if(bit)
{
DS18B20_HIGH();
}
else
{
DS18B20_LOW(); /*可省略*/
}
t_Delay_us(45);
DS18B20_HIGH();
t_Delay_us(2);
}
static void DS18B20_WriteByte(uint8_t byte)
{
uint8_t i;
for(i = 0; i < 8; i++)
{
DS18B20_WriteBit(byte & (0x01 << i));
}
}
uint8_t DS18B20_ReadBit(void)
{
uint8_t bit;
DS18B20_LOW();
t_Delay_us(5);
DS18B20_HIGH();
t_Delay_us(10);
if(DS18B20_IN())
{
bit = 1;
}
else
{
bit = 0;
}
t_Delay_us(45);
set_gpio();
return bit;
}
void DS18B20_ReadByte(uint8_t *byte)
{
uint8_t i;
for(i = 0; i < 8; i++)
{
*byte >>= 1; // 右移一位
if(DS18B20_ReadBit())
{
*byte |= 0x80; // 設置最高位
}
}
}
void DS18B20_Tem_Convert(void)
{
if(DS18B20_Init() == 0)
{
DS18B20_WriteByte(DS18B20_SKIP_ROM);
DS18B20_WriteByte(DS18B20_CONVERT_T);
}
else
{
/*打印錯誤信息*/
printf("%s DS18B20_Init ERROR\n",__FUNCTION__);
}
}
void DS18B20_Read_Tem(float* tem)
{
uint16_t temValue;
uint8_t tem_LSB, tem_MSB;
if(DS18B20_Init() == 0)
{
DS18B20_WriteByte(DS18B20_SKIP_ROM);
DS18B20_WriteByte(DS18B20_READ_SCRATCHPAD);
DS18B20_ReadByte(&tem_LSB);
DS18B20_ReadByte(&tem_MSB);
temValue = (tem_MSB << 8) | tem_LSB;
*tem = (float)temValue / 16;
}
else
{
/*打印錯誤信息*/
printf("%s DS18B20_Init ERROR\n",__FUNCTION__);
}
}
#define TEMP_TASK_PRORITY 3
#define TEMP_TASK_STACK_SIZE 1024
static OS_STK TaskStk[TEMP_TASK_STACK_SIZE/4];
void user_temp_task(void)
{
float temp=0;
uint8_t time=10;
uint8_t flag=0;
uint8_t AckBit;
printf("%s %d \n",__FUNCTION__,__LINE__);
set_gpio();
// tls_gpio_cfg(WM_IO_PA_04, WM_GPIO_DIR_OUTPUT, WM_GPIO_ATTR_FLOATING);
while (1)
{
// tls_os_time_delay(2000);
DS18B20_Tem_Convert();
tls_os_time_delay(1000);
DS18B20_Read_Tem(&temp);
printf("temp = %f \n",temp);
}
}
void user_temp_task_create(void)
{
tls_os_task_create(NULL,
"temperatrue",
user_temp_task,
NULL,
(void *)TaskStk, /* task's stack start address */
TEMP_TASK_STACK_SIZE, /* task's stack size, unit:byte */
TEMP_TASK_PRORITY,
0);
}