单片机源码:
- #include <reg52.h>
- #include <intrins.h>
- #define uchar unsigned char
- #define uint unsigned int
- uchar tab0[]="00.0";
- uchar tab1[]="0123456789";
- uchar tab2[]="xinqi";
- uchar knum;
- uchar miao,miao1,miao2,nian,nian1,nian2,
- fen,fen1,fen2,yue,yue1,yue2,
- shi,shi1,shi2,ri,ri1,ri2,xq,xq1;
- bit flag;
- uint temp; //是否读取时间的标志位
- uchar setn=0; //复位键、增加键、减少键按下次数
- //sbit led1 = P2^4;
- sbit led2 = P0^1;
- sbit SCLK = P2^1;
- sbit IO = P2^2;
- sbit RST = P2^3;
- sbit rs = P3^5;
- sbit rw = P3^3;
- sbit lcden = P3^4;
- sbit ds = P3^7;
- sbit s1 = P2^7;
- sbit s2 = P2^4;
- sbit set= P2^6;
- void delays(uchar t)
- {
- while(t--);
- }
- 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;
- rw = 0;
- P1=com&0xf0;
- delay(2);
- lcden = 1;
- delay(2);
- lcden = 0;
- P1=(com<<4)&0xf0;
- delay(2);
- lcden = 1;
- delay(2);
- lcden = 0;
- }
- void write_data(uchar dat)
- {
- rs = 1;
- rw = 0;
- P1=dat&0xf0;
- delay(2);
- lcden = 1;
- delay(2);
- lcden = 0;
- P1=(dat<<4)&0xf0;
- delay(2);
- lcden = 1;
- delay(2);
- lcden = 0;
- }
- void init_1602()
- {
- write_com(0x32);
- write_com(0x28);
- write_com(0x0c);
- write_com(0x06);
- write_com(0x01);
- }
- void input_1byte(uchar dat)
- {
- uchar i;
- for (i=0; i<8; i++) //8位计数器
- {
- SCLK = 0; //时钟线拉低
- _nop_(); //延时等待
- _nop_();
- dat >>= 1; //移出数据
- IO = CY; //送出到端口
- SCLK = 1; //时钟线拉高
- _nop_(); //延时等待
- _nop_();
- }
- }
- uchar output_1byte()
- {
- uchar i;
- uchar dat = 0;
- for (i=0; i<8; i++) //8位计数器
- {
- SCLK = 0; //时钟线拉低
- _nop_(); //延时等待
- _nop_();
- dat >>= 1; //数据右移一位
- if (IO) dat |= 0x80; //读取数据
- SCLK = 1; //时钟线拉高
- _nop_(); //?óê±μè′y
- _nop_();
- }
- return dat;
- }
- void write_ds1302(uchar add,uchar dat)
- {
- {
- RST = 0;
- _nop_(); //延时等待
- _nop_();
- SCLK = 0;
- _nop_(); //延时等待
- _nop_();
- RST = 1;
- _nop_(); //延时等待
- _nop_();
- input_1byte(add); //写地址
- input_1byte(dat); //写数据
- SCLK = 1;
- RST = 0;
- }
- }
- uchar read_ds1302(uchar add)
- {
- uchar dat;
- RST = 0;
- _nop_();
- _nop_();
- SCLK = 0;
- _nop_();
- _nop_();
- RST = 1;
- _nop_();
- _nop_();
- input_1byte(add);
- dat = output_1byte();
- SCLK = 1;
- RST = 0;
- return dat;
- }
- void ds1302_init()
- {
- write_ds1302(0x8e, 0x00);
- //write_ds1302(0x90, 0xaa);
- write_ds1302(0x80, 0x32); //miao
- write_ds1302(0x82, 0x43); //fen
- write_ds1302(0x84, 0x12); //shi
- write_ds1302(0x86, 0x11); //ri
- write_ds1302(0x88, 0x12); //yue
- write_ds1302(0x8a, 0x02); //xq
- write_ds1302(0x8c, 0x18); //nian
- write_ds1302(0x8e, 0x80);
- }
- void display()
- {
- miao=read_ds1302(0x81);
- miao1=miao/16+'0';
- miao2=miao%16+'0';
- fen=read_ds1302(0x83);
- fen1=fen/16+'0';
- fen2=fen%16+'0';
- shi=read_ds1302(0x85);
- shi1=shi/16+'0';
- shi2=shi%16+'0';
- xq=read_ds1302(0x8b);
- xq1=xq%16+'0';
- nian=read_ds1302(0x8d);
- nian1=nian/16+'0';
- nian2=nian%16+'0';
- yue=read_ds1302(0x89);
- yue1=yue/16+'0';
- yue2=yue%16+'0';
- ri=read_ds1302(0x87);
- ri1=ri/16+'0';
- ri2=ri%16+'0';
- write_com(0x81);
- write_data(shi2);
- write_com(0x80);
- write_data(shi1);
- write_com(0x82);
- write_data(':');
- write_com(0x84);
- write_data(fen2);
- write_com(0x83);
- write_data(fen1);
- write_com(0x85);
- write_data(':');
- write_com(0x87);
- write_data(miao2);
- write_com(0x86);
- write_data(miao1);
- write_com(0xc1);
- write_data(nian2);
- write_com(0xc0);
- write_data(nian1);
- write_com(0xc2);
- write_data('/');
- write_com(0xc4);
- write_data(yue2);
- write_com(0xc3);
- write_data(yue1);
- write_com(0xc5);
- write_data('/');
- write_com(0xc7);
- write_data(ri2);
- write_com(0xc6);
- write_data(ri1);
- write_com(0xcf);
- write_data(xq1);
- }
- uchar change(uchar x)//十进制转换成十六进制
- {
- uchar tep,y;
- y=x;
- tep=y/16;
- y=y%16;
- y=y+tep*10;
- return y;
- }
- void keyscan()
- {
- uchar tem[2],tp;//存储要更改的时间数据
- if(set==0)
- {
- flag=1;//时间停止
- delay(40);//去键盘按下抖动
- if(set==0)
- {
- delay(40);
- while(!set);
- delay(40);//去键盘抬起时抖动
- while(!set);
- delay(40);
- setn++;
- if(setn==1)
- {
- write_ds1302(0x8E,0X00);
- write_ds1302(0x80,0X80);
- write_com(0x87);
- }
- if(setn==2)
- {
- write_com(0x84);
- }
- if(setn==3)
- {
- write_com(0x81);
- }
- if(setn==4)
- {
- write_com(0xc7);
- }
- if(setn==5)
- {
- write_com(0xc4);
- }
- if(setn==6)
- {
- write_com(0xc1);
- }
- if(setn==7)
- {
- write_com(0xcf);
- }
- if(setn==8)
- {
- setn=0;
- write_ds1302(0x8E,0X00); //关闭写保护
- write_ds1302(0x80,0x00);
- write_ds1302(0x8E,0x80); //打开写保护
- flag=0;//时钟正常运行
- write_com(0x0c);//光标停止闪烁
- }
- }
- }
- if(setn!=0)//更改时间
- {
- if(s1==0)//增加时间
- {
- delay(40);
- if(s1==0)
- {
- while(!s1);
- delay(40);//去键盘抬起时抖动
- while(!s1);
- if(setn==2)
- {
- fen=change(fen);
- fen++;
- if(fen==60)
- fen=0;
- {
- tp=fen/10;
- fen=fen%10;
- fen=fen+tp*16;
- write_ds1302(0x8E,0X00);//关闭写保护
- write_ds1302(0x82,fen);
- write_ds1302(0x8E,0x80);//打开写保护
- }
- write_com(0x83);
- tem[0]=fen/16;//数据的转换
- tem[1]=fen&0x0f;
- write_data(0x30+tem[0]);
- write_data(0x30+tem[1]);
- write_com(0x84);
- }
- if(setn==3)
- {
- shi=change(shi);
- shi++;
- if(shi>=24)
- shi=0;
- {
- tp=shi/10;
- shi=shi%10;
- shi=shi+tp*16;
- write_ds1302(0x8E,0X00);//关闭写保护
- write_ds1302(0x84,shi);
- write_ds1302(0x8E,0x80);//打开写保护
- }
- write_com(0x80);
- tem[0]=shi/16;//数据的转换
- tem[1]=shi&0x0f;
- write_data(0x30+tem[0]);
- write_data(0x30+tem[1]);
- write_com(0x81);
- }
- if(setn==4)
- {
- ri=change(ri);
- ri++;
- if(ri>=32)
- ri=0;
- {
- tp=ri/10;
- ri=ri%10;
- ri=ri+tp*16;
- write_ds1302(0x8E,0X00);//关闭写保护
- write_ds1302(0x86,ri);
- write_ds1302(0x8E,0x80);//打开写保护
- }
- write_com(0xc6);
- tem[0]=ri/16;//数据的转换
- tem[1]=ri&0x0f;
- write_data(0x30+tem[0]);
- write_data(0x30+tem[1]);
- write_com(0xc7);
- }
- if(setn==5)
- {
- yue=change(yue);
- yue++;
- if(yue>=13)
- yue=1;
- {
- tp=yue/10;
- yue=yue%10;
- yue=yue+tp*16;
- write_ds1302(0x8E,0X00);//关闭写保护
- write_ds1302(0x88,yue);
- write_ds1302(0x8E,0x80);//打开写保护
- }
- write_com(0xc3);
- tem[0]=yue/16;//数据的转换
- tem[1]=yue&0x0f;
- write_data(0x30+tem[0]);
- write_data(0x30+tem[1]);
- write_com(0xc4);
- }
- if(setn==6)
- {
- nian=change(nian);
- nian++;
- if(nian>=99)
- nian=0;
- {
- tp=nian/10;
- nian=nian%10;
- nian=nian+tp*16;
- write_ds1302(0x8E,0X00);//关闭写保护
- write_ds1302(0x8c,nian);
- write_ds1302(0x8E,0x80);//打开写保护
- }
- write_com(0xc0);
- tem[0]=nian/16;//数据的转换
- tem[1]=nian&0x0f;
- write_data(0x30+tem[0]);
- write_data(0x30+tem[1]);
- write_com(0xc1);
- }
- if(setn==7)
- {
- xq=change(xq);
- xq++;
- if(xq>=8)
- xq=1;
- {
- tp=xq/10;
- xq=xq%10;
- xq=xq+tp*16;
- write_ds1302(0x8E,0X00);//关闭写保护
- write_ds1302(0x8a,xq);
- write_ds1302(0x8E,0x80);//打开写保护
- }
- write_com(0xce);
- tem[0]=xq/16;//数据的转换
- tem[1]=xq&0x0f;
- write_data(0x30+tem[0]);
- write_data(0x30+tem[1]);
- write_com(0xcf);
- }
- }
- }
- if(s2==0)//减少时间
- {
- delay(40);
- if(s2==0)
- {
- while(!s2);
- delay(40);//去键盘抬起时抖动
- while(!s2);
- if(setn==2)
- {
- fen=change(fen);
- if(fen!=0)
- fen--;
- if(fen==0)
- fen=59;
- {
- tp=fen/10;
- fen=fen%10;
- fen=fen+tp*16;
- write_ds1302(0x8E,0X00);//关闭写保护
- write_ds1302(0x82,fen);
- write_ds1302(0x8E,0x80);//打开写保护
- }
- write_com(0x83);
- tem[0]=fen/16;//数据的转换
- tem[1]=fen&0x0f;
- write_data(0x30+tem[0]);
- write_data(0x30+tem[1]);
- write_com(0x84);
- }
- if(setn==3)
- { shi=change(shi);
- if(shi!=0)
- shi--;
- if(shi==0)
- shi=23;
- {
- tp=shi/10;
- shi=shi%10;
- shi=shi+tp*16;
- write_ds1302(0x8E,0X00);//关闭写保护
- write_ds1302(0x84,shi);
- write_ds1302(0x8E,0x80);//打开写保护
- }
- write_com(0x80);
- tem[0]=shi/16;//数据的转换
- tem[1]=shi&0x0f;
- write_data(0x30+tem[0]);
- write_data(0x30+tem[1]);
- write_com(0x81);
- }
- if(setn==4)
- {
- ri=change(ri);
- if(ri!=0)
- ri--;
- if(ri==0)
- ri=31;
- {
- tp=ri/10;
- ri=ri%10;
- ri=ri+tp*16;
- write_ds1302(0x8E,0X00);//关闭写保护
- write_ds1302(0x86,ri);
- write_ds1302(0x8E,0x80);//打开写保护
- }
- write_com(0xc6);
- tem[0]=ri/16;//数据的转换
- tem[1]=ri&0x0f;
- write_data(0x30+tem[0]);
- write_data(0x30+tem[1]);
- write_com(0xc7);
- }
- if(setn==5)
- {
- yue=change(yue);
- if(yue!=0)
- yue--;
- if(yue==0)
- yue=12;
- {
- tp=yue/10;
- yue=yue%10;
- yue=yue+tp*16;
- write_ds1302(0x8E,0X00);//关闭写保护
- write_ds1302(0x88,yue);
- write_ds1302(0x8E,0x80);//打开写保护
- }
- write_com(0xc6);
- tem[0]=yue/16;//数据的转换
- tem[1]=yue&0x0f;
- write_data(0x30+tem[0]);
- write_data(0x30+tem[1]);
- write_com(0xc7);
- }
- if(setn==6)
- {
- nian=change(nian);
- if(nian!=0)
- nian--;
- if(nian==0)
- nian=99;
- {
- tp=nian/10;
- nian=nian%10;
- nian=nian+tp*16;
- write_ds1302(0x8E,0X00);//关闭写保护
- write_ds1302(0x8c,nian);
- write_ds1302(0x8E,0x80);//打开写保护
- }
- write_com(0xc0);
- tem[0]=nian/16;//数据的转换
- tem[1]=nian&0x0f;
- write_data(0x30+tem[0]);
- write_data(0x30+tem[1]);
- write_com(0xc1);
- }
- if(setn==7)
- {
- xq=change(xq);
- if(xq!=0)
- xq--;
- if(xq==0)
- xq=7;
- {
- tp=xq/10;
- xq=xq%10;
- xq=xq+tp*16;
- write_ds1302(0x8E,0X00);//关闭写保护
- write_ds1302(0x8a,xq);
- write_ds1302(0x8E,0x80);//打开写保护
- }
- write_com(0xce);
- tem[0]=xq/16;//数据的转换
- tem[1]=xq&0x0f;
- write_data(0x30+tem[0]);
- write_data(0x30+tem[1]);
- write_com(0xcf);
- }
- }
- }
- }
- }
- /*******************************************************/
- void ds_reset()
- {
- ds=1;
- delays(220);
- ds=0;
- delays(220);
- ds=1;
- delays(85);
- if(ds==0)
- flag=1;
- else
- flag=0;
- delays(130);
- }
- bit ds_read_bit()
- {
- bit dat;
- ds=0;
- _nop_();
- _nop_();
- ds=1;
- _nop_();
- _nop_();
- dat=ds;
- delays(50);
- return dat;
- }
- uchar ds_read_byte()
- {
- uchar i,j,k;
- for(i=0;i<8;i++)
- {
- j= ds_read_bit();
- k=(j<<7)|(k>>1);
- delays(100);
- }
- return k;
- }
- void ds_write_byte(uchar dat)
- {
- uchar i;
- for(i=0;i<8;i++)
- {
- ds=0;
- _nop_();
- _nop_();
- ds=dat&0x01;
- delays(50);
- ds=1;
- dat=dat>>1;
- }
- delays(85);
- }
- uint read_temperture()
- {
- uchar a,b;
- ds_reset();
- ds_write_byte(0xcc);
- ds_write_byte(0xbe);
- a=ds_read_byte();
- b=ds_read_byte();
- temp=b;
- temp=temp<<8;
- temp=temp|a;
- if(b>=0x08)
- {
- temp=~temp+1;
- write_com(0x89);
- write_data('-');
- }
- else
- {
- write_com(0x89);
- write_data(' ');
- }
- temp=temp*0.0625*10+0.5;
- return temp;
- }
- void display3(uint num)
- {
- uchar bai1,shi1,ge1,i;
- bai1=num/100;
- shi1=num%100/10;
- ge1=num%100%10;
- {
- tab0[0]=tab1[bai1];
- tab0[1]=tab1[shi1];
- tab0[3]=tab1[ge1];
- write_com(0x8a);
- for(i=0;i<4;i++)
- {
- write_data(tab0[i]);
- }
- write_com(0x8e);
- write_data(0xdf);
- write_com(0x8f);
- write_data(0x43);
- write_com(0xca);
- for(i=0;i<5;i++)
- {
- write_data(tab2[i]);
- }
- }
- }
- void main()
- {
- init_1602();
- delay(2);
- //ds1302_init();
- //delay(2);
- while(1)
- {
- ds_reset();
- ds_write_byte(0xcc);
- ds_write_byte(0x44);
- display3(read_temperture());
- keyscan();
- display();
- }
- }
复制代码 |