📄 带农历带生肖万年历.c
字号:
//头文件
#include <REG52.h>
#include <string.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
/*****************************************************************************/
sbit LCM_cs = P2^5;
sbit LCM_std = P2^6;
sbit LCM_sclk = P2^7;
sbit LCM_psb = P2^3; //H=并口; L=串口;
sbit LCM_rst = P2^4; //Reset Signal 低电平有效
sbit beep = P1^2;
/*****************************************************************************/
//温度传感器定义
sbit DQ = P1^ 0;//ds18B20
/*****************************************************************************/
//键盘引脚定义
sbit KEY_1 = P3^4; //左上
sbit KEY_2 = P3^5; //左下
sbit KEY_3 = P3^6; //右上
sbit KEY_4 = P3^7; //右下
//定义DS1302时钟接口
sbit DS1302_CLK = P1^3; //实时时钟时钟线引脚
sbit DS1302_IO = P1^4; //实时时钟数据线引脚
sbit DS1302_RST = P1^7; //实时时钟复位线引脚/*****************************************************************************/
//定义累加器A中的各位
sbit ACC0 = ACC^0;
sbit ACC7 = ACC^7;
/*****************************************************************************/
//定义全局变量
unsigned char yy,mo,dd,xq,hh,mm,ss,e;//定义时间映射全局变量(专用寄存器)
bit c_moon;
data uchar year_moon,month_moon,day_moon,week;
bit w = 0; //调时标志位
static unsigned char menu = 0;//定义静态小时更新用数据变量
static unsigned char keys = 0;//定义静态小时更新用数据变量
static unsigned char timecount = 0;//定义静态软件计数器变量
uchar code tab1[]={
" 电子万年历 "
" 版本: V1.0 "
"2008年11月22日夜"
" 骆瑶 "
};
/****************************************************************************/
void DelayM(unsigned int a) //延时函数 1MS/次
{
unsigned char i;
while( --a != 0)
{
for(i = 0; i < 125; i++); //一个 ; 表示空语句,CPU空转。
} //i 从0加到125,CPU大概就耗时1毫秒
}
/****************************************************************************/
//函数声明
void LCM_WriteString(unsigned char *str) ; //写字符 例:LCM_WriteString("您好!");
void LCM_WriteDatOrCom(bit ,unsigned char ); //(0,指令) (1,数据)
void LCM_w_byte(unsigned char bbyte); //写一个字节给LCM
void delaynms(unsigned int di); //延时
void LCM_init(void); //LCM初始化
void LCM_clr(void); //清屏
void Delay(int num);
bit get_moon_day(uchar month_p,uint table_addr);
void Conversion(bit c,uchar year,uchar month,uchar day);
void nl();
void days (viod) //公历节日数据库
{
LCM_WriteDatOrCom(0,0x80);
// LCM_WriteString("[Dis] [Set]");
LCM_WriteDatOrCom(0,0x98);
// ----|----------------|
if ( mo == 0x01 && dd == 0x01 ){ LCM_WriteString("Happy New Year!!"); }//1月
if ( mo == 0x01 && dd == 0x28 ){ LCM_WriteString(" 世界麻风日 "); }
if (month_moon== 0x01 && day_moon == 0x01 ){ LCM_WriteString(" 今日春节 "); }
if (month_moon== 0x01 && day_moon == 0x15 ){ LCM_WriteString(" 元宵节快乐 "); }
if (month_moon== 0x02 && day_moon == 0x02 ){ LCM_WriteString(" 青龙节 "); }
if ( mo == 0x02 && dd == 0x02 ){ LCM_WriteString(" 世界湿地日 "); }//2月
if ( mo == 0x02 && dd == 0x13 ){ LCM_WriteString(" 明天情人节了 "); }
if ( mo == 0x02 && dd == 0x14 ){ LCM_WriteString(" 今天是情人节 "); }
if ( mo == 0x03 && dd == 0x01 ){ LCM_WriteString(" 国际海豹日 "); }//3月
if ( mo == 0x03 && dd == 0x03 ){ LCM_WriteString(" 全国爱耳日 "); }
if ( mo == 0x03 && dd == 0x08 ){ LCM_WriteString(" 3.8妇女节 "); }
if ( mo == 0x03 && dd == 0x12 ){ LCM_WriteString(" 植树节 "); }
if ( mo == 0x03 && dd == 0x14 ){ LCM_WriteString(" 国际警察日 "); }
if ( mo == 0x03 && dd == 0x15 ){ LCM_WriteString(" 消费者权益日 "); }
if ( mo == 0x03 && dd == 0x17 ){ LCM_WriteString(" 国际航海日 "); }
if ( mo == 0x03 && dd == 0x21 ){ LCM_WriteString(" 世界森林日 "); }
if ( mo == 0x03 && dd == 0x22 ){ LCM_WriteString(" 世界水日 "); }
if ( mo == 0x03 && dd == 0x23 ){ LCM_WriteString(" 世界气象日 "); }
if ( mo == 0x03 && dd == 0x24 ){ LCM_WriteString("世界防治结核病日"); }
if ( mo == 0x04 && dd == 0x01 ){ LCM_WriteString("愚人节 小心上当"); }//4
if ( mo == 0x04 && dd == 0x07 ){ LCM_WriteString(" 世界卫生日 "); }
if ( mo == 0x04 && dd == 0x08 ){ LCM_WriteString(" 复活节 "); }
if ( mo == 0x04 && dd == 0x13 ){ LCM_WriteString(" 黑色星期五 "); }
if (month_moon== 0x05 && day_moon == 0x05 ){ LCM_WriteString(" 端午节 "); }
if ( mo == 0x05 && dd == 0x01 ){ LCM_WriteString(" 劳动节 放假 "); }//5
if ( mo == 0x05 && dd == 0x04 ){ LCM_WriteString(" 青年节 "); }
if ( mo == 0x05 && dd == 0x08 ){ LCM_WriteString(" 世界红十字日 "); }
if ( mo == 0x05 && dd == 0x12 ){ LCM_WriteString(" 国际护士节 "); }
if ( mo == 0x05 && dd == 0x05 ){ LCM_WriteString("近日注意母亲节 "); }
if ( mo == 0x05 && dd == 0x15 ){ LCM_WriteString(" 国际家庭日 "); }
if ( mo == 0x05 && dd == 0x31 ){ LCM_WriteString(" 世界无烟日 "); }
if ( mo == 0x06 && dd == 0x01 ){ LCM_WriteString(" 国际儿童节 "); }//6
if ( mo == 0x06 && dd == 0x05 ){ LCM_WriteString(" 世界环境日 "); }
if ( mo == 0x06 && dd == 0x26 ){ LCM_WriteString(" 国际禁毒日 "); }
if ( mo == 0x06 && dd == 0x06 ){ LCM_WriteString(" 全国爱眼日 "); }
if ( mo == 0x06 && dd == 0x13 ){ LCM_WriteString("近日注意父亲节 "); }
if ( mo == 0x06 && dd == 0x15 ){ LCM_WriteString("近日注意父亲节 "); }
if (month_moon== 0x07 && day_moon == 0x07 ){ LCM_WriteString("乞巧节中国情人节"); }
if (month_moon== 0x07 && day_moon == 0x15 ){ LCM_WriteString(" 中元节 "); }
if ( mo == 0x07 && dd == 0x01 ){ LCM_WriteString("香港回归记念日 "); }//7
if ( mo == 0x07 && dd == 0x07 ){ LCM_WriteString("抗日战争记念日 "); }
if ( mo == 0x07 && dd == 0x11 ){ LCM_WriteString(" 世界人口日 "); }
if (month_moon== 0x08 && day_moon == 0x15 ){ LCM_WriteString(" 中秋节快乐 "); }
if ( mo == 0x08 && dd == 0x01 ){ LCM_WriteString(" 八一建军节 "); }//8
if ( mo == 0x08 && dd == 0x08 ){ LCM_WriteString(" 中国男子节 "); }
if ( mo == 0x08 && dd == 0x15 ){ LCM_WriteString("抗战胜利记念日 "); }
if (month_moon== 0x09 && day_moon == 0x09 ){ LCM_WriteString(" 今日重阳节 "); }
if ( mo == 0x09 && dd == 0x10 ){ LCM_WriteString(" 中国教师节 "); }//9
if ( mo == 0x09 && dd == 0x18 ){ LCM_WriteString("九·一八事变记念"); }
if ( mo == 0x09 && dd == 0x20 ){ LCM_WriteString(" 国际爱牙日 "); }
if ( mo == 0x09 && dd == 0x27 ){ LCM_WriteString(" 世界旅游日 "); }
if ( mo == 0x10 && dd == 0x01 ){ LCM_WriteString(" 中国国庆节 "); }//10
if ( mo == 0x10 && dd == 0x04 ){ LCM_WriteString(" 世界动物日 "); }
if ( mo == 0x10 && dd == 0x24 ){ LCM_WriteString(" 联合国日 "); }
if ( mo == 0x10 && dd == 0x12 ){ LCM_WriteString("明天国际教师节 "); }
if ( mo == 0x10 && dd == 0x13 ){ LCM_WriteString(" 国际教师节 "); }
if ( mo == 0x11 && dd == 0x10 ){ LCM_WriteString(" 世界青年节 "); }//11
if ( mo == 0x11 && dd == 0x17 ){ LCM_WriteString(" 世界学生节 "); }
if (month_moon== 0x12 && day_moon == 0x08 ){ LCM_WriteString(" 今日腊八节 "); }
if (month_moon== 0x12 && day_moon == 0x15 ){ LCM_WriteString(" 过小年 "); }
if (month_moon== 0x12 && day_moon == 0x29 ){ LCM_WriteString(" 腊月二十九 "); }
if (month_moon== 0x12 && day_moon == 0x30 ){ LCM_WriteString(" 大年三十(^o^) "); }
if ( mo == 0x12 && dd == 0x01 ){ LCM_WriteString(" 世界艾滋病日 "); }//12
if ( mo == 0x12 && dd == 0x23 ){ LCM_WriteString(" 明晚平安夜 "); }
if ( mo == 0x12 && dd == 0x24 ){ LCM_WriteString(" 今晚平安夜 "); }
if ( mo == 0x12 && dd == 0x25 ){ LCM_WriteString(" 圣诞快乐 "); }
if ( mo == 0x12 && dd == 0x31 ){ LCM_WriteString(" 明日新年 "); }
else{
if ( hh >= 0x04 && hh < 0x06 ){ LCM_WriteString("★★__▲▲__凌晨"); }
if ( hh >= 0x06 && hh < 0x08 ){ LCM_WriteString("☆○__▲△__早晨"); }
if ( hh >= 0x08 && hh < 0x12 ){ LCM_WriteString("__●__▲▲__上午"); }
if ( hh == 0x12) { LCM_WriteString("____▲●▲__中午"); }
if ( hh >= 0x13 && hh < 0x18 ){ LCM_WriteString("__▲▲__●__下午"); }
if ( hh >= 0x18 && hh < 0x22 ){ LCM_WriteString("△▲__●☆__晚上"); }
if ( hh >= 0x22 && hh <= 0x23 ){ LCM_WriteString("△▲__★☆__夜里"); }
if ( hh >= 0x00 && hh < 0x04 ){ LCM_WriteString("__★▲▲★__深夜"); }
}
}
/****************************************************************************/
//初始化LCM
void LCM_init(void)
{
LCM_rst=1;
LCM_psb=0;
LCM_WriteDatOrCom (0,0x30); /*30---基本指令动作*/
LCM_WriteDatOrCom (0,0x01); /*清屏,地址指针指向00H*/
Delay (100);
LCM_WriteDatOrCom (0,0x06); /*光标的移动方向*/
LCM_WriteDatOrCom(0,0x0c); /*开显示,关游标*/
}
/******************************************************************************/
//写指令或数据 (0,指令) (1,数据)
void LCM_WriteDatOrCom(bit dat_comm,uchar content)
{
uchar a,i,j;
Delay(50);
a=content;
LCM_cs=1;
LCM_sclk=0;
LCM_std=1;
for(i=0;i<5;i++)
{
LCM_sclk=1;
LCM_sclk=0;
}
LCM_std=0;
LCM_sclk=1;
LCM_sclk=0;
if(dat_comm)
LCM_std=1; //data
else
LCM_std=0; //command
LCM_sclk=1;
LCM_sclk=0;
LCM_std=0;
LCM_sclk=1;
LCM_sclk=0;
for(j=0;j<2;j++)
{
for(i=0;i<4;i++)
{
a=a<<1;
LCM_std=CY;
LCM_sclk=1;
LCM_sclk=0;
}
LCM_std=0;
for(i=0;i<4;i++)
{
LCM_sclk=1;
LCM_sclk=0;
}
}
}
/*********************************************************************************/
void chn_disp (uchar code *chn)
{
uchar i,j;
LCM_WriteDatOrCom (0,0x30);
LCM_WriteDatOrCom (0,0x80);
for (j=0;j<4;j++)
{
for (i=0;i<16;i++)
LCM_WriteDatOrCom (1,chn[j*16+i]);
}
}
/*****************************************************************************/
//清屏函数
void LCM_clr(void)
{
LCM_WriteDatOrCom (0,0x30);
LCM_WriteDatOrCom (0,0x01);
Delay (180);
}
/*****************************************************************************/
//向LCM发送一个字符串,长度64字符之内。
//应用:LCM_WriteString("您好!");
void LCM_WriteString(unsigned char *str)
{
while(*str != '\0')
{
LCM_WriteDatOrCom(1,*str++);
}
*str = 0;
}
/******************************************************************************/
void Delay(int num)//延时函数
{
while(num--);
}
/******************************************************************************/
void Init_DS18B20(void)//初始化ds1820
{
unsigned char x=0;
DQ = 1; //DQ复位
Delay(8); //稍做延时
DQ = 0; //单片机将DQ拉低
Delay(80); //精确延时 大于 480us
DQ = 1; //拉高总线
Delay(14);
x=DQ; //稍做延时后 如果x=0则初始化成功 x=1则初始化失败
Delay(20);
}
/******************************************************************************/
unsigned char ReadOneChar(void)//读一个字节
{
unsigned char i=0;
unsigned char dat = 0;
for (i=8;i>0;i--)
{
DQ = 0; // 给脉冲信号
dat>>=1;
DQ = 1; // 给脉冲信号
if(DQ)
dat|=0x80;
Delay(4);
}
return(dat);
}
/******************************************************************************/
void WriteOneChar(unsigned char dat)//写一个字节
{
unsigned char i=0;
for (i=8; i>0; i--)
{
DQ = 0;
DQ = dat&0x01;
Delay(5);
DQ = 1;
dat>>=1;
}
}
/******************************************************************************/
unsigned int ReadTemperature(void)//读取温度
{
unsigned char a=0;
unsigned char b=0;
unsigned int t=0;
float tt=0;
Init_DS18B20();
WriteOneChar(0xCC); // 跳过读序号列号的操作
WriteOneChar(0x44); // 启动温度转换
Init_DS18B20();
WriteOneChar(0xCC); //跳过读序号列号的操作
WriteOneChar(0xBE); //读取温度寄存器
a=ReadOneChar(); //读低8位
b=ReadOneChar(); //读高8位
t=b;
t<<=8;
t=t|a;
tt=t*0.0625;
t= tt*10+0.5; //放大10倍输出并四舍五入
return(t);
}
/*****************************************************************************/
//声明
void LCM_w_ss(void);
void LCM_w_mm(void);
void LCM_w_hh(void);
void LCM_w_dd(void);
void LCM_w_mo(void);
void LCM_w_yy(void);
void LCM_w_xq(void);
void DS1302InputByte(unsigned char d) ;
unsigned char DS1302OutputByte(void) ;
void Init_1302(void);
unsigned char Read1302(unsigned char);
void Set_time(unsigned char sel);
void Write1302(unsigned char , unsigned char );
void updata (void);
/*****************************************************************************/
/*****************************************************************************/
//调时用加1程序
void Set_time(unsigned char sel) //根据选择调整的相应项目加1并写入DS1302
{
signed char address,item;
signed char max,mini;
LCM_WriteDatOrCom(0,0x9a);
LCM_WriteString("调整");
//if(sel==6) {LCM_WriteString("秒钟");address=0x80; max=0;mini=0;} //秒7
if(sel==5) {LCM_WriteString("分钟");address=0x82; max=59;mini=0;} //分钟6
if(sel==4) {LCM_WriteString("小时");address=0x84; max=23;mini=0;} //小时5
if(sel==3) {LCM_WriteString("星期");address=0x8a; max=7;mini=1;} //星期4
if(sel==2) {LCM_WriteString("日期");address=0x86; max=31;mini=1;} //日3
if(sel==1) {LCM_WriteString("月份");address=0x88; max=12;mini=1;} //月2
if(sel==0) {LCM_WriteString("年份");address=0x8c; max=99; mini=0;} //年1
//读取1302某地址上的数值转换成10进制赋给item
item=((Read1302(address+1))/16)*10 + (Read1302(address+1))%16;
if(KEY_2 == 0)
{
item++;//数加 1
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -