📄 12864
字号:
/*******************************************/
/* LCM128*64万年历 */
/* 硬件:LCM128*64 89s52 晶振:24M */
/* 制作:hyj */
/* 时间:2006.11.3 V0.1 */
/* 2006.12.4 V0.2(修正"三"显示) */
/* 2006.12.7 V0.3(增加闹钟功能) */
/*******************************************/
#include<reg51.h>
#include<stdio.h>
#include<intrins.h>
#define uchar unsigned char
#define DELTA 500
bit c_moon;
bit flash,id,alarm_controls,alarm;
unsigned char k,jieqi1,jieqi2;
data uchar year_moon,month_moon,day_moon;
unsigned char year,month,day,week,hour,min,sec;
unsigned char alarm_hour,alarm_min,sec_temp;
uchar mode=0,add=1,ir_data1,ir_data2,ir_data3,ir_data4,ir_enable,count,count2=0,en=0;
code unsigned char shuzi[]={"0123456789:."};
code unsigned char riqi[]= {"年月日农历星期室温 闹钟开关"};
code unsigned char xingqi[]={" 一二33四五六日"};
code unsigned char rongli[]={"初十廿卅"};
code unsigned char shuzi2[]={"十一二33四五六七八九"};
code unsigned char shuzi3[]={" 十"};
code unsigned char shuzi4[]={" 一二33四五六七八九"};
unsigned char code inittime[]={0x48,0x21,0x15,0x11,0x11,0x06,0x06}; //初始化参数
// 秒 分 时 日 月 星期 年
code uchar san[]={
/*-- 文字: 三 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff,
};
code unsigned char jieqi[]=
{
" "
"小寒大寒"
"立春雨水"
"惊蛰春分"
"清明谷雨"
"立夏小满"
"芒种夏至"
"小暑大暑"
"立秋处暑"
"白露秋分"
"寒露霜降"
"立冬小雪"
"大雪冬至"
};
sbit T_CLK=P3^5;
sbit T_IO =P3^6;
sbit T_RST=P3^7; //引脚定义
sbit DQ =P1^4;
sbit ACC0=ACC^0;
sbit ACC7=ACC^7;
sbit rs = P2^1;
sbit rw = P2^2;
sbit e = P2^4;
sbit psb = P2^5;
sbit rst = P3^4;
sbit P07=P0^7;
sbit P15=P1^5;
void RTInputByte(uchar); // 输入 1Byte
uchar RTOutputByte(void); // 输出 1Byte
void W1302(uchar, uchar); // 向DS1302写入一个字节
uchar R1302(uchar); // 从DS1302读出一个字节
void Set1302(unsigned char* ); // 设置初始时间
ReadTemperature(void); //读取室温
void Conversion(bit c,char year,char month,char day); //计算农历日期
unsigned char calendar_calculate_solar_term_1(void); //计算当月第一个节气对应的公历日期
unsigned char calendar_calculate_solar_term_2(void); //计算当月第二个节气
mode_set()
{
switch(mode)
{
case 0:mode=1;
W1302(0x8e,0x00); //允许写操作
W1302(0x80,0x80); //写入DS1302 时间停止
W1302(0x8e,0x80); //写保护,禁止写操作
TR0=1;
break;
case 1:mode=2;
break;
case 2:mode=3;
break;
case 3:mode=4;
break;
case 4:mode=5;
break;
case 5:mode=6;
break;
case 6:mode=0;
W1302(0x8e,0x00); //允许写操作
W1302(0x80,0x00); //写入DS1302 时间运行
W1302(0x8e,0x80); //写保护,禁止写操作
TR0=0;
break;
}
if(id==1)
{
switch(mode)
{
case 7:mode=8;
break;
case 8:mode=7;
break;
}
}
}
add_set()
{
uchar address,item;
uchar max,min;
if(alarm==1) {alarm=0;return;}
if(mode==0) return;
if(mode==1) {address=0x8c; max=99;min=0;} //年
if(mode==2) {address=0x88; max=12;min=1;} //月
if(mode==3) {address=0x86; max=31;min=1;} //日
if(mode==4) {address=0x8a; max=7; min=1;} //星期
if(mode==5) {address=0x84; max=23;min=0;} //小时
if(mode==6) {address=0x82; max=59;min=0;} //分钟
if(mode==7) {address=0xc0; max=23;min=0;}
if(mode==8) {address=0xc2; max=59;min=0;}
item=R1302(address+1);
item=item/16*10+item%16;
if(add)
item++;
else
item--;
if(item<min||item==255) item=max;
if(item>max) item=min;
W1302(0x8e,0x00); //允许写操作
W1302(address,item/10*16+item%10); //写入DS1302 //转成BCD码
W1302(0x8e,0x80); //写保护,禁止写操作
}
ir_key()
{
switch(ir_data3)
{
case 0xe2: mode_set();
break;
case 0x52: add=1;
add_set();
break;
case 0x78: add=0;
add_set();
break;
case 0xb8: id=1;
mode=7;
TR0=1;
break;
case 0x42: id=0;
mode=0;
TR0=0;
break;
case 0xc8:if(id==1) //设置闹钟 开/关(必须在闹钟调整状态下)
{
alarm_controls=~alarm_controls;
}
}
}
/**********************************************
遥控外部中断程序
接收数据:ir_data1--ir_data4
倍数计数:count
DELTA :误差
/**********************************************/
void int0remoter() interrupt 2
{
TR1=0;
if((TL1+TH1*256)==0)
{
count=0;
TL1=0;
TH1=0;
TR1=1; //开定时器
return;//有遥控信号产生中断,退出中断
}
if(!count)
{
if(((TL1+TH1*256)>(27000-DELTA))&&((TL1+TH1*256)<(27000+DELTA)))
{
count++;
TH1=0;
TL1=0;
TR1=1;
return;//前导码正确,准备接收数据,退出中断
}
count=0;
TR1=0;
TH1=0;
TL1=0;
return;//前导码错误,非遥控信号,退出中断
}
if(count<33)
{
if(count<=8)
{
if(((TL1+TH1*256)>(2250-DELTA))&&((TL1+TH1*256)<(2250+DELTA)))
{
ir_data1<<=1;
count++;
TH1=0;
TL1=0;
TR1=1;
return; //数据为0,退出中断
}
if(((TL1+TH1*256)>(4500-DELTA))&&((TL1+TH1*256)<(4500+DELTA)))
{
ir_data1<<=1;
ir_data1|=0x01;
count++;
TH1=0;
TL1=0;
TR1=1;
return;//数据为1,退出中断
}
count=0;
TR1=0;
TH1=0;
TL1=0;
return; //错误,清零退出中断
}
if(count>8&&count<=16)
{
if(((TL1+TH1*256)>(2250-DELTA))&&((TL1+TH1*256)<(2250+DELTA)))
{
ir_data2<<=1;
count++;
TH1=0;
TL1=0;
TR1=1;
return; //数据为0,退出中断
}
if(((TL1+TH1*256)>(4500-DELTA))&&((TL1+TH1*256)<(4500+DELTA)))
{
ir_data2<<=1;
ir_data2|=0x01;
count++;
TH1=0;
TL1=0;
TR1=1;
return;//数据为1,退出中断
}
count=0;
TR1=0;
TH1=0;
TL1=0;
return; //错误,清零退出中断
}
if(count>16&&count<=24)
{
if(((TL1+TH1*256)>(2250-DELTA))&&((TL1+TH1*256)<(2250+DELTA)))
{
ir_data3<<=1;
count++;
TH1=0;
TL1=0;
TR1=1;
return; //数据为0,退出中断
}
if(((TL1+TH1*256)>(4500-DELTA))&&((TL1+TH1*256)<(4500+DELTA)))
{
ir_data3<<=1;
ir_data3|=0x01;
count++;
TH1=0;
TL1=0;
TR1=1;
return;//数据为1,退出中断
}
count=0;
TR1=0;
TH1=0;
TL1=0;
return; //错误,清零退出中断
}
if(count>24&&count<=32)
{
if(((TL1+TH1*256)>(2250-DELTA))&&((TL1+TH1*256)<(2250+DELTA)))
{
ir_data4<<=1;
count++;
TH1=0;
TL1=0;
TR1=1;
if(count==33)
{
EX1=0;
en=1;
ir_enable = 1;
}
return; //数据为0,退出中断
}
if(((TL1+TH1*256)>(4500-DELTA))&&((TL1+TH1*256)<(4500+DELTA)))
{
ir_data4<<=1;
ir_data4|=0x01;
count++;
TH1=0;
TL1=0;
TR1=1;
if(count==33)
{
EX1=0;
en=1;
ir_enable = 1;
}
return;//数据为1,退出中断
}
count=0;
TR1=0;
TH1=0;
TL1=0;
return; //错误,清零退出中断
}
}
}
/********定时器中断***********/
/* 作用:遥控接收 */
/*****************************/
void timer1_int() interrupt 3
{
if(en!=0)
{
en--;
TL1=0;
TH1=0;
}
EX1=1;
count=0;
TR1=0;
TH1=0;
TL1=0;
}
/**********定时器中断***********/
/* 作用:调时闪动定时 */
/* 闪动时间:50(ms)*20(次) */
/*******************************/
void timer0_int() interrupt 1
{
TH0=0x3c; //50ms定时
TL0=0xb0;
if(count2++>=20)
{
count2=0;
flash=~flash;
}
}
/*************************/
/* 延时函数 */
/*************************/
void delay(unsigned char cnt)
{
unsigned char i;
while(cnt--!=0)
for(i=10;i!=0;i--);
}
/***********************/
/* LCM12864忙检测 */
/***********************/
void tase_busy()
{
rs=0;
_nop_();
_nop_();
rw=1;
_nop_();
_nop_();
e=1;
_nop_();
_nop_();
while(P07==1);
e=0;
}
/***********************/
/* LCM12864发送指令 */
/***********************/
void send_dictate(unsigned char interim)
{
tase_busy();
rs=0;
_nop_();
_nop_();
rw=0;
_nop_();
_nop_();
P0=interim;
e=1;
delay(1);
e=0;
}
/***********************/
/* LCM12864发送数据 */
/***********************/
void send_data(unsigned char interim)
{
tase_busy();
rs=1;
_nop_();
_nop_();
rw=0;
_nop_();
_nop_();
P0=interim;
e=1;
_nop_();
_nop_();
e=0;
}
/***********************/
/* 读取DS1302时间并转 */
/* 换成10进制 */
/***********************/
init()
{
alarm_hour=R1302(0xc1);
alarm_hour=alarm_hour/16*10+alarm_hour%16;
alarm_min=R1302(0xc3);
alarm_min=alarm_min/16*10+alarm_min%16;
sec=R1302(0x81);
sec=sec/16*10+sec%16;
min=R1302(0x83);
min=min/16*10+min%16;
hour=R1302(0x85);
hour=hour/16*10+hour%16;
day=R1302(0x87);
day=day/16*10+day%16;
week=R1302(0x8b);
month=R1302(0x89);
month=month/16*10+month%16;
year=R1302(0x8d);
year=year/16*10+year%16;
Conversion(0,year,month,day);
jieqi1= calendar_calculate_solar_term_1();
jieqi2= calendar_calculate_solar_term_2();
}
/***************************/
/* 写12864_CGROM(未使用) */
/***************************/
void cgram()
{
uchar addr=0x40;
uchar k;
for(k=1;k<32;k+=2)
{
send_dictate(addr);
send_data(san[k]);
send_data(san[k-1]);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -