📄 ds1302+lcd1602时钟.txt
字号:
#include "AT89x51.h"
#include "string.h"
#include "intrins.h"
#include "DS1302.H"
#include "LCD1602.H"
unsigned char adj_time_flag; //0代表正常显示,1代表进入时间调整状态,相应位光标闪烁
unsigned char shanshuo_flag; //光标闪烁标志
unsigned char adj_time_section;
unsigned char add_OR_dec_flag; //直加减标志位,0表示加,1表示减
unsigned char time_count;
unsigned char read_rtc_code[7];
unsigned char read_rtc_address[7]={0x8d,0x89,0x87,0x8b,0x85,0x83,0x81};
/********1ms延时子程序***********/
delay_nms(unsigned int n)
{
unsigned int i;
unsigned char j;
for(i=0;i<n;i++)
for(j=0;j<120;j++)
; //空操作
}
void Read_RTC(void) //读出DS1302里的相关信息,存放在read_rtd_code[]数组中
{
unsigned char i,*p;
p=read_rtc_address;
for(i=0;i<7;i++)
{
read_rtc_code[i]=R1302(*p);
p++;
}
}
//时间显示程序
void RTC_display(void)
{
Read_RTC();
LCD_write_char(0,0x02,(read_rtc_code[0]/16)); //年十位
LCD_write_char(0,0x03,(read_rtc_code[0]%16)); //年个位
LCD_write_char(0,0x05,(read_rtc_code[1]/16)); //月十位
LCD_write_char(0,0x06,(read_rtc_code[1]%16)); //月个位
LCD_write_char(0,0x08,(read_rtc_code[2]/16)); //日十位
LCD_write_char(0,0x09,(read_rtc_code[2]%16)); //日个位
LCD_write_char(0,0x0f,(read_rtc_code[3]%16)); //星期
LCD_write_char(1,0x05,(read_rtc_code[4]/16)); //时十位
LCD_write_char(1,0x06,(read_rtc_code[4]%16)); //时个位
LCD_write_char(1,0x08,(read_rtc_code[5]/16)); //分十位
LCD_write_char(1,0x09,(read_rtc_code[5]%16)); //分个位
LCD_write_char(1,0x0b,(read_rtc_code[6]/16)); //秒十位
LCD_write_char(1,0x0c,(read_rtc_code[6]%16)); //秒个位
}
//时间调整显示程序
void adj_time_display(void)
{
Read_RTC();
switch(adj_time_section)
{
case 0x01:
if(shanshuo_flag==1)
lcd_displaystr(0,0x02," ");
else
RTC_display();break;
case 0x02:
if(shanshuo_flag==1)
lcd_displaystr(0,0x05," ");
else
RTC_display(); break;
case 0x03:
if(shanshuo_flag==1)
lcd_displaystr(0,0x08," ");
else
RTC_display();break;
case 0x04:
if(shanshuo_flag==1)
lcd_displaystr(0,0x0f," ");
else
RTC_display();break;
case 0x05:
if(shanshuo_flag==1)
lcd_displaystr(1,0x05," ");
else
RTC_display();break;
case 0x06:
if(shanshuo_flag==1)
lcd_displaystr(1,0x08," ");
else
RTC_display();break;
case 0x07:
if(shanshuo_flag==1)
lcd_displaystr(1,0x0b," ");
else
RTC_display();break;
default:break;
}
}
void system_initial(void)
{
initial_lcd1602(); //初始化LCD1602
}
void main(void)
{
TMOD=0X01;//使用定时器0,16位定时
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
ET0=1;
EA=1;
system_initial();
delay_nms(10);
delay_nms(10);
//Set1302(inittime); //这个是时钟初始化时间,当时钟有掉电电池的时候,
//这一函数只使用一次,也可以不用,就在按键上调整时间就可以了
//输出显示
W1302(0x8e,0x80);//写保护,禁止写操作
{
lcd_displaystr(0,0x00,"20");
delay_nms(10);
lcd_displaystr(0,0x04,"-");
delay_nms(10);
lcd_displaystr(0,0x07,"-");
lcd_displaystr(0,0x0b,"Week:");
lcd_displaystr(1,0x00,"Time ");
lcd_displaystr(1,0x07,":");
lcd_displaystr(1,0x0a,":");
}
while(1)
{
P0=0XFF;
P1=0XFF;
//按键扫描程序
if(P2_0==0)
{
delay_nms(10);
if(P2_0==0)
{
TR0=1;
adj_time_section++; //光标移位
if(adj_time_section==8)
{adj_time_section=0;TR0=0;}
while(P2_0==0);
}
}
if(P2_1==0) //加按键
{
delay_nms(10);
if(P2_1==0)
{
add_OR_dec_flag=0;
Set(adj_time_section,add_OR_dec_flag);//调用时间调整子程序,此程序在DS1302.H文件中
while(P2_1==0);
}
}
if(P2_2==0)
{
delay_nms(10);
if(P2_2==0)
{
add_OR_dec_flag=1;
Set(adj_time_section,add_OR_dec_flag);//调用时间调整子程序,此程序在DS1302.H文件中
while(P2_2==0);
}
}
if(P2_3==0) //调整完毕,按下本按键SW4,闪烁停止
{
delay_nms(10);
if(P2_3==0)
{
adj_time_section=0;
while(P2_3==0);
}
}
if(adj_time_section==0)//正常时间显示
RTC_display();
else //时间调整时候时间显示
adj_time_display();
P0=0XFF;
P1=0XFF;
}
}
void t0_ovf(void) interrupt 1 //中断主要是位在时间调整的时候,调整相应位会闪烁
{
TH0=(65535-50000)/256;
TL0=(65535-50000)%256;
time_count++;
if(time_count==10)
shanshuo_flag=1;
if(time_count==20)
{time_count=0;shanshuo_flag=0;}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -