仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
这里展示主函数
#include "system.h"
#include "led.h"
#include "ds18b20.h"
//打印浮点型数据,proteus不能有效打印浮点型数据
void PrintfFloat(float value)
{
int tmp,tmp1,tmp2,tmp3,tmp4,tmp5,tmp6;
tmp=(int)value;
tmp1=(int)((value-tmp)*10)%10;
tmp2=(int)((value-tmp)*100)%10;
tmp3=(int)((value-tmp)*1000)%10;
tmp4=(int)((value-tmp)*10000)%10;
tmp5=(int)((value-tmp)*100000)%10;
tmp6=(int)((value-tmp)*1000000)%10;
printf("%d.%d%d%d%d",tmp,tmp1,tmp2,tmp3,tmp4);
}
int main()
{
u8 i=0;
u16 temper;
float value;
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2); //中断优先级分组 分2组
LED_Init();
USART1_Init(115200);
while(DS18B20_Init())
{
printf("DS18B20检测失败,请插好!\r\n");
delay_ms(500);
}
printf("DS18B20检测成功!\r\n");
while(1)
{
i++;
if(i%20==0)
{
LED1=!LED1;
}
if(i%50==0)
{
temper=DS18B20_Get_Temp();
if((temper&0xf800)==0xf800)
{
temper=(~temper)+1;
printf("检测的温度为:- ");
}
else
{
printf("检测的温度为: ");
}
value=(float)temper*0.0625;
PrintfFloat(value);
printf("\r\n");
}
delay_ms(10);
}
}
Keil代码下载:
Keil工程.7z
(313.62 KB, 下载次数: 114)
|