LCD12864万年历(可调)

合集下载
  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

#include
#define uint unsigned int
#define uchar unsigned char

sbit down = P3^5;
sbit up = P3^6;

sbit SID = P1^7; //串行数据
sbit SCLK = P1^6; //串行同步时钟

/*****汉字地址表*****/
uchar code addr_tab[]={
0x80,0x81,0x82,0x83,0x84,0x85,0x86,0x87,//第一行汉字位置
0x90,0x91,0x92,0x93,0x94,0x95,0x96,0x97,//第二行汉字位置
0x88,0x89,0x8a,0x8b,0x8c,0x8d,0x8e,0x8f,//第三行汉字位置
0x98,0x99,0x9a,0x9b,0x9c,0x9d,0x9e,0x9f,//第四行汉字位置
};


uint jishu_xq =0,jishu_nl = 1, nian =2013 , yue =1, ri =1 , shi =18 , fen =30 , miao =0 , num = 0 ;
int ri_nl;
uint nian_nl =2013 , yue_nl =1 ,ri_nl_xs = 0;
uchar code shuzi[]={'0','1','2','3','4','5','6','7','8','9'};
uchar code sxiao[]={"鼠牛虎兔龙蛇马羊猴鸡狗猪"};
uchar code xqi[]={"一二三 四五六日"};
int ny = 1 , temp = 0 ;


void delayms(uint t) //约延时n(ms)
{
uint i;
while(t--)
{
for(i=0;i<125;i++);
}
}


void SendByte(uchar Dbyte)
{
uchar i;
for(i=0;i<8;i++)
{
SCLK = 0;
Dbyte=Dbyte<<1; //左移一位
SID = CY; //移出的位给SID
SCLK = 1;
SCLK = 0;

}
}

/*****写指令*****/
void Lcd_WriteCmd(uchar Cbyte )
{
delayms(1);//检测忙的子程序不写,因为串行的不支持读操作,用延时代替
SendByte(0xf8); //11111,RW(0),RS(0),0
SendByte(0xf0&Cbyte); //高四位
SendByte(0xf0&Cbyte<<4); //低四位(先执行<<)
}

/*****写数据*****/
void Lcd_WriteData(uchar Dbyte )
{
delayms(1);
SendByte(0xfa); //11111,RW(0),RS(1),0
SendByte(0xf0&Dbyte); //高四位
SendByte(0xf0&Dbyte<<4); //低四位(先执行<<)
}

/*****初始化LCD*****/
void Lcd_Init(void)
{
delayms(50);
Lcd_WriteCmd(0x30); //选择基本指令集
delayms(1);
Lcd_WriteCmd(0x30); //选择8bit数据流
delayms(1);
Lcd_WriteCmd(0x0c); //开显示(无游标、不反白)
delayms(1);
Lcd_WriteCmd(0x01); //清除显示,并且设定地址指针为00H
delayms(30);
}

/*****显示汉字*****/
void hanzi_Disp(uchar x,uchar y,uchar code *s)
{ x=x-1;y=y-1;
Lcd_WriteCmd(addr_tab[8*x+y]); //写地址
while(*s>0)
{
Lcd_WriteData(*s); //写数据
s++;
}
}


void display()
{

if(ny ==1) //阳历
{
hanzi_Disp(1,1,"阳历");
hanzi_Disp(2,1," 年 月 日 ");
hanzi_Disp(2,1,"");
Lcd_WriteData(shuzi[nian/1000]);
Lcd_WriteData(shuzi[(nian%1000)/100]);
Lcd_WriteData(shuzi[(nian%100)/10]);
Lcd_WriteData(shuzi[nian%10]);
hanzi_Disp(2,4,"");
Lcd_WriteData(shuzi[yue/10]);
Lcd_WriteData(shuzi[yue%10]);
hanzi_Disp(2,6,"");
Lcd_WriteData(shuzi[ri/10]);
Lcd_WriteData(shuzi[ri%10]);

//鼠、牛、虎、兔、龙

、蛇、马、羊、猴、鸡、狗、猪
hanzi_Disp(1,4,"");
Lcd_WriteData(sxiao[2*((nian-4)%12)]);
Lcd_WriteData(sxiao[2*((nian-4)%12)+1]);
hanzi_Disp(1,5,"年");
}

if(ny == 2) //农历
{
ri_nl = jishu_nl - 40 ;
if(ri_nl < 0 )
nian_nl = nian - 1 ;
if(ri_nl == 0 )
ri_nl = 1 , nian_nl = nian ;
if(ri_nl == (-39))
{
ri_nl_xs = jishu_nl + 19 ;//40 + ri_nl + 19;
yue_nl = 11 ;
nian_nl = nian - 1 ;
}
if(ri_nl < -28)
{
ri_nl_xs = jishu_nl + 19 ;//40 + ri_nl + 19;
yue_nl = 11 ;
nian_nl = nian - 1 ;
}
if((ri_nl > -29) && (ri_nl < 0))
{
ri_nl_xs = jishu_nl - 12 ;//40 + ri_nl - 11;
yue_nl = 12 ;
nian_nl = nian - 1 ;
}
if((ri_nl > 0) && (ri_nl < 31)) //30T
{
ri_nl_xs = ri_nl;
yue_nl = 1 ;
}
if((ri_nl > 30) && (ri_nl < 60)) //29T
{
ri_nl_xs = ri_nl - 30;
yue_nl = 2 ;
}
if((ri_nl > 59) && (ri_nl < 90)) //30T
{
ri_nl_xs = ri_nl - 59;
yue_nl = 3 ;
}
if((ri_nl > 89) && (ri_nl < 119)) //29T
{
ri_nl_xs = ri_nl - 89;
yue_nl = 4 ;
}
if((ri_nl > 118) && (ri_nl < 149)) //30T
{
ri_nl_xs = ri_nl - 118;
yue_nl = 5 ;
}
if((ri_nl > 148) && (ri_nl < 179)) //30T
{
ri_nl_xs = ri_nl - 148;
yue_nl = 6 ;
}
if((ri_nl > 178) && (ri_nl < 208)) //29T
{
ri_nl_xs = ri_nl - 178;
yue_nl = 7 ;
}
if((ri_nl > 207) && (ri_nl < 238)) //30T
{
ri_nl_xs = ri_nl - 207;
yue_nl = 8 ;
}
if((ri_nl > 237) && (ri_nl < 268)) //29T
{
ri_nl_xs = ri_nl - 237;
yue_nl = 9 ;
}
if((ri_nl > 266) && (ri_nl < 297)) //30T
{
ri_nl_xs = ri_nl - 266;
yue_nl = 10 ;
}
if((ri_nl > 296) && (ri_nl < 326)) //29T
{
ri_nl_xs = ri_nl - 296;
yue_nl = 11 ;
}
if((ri_nl > 325) && (ri_nl < 356)) //30T
{
ri_nl_xs = ri_nl - 325;
yue_nl = 12 ;
}

hanzi_Disp(1,1,"农历");
hanzi_Disp(2,1," 年 月 日 ");
hanzi_Disp(2,1,"");
Lcd_WriteData(shuzi[nian_nl/1000]);
Lcd_WriteData(shuzi[(nian_nl%1000)/100]);
Lcd_WriteData(shuzi[(nian_nl%100)/10]);
Lcd_WriteData(shuzi[nian_nl%10]);

hanzi_Disp(2,4,"");
Lcd_WriteData(shuzi[yue_nl/10]);
Lcd_WriteData(shuzi[yue_nl%10]);
hanzi_Disp(2,6,"");
Lcd_WriteData(shuzi[ri_nl_xs/10]);
Lcd_WriteData(shuzi[ri_nl_xs%10]);

//鼠、牛、虎、兔、龙、蛇、马、羊、猴、鸡、狗、猪
hanzi_Disp(1,4,"");


Lcd_WriteData(sxiao[2*((nian_nl-4)%12)]);
Lcd_WriteData(sxiao[2*((nian_nl-4)%12)+1]);
hanzi_Disp(1,5,"年");
}

//时分秒
hanzi_Disp(3,1,"");
Lcd_WriteData(shuzi[shi/10]);
Lcd_WriteData(shuzi[shi%10]);
hanzi_Disp(3,2," :");
Lcd_WriteData(shuzi[fen/10]);
Lcd_WriteData(shuzi[fen%10]);
hanzi_Disp(3,4," :");
Lcd_WriteData(shuzi[miao/10]);
Lcd_WriteData(shuzi[miao%10]);
hanzi_Disp(3,6," ");

//星期
hanzi_Disp(3,8,"");
if(2*((jishu_xq+1)%7)==4)
{
hanzi_Disp(3,8,"三");
hanzi_Disp(4,1,"自信,最美的自己");
}
else
{
Lcd_WriteData(xqi[2*((jishu_xq+1)%7)]);
Lcd_WriteData(xqi[2*((jishu_xq+1)%7)+1]);
}
hanzi_Disp(3,7,"周");
if(2*((jishu_xq+1)%7)==0) //对应周一
hanzi_Disp(4,1,"每天都是新的开始");
if(2*((jishu_xq+1)%7)==2) //对应周二
hanzi_Disp(4,1,"抓住现在才有未来");
if(2*((jishu_xq+1)%7)==6) //对应周四
hanzi_Disp(4,1,"努力,它不论迟早");
if(2*((jishu_xq+1)%7)==8) //对应周五
hanzi_Disp(4,1,"成功,与勇者同行");
if(2*((jishu_xq+1)%7)==10) //对应周六
hanzi_Disp(4,1,"机会给有准备的人");
if(2*((jishu_xq+1)%7)==12) //对应周日
hanzi_Disp(4,1,"及时行事不留遗憾");
}


void jiaoshi()
{
if(temp == 2)
{
delayms(20);
{
if(up == 0)
{
delayms(20);
nian ++ ;
}
if(down == 0)
{
delayms(20);
nian -- ;
}
}
}

if(temp == 3)
{
delayms(20);
{
if(up == 0)
{
delayms(20);
yue ++ ;
}
if(down == 0)
{
delayms(20);
yue -- ;
}
}
}

if(temp == 4)
{
delayms(20);
{
if(up == 0)
{
delayms(20);
ri ++ ;
jishu_nl ++ ;
}
if(down == 0)
{
delayms(20);
ri -- ;
jishu_nl -- ;
}
}
}

if(temp == 5)
{
delayms(20);
{
if(up == 0)
{
delayms(20);
shi ++ ;
}
if(down == 0)
{
delayms(20);
shi -- ;
}
}
}

if(temp == 6)
{
delayms(20);
{
if(up == 0)
{
delayms(20);
fen ++ ;
}
if(down == 0)
{
delayms(20);
fen -- ;
}
}
}

if(temp == 7)
{
delayms(20);
{
if(up == 0)
{
delayms(20);
jishu_xq ++ ;
}
if(down == 0)
{
delayms(20);
jishu_xq -- ;
}
}
}

}


void jishi()
{
if(miao == 60)
{
miao = 0;
fen ++ ;
}

if(fen == 60)
{
fen = 0;
shi ++ ;
}

if(shi == 2

4)
{
shi = 0;
ri ++ ;
jishu_xq ++ ;
jishu_nl ++ ;
}

if(yue == 2) //二月
{
if( (ri == 30) && ( (((nian%4) == 0) && ((nian%100)!=0)) || (((nian%400) == 0) && ((nian%100)==0)) ) ) //闰年
{
ri = 1;
yue ++ ;
}
if( (ri == 29) && ( !((((nian%4) == 0) && ((nian%100)!=0)) || (((nian%400) == 0) && ((nian%100)==0))) ) ) //非闰年
{
ri = 1;
yue ++ ;
}
}
else //非二月
{
if((ri == 32) && ((yue%2) == 1)) //31
{
ri = 1;
yue ++ ;
}

if((ri == 31) && ((yue%2) == 0)) //30
{
ri = 1;
yue ++ ;
}
}

if(yue == 13)
{
yue = 1;
nian ++ ;
jishu_xq = 0 ;
}
}


void clear_gcrom()
{
hanzi_Disp(1,1," ");
hanzi_Disp(2,1," ");
hanzi_Disp(3,1," ");
hanzi_Disp(4,1," ");
}


/*****主函数*****/
void main(void)
{
TMOD=0x01;
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
EA=1;

ET0=1;
TR0=1;

//外部中断1 校时
EX1 = 1 ;
IT1 = 1 ; // 下降沿触发

//外部中断0 阳历农历切换
EX0 = 1 ;
IT0 = 1 ; // 下降沿触发

Lcd_Init();

while(1)
{
//clear_gcrom();
jishi();
display();
if((temp < 8) && (temp > 0))
{
hanzi_Disp(1,7,"校时");
jiaoshi();
}
}
}


void T0_time() interrupt 1
{
TH0=(65536-45872)/256;
TL0=(65536-45872)%256;
num ++ ;
if(num == 20) //1s
miao ++ , num = 0 ;
}


//外部中断1 校时
void IN1() interrupt 2
{
delayms(20);
temp++;
if(temp == 1) //开始校时
{
ET0=0;
TR0=0;
}
if(temp == 8) //结束校时
{
temp = 0;
clear_gcrom();
ET0=1;
TR0=1;
}
}


//外部中断0 阳历农历切换
void IN0() interrupt 0
{
delayms(20);
clear_gcrom();
ny++;
if(ny == 3)
ny = 1 ;
}

相关文档
最新文档