《单片机设计温度计》由会员分享,可在线阅读,更多相关《单片机设计温度计(10页珍藏版)》请在金锄头文库上搜索。
1、单片机设计温度计第1部分:原理图第2部分:程序代码#include#include#define uchar unsigned char#define uint unsigned int/*七段共阴管显示定义*/uchar code dispcode =0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0xBF,0x86,0xCB,0xCF,0xE F,0xED,0xFD,0x87,0xFF,0xDF;uchar code TAB=0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f;uchar c
2、ode TAB1=0xbf,0x86,0xdb,0xcf,0xe6,0xed,0xfd,0x87,0xff,0xef; /*定义并初始化变量*/uchar seconde=0;uchar minite=0;uchar hour=12;uchar mstcnt=0;uchar flag=0;float tp;sbit DQ=P17;sbit P1_0=P10;/ second 调整定义sbit P1_1=P11;/minite调整定义sbit P1_2=P12;sbit P3_2=P32;void delay(uchar k );/延时子程序void time_pro( );/时间处理子程序vo
3、id display( );/显示子程序void keyscan( );void TMP(void) ;void Time(void);void delayx(uint i)while(i-);void DelayMS(int x) char t; while(x-) for(t=0;t0;i-)/DQ=1;/delay(1);DQ=0;dat=1;DQ=1;if(DQ)dat|=0x80;/读出数据放入高位,最后右移到低位delayx(4);return(dat); write(uchar dat)uchar i=0;for(i=8;i0;i-)DQ=0;DQ=dat/从地位开始写入dela
4、yx(5);DQ=1;dat=1;delayx(4);Readtem(void)uchar a=0;uchar b=0;uchar t=0,t1;init_DS();write(0xcc);/跳过读序列号操作write(0x44);/启动温度转换delayx(125);init_DS();write(0xcc);write(0xbe);/ 读温度寄存器a=Read();/读取温度低位LSBb=Read();/读取温度低位HSBt1=(a/取小数部分t=(b4);/取整数部分if(t128)t=t+1;t=t+t1;return(t);void time_pro( void) if(second
5、e=60) seconde=0;minite+;if( minite=60) minite=0;hour+;if(hour=24)hour=0; void display(void)P2=0xfe;P0=dispcodehour/10;delay(4);P2=0xfd;P0=(dispcode(hour%10)|0X80;delay(4);P2=0xfb;P0=dispcodeminite/10;delay(4);P2=0xf7;P0=(dispcodeminite%10)|0X80;delay(4);P2=0xef;P0=dispcodeseconde/10;delay(4);P2=0xdf
6、;P0=dispcodeseconde%10;delay(4);void keyscan (void)if(P1_0=0) delay(30);if(P1_0=0)while(!P1_0);seconde+;if(seconde=60)seconde=0; if(P1_1=0) delay(30);if(P1_1=0)while(!P1_1);minite+;if(minite=60)minite=0;if(P1_2=0) delay(30);if(P1_2=0)while(!P1_2);hour+;if(hour=24)hour=0; void displayqq(int k)/数码管显示P
7、2=0xf7;P0=TABk%10;DelayMS(5);P2=0xfb;P0=TABk/10%10;DelayMS(5);P2=0xfd;P0=TAB1k/100%10;DelayMS(5);P2=0xfe;P0=TABk/1000;DelayMS(5);void timer0(void) interrupt 1 using 0TH0=0x3c;TMOD = 0x11;mstcnt+;if(mstcnt=20)seconde+;mstcnt=0; void main(void)P1=0xff;TMOD = 0x11; TH0=0x3c;TL0=0xb0;EA=1;ET0=1;EX0=1;TR0=1;while(1)if(flag=0)Time();elseTMP();void Time(void)keyscan( );time_pro( ); display( );void TMP(void) float tp;tp=Readtem();tp=tp*100;/把二位数乘以100变成四位数displayqq(tp);void EX_INT0() interrupt 0while(!P3_2)flag=flag;