我做了一个STM32驱动DS18B20在LCD1602上显示温度,时序我检查了没问题,但是仿真和实物都不行,一直显示0,各位大佬能不能帮帮我,感谢
DS18B20.C
void DS18B20_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_DS18B20_PORT, ENABLE);
GPIO_InitStructure.GPIO_Pin = DS18B20_GPIO_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(DS18B20_GPIO_PORT, &GPIO_InitStructure);
GPIO_SetBits(DS18B20_GPIO_PORT,DS18B20_GPIO_PIN);
}
u8 DS18B20_Init(void)
{
unsigned char wait=0;
DS18B20_IO_OUT();
DS18B20_OUT_0;
delay_us(750);
DS18B20_OUT_1;
delay_us(15);
DS18B20_IO_IN();
while(READ_DS18B20_IO && wait++<200)delay_us(1);
if(wait>=200)return 1;
else wait=0;
while(!READ_DS18B20_IO && wait++<240)delay_us(1);
if(wait>=240)return 1;
else return 0;
}
unsigned char DS18B20_ReadByte(void)
{
unsigned char i;
unsigned char dat = 0;
for (i=0; i<8; i++)
{
dat >>= 1;
DS18B20_IO_OUT();
DS18B20_OUT_0;
delay_us(2);
DS18B20_OUT_1;
DS18B20_IO_IN();
delay_us(12);
if(READ_DS18B20_IO) dat |= 0x80;
delay_us(60);
}
return dat;
}
void DS18B20_WriteByte(unsigned char dat)
{
unsigned char i;
unsigned char temp;
DS18B20_IO_OUT();
for (i=1; i<=8; i++)
{
temp = dat & 0x01;
dat = dat >> 1;
if (temp)
{
DS18B20_OUT_0;
delay_us(2);
DS18B20_OUT_1;
delay_us(60);
}
else
{
DS18B20_OUT_0;
delay_us(60);
DS18B20_OUT_1;
delay_us(2);
}
}
}
float ReadTemperature(void)
{
unsigned char TPH;
unsigned char TPL;
short i16=0;
float f32=0;
DS18B20_Init();
DS18B20_WriteByte(0xCC);
DS18B20_WriteByte(0x44);
DS18B20_Init();
DS18B20_WriteByte(0xCC);
DS18B20_WriteByte(0xBE);
TPL = DS18B20_ReadByte();
TPH = DS18B20_ReadByte();
i16 = 0;
i16 = (TPH<<8) |TPL;
f32 = i16 * 0.0625;
return(f32);
}
main.c调用
temperature=ReadTemperature();
LCD_Write_Char(5,0,temperature/100+0x30);
LCD_Write_Char(6,0,temperature%100/10+0x30);
附件太大了上传不了
|