单片机驱动ds18b20四路温度巡回检测程序
时间:08-22
来源:互联网
点击:
#include "reg52.h"//头文件#include "intrins.h"#define uchar unsigned char//宏定义#define uint unsigned int//宏定义sbit led_7=P1^7;//;;;LED秒闪烁sbit DQ=P2^2;////定义I/O的硬件接口--温度uchar code temp[4][8]= {{40,92,34,152,2,0,0,132}, //张{40,42,193,186,2,0,0,77}, //江{40,153,209,186,2,0,0,111}, //斌{40,14,151,7,3,0,0,137}}; //云sbit beep=P3^6;sbit rs=P3^5; sbit lcden=P3^4;sbit key1=P2^4;sbit key2=P2^5;uchar table1[8]={0};uchar fuhao;uchar count=1;void delay1(uchar x2)
//经精准计算,该段程序1次延时时间为 19.53us;多次调用需要重新计算。{while(x2>0){x2--;}}void delay3(uint t){while(t--);}void DB18B20_init()//DB18B20复位{DQ=1;delay1(1);//程序1次延时时间为 19.53usDQ=0;delay1(110);//程序110次延时时间为 965.71usDQ=1;delay1(1);//程序1次延时时间为 19.53uswhile(DQ==1);if(DQ==0)led_7=~led_7;//led检测复位正常否,如果DB18B20存在,且正常;led闪烁。while(DQ==0);delay1(20);//程序20次延时时间为 197.32us}void write_DB18B20(uchar m1)//写DB18B20{uchar i,m2;for(i=0;i8;i++){m2=m1;m2=m20x01;DQ=0; _nop_();DQ=1;if(m2==0x01) DQ=1;elseDQ=0;delay1(5);//程序5次延时时间为 54.25usm1=m1>>1;DQ=1;}}//从总线上取1 个字节uchar read_byte(void){uchar i;uchar value=0;for(i=8;i>0;i--){DQ=1;value>>=1;DQ=0;nop_();_nop_(); //2 usDQ=1;_nop_();_nop_();_nop_();_nop_(); //4 usif(DQ)value|=0x80;delay3(6); //66 us}DQ=1;return(value);}void delay2(uint z) //延时函数{uint x,y;for(x=z;x>0;x--)for(y=110;y>0;y--);}void write_com(uchar com)//液晶写命令{rs=0; //指令选通lcden=0;P0=com; //送指令delay2(5);lcden=1;delay2(5);lcden=0; }void write_date(uchar date)//液晶写数据{rs=1; //数据选通lcden=0; //液晶使能信号P0=date; //送数据delay2(5);lcden=1; delay2(5);lcden=0; }void init() //初始化函数{lcden=0;write_com(0x38); //设置显示模式write_com(0x0c); // 不显示光标write_com(0x06);// 地址指针自动加1write_com(0x01);// 显示开始清屏write_com(0x80); // 设置数据指针write_date(0x35);}void write_sfm(uchar date) //写数据{uchar bai,shi,ge;bai=date/100;shi=date%100/10;ge=date%10;if(bai==0){write_date(0x30+shi);write_date(0x30+ge);}else{ write_date(0x30+bai);write_date(0x30+shi);write_date(0x30+ge);}}void display(uchar dis){ write_com(0x80+0x40);write_date(0x30+dis+1);if(fuhao==1){write_com(0x80+0x40+5);write_date(0x2d);}else write_com(0x80+0x40+5);write_sfm(table1[2*dis]);write_date(0x2e);write_sfm(table1[2*dis+1]);}void read_tmper(uchar tt){ uint twork;uchar i_s;uint zhenshu,xiaoshu;DB18B20_init();delay1(10);write_DB18B20(0xcc);write_DB18B20(0x44);delay1(20);DB18B20_init();delay1(10);write_DB18B20(0x55);for(i_s=0;i_s8;i_s++){write_DB18B20(temp[tt][i_s]);}write_DB18B20(0xbe);table1[2*tt]=read_byte();table1[2*tt+1]=read_byte();twork=table1[2*tt+1];twork=8;twork=twork|table1[2*tt];if(twork0x0800){fuhao=1;twork=~twork;zhenshu=twork>>4;zhenshu=zhenshu0x007f;table1[2*tt]=zhenshu;xiaoshu=twork0x000f;xiaoshu=xiaoshu*625/100;table1[2*tt+1]=xiaoshu;}else{ fuhao=0;zhenshu=twork>>4;zhenshu=zhenshu0x007f;table1[2*tt]=zhenshu;xiaoshu=twork0x000f;xiaoshu=xiaoshu*625/100;table1[2*tt+1]=xiaoshu;}}void keyscan(){if(key1==0){ while(!key1);count++;if(count==5)count=1;}if(key2==0){ while(!key2);count--;if(count==0)count=4;}}void alarm(){if((table1[3]>50count==0)||(table1[3]>10fuhao==1count==0)){beep=~beep;}if((table1[3]>50count==1)||(table1[3]>10fuhao==1count==1)){beep=0;delay1(100);beep=1;}if((table1[3]>50count==2)||(table1[3]>10fuhao==1count==2)){beep=0;delay1(50);beep=1;delay1(50);beep=0;delay1(50);beep=1;}if((table1[3]>50count==3)||(table1[3]>10fuhao==1count==3)){beep=0;delay1(70);beep=1;delay1(70);beep=0;}}void main(){ init();while(1){ read_tmper(0);read_tmper(1);read_tmper(2);read_tmper(3);display(count-1); keyscan();alarm();}}
- FPGA的DSP性能揭秘(06-16)
- 基于单片机通用引脚的软件UART设计(10-16)
- 分时操作系统思想在单片机中的具体应用 (10-30)
- 基于AT89C51+DSP的双CPU伺服运动控制器的研究(05-26)
- 关于RTX51 TINY的分析与探讨(05-30)
- 基于MC9S12DGl28单片机的智能寻迹车设计(04-03)