- UID
- 153166
- 阅读权限
- 10
- 推广
-
- 幽默
-
- 注册时间
- 2010-10-12
- 最后登录
- 1970-1-1
|
注册家电维修技术论坛,与同行畅聊维修技术,享更多技术论坛功能。
您需要 登录 才可以下载或查看,没有帐号?快速注册
x
- #include <AT89X52.H>
- #define uchar unsigned char
- #define uint unsigned int
- #define lcd_date P0 //lcd 数据口
- sbit lcden=P2^7;//lcd
- sbit lcdrs=P2^6;//lcd
- sbit lcdrw=P2^5;//lcd
- void lcd_display();
- void keyscan();
- char second,minute,hour,week,day,month,year;//年的取值范围0~99
- //double temperature;
- uchar code table[]= {"2011:05:06"};//10个字符
- uchar code table1[]={" 18:00:00 W:5"};//15个字符
- sbit key0=P1^0; //按键
- sbit key1=P1^1;
- sbit key2=P1^2;
- sbit key3=P1^3;
- sbit key4=P1^4;
- char flag,flag1,move,control;
- //sbit fu=P1^7;
- sbit DS1302_CLK = P3^6; //时钟引脚
- sbit DS1302_IO = P3^4; //数据引脚
- sbit DS1302_RST = P3^5; //复位引脚
- sbit ACC0 = ACC^0;
- sbit ACC7 = ACC^7;
- #define DS1302_SECOND 0x80
- #define DS1302_MINUTE 0x82
- #define DS1302_HOUR 0x84
- #define DS1302_WEEK 0x8A
- #define DS1302_DAY 0x86
- #define DS1302_MONTH 0x88
- #define DS1302_YEAR 0x8C
- //*****延时******
- void delay(uint z)//定时1ms
- {uint x,y;
- for(x=z;x>0;x--)
- for(y=110;y>0;y--);
- }
- ///****写入一个字节***///
- void DS1302_InputByte(uchar Data)
- { uchar i;
- ACC = Data;
- for(i=8; i>0; i--)
- { DS1302_CLK = 0;
- DS1302_IO = ACC0; //由低位到高位
- DS1302_CLK = 1; //上升沿写入数据
- ACC >>= 1;
- }}
- //读取一个字节(内部函数)
- uchar DS1302_OutputByte(void)
- { uchar i;
- for(i=8; i>0; i--)
- { DS1302_CLK = 0; //下降沿读出DS1302的数据
- ACC >>= 1; //读出由低到高位,只能移7次有效位!!!
- ACC7 = DS1302_IO; //第一个数据
- DS1302_CLK = 1;
- }
- return (ACC);
- }
- //在指定地址写入指定的数据
- void Write1302(uchar Address, uchar Data)
- { DS1302_RST = 0;
- DS1302_CLK = 0;
- DS1302_RST = 1;
- DS1302_InputByte(Address);
- DS1302_InputByte(Data);
- DS1302_CLK = 1;
- DS1302_RST = 0;
- }
- //读取1302指定地址中的数据
- uchar Read1302(uchar Address)
- { uchar Data;
- DS1302_RST = 0;
- DS1302_CLK = 0;
- DS1302_RST = 1; //RST拉高,启动数据传送
- DS1302_InputByte(Address|0x01);//读取指定地址数据指令
- Data = DS1302_OutputByte();
- DS1302_CLK = 1;
- DS1302_RST = 0;
- return (Data);
- }
- //是否写入保护***
- void DS1302_SetProtect(bit Flag)
- { if(Flag)
- Write1302(0x8E,0x80); //0x8e控制字节地址,bit7=WP WP=1 禁止数据写入DS1302
- else
- Write1302(0x8E,0x00); //WP=0 允许数据写入DS1302
- }
- //***设置时间函数*****
- void DS1302_SetTime(uchar Address, uchar Value)
- { DS1302_SetProtect(0);
- Write1302(Address,((Value/10)<<4|(Value%10)));
- DS1302_SetProtect(1);
- }
- //***初始化DS1302******
- void DS1302_init()
- { second=Read1302(DS1302_SECOND);
- if(second&0x80)//bit7=CH CH=0 振荡器允许工作,CH=1振荡器停止工作
- DS1302_SetTime(DS1302_SECOND,0);
- }
- void DS1302_GetTime()
- {
- uchar ReadValue;
- ReadValue = Read1302(DS1302_SECOND);
- second = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0f);
- ReadValue = Read1302(DS1302_MINUTE);
- minute = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0f);
- ReadValue = Read1302(DS1302_HOUR);
- hour = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0f);
- ReadValue = Read1302(DS1302_DAY);
- day = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0f);
- ReadValue = Read1302(DS1302_WEEK);
- week = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0f);
- ReadValue = Read1302(DS1302_MONTH);
- month = ((ReadValue&0x70)>>4)*10 + (ReadValue&0x0f);
- ReadValue = Read1302(DS1302_YEAR);
- year = ((ReadValue&0xF0)>>4)*10 + (ReadValue&0x0f);
- }
- //*****1602写命令****
- void write_com(uchar com)
- { lcdrs=0;
- lcdrw=0;
- lcd_date=com;
- delay(5);//定时(5x1ms=5ms)
- lcden=1;
- delay(5);
- lcden=0;
- }
- //***1602写数据*****
- void write_date(uchar date)
- { lcdrs=1;
- lcdrw=0;
- lcd_date=date;
- delay(5);//定时(5x1ms=5ms)
- lcden=1;
- delay(5);
- lcden=0;
- }
- ///*****初始化*****
- void _1602_init()
- { uchar i;
- lcden=0;
- write_com(0x38);//设置16x2显示5x7. 8位数据接口
- write_com(0x0F);//设置开显示。不显示光标0000 1100
- write_com(0x06);//写一个字符后地址指针自动加1;0000 0110
- write_com(0x01);//显示清0,数据指针清0
- write_com(0x80);
- for(i=0;i<10;i++)
- write_date(table[i]);
- write_com(0x80+0x40);
- for(i=0;i<15;i++)
- write_date(table1[i]);
- //****时间初始化****
- /*
- DS1302_SetTime(DS1302_SECOND,13);
- DS1302_SetTime(DS1302_MINUTE,59);
- DS1302_SetTime(DS1302_HOUR,23);
- DS1302_SetTime(DS1302_WEEK,7);
- DS1302_SetTime(DS1302_DAY,31);
- DS1302_SetTime(DS1302_MONTH,12);
- DS1302_SetTime(DS1302_YEAR,99);
- */
- }
- void main()
- {
- //fu=0;//引出一条地线
- _1602_init();
- DS1302_init(); //由于1302外接晶振32.768khz 即每秒振荡一次
- //所以不要加定时器
- while(1)
- {
-
- DS1302_GetTime(); //获得DS1302时钟数据
- keyscan();
- if(!flag1)
- lcd_display();
-
- }
- }
- //****1602 显示***
- void lcd_display()
- {
- write_com(0x0c);//不显示光标
- write_com(0x80+0x42);//指针定位在小时十位上
- write_date(0x30+hour/10);//刷新 小时的十位
- write_date(0x30+hour%10);//刷新 小时的个位
-
- write_com(0x80+0x45);//指针定位在分钟十位上
- write_date(0x30+minute/10);//刷新 分钟的十位
- write_date(0x30+minute%10);//刷新 分钟的个位
-
- write_com(0x80+0x48);//指针定位在秒钟十位上
- write_date(0x30+second/10);//刷新 秒钟的十位
- write_date(0x30+second%10);//刷新 秒钟的个位
-
- write_com(0x80+0x4e);//指针定位在星期上
- write_date(0x30+week);//刷新 星期
- write_com(0x80+0x02);//指针定位在年十位上
- write_date(0x30+year/10);//刷新 年的十位
- write_date(0x30+year%10);//刷新 年的个位
-
- write_com(0x80+0x05);//指针定位在月十位上
- write_date(0x30+month/10);//刷新 月的十位
- write_date(0x30+month%10);//刷新 月的个位
-
- write_com(0x80+0x08);//指针定位在天十位上
- write_date(0x30+day/10);//刷新 天的十位
- write_date(0x30+day%10);//刷新 天的个位
-
- }
- //*******时间按键调节***
- void keyscan()
- {
- if(key4==0)//调节确定 键
- { control++;
- while(!key4);
- if(control==1)
- { flag=1;//
- flag1=1;//关闭显示扫描
- DS1302_SetTime(DS1302_SECOND,0x80);
-
- }
- if(control==2)
- {
- flag=0;
- flag1=0;
- control=0;
- DS1302_SetTime(DS1302_SECOND,0);
-
- }
- }
- if(flag)
- { if(key0==0)//左移
- {
- move++;
- if(move>6)
- move=1;
- while(!key0);
- write_com(0x0f);//显示光标
-
- switch(move)
- {
- case 1: write_com(0xc6);break;//分 个位
- case 2: write_com(0xc3);break;// 时
- case 3: write_com(0x89);break;//天
- case 4: write_com(0x86);break;//月
- case 5: write_com(0x83);break;// 年
- case 6: write_com(0xce);break;// 星期
- }
- }
- if(key1==0)//右移
- {move--;
- if(move<1)
- move=6;
- while(!key1);
- switch(move)
- {
- case 1: write_com(0xc6);break;//分 个位
- case 2: write_com(0xc3);break;// 时
- case 3: write_com(0x89);break;//天
- case 4: write_com(0x86);break;//月
- case 5: write_com(0x83);break;// 年
- case 6: write_com(0xce);break;// 星期
- }
- }
- if(key2==0)//加数
- {
- if(move==1) //移到分
- { minute++;
- if(minute>=60)
- minute=0;
- DS1302_SetTime(DS1302_MINUTE,minute);
- lcd_display();
- write_com(0x0f);//显示光标
- write_com(0xc6);//指针指在分的各位上
- }
- if(move==2)//移到时
- {
- hour++;
- if(hour>23)
- hour=0;
- DS1302_SetTime(DS1302_HOUR,hour);
- lcd_display();
- write_com(0x0f);//显示光标
- write_com(0xc3);//指针指在时的各位上
-
- }
- if(move==3) //移到天
- {
- day++;
- if(day>31)
- day=1;
- DS1302_SetTime(DS1302_DAY,day);
- lcd_display();
- write_com(0x0f);//显示光标
- write_com(0x89);//指针指在天的各位上
-
- }
- if(move==4) //移到月
- {
- month++;
- if(month>12)
- month=1;
- DS1302_SetTime(DS1302_MONTH,month);
- lcd_display();
- write_com(0x0f);//显示光标
- write_com(0x85);//指针指在月的各位上
- }
- if(move==5) //移到年
- {
- year++;
- if(year>99)
- year=0;
- DS1302_SetTime(DS1302_YEAR,year);
- lcd_display();
- write_com(0x0f);//显示光标
- write_com(0x83);//指针指在年的各位上
-
- }
- if(move==6) //移到星期
- {
- week++;
- if(week>7)
- week=1;
- DS1302_SetTime(DS1302_WEEK,week);
- lcd_display();
- write_com(0x0f);//显示光标
- write_com(0xce);//指针指在星期的各位上
-
- }
- }
- if(key3==0)//减数
- {
- if(move==1)
- {minute--;
- if(minute<0)
- minute=59;
- DS1302_SetTime(DS1302_MINUTE,minute);
- lcd_display();
- write_com(0x0f);//显示光标
- write_com(0xc6);//指针指在分的各位上
- }
- if(move==2)
- {
- hour--;
- if(hour<0)
- hour=23;
- DS1302_SetTime(DS1302_HOUR,hour);
- lcd_display();
- write_com(0x0f);//显示光标
- write_com(0xc3);//指针指在时的各位上
- }
- if(move==3)
- {
- day--;
- if(day<1)
- day=31;
- DS1302_SetTime(DS1302_DAY,day);
- lcd_display();
- write_com(0x0f);//显示光标
- write_com(0x89);//指针指在天的各位上
- }
- if(move==4)
- {
- month--;
- if(month<1)
- month=12;
- DS1302_SetTime(DS1302_MONTH,month);
- lcd_display();
- write_com(0x0f);//显示光标
- write_com(0x85);//指针指在月的各位上
- }
- if(move==5)
- {
- year--;
- if(year<0)
- year=99;
- DS1302_SetTime(DS1302_YEAR,year);
- lcd_display();
- write_com(0x0f);//显示光标
- write_com(0x83);//指针指在年的各位上
- }
- if(move==6)
- {
- week--;
- if(week<1)
- week=7;
- DS1302_SetTime(DS1302_WEEK,week);
- lcd_display();
- write_com(0x0f);//显示光标
- write_com(0xce);//指针指在星期的各位上
- }
- }
- }
- }
复制代码 |
|