這是用1062,DS18B20顯示一個簡單的時鐘和溫度。下載程序成功后,當時間跑了一分多后,開始亂碼。
- #include<reg52.h>
- #include <intrins.H>
- #include <math.H>
- #define uchar unsigned char
- #define uint unsigned int
- sbit dula=P2^6;
- sbit wela=P2^7;
- sbit rs=P3^5;
- sbit lcden=P3^4;
- sbit s1=P3^0;
- sbit s2=P3^1;
- sbit s3=P3^2;
- sbit rd=P3^7;
- sbit ds = P2^2;//ζè′«¸DÆ÷DÅoÅÏß
- uchar why,s1num;
- char shi,fen,miao;
- //-----------------------------------------------??
- uchar code table[]="wendu:";
- uchar code table1[]="00:00:00";
- void delay(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;
- delay(5);
- lcden=1;
- delay(5);
- lcden=0;
- }
- void write_data(uchar date)
- {
- rs=1;
- lcden=0;
- P0=date;
- delay(5);
- lcden=1;
- delay(5);
- lcden=0;
- }
- void init()
- {
- uchar num;
- dula=0;
- wela=0;
- lcden=0;
- write_com(0x38);
- write_com(0x0c);
- write_com(0x06);
- write_com(0x01);
- write_com(0x80);
- for(num=0;num<6;num++)
- {
- write_data(table[num]);
- delay(20);
- }
- write_com(0x80+0x40);
- for(num=0;num<8;num++)
- {
- write_data(table1[num]);
- delay(20);
- }
- TMOD=0x01;
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- EA=1;
- ET0=1;
- TR0=1;
-
- }
- void write_string(uchar * str, uchar length)
- {
- uchar i;
- for(i = 0; i < length; i++)
- {
- write_data(str[i]);
- }
- }
-
- //-----------------------------------------------........................................................................ê±¼ä
- void write_sfm(uchar add,uchar date)
- {
- uchar shi,ge;
- shi=date/10;
- ge=date%10;
- write_com(0x80+0x40+add);
- write_data(0x30+shi);
- write_data(0x30+ge);
- }
- void keyscan()
- {
- rd=0;
- if(s1==0)
- {
- delay(5);
- if(s1==0)
- {
- s1num++;//????
- while(!s1);//??s1??
- if(s1num==1)
- {
- TR0=0;
- write_com(0x80+0x40+6);//????
- write_com(0x0f);
- }
- if(s1num==2)
- {
- write_com(0x80+0x40+3);//????
- }
- if(s1num==3)
- {
- write_com(0x80+0x40+0);//????
- }
- if(s1num==4)
- {
- s1num=0;
- write_com(0x0c);
- TR0=1;//?????,????,?????
-
- }
- }
-
- }
- if(s1num!=0)
- {
- if(s2==0)
- {
- delay(5);
- if(s2==0)
- {
- while(!s2);//????
- if(s1num==1)//?????????
- {
- miao++;
- if(miao==60)
- miao=0;
- write_sfm(6,miao);
- write_com(0x80+0x40+6);
-
- }
- if(s1num==2)//...?
- {
- fen++;
- if(fen==60)
- fen=0;
- write_sfm(3,fen);
- write_com(0x80+0x40+3);
- }
- if(s1num==3)//...?
- {
- shi++;
- if(shi==24)
- shi=0;
- write_sfm(0,shi);
- write_com(0x80+0x40+0);
- }
- }
- }
- if(s3==0)
- {
- delay(5);
- if(s3==0)
- {
- while(!s3);
- if(s1num==1)//?????????
- {
- miao--;
- if(miao==-1)
- miao=59;
- write_sfm(6,miao);
- write_com(0x80+0x40+6);
- }
- if(s1num==2)//...?
- {
- fen--;
- if(fen==-1)
- fen=59;
- write_sfm(3,fen);
- write_com(0x80+0x40+3);
- }
- if(s1num==3)//...?
- {
- shi--;
- if(shi==-1)
- shi=23;
- write_sfm(0,shi);
- write_com(0x80+0x40+0);
- }
- }
-
- }
- }
- }
- void timer0() interrupt 1
- {
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- why++;
- if(why==20)
- {
- why=0;
- miao++;
- if(miao==60)
- {
- miao=0;
- fen++;
- if(fen==60)
- {
- fen=0;
- shi++;
- if(shi==24)
- {
- shi=0;
- }
- write_sfm(0,shi);
- }
- write_sfm(3,fen);
- }
- write_sfm(6,miao);
- }
-
- }
- //.........................................................................................................
- //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++릏
- void dsInit()//DS18B20¸′룬3õê¼»ˉoˉêy
- {
-
- unsigned int i;
- ds = 0;
- i = 100;
- while(i>0) i--;
- ds = 1;
- i = 4;
- while(i>0) i--;
- }
-
- void dsWait()
- {
- unsigned int i;
- while(ds);
- while(~ds);
- i = 4;
- while(i > 0) i--;
- }
- bit readBit()
- {
- unsigned int i;
- bit b;
- ds = 0;
- i++;
- ds = 1;
- i++; i++;
- b = ds;
- i = 8;
- while(i>0) i--;
- return b;
- }
- unsigned char readByte()
- {
- unsigned int i;
- unsigned char j, dat;
- dat = 0;
- for(i=0; i<8; i++)
- {
- j = readBit();
-
- dat = (j << 7) | (dat >> 1);
- }
- return dat;
- }
- void writeByte(unsigned char dat)
- {
- unsigned int i;
- unsigned char j;
- bit b;
- for(j = 0; j < 8; j++)
- {
- b = dat & 0x01;
- dat >>= 1;
-
- if(b)
- {
- ds = 0; i++; i++;
- ds = 1;
- i = 8; while(i>0) i--;
- }
- else
- {
- ds = 0;
- i = 8; while(i>0) i--;
- ds = 1;
- i++; i++;
- }
- }
- }
- void sendChangeCmd()//¿aê¼»ñè¡Î¶è2¢×a»»
- {
- dsInit();
- dsWait();
- delay(1);
- writeByte(0xcc);
- writeByte(0x44);
- }
- void sendReadCmd()
- {
- dsInit();
- dsWait();
- delay(1);
- writeByte(0xcc);
- writeByte(0xbe);
- }
- int getTmpValue()
- {
- unsigned int tmpvalue;
- int value;
- float t;
- unsigned char low, high;
- sendReadCmd();
-
- low = readByte(); //¶á¸ß°Ëλ
- high = readByte();//¶áμíËÄλ
-
- tmpvalue = high;
- tmpvalue <<= 8;//ὸö×Ö½ú×éoÏÎaò»¸ö×Ö
- tmpvalue |= low;
- value = tmpvalue;
-
- \
- t = value * 0.0625;//ζèÔú¼Ä′æÆ÷ÖDλ12룬·Ö±æÂêÎa0.0625¡ã
-
- value = t * 100 + (value > 0 ? 0.5 : -0.5); //μ±value>0ê±£¬value=t*100+0.5--------
- return value;//valueêÇÕûDÎ
- }
- void display(int v)
- {
- unsigned char count;
- unsigned char datas[] = {0, 0, 0, 0, 0};
- unsigned int tmp = abs(v);
- datas[0] = tmp / 10000;
- datas[1] = tmp % 10000 / 1000;
- datas[2] = tmp % 1000 / 100;
- datas[3] = tmp % 100 / 10;
- datas[4] = tmp % 10;
- write_com(0x80+6);
- if(v < 0)
- {
- write_string("- ", 2);//"-"êÇ×Ö·û′®
- }
- else
- {
- write_string("+ ", 2);
- }
- if(datas[0] != 0)
- {
- write_data('0'+datas[0]);
- }
- for(count = 1; count != 5; count++)
- {
- write_data('0'+datas[count]);
- if(count == 2)
- {
- write_data('.');
- }
- }
- }
- void main()
- {
- uchar table[] = "wendu: ";
- sendChangeCmd();//ζè×a»»oˉêy
- init();
- while(1)
- {
- delay(1000); //750ms
- write_com(0x80);
- display(getTmpValue());
- sendChangeCmd();
-
- keyscan();
- }
- while(1);
- }
復制代碼 |