C51的數字溫度計仿真原理圖如下(proteus仿真工程文件可到本帖附件中下載)
單片機源程序如下:
- #include<reg52.h>
- #include"1602.h"
- #include"ds18b20.h"
- #define uint unsigned int
- #define uchar unsigned char
- sbit speaker=P2^4;
- sbit red=P2^5;
- sbit green=P2^6;
- sbit key1=P3^0;
- sbit key2=P3^1;
- uint tem;
- int htem,ltem;
- uchar mode;
- uchar code t3[]={" high temp: . C"};
- uchar code t4[]={" low temp: . C "};
- void display(uint dat,uchar add)
- {
- uchar bai,shi ,ge;
- bai=dat/100;
- shi=dat%100/10;
- ge=dat%10;
- writelcd_cmd(add);
- writelcd_dat(bai+0x30);
- writelcd_dat(shi+0x30);
- writelcd_cmd(add+3);
- writelcd_dat(ge+0x30);
- }
- void keyscan()
- { uchar i,j;
- writelcd_cmd(0x80);
- for(i=0;i<16;i++)
- {writelcd_dat(t3[i]);}
- writelcd_cmd(0xc0);
- for(j=0;j<16;j++)
- {writelcd_dat(t4[j]);}
-
-
- while(mode!=0)
- {
- display(htem,0x8b);
- display(ltem,0xca);
- while(mode!=0)
- {
- switch(mode)
- {
- case 1:writelcd_cmd(0xc0+13);writelcd_cmd(0x0f);break;
- case 2:writelcd_cmd(0xc0+11);writelcd_cmd(0x0f);break;
- case 3:writelcd_cmd(0xc0+10);writelcd_cmd(0x0f);break;
- case 4:writelcd_cmd(0x80+14);writelcd_cmd(0x0f);break;
- case 5:writelcd_cmd(0x80+12);writelcd_cmd(0x0f);break;
- case 6:writelcd_cmd(0x80+11);writelcd_cmd(0x0f);break;
- default:mode=0;break;
- }
- if(key1==0)
- {
- delay(10);
- if(key1==0)
- {
- while(key1==0);
- switch(mode)
- {
- case 1:ltem=ltem+1;if(ltem>999)ltem=999;if(ltem>=htem)ltem=htem-1;display(ltem,0xca);writelcd_cmd(0xc0+13);break;
- case 2:ltem=ltem+10;if(ltem>999)ltem=999;if(ltem>=htem)ltem=htem-1;display(ltem,0xca);writelcd_cmd(0xc0+11);break;
- case 3:ltem=ltem+100;if(ltem>999)ltem=999;if(ltem>=htem)ltem=htem-1;display(ltem,0xca);writelcd_cmd(0xc0+10);break;
- case 4:htem=htem+1;if(htem>999)htem=999;if(htem<=ltem)htem=ltem+1;display(htem,0x8b);writelcd_cmd(0xc0+14);break;
- case 5:htem=htem+10;if(htem>999)htem=999;if(htem<=ltem)htem=ltem+1;display(htem,0x8b);writelcd_cmd(0xc0+12);break;
- case 6:htem=htem+100;if(htem>999)htem=999;if(htem<=ltem)htem=ltem+1;display(htem,0x8b);writelcd_cmd(0xc0+11);break;
- default:break;
- }
- }
- }
- if(key2==0)
- {
- delay(10);
- if(key2==0)
- {
- while(key2==0);
- switch(mode)
- {
- case 1:ltem=ltem-1;if(ltem<0)ltem=0;if(ltem>=htem)ltem=htem-1;display(ltem,0xca);writelcd_cmd(0xc0+13);break;
- case 2:ltem=ltem-10;if(ltem<0)ltem=0;if(ltem>=htem)ltem=htem-1;display(ltem,0xca);writelcd_cmd(0xc0+11);break;
- case 3:ltem=ltem-100;if(ltem<0)ltem=0;if(ltem>=htem)ltem=htem-1;display(ltem,0xca);writelcd_cmd(0xc0+10);break;
- case 4:htem=htem-1;if(ltem<0)htem=0;if(htem<=ltem)htem=ltem+1;display(htem,0x8b);writelcd_cmd(0xc0+14);break;
- case 5:htem=htem-10;if(ltem<0)htem=0;if(htem<=ltem)htem=ltem+1;display(htem,0x8b);writelcd_cmd(0xc0+12);break;
- case 6:htem=htem-100;if(ltem<0)htem=0;if(htem<=ltem)htem=ltem+1;display(htem,0x8b);writelcd_cmd(0xc0+11);break;
- default:break;
- }
- }
- }
- }
- }
- lcd_init();
- }
- void init()
- {
- lcd_init();
- speaker=1;
- red=0;
- green=0;
- mode=0;
- htem=360;
- ltem=270;
- EA=1;
- EX0=1;
- ET0=1;
- TMOD=0X01;
- IT0=1;
- TH0=0XFD;
- TL0=0X81;
- }
- void main()
- {
- init();
- while(1)
- {
- if(mode!=0)
- {keyscan();}
- else if(mode==0)
- {
- delay(100);
- tmpchange();
- tem=tmp();
- if(tem<=ltem){red=0;green=1;TR0=1;}
- else if(tem>=htem){green=0;red=1;TR0=1;}
- else
- {
- TR0=0;
- red=0;
- green=0;
- }
- delay(10);
- display(tem,0xca);
- delay(100);
- ……………………
- …………限于本文篇幅 余下代碼請從51黑下載附件…………
復制代碼
Keil5代碼與Proteus7.8仿真下載:
備份_20130528112004.zip
(103.34 KB, 下載次數: 157)
2017-6-19 12:16 上傳
點擊文件名下載附件
|