基于LCM1602液晶、DS1302时钟芯片、E2PROM的电子时钟设计
时间:11-29
来源:互联网
点击:
历时一个下午的功夫!很多地方可以用循环优化的,但基于简洁,可读性好!故使用传统方法!
By DAVID QQ:1205946980
程序代码下载:http://www.51hei.com/f/1302eep.rar
#include#define uchar unsigned char#define uint unsigned intuchar code tab1[]="*DAVID*";uchar code tab2[]="Week-";uchar code tab3[]="0123456789";uchar wwe[13];uchar xdw[7]={0x00,0x27,0x22,0x01,0x02,0x03,0x12};sbit LCD_RS=P2^0;sbit LCD_RW=P2^1;sbit LCD_EN=P2^2;sbit E2R_SCK=P2^3;sbit E2R_SDA=P2^4;sbit DS1302_RST=P2^5;sbit DS1302_SCK=P2^6;sbit DS1302_DS=P2^7;sbit HC164_SCK=P1^0;sbit HC164_AB=P1^1;void start_24c02_hs();void stop_24c02_hs();void yingda_24c02_hs();void E2R_inti();uchar du_1byte_24c02_hs();void xie_1byte_24c02_hs(uchar dat);void xie_24c02_hs(uchar add,uchar cmd,uchar dat);uchar du_24c02_hs(uchar add,uchar cmd);void init_du_24c02();void DS1302_xie_1byte(uchar dat);void DS1302_xie_hs(uchar cmd,uchar dat);void DS1302_inti();uchar DS1302_du_hs(uchar cmd);uchar DS1302_du_1buyte();void lcm1602_xie_cmd_hs(uchar cmd);void lcm1602_xie_dat_hs(uchar dat);void lcm1602_init();void delay(uint z);void delaysp(uchar t);void cang();void display();void display_inti();/******************************************************************/void delay(uint z){ uint x,y;for(x=z;x>0;x--)for(y=110;y>0;y--);}void delaysp(uchar t){while(t--) ;}void cang(){ uchar seg;seg=DS1302_du_hs(0x81);xdw[0]=seg;wwe[10]=(seg>>4);wwe[11]=(seg&0x0f);seg=DS1302_du_hs(0x83);xdw[1]=seg;wwe[8]=(seg>>4);wwe[9]=(seg&0x0f);seg=DS1302_du_hs(0x85);xdw[2]=seg;wwe[6]=(seg>>4);wwe[7]=(seg&0x0f);seg=DS1302_du_hs(0x87);xdw[3]=seg;wwe[4]=(seg>>4);wwe[5]=(seg&0x0f);seg=DS1302_du_hs(0x89);xdw[4]=seg;wwe[2]=seg>>4;wwe[3]=seg&0x0f;seg=DS1302_du_hs(0x8b);xdw[5]=seg;wwe[12]=seg&0x0f;seg=DS1302_du_hs(0x8d);xdw[6]=seg;wwe[0]=seg>>4;wwe[1]=seg&0x0f;}void display_inti(){uchar i;lcm1602_xie_cmd_hs(0x80+0x00);for(i=0;i<7;i++){lcm1602_xie_dat_hs(tab1[i]);delay(2);}lcm1602_xie_cmd_hs(0x80+0x40);for(i=0;i<5;i++){lcm1602_xie_dat_hs(tab2[i]);delay(2);}}void display(){uchar i,k;lcm1602_xie_cmd_hs(0x80+0x08);for(i=0;i<3;i++){for(k=0;k<2;k++){lcm1602_xie_dat_hs(tab3[wwe[i*2+k]]);}if(i!=2){lcm1602_xie_dat_hs(/);}}lcm1602_xie_cmd_hs(0x80+0x48);for(i=0;i<3;i++){for(k=0;k<2;k++){lcm1602_xie_dat_hs(tab3[wwe[i*2+k+6]]);}if(i!=2){lcm1602_xie_dat_hs(:);}}lcm1602_xie_cmd_hs(0x80+0x45);lcm1602_xie_dat_hs(tab3[wwe[12]]);delay(2);}void lcm1602_xie_cmd_hs(uchar cmd){uchar i,tmp;LCD_RW=0;LCD_RS=0;LCD_EN=1;tmp=cmd;HC164_SCK=0;for(i=0;i<8;i++){HC164_AB=(bit)(tmp&0x01);HC164_SCK=1;HC164_SCK=0;tmp>>=1;}LCD_EN=0;}void lcm1602_xie_dat_hs(uchar dat){uchar i;LCD_RW=0;LCD_RS=1;LCD_EN=1;HC164_SCK=0;for(i=0;i<8;i++){HC164_AB=(bit)(dat&0x01);HC164_SCK=1;HC164_SCK=0;dat>>=1;}LCD_EN=0;}void lcm1602_init(){LCD_RW=0;LCD_RS=0;LCD_EN=0;lcm1602_xie_cmd_hs(0x38);lcm1602_xie_cmd_hs(0x0c);lcm1602_xie_cmd_hs(0x06);}/*************************ds1302*******************************************************/void DS1302_xie_1byte(uchar dat){uchar i;for(i=0;i<8;i++){DS1302_DS=(bit)(dat&0x01);DS1302_SCK=1;DS1302_SCK=0;dat>>=1;}}uchar DS1302_du_1buyte(){uchar i,num;for(i=0;i<8;i++){num>>=1;if(DS1302_DS)num|=0x80;DS1302_SCK=1;DS1302_SCK=0;}return(num);}void DS1302_xie_hs(uchar cmd,uchar dat){DS1302_RST=0;DS1302_SCK=0;DS1302_RST=1;DS1302_xie_1byte(cmd);DS1302_xie_1byte(dat);DS1302_SCK=1;DS1302_RST=0;} uchar DS1302_du_hs(uchar cmd){uchar tmp;DS1302_RST=0;DS1302_SCK=0;DS1302_RST=1;DS1302_xie_1byte(cmd);tmp=DS1302_du_1buyte();DS1302_SCK=1;DS1302_RST=0;return(tmp);}void DS1302_inti(){DS1302_RST=0;DS1302_SCK=0;DS1302_xie_hs(0x8e,0x00);DS1302_xie_hs(0x80,xdw[0]);DS1302_xie_hs(0x82,xdw[1]);DS1302_xie_hs(0x84,xdw[2]);DS1302_xie_hs(0x86,xdw[3]);DS1302_xie_hs(0x88,xdw[4]);DS1302_xie_hs(0x8a,xdw[5]);DS1302_xie_hs(0x8c,xdw[6]);DS1302_xie_hs(0x8e,0x80);}/*************************24c02*******************************************************/void start_24c02_hs(){E2R_SCK=1;delaysp(1);E2R_SDA=1;delaysp(1);E2R_SDA=0;delaysp(1);}void stop_24c02_hs(){E2R_SDA=0;delaysp(1);E2R_SCK=1;delaysp(1);E2R_SDA=1;delaysp(1);}void yingda_24c02_hs(){uchar i;E2R_SCK=1;E2R_SDA=1;delaysp(1);while((E2R_SDA==1)||(i<220)){i++;} E2R_SCK=0;delaysp(1);}void E2R_inti(){E2R_SCK=1;E2R_SDA=1;}uchar du_1byte_24c02_hs(){uchar i,num;E2R_SCK=0;delaysp(1);for(i=0;i<8;i++){num<=1;E2R_SCK=1;delaysp(1);if(E2R_SDA)num|=0x01;E2R_SCK=0;delaysp(1);}return(num);}void xie_1byte_24c02_hs(uchar dat){uchar i;E2R_SCK=0;delaysp(1);for(i=0;i<8;i++){E2R_SDA=(bit)(dat&0x80);E2R_SCK=1;delaysp(1);E2R_SCK=0;delaysp(1);dat<=1;}E2R_SDA=1;delaysp(1);}void xie_24c02_hs(uchar add,uchar cmd,uchar dat){start_24c02_hs();xie_1byte_24c02_hs(add);yingda_24c02_hs();xie_1byte_24c02_hs(cmd);yingda_24c02_hs();xie_1byte_24c02_hs(dat);yingda_24c02_hs();stop_24c02_hs();}uchar du_24c02_hs(uchar add,uchar cmd){uchar tmp;start_24c02_hs();xie_1byte_24c02_hs(add);yingda_24c02_hs();xie_1byte_24c02_hs(cmd);yingda_24c02_hs();start_24c02_hs();xie_1byte_24c02_hs(add|0x01);yingda_24c02_hs();tmp=du_1byte_24c02_hs();stop_24c02_hs();return(tmp);}/*************************************************************************************/void main(){uchar i,j;E2R_inti();lcm1602_init();display_inti();init_du_24c02();DS1302_inti();while(1){cang();for(i=0;i<7;i++){xie_24c02_hs(0xa0,i,xdw[i]);while(j<30){ j++;display();}j=0;} }}void init_du_24c02(){uchar seg;seg=du_24c02_hs(0xa0,0);xdw[0]=seg;seg=du_24c02_hs(0xa0,1);xdw[1]=seg;seg=du_24c02_hs(0xa0,2);xdw[2]=seg;seg=du_24c02_hs(0xa0,3);xdw[3]=seg;seg=du_24c02_hs(0xa0,4);xdw[4]=seg;seg=du_24c02_hs(0xa0,5);xdw[5]=seg;seg=du_24c02_hs(0xa0,6);xdw[6]=seg;}
LCM1602液晶DS1302时钟芯片E2PROM电子时 相关文章:
- Windows CE 进程、线程和内存管理(11-09)
- RedHatLinux新手入门教程(5)(11-12)
- uClinux介绍(11-09)
- openwebmailV1.60安装教学(11-12)
- Linux嵌入式系统开发平台选型探讨(11-09)
- Windows CE 进程、线程和内存管理(二)(11-09)