- #include <regx51.h>
- #include <intrins.h>
- #include <absacc.h>
- #define uchar unsigned char
- #define uint unsigned int
- #define PA XBYTE[0x0000]
- #define PB XBYTE[0x2000]
- #define PC XBYTE[0x4000]
- #define COM XBYTE[0x6000]
- void delay(uint i){ while(--i);}
- uchar num1=0,num2=0;
- sbit DQ=P2^3;
- sbit jr=P2^4;
- sbit row0=P3^0;sbit row1=P3^1;sbit col0=P3^2;
- sbit col1=P3^3;sbit col2=P3^4;sbit col3=P3^5;
- sbit RS=P2^0; //1=数据;0=命令
- sbit RW=P2^1; //1=读;0=写
- sbit E=P2^2; //1=使能;0=禁止
- #define Dat1602 P0 /** 数据端口 **/
- sbit BF=Dat1602^7; /** 忙信号线 **/
- uchar code zm[][8]={
- 0x1C,0x22,0x32,0x2A,0x26,0x22,0x1C,0x00, // -0-
- 0x08,0x0C,0x08,0x08,0x08,0x08,0x1C,0x00, // -1-
- 0x1C,0x22,0x20,0x18,0x04,0x02,0x3E,0x00, // -2-
- 0x3E,0x20,0x10,0x18,0x20,0x22,0x1C,0x00, // -3-
- 0x10,0x18,0x14,0x12,0x3E,0x10,0x10,0x00, // -4-
- 0x3E,0x02,0x1E,0x20,0x20,0x22,0x1C,0x00, // -5-
- 0x38,0x04,0x02,0x1E,0x22,0x22,0x1C,0x00, // -6-
- 0x3E,0x20,0x10,0x08,0x04,0x04,0x04,0x00, // -7-
- 0x1C,0x22,0x22,0x1C,0x22,0x22,0x1C,0x00, // -8-
- 0x1C,0x22,0x22,0x3C,0x20,0x10,0x0E,0x00, // -9-
- 0x00,0x00,0x04,0x08,0xDE,0x90,0x88,0x9C, //初始化
- 0x00,0x00,0x00,0x00,0x87,0x44,0xF4,0xA4,
- 0x00,0x00,0x00,0x10,0x08,0x24,0x7D,0x00,
- 0x00,0x00,0x04,0x12,0x93,0x52,0x32,0x1A,
- 0xAA,0x88,0x48,0x28,0x08,0x00,0x00,0x00,
- 0x44,0xA4,0x04,0x06,0x04,0x00,0x00,0x00,
- 0x3C,0x24,0x24,0x3C,0x00,0x00,0x00,0x00,
- 0x12,0x12,0x12,0x92,0xE2,0x00,0x00,0x00,
- 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, //不亮 18
- 0x02,0xF5,0x0A,0x08,0x08,0x08,0x08,0xF0, //℃ 19
- };
- uchar key(void)
- {
- uchar k=25,n;
- COM=0x8a;
- PC=0xfe;
- n=PC;
- n&=0xf0;
- if(n!=0xf0)
- {
- switch(n)
- {
- case 0xe0:k=0;break;
- case 0xd0:k=1;break;
- case 0xb0:k=2;break;
- case 0x70:k=3;break;
- }
- while(PC!=0xfe);
- }
- else k=25;
- return k;
- }
- //初始化DS18B20,成功返回0,失败返回1
- bit STA_1820(void){
- bit ack;
- DQ=1;delay(5);
- DQ=0;delay(80); //拉低总线480~960μS
- DQ=1;delay(5); //拉高等待15~60μS
- ack=DQ;delay(50); //读应答位并延时480μS以上
- return ack;
- }
- //读DS18B20一字节,返回读到的数
- char Read_1820(void){
- unsigned char i,dat=0;
- for(i=0;i<8;i++){
- DQ=0; dat>>=1; //拉低,右移兼延时
- DQ=1; if(DQ)dat|=0x80; //拉高,读数据
- delay(7); //延时使总时间>60μS
- }
- return(dat);
- }
- //向DS18B20写一字节,参数为要写的数
- void Write_1820(unsigned char dat){
- unsigned char i;
- for(i=0;i<8;i++)
- {
- DQ=0; //拉低开始
- DQ=dat&0x01; delay(7);//送数并保持60μS以上
- DQ=1; dat>>=1; //拉高,右移准备下一位
- }
- }
- //启动温度转换
- void startTemperature(){
- if(STA_1820())return;
- Write_1820(0xcc);
- Write_1820(0x44);
- }
- //读温度
- int readTemperature(void) {
- unsigned char TCL,TCH;
- STA_1820();
- Write_1820(0xcc);
- Write_1820(0xbe);
- TCL = Read_1820();
- TCH = Read_1820();
- return (TCH*0x100+TCL)*0.0625;
-
- }
- uchar buf[8];
- void dis(){
- uchar i,n;
- uint row=0x01;
- for(i=0;i<16;i++){
- n=i<8?0:1;
- P0=zm[buf[0+4*n]][i-8*n]; col0=1;col0=0;
- P0=zm[buf[1+4*n]][i-8*n]; col1=1;col1=0;
- P0=zm[buf[2+4*n]][i-8*n]; col2=1;col2=0;
- P0=zm[buf[3+4*n]][i-8*n]; col3=1;col3=0;
- P0=row%0x100; row0=1;row0=0;
- P0=row/0x100; row1=1;row1=0;
- delay(50);
- row=_irol_(row,1);
- P0=0;
- row0=row1=col0=col1=col2=col3=1;
- row0=row1=col0=col1=col2=col3=0;
- }
- }
- /** LCM忙检测 **/
- bit busy1602(){
- bit busy=0; //暂存标志
- Dat1602=0xff; //口线置1,防止干扰
- RS=0; RW=1; //置“命令、读”模式
- E=1; E=1; //使能,写两次做短延时
- busy=BF;E=0; //暂存标志,关闭使能
- return busy; //返回忙信号,1=忙
- }
- /** 写命令 参数:comDat为要发送的命令 **/
- void WriteComm(uchar comDat){
- while (busy1602()); //忙,则等待
- RS=0;RW=0; //置“命令、写”模式
- Dat1602=comDat;E=1;E=0; //送出命令,并使之有效
- }
- /** 写数据 参数:dat为要发送的数据 **/
- void WriteData(uchar dat){
- while (busy1602()); //忙,则等待
- RS=1;RW=0; //置“数据、写”模式
- Dat1602=dat;E=1;E=0; //送出数据,并使之有效
- }
- void Init1602(){
- WriteComm(0x38);//8位,两行,5*7点阵
- WriteComm(0x0c);//显示开,光标关
- WriteComm(0x06);//默认,光标右移
- WriteComm(1); //清显示
- }
- uint i=55500,j;
- uint k;
- bit z=0,x=0,c=0,v=0;
- void init()
- {
-
-
- buf[0]=10;buf[1]=11;buf[2]=12;buf[3]=13; //显示初始化
- buf[4]=14;buf[5]=15;buf[6]=16;buf[7]=17;
-
-
- Init1602();
- WriteComm(0x80+4);//光标到第1行,第6位
- WriteData('W');WriteData('e');WriteData('l');WriteData('c');
- WriteData('o');WriteData('m');WriteData('e');WriteData('!');
-
- WriteComm(1); //清屏
- WriteComm(0x80+1);//光标到第1行,第2位
- WriteData('W');WriteData(':');WriteData(' ');WriteData(num1/10%10+'0');WriteData(num1%10+'0');WriteData(' ');
- WriteData('Y');WriteData(':');WriteData(' ');WriteData(num2/10%10+'0');WriteData(num2%10+'0');
- WriteComm(0xc0+1);//光标到第2行,第6位
- WriteData('Z');WriteData('t');WriteData(':');
- WriteData('S');WriteData('t');WriteData('a');WriteData('y');WriteData(' ');
- WriteData('h');WriteData('o');WriteData('t');
- while(i>0){dis();i--;}
- }
- void main(){
- COM=0X8A;
- TMOD=0x01;
- EA=ET0=TR0=1;
- init();
- while(1){
- EA=0; startTemperature(); EA=1; //启动温度转换
- EA=0; j=readTemperature();EA=1; //读温度值
- buf[0]=j/10;buf[1]=j%10;buf[2]=19;buf[3]=18;
- buf[4]=18;buf[5]=18;buf[6]=18;buf[7]=18;
- dis();
- if(key()==0){c=1;x=0;break;}
- if(key()==1){
- WriteComm(1);
- buf[0]=18;buf[1]=18;buf[2]=18;buf[3]=18;
- buf[4]=18;buf[5]=18;buf[6]=18;buf[7]=18;
- dis();
- x=1;
- }
- if(key()==2){
- WriteComm(0xc0+4);//光标到第2行,第1位
- WriteData('P');WriteData('a');WriteData('u');WriteData('s');WriteData('e');WriteData(' ');
- WriteData(' ');WriteData(' ');WriteData(' ');WriteData(' ');WriteData(' ');WriteData(' ');
- v=1;z=0;
- }
- if(key()==3){
- WriteComm(0xc0+4);//光标到第2行,第1位
- WriteData('R');WriteData('u');WriteData('n');WriteData(' ');WriteData(' ');WriteData(' ');
- WriteData(' ');WriteData(' ');WriteData(' ');WriteData(' ');WriteData(' ');WriteData(' ');
- z=1;v=0;
- }
- if(j<45&&z==1&&v==0){jr=0;}
- else jr=1;
- }
- }
- void TIME0() interrupt 1
- {
- TH0=0xfc;
- TL0=0x17;
- //dis();
- }
复制代码
|