DS1302+LCD1602时钟文档格式.docx
《DS1302+LCD1602时钟文档格式.docx》由会员分享,可在线阅读,更多相关《DS1302+LCD1602时钟文档格式.docx(19页珍藏版)》请在冰豆网上搜索。
(详细DS1302资料请查看ds1302.pdf文档)
2.原理图:
DS1302连接原理图
LCD1602连接原理图
3.实验板上操作:
1)用三个短路冒将SCLK-P3.5,I/O-P3.6,CE-P3.7短接起来。
2)将板上的相应拨码开关拨到拨到ON上。
(建议把原来做过数码管显示的那个拨码开关全部拨到OFF那边)
3)把LCD1602液晶插到实验板上的LCD1602液晶插槽上。
(提示:
程序烧进单片机后,液晶上没有显示,请调节板子上左上角那个电位器)
4)按一下SW1进入时间调整,关标会闪烁
按一下SW2光标会在年份上闪烁,表明可以对年份进行调整,接着按SW3年份将加一,按SW4年份将减一;
再按一下SW2,光标闪烁移到月份上,同样按SW3月份加一,按SW4月份减一;
再按一下SW2,光标有下移到星期上,同样按SW3星期加一,按SW4星期减一;
同样的道理可以调整时,分,秒。
调整完毕按一下SW1,光标闪烁退出,时间调整结束。
4.实物连接图如下:
5.C语言程序:
#include"
AT89X51.h"
intrins.h"
#defineHIGH1
#defineLOW0
#defineTRUE1
#defineZERO0
#defineMSB0x80
#defineLSB0x01
//ds1302PreDefinition
#defineDS1302_WP0x8E
#defineDS1302_RESETRST=LOW;
SCL=LOW;
RST=HIGH
#defineDS1302_WP_ENABLEWrite_Ds1302(DS1302_WP,0X00)
#defineDS1302_WP_DISENABLEWrite_Ds1302(DS1302_WP,0x00)
#defineLCDPORTP0
sbitSCL=P3^5;
//DS1302Serial-ClockInputpin5
sbitSDA=P3^6;
//DS1302Serial-DataInputpin6
sbitRST=P3^7;
//DS1302Chip-SecletInputpin7
sbitRS=P1^0;
sbitRW=P1^1;
sbitEN=P1^2;
codeunsignedcharset_rtc_code[7]={0x07,0x03,0x29,0x02,0x21,0x07,0x40};
codeunsignedcharwrite_rtc_address[7]={0x8c,0x88,0x86,0x8a,0x84,0x82,0x80};
codeunsignedcharread_rtc_address[7]={0x8d,0x89,0x87,0x8b,0x85,0x83,0x81};
unsignedcharread_rtc_code[7];
//DS1302读出信息(年份,星期,时间)暂存数组,
/********1ms延时子程序***********/
delay_nms(unsignedintn)
{
unsignedinti;
unsignedcharj;
for(i=0;
i<
n;
i++)
for(j=0;
j<
120;
j++)
;
//空操作
}
//*************LCD1602驱动程序***************************//
voidlcd_delay(unsignedchardelaytime)
while(delaytime)delaytime--;
voidwrite_lcd_command(unsignedcharwrite_command)
lcd_delay(40);
RS=0;
RW=0;
LCDPORT=write_command;
EN=1;
EN=0;
voidwrite_lcd_data(unsignedcharwrite_data)
RS=1;
LCDPORT=write_data;
voidLCD_write_char(unsignedx,unsignedchary,unsignedchardat)//x表示行,y表示该行的位置(也就是列)
//LCD_set_xy(x,y);
switch(x)
{
case0:
write_lcd_command(0x80+y);
break;
case1:
write_lcd_command(0xc0+y);
}
//LCD_en_dat(dat);
write_lcd_data(dat|0x30);
//dat或上0x30,就时把数字0123456789转换成ASIIC码
voidlcd_displaystr(unsignedcharx,unsignedchary,unsignedcharcode*str)
while(*str)
write_lcd_data(*str);
str++;
voidinitial_lcd1602(void)//初始化LCD1602
unsignedchari;
EN=0;
for(i=200;
i>
0;
i--)
lcd_delay(248);
write_lcd_command(0x38);
for(i=4;
write_lcd_command(0x0f);
write_lcd_command(0x06);
write_lcd_command(0x01);
//********************lcd1602驱动程序到此完********************//
/////////////////////////////////////////////////////////////////////
//**************ds1302读写程序*************************//
voidWrite_Ds1302_Byte(unsignedchartemp)//写一个字节
for(i=0;
8;
i++)
{
SCL=LOW;
SDA=temp&
LSB;
temp>
>
=1;
SCL=HIGH;
}
/****************************************************************************/
voidWrite_Ds1302(unsignedcharaddress,unsignedchardat)//写ds1302
DS1302_RESET;
Write_Ds1302_Byte(address);
Write_Ds1302_Byte(dat);
RST=LOW;
unsignedcharRead_Ds1302(unsignedcharaddress)//读ds1302
unsignedchari,temp=0x00,temp_temp;
if(SDA)
temp|=0x80;
}
temp_temp=temp/16;
temp=temp%16;
temp=temp+temp_temp*10;
return(temp);
voidRead_RTC(void)//读出DS1302里的相关信息,存放在read_rtd_code[]数组中
unsignedchari,*p;
p=read_rtc_address;
7;
read_rtc_code[i]=Read_Ds1302(*p);
p++;
/***********************************************************************/
voidSet_RTC(void)//初始化设置DS1302
p=write_rtc_address;
Write_Ds1302(*p,set_rtc_code[i]);
/******************************************************/
voidinitial_ds1302(void)//初始化ds1302
DS1302_WP_ENABLE;
Set_RTC();
/**************ds1302读写程序到此完************************/
///////////////////////////////////////////////////
//系统初始化
voidsystem_initial(void)
initial_ds1302();
initial_lcd1602();
voidmain(void)
unsignedcharcount,staus;
system_initial();
//输出显示
lcd_displaystr(0,0x00,"
20"
);
delay_nms(10);
lcd_displaystr(0,0x04,"
-"
lcd_displaystr(0,0x07,"
lcd_displaystr(0,0x0b,"
Week:
"
lcd_displaystr(1,0x00,"
Time:
lcd_displaystr(1,0x07,"
lcd_displaystr(1,0x0a,"
while
(1)
//按键扫描,实现对时间的调整,对应实验板的独立按钮
//开始时间设置,staus=1
if(P2_0==0)
staus^=1;
count=0;
while(P2_0==0);
if(P2_1==0)
count++;
//光标移位
if(count==8)
count=1;
while(P2_1==0);
if(P2_2==0)//加按钮
if(P2_2==0)
switch(count)
{
case0x01:
read_rtc_code[0]++;
DS1302_WP_ENABLE;
Write_Ds1302(write_rtc_address[0],(read_rtc_code[0]+(read_rtc_code[0]/10)*6));
LCD_write_char(0,0x02,(read_rtc_code[0]/10));
//年十位
LCD_write_char(0,0x03,(read_rtc_code[0]%10));
//年个位
break;
case0x02:
read_rtc_code[1]++;
if(read_rtc_code[1]==13)
read_rtc_code[1]=1;
Write_Ds1302(write_rtc_address[1],(read_rtc_code[1]+(read_rtc_code[1]/10)*6));
LCD_write_char(0,0x05,(read_rtc_code[1]/10));
//月十位
LCD_write_char(0,0x06,(read_rtc_code[1]%10));
//月个位
case0x03:
read_rtc_code[2]++;
if(read_rtc_code[2]==31)
read_rtc_code[2]=1;
Write_Ds1302(write_rtc_address[2],(read_rtc_code[2]+(read_rtc_code[2]/10)*6));
LCD_write_char(0,0x08,(read_rtc_code[2]/10));
//日十位
LCD_write_char(0,0x09,(read_rtc_code[2]%10));
//日个位
case0x04:
read_rtc_code[3]++;
if(read_rtc_code[3]==8)
read_rtc_code[3]=1;
Write_Ds1302(write_rtc_address[3],(read_rtc_code[3]+(read_rtc_code[3]/10)*6));
LCD_write_char(0,0x0f,(read_rtc_code[3]%10));
//星期
case0x05:
read_rtc_code[4]++;
if(read_rtc_code[4]==25)
read_rtc_code[4]=0;
Write_Ds1302(write_rtc_address[4],(read_rtc_code[4]+(read_rtc_code[4]/10)*6));
LCD_write_char(1,0x05,(read_rtc_code[4]/10));
//时十位
LCD_write_char(1,0x06,(read_rtc_code[4]%10));
//时个位
case0x06:
read_rtc_code[5]++;
if(read_rtc_code[5]==60)
read_rtc_code[5]=0;
Write_Ds1302(write_rtc_address[5],(read_rtc_code[5]+(read_rtc_code[5]/10)*6));
LCD_write_char(1,0x08,(read_rtc_code[5]/10));
//分十位
LCD_write_char(1,0x09,(read_rtc_code[5]%10));
//分个位
case0x07:
read_rtc_code[6]++;
if(read_rtc_code[6]==60)
read_rtc_code[6]=0;
Write_Ds1302(write_rtc_address[6],(read_rtc_code[6]+(read_rtc_code[6]/10)*6));
LCD_write_char(1,0x0b,(read_rtc_code[6]/10));
//秒十位
LCD_write_char(1,0x0c,(read_rtc_code[6]%10));
//秒个位
break;
default:
break;
}
while(P2_2==0);
if(P2_3==0)//减按钮
if(P2_3==0)
if(read_rtc_code[0]==0)
lcd_displaystr(0,0x00,"
19"
read_rtc_code[0]=99;
}
else
read_rtc_code[0]--;
if(read_rtc_code[1]==1)
read_rtc_code[1]=12;
read_rtc_code[1]--;
if(read_rtc_code[2]==1)
read_rtc_code[2]=30;
else
read_rtc_code[2]--;
if(read_rtc_code[3]==1)
read_rtc_code[3]=7;
read_rtc_code[3]--;
if(read_rtc_code[4]==0)
read_rtc_code[4]=24;
read_rtc_code[4]--;
if(read_rtc_code[5]==00)
read_rtc_code[5]=59;
read_rtc_code[5]--;
Write_Ds