基于51单片机的12864万年历.docx
《基于51单片机的12864万年历.docx》由会员分享,可在线阅读,更多相关《基于51单片机的12864万年历.docx(20页珍藏版)》请在冰点文库上搜索。
基于51单片机的12864万年历
#include//该程序具有显示日期、月份、日期和时间功能
#include
#include
#defineucharunsignedchar
#defineuintunsignedint
#definesintsignedint
#definedisp_off0x3e//显示关
#definedisp_on0x3f//显示开
#definedisp_x0xb8//页地址为0页
#definedisp_z0xc0//行地址为0行
#definedisp_y0x40//列地址为0列
#definecomm0//命令标志位
#definedat11//数据标志位
#definedata_oraP0//液晶12864的数据端与单片机的P0相连
sbitdi=P2^0;//DataorInstrumentSelect,H:
写数据,L:
写指令
sbitrw=P2^1;//WriteorRead,H:
read,L:
write
sbite=P2^4;//读写使能
sbitcs1=P2^2;//cs1=H,选择左半屏
sbitcs2=P2^3;//cs2=H,选择右半屏
sbitclk=P1^0;//8563clk
sbitdat=P1^1;//8563data
ucharfen=0x42,miao=0x38,shi=0x17,riqi=0x02,//设置时间用
yuefen=0x09,xingqi=0x00,nian=0x07,zhongduan;//设置时间用
ucharcodetabma[10][16]=
//阴码点阵格式、取模方式为列行式、逆向取模(低位在前),
//十六进制输出,中文16*16,英文8*16宋体
{
//0(0)1
(1)2
(2)3(3)4(4)5(5)6(6)7(7)8(8)9(9)
{0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,
0x00,0x0F,0x10,0x20,0x20,0x10,0x0F,0x00},/*"0",0*/
{0x00,0x10,0x10,0xF8,0x00,0x00,0x00,0x00,
0x00,0x20,0x20,0x3F,0x20,0x20,0x00,0x00},/*"1",1*/
{0x00,0x70,0x08,0x08,0x08,0x88,0x70,0x00,
0x00,0x30,0x28,0x24,0x22,0x21,0x30,0x00},/*"2",2*/
{0x00,0x30,0x08,0x88,0x88,0x48,0x30,0x00,
0x00,0x18,0x20,0x20,0x20,0x11,0x0E,0x00},/*"3",3*/
{0x00,0x00,0xC0,0x20,0x10,0xF8,0x00,0x00,
0x00,0x07,0x04,0x24,0x24,0x3F,0x24,0x00},/*"4",4*/
{0x00,0xF8,0x08,0x88,0x88,0x08,0x08,0x00,
0x00,0x19,0x21,0x20,0x20,0x11,0x0E,0x00},/*"5",5*/
{0x00,0xE0,0x10,0x88,0x88,0x18,0x00,0x00,
0x00,0x0F,0x11,0x20,0x20,0x11,0x0E,0x00},/*"6",6*/
{0x00,0x38,0x08,0x08,0xC8,0x38,0x08,0x00,
0x00,0x00,0x00,0x3F,0x00,0x00,0x00,0x00},/*"7",7*/
{0x00,0x70,0x88,0x08,0x08,0x88,0x70,0x00,
0x00,0x1C,0x22,0x21,0x21,0x22,0x1C,0x00},/*"8",8*/
{0x00,0xE0,0x10,0x08,0x08,0x10,0xE0,0x00,
0x00,0x00,0x31,0x22,0x22,0x11,0x0F,0x00},/*"9",9*/
};
ucharcodetab2ma[7][32]=
//日(0)一
(1)二
(2)三(3)四(4)五(5)六(6)
{
{0x00,0x00,0x00,0xFE,0x42,0x42,0x42,0x42,
0x42,0x42,0x42,0xFE,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x3F,0x10,0x10,0x10,0x10,
0x10,0x10,0x10,0x3F,0x00,0x00,0x00,0x00},/*"日",0*/
{0x00,0x80,0x80,0x80,0x80,0x80,0x80,0x80,
0x80,0x80,0x80,0x80,0x80,0xC0,0x80,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00},/*"一",1*/
{0x00,0x00,0x04,0x04,0x04,0x04,0x04,0x04,
0x04,0x04,0x04,0x06,0x04,0x00,0x00,0x00,
0x00,0x10,0x10,0x10,0x10,0x10,0x10,0x10,
0x10,0x10,0x10,0x10,0x10,0x18,0x10,0x00},/*"二",2*/
{0x00,0x04,0x84,0x84,0x84,0x84,0x84,0x84,
0x84,0x84,0x84,0x84,0x84,0x04,0x00,0x00,
0x00,0x20,0x20,0x20,0x20,0x20,0x20,0x20,
0x20,0x20,0x20,0x20,0x20,0x20,0x20,0x00},/*"三",3*/
{0x00,0xFE,0x02,0x02,0x02,0xFE,0x02,0x02,
0xFE,0x02,0x02,0x02,0x02,0xFE,0x00,0x00,
0x00,0x7F,0x28,0x24,0x23,0x20,0x20,0x20,
0x21,0x22,0x22,0x22,0x22,0x7F,0x00,0x00},/*"四",4*/
{0x00,0x02,0x82,0x82,0x82,0x82,0xFE,0x82,
0x82,0x82,0xC2,0x82,0x02,0x00,0x00,0x00,
0x20,0x20,0x20,0x20,0x20,0x3F,0x20,0x20,
0x20,0x20,0x3F,0x20,0x20,0x30,0x20,0x00},/*"五",5*/
{0x10,0x10,0x10,0x10,0x10,0x91,0x12,0x1E,
0x94,0x10,0x10,0x10,0x10,0x10,0x10,0x00,
0x00,0x40,0x20,0x10,0x0C,0x03,0x01,0x00,
0x00,0x01,0x02,0x0C,0x78,0x30,0x00,0x00},/*"六",6*/
};
ucharcodenianma[]=
//年(0)月
(1)日
(2)星(3)期(4)
{0x40,0x20,0x10,0x0C,0xE3,0x22,0x22,0x22,
0xFE,0x22,0x22,0x22,0x22,0x02,0x00,0x00,
0x04,0x04,0x04,0x04,0x07,0x04,0x04,0x04,
0xFF,0x04,0x04,0x04,0x04,0x04,0x04,0x00};/*"年",0*/
ucharcodeyuema[]=
{0x00,0x00,0x00,0x00,0x00,0xFF,0x11,0x11,
0x11,0x11,0x11,0xFF,0x00,0x00,0x00,0x00,
0x00,0x40,0x20,0x10,0x0C,0x03,0x01,0x01,
0x01,0x21,0x41,0x3F,0x00,0x00,0x00,0x00};/*"月",1*/
ucharcoderima[]=
{0x00,0x00,0x00,0xFE,0x42,0x42,0x42,0x42,
0x42,0x42,0x42,0xFE,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x3F,0x10,0x10,0x10,0x10,
0x10,0x10,0x10,0x3F,0x00,0x00,0x00,0x00};/*"日",2*/
ucharcodexingqima[]=
{0x00,0x00,0x00,0xBE,0x2A,0x2A,0x2A,0xEA,
0x2A,0x2A,0x2A,0x2A,0x3E,0x00,0x00,0x00,
0x00,0x48,0x46,0x41,0x49,0x49,0x49,0x7F,
0x49,0x49,0x49,0x49,0x49,0x41,0x40,0x00,/*"星",3*/
0x00,0x04,0xFF,0x54,0x54,0x54,0xFF,0x04,
0x00,0xFE,0x22,0x22,0x22,0xFE,0x00,0x00,
0x42,0x22,0x1B,0x02,0x02,0x0A,0x33,0x62,
0x18,0x07,0x02,0x22,0x42,0x3F,0x00,0x00};/*"期",4*/
//时(0)分
(1)秒
(2)
ucharcodeshima[]=
{0x00,0xFC,0x44,0x44,0x44,0xFC,0x10,0x90,
0x10,0x10,0x10,0xFF,0x10,0x10,0x10,0x00,
0x00,0x07,0x04,0x04,0x04,0x07,0x00,0x00,
0x03,0x40,0x80,0x7F,0x00,0x00,0x00,0x00};/*"时",0*/
ucharcodefenma[]=
{0x80,0x40,0x20,0x98,0x87,0x82,0x80,0x80,
0x83,0x84,0x98,0x30,0x60,0xC0,0x40,0x00,
0x00,0x80,0x40,0x20,0x10,0x0F,0x00,0x00,
0x20,0x40,0x3F,0x00,0x00,0x00,0x00,0x00};/*"分",1*/
ucharcodemiaoma[]=
{0x12,0x12,0xD2,0xFE,0x91,0x11,0xC0,0x38,
0x10,0x00,0xFF,0x00,0x08,0x10,0x60,0x00,
0x04,0x03,0x00,0xFF,0x00,0x83,0x80,0x40,
0x40,0x20,0x23,0x10,0x08,0x04,0x03,0x00};/*"秒",2*/
voiddelay(sintn);//延时程序
voidxie_start();//开始条件
voidxie_stop();//停止条件
bitxie1(ucharshu);//向8563中写入1个字
uchardu1();//从8563中读出1个字
uchardu();//从8563中读出时间和日期
ucharxie();//向8563中设置时间和日期
//ucharxie_dingshi();//8563定时器设置
//ucharxie_fangbo_dingshi();//8563的方波设置
voiddelay1(uintms);//延时
voidwr_lcd(uchardat_comm,ucharcontent);//向12864中写命令
//ucharrd_lcd();//读12864数据
voidchk_busy();//忙闲检测
voidlat_disp(uchardata1,uchardata2);//写点钟
//voidimg_disp(ucharcode*img);//显示图像
voidchn_disp(ucharx,uchary,ucharxl,ucharyl,ucharrow_xl,ucharrow_yl,ucharcode*chn);
//显示汉字
voidinit_lcd();//12864初始化
voiddisp();//128显示程序
//-------------主函数--------------------
voidmain()
{
ucharml=0;
init_lcd();//LCD初始化
lat_disp(0x00,0x00);
xie();//预设时间用,设置好时间后可以屏蔽
for(;;)
{
du();//读8563的时间
lat_disp(0x00,0x00);
disp();//显示时间
delay1(100);
}
}
//-------------------延时---------------------------//
voiddelay(sintn)
{
for(n;n>0;n--);
}
/////////////////////以下是PCF8563读写/////////////////////以下是PCF8563读写
/*I2C的起始条件,在时钟端SCK为高电平时,数据端SDA发生由高到低的变化,为起始条件,
启动I2C总线。
程序代码如下。
*/
voidxie_start()
{
clk=1;_nop_();
dat=1;_nop_();
dat=0;_nop_();
clk=0;_nop_();
}
/*I2C的停止条件,在时钟端SCL为高电平时,数据端SDA发生由低到高的变化,
为停止条件,停止I2C总线数据传输。
*/
voidxie_stop()
{
dat=0;_nop_();
clk=1;_nop_();
dat=1;_nop_();
}
bitxie1(ucharshu)//向8563中写入1个字
{
uchari=8;
bitfan_w;
clk=0;_nop_();
while(i--)
{
dat=(bit)(shu&0x80);//取字节的最高位
_nop_();
shu<<=1;//字节左移一位
clk=1;//上升沿锁存数据
_nop_();
clk=0;
_nop_();
}
dat=1;
_nop_();
clk=1;
_nop_();
fan_w=dat;//读应答信号
clk=0;
_nop_();
return(fan_w);//返回应答信号
}
uchardu1()//从8563中读出1个字
{
ucharshu=0;
uchardatardata;
uchari=8;
while(i--)
{
rdata<<=1;//字节左移一位
dat=1;
clk=1;_nop_();//上升沿锁存数据
if(dat)rdata|=0x01;//数据线为1时对应位存1
clk=0;_nop_();
}
shu=rdata;//读到的数据存于SHU
return(shu);//返回读到的数据
}
uchardu()
{
EA=0;//关总中断
xie_start();//起始条件
if(xie1(0xa2)!
=0){xie_stop();return(0);}//取器件地址写
if(xie1(0x02)!
=0){xie_stop();return(0);}//取读时间的首字节地址从秒开始读
xie_start();//起始条件
if(xie1(0xa3)!
=0){xie_stop();return(0);}//取器件地址读
miao=du1()&0x7f;//读秒屏蔽最高位
xie_stop();//停止条件
xie_start();//起始条件
if(xie1(0xa2)!
=0){xie_stop();return(0);}//取器件地址写
if(xie1(0x03)!
=0){xie_stop();return(0);}//取读分钟字节地址
xie_start();//起始条件
if(xie1(0xa3)!
=0){xie_stop();return(0);}//取器件地址读
fen=du1()&0x7f;//读分屏蔽最高位
xie_stop();//停止条件
xie_start();//起始条件
if(xie1(0xa2)!
=0){xie_stop();return(0);}//取器件地址写
if(xie1(0x04)!
=0){xie_stop();return(0);}//取读小时字节地址
xie_start();//起始条件
if(xie1(0xa3)!
=0){xie_stop();return(0);}//取器件地址读
shi=du1();//读时
shi&=0x3f;//屏蔽最高2位
xie_stop();//停止条件
xie_start();//起始条件
if(xie1(0xa2)!
=0){xie_stop();return(0);}//写
if(xie1(0x05)!
=0){xie_stop();return(0);}//取读日期字节地址
xie_start();//起始条件
if(xie1(0xa3)!
=0){xie_stop();return(0);}//读
riqi=du1();//读日期
riqi&=0x3f;//屏蔽最高2位
xie_stop();//停止条件
xie_start();//起始条件
if(xie1(0xa2)!
=0){xie_stop();return(0);}//写
if(xie1(0x07)!
=0){xie_stop();return(0);}//取读月份字节地址
xie_start();//起始条件
if(xie1(0xa3)!
=0){xie_stop();return(0);}//读
yuefen=du1();//读月份
yuefen&=0x1f;//屏蔽最高3位
xie_stop();//停止条件
xie_start();//起始条件
if(xie1(0xa2)!
=0){xie_stop();return(0);}//写
if(xie1(0x06)!
=0){xie_stop();return(0);}//星期字节地址
xie_start();//起始条件
if(xie1(0xa3)!
=0){xie_stop();return(0);}//读
xingqi=du1();//读星期
xingqi&=0x07;//保留低3位
xie_stop();//停止条件
xie_start();//起始条件
if(xie1(0xa2)!
=0){xie_stop();return(0);}//写
if(xie1(0x08)!
=0){xie_stop();return(0);}//年份字节地址
xie_start();//起始条件
if(xie1(0xa3)!
=0){xie_stop();return(0);}//读
nian=du1();//读年份
xie_stop();//停止条件
EA=1;//开总中断
return
(1);
}
ucharxie()
{
EA=0;
xie_start();//起始条件
if(xie1(0xa2)!
=0){xie_stop();return(0);}//取器件地址写
if(xie1(0x02)!
=0){xie_stop();return(0);}//写秒地址
if(xie1(miao)!
=0){xie_stop();return(0);}//写秒
xie_stop();//停止条件
xie_start();//起始条件
if(xie1(0xa2)!
=0){xie_stop();return(0);}//取器件地址写
if(xie1(0x03)!
=0){xie_stop();return(0);}//写分地址
if(xie1(fen)!
=0){xie_stop();return(0);}//写分
xie_stop();
xie_start();//起始条件
if(xie1(0xa2)!
=0){xie_stop();return(0);}//取器件地址写
if(xie1(0x04)!
=0){xie_stop();return(0);}//写小时地址
if(xie1(shi)!
=0){xie_stop();return(0);}//写时
xie_stop();
xie_start();//写日期
if(xie1(0xa2)!
=0){xie_stop();return(0);}
if(xie1(0x05)!
=0){xie_stop();return(0);}
if(xie1(riqi)!
=0){xie_stop();return(0);}
xie_stop();
xie_start();//写月份
if(xie1(0xa2)!
=0){xie_stop();return(0);}
if(xie1(0x07)!
=0){xie_stop();return(0);}
if(xi