/********************************************************
**这个程序是时钟程序用LCM12832作为显示.
**P1口作为键盘输入P1.0=增加键;P11为选择键
**时钟显示格式为:“时:分:秒”字体为16*16点阵
**修改方式为两键式选择键合修改键
**还可显示万年历:“年:月:日”
**需要子函数
** 1、HEX->BCD子函数hex_bcd()
** 2、显示子函数display()
** 3、键盘子函数key()
** 4、时钟子函数time()
** 5、日历子函数day()
** 6、显示控制子函数discommond()
** 7、定时器中断0 time0()
** 8、初始化子函数 begin().
** 9、初始屏显display_begin().
** 10、时间屏显display_time().
** 11、润年判断leapf().
**现有问题:键盘修改程序还未完成
** 程序不够精简
*********************************************************/
#include
#define time_TH 0x3c; // time0 :50ms 12M
#define time_TL 0xba;
unsigned char data msecond=0;
bit data second_flag=0,day_flag=0,leap=0,display_flag=0,ku_flag;
unsigned char day_temp;
struct time_str{
unsigned char hour;
unsigned char minute;
unsigned char second;
}time_now;
struct day_str{
unsigned int year;
unsigned char month;
unsigned char day;
}day_now;
//显示缓冲区
unsigned char data display_bcd[13];
//每月天数表
unsigned char code day_num[]=
{31,28,31,30,31,30,31,31,30,31,30,31};
//时间显示位置表
unsigned char code time_LOC[][1]=
{0,2,0,3,0,4,0,5,0,8,0,9,0,12,0,13
,16,4,16,5,16,8,16,9,16,12,16,13};
//汉字显示位置表
//指定位置显示“年,月,日,时,分,秒”
unsigned char code zhi_loc[]=
{0x06,0x01,0x0a,0x01,0x0e,0x01,0x06,0x10,0x0a,0x10,0x0e,0x10};
/********************************************************************
**字库开始
**********************************************************************/
unsigned char code shi[]={
/*-- 文字: 年 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
0x08,0x00,0x0F,0xFC,0x10,0x80,0x10,0x80,0x20,0x80,0x4F,0xF8,0x88,0x80,0x08,0x80,
0x08,0x80,0x08,0x80,0xFF,0xFE,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,0x00,0x80,
/*-- 文字: 月 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
0x07,0xF0,0x04,0x10,0x04,0x10,0x04,0x10,0x07,0xF0,0x04,0x10,0x04,0x10,0x04,0x10,
0x07,0xF0,0x04,0x10,0x08,0x10,0x08,0x10,0x10,0x10,0x20,0x50,0x40,0x20,0x00,0x00,
/*-- 文字: 日 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
0x00,0x00,0x1F,0xF0,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x1F,0xF0,0x10,0x10,
0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x1F,0xF0,0x10,0x10,0x00,0x00,0x00,0x00,
/*-- 文字: 时 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
0x00,0x10,0x00,0x10,0x7C,0x10,0x44,0x10,0x47,0xFE,0x44,0x10,0x7C,0x10,0x45,0x10,
0x44,0x90,0x44,0x90,0x7C,0x10,0x00,0x10,0x00,0x10,0x00,0x10,0x00,0x50,0x00,0x20,
/*-- 文字: 分 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
0x08,0x80,0x0C,0x80,0x08,0x40,0x10,0x20,0x10,0x30,0x20,0x18,0x40,0x0E,0x9F,0xE4,
0x04,0x20,0x04,0x20,0x04,0x20,0x04,0x20,0x08,0x20,0x10,0xA0,0x20,0x40,0x40,0x00,
/*-- 文字: 秒 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=16x16 --*/
0x0C,0x20,0xF0,0x20,0x10,0x20,0x11,0x28,0xFD,0xA4,0x11,0x22,0x32,0x22,0x3A,0x20,
0x54,0x22,0x54,0x22,0x90,0x04,0x10,0x08,0x10,0x10,0x10,0x60,0x11,0x80,0x16,0x00};
unsigned char code num[]={
/*-- 文字: 0 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
0x00,0x00,0x00,0x18,0x24,0x42,0x42,0x42,0x42,0x42,0x42,0x42,0x24,0x18,0x00,0x00,
/*-- 文字: 1 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
0x00,0x00,0x00,0x10,0x70,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x10,0x7C,0x00,0x00,
/*-- 文字: 2 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
0x00,0x00,0x00,0x3C,0x42,0x42,0x42,0x04,0x04,0x08,0x10,0x20,0x42,0x7E,0x00,0x00,
/*-- 文字: 3 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
0x00,0x00,0x00,0x3C,0x42,0x42,0x04,0x18,0x04,0x02,0x02,0x42,0x44,0x38,0x00,0x00,
/*-- 文字: 4 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
0x00,0x00,0x00,0x04,0x0C,0x14,0x24,0x24,0x44,0x44,0x7E,0x04,0x04,0x1E,0x00,0x00,
/*-- 文字: 5 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
0x00,0x00,0x00,0x7E,0x40,0x40,0x40,0x58,0x64,0x02,0x02,0x42,0x44,0x38,0x00,0x00,
/*-- 文字: 6 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
0x00,0x00,0x00,0x1C,0x24,0x40,0x40,0x58,0x64,0x42,0x42,0x42,0x24,0x18,0x00,0x00,
/*-- 文字: 7 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
0x00,0x00,0x00,0x7E,0x44,0x44,0x08,0x08,0x10,0x10,0x10,0x10,0x10,0x10,0x00,0x00,
/*-- 文字: 8 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
0x00,0x00,0x00,0x3C,0x42,0x42,0x42,0x24,0x18,0x24,0x42,0x42,0x42,0x3C,0x00,0x00,
/*-- 文字: 9 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
0x00,0x00,0x00,0x18,0x24,0x42,0x42,0x42,0x26,0x1A,0x02,0x02,0x24,0x38,0x00,0x00,
/*-- 文字: 0 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
0xFF,0xFF,0xFF,0xE7,0xDB,0xBD,0xBD,0xBD,0xBD,0xBD,0xBD,0xBD,0xDB,0xE7,0xFF,0xFF,
/*-- 文字: 1 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
0xFF,0xFF,0xFF,0xEF,0x8F,0xEF,0xEF,0xEF,0xEF,0xEF,0xEF,0xEF,0xEF,0x83,0xFF,0xFF,
/*-- 文字: 2 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
0xFF,0xFF,0xFF,0xC3,0xBD,0xBD,0xBD,0xFB,0xFB,0xF7,0xEF,0xDF,0xBD,0x81,0xFF,0xFF,
/*-- 文字: 3 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
0xFF,0xFF,0xFF,0xC3,0xBD,0xBD,0xFB,0xE7,0xFB,0xFD,0xFD,0xBD,0xBB,0xC7,0xFF,0xFF,
/*-- 文字: 4 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
0xFF,0xFF,0xFF,0xFB,0xF3,0xEB,0xDB,0xDB,0xBB,0xBB,0x81,0xFB,0xFB,0xE1,0xFF,0xFF,
/*-- 文字: 5 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
0xFF,0xFF,0xFF,0x81,0xBF,0xBF,0xBF,0xA7,0x9B,0xFD,0xFD,0xBD,0xBB,0xC7,0xFF,0xFF,
/*-- 文字: 6 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
0xFF,0xFF,0xFF,0xE3,0xDB,0xBF,0xBF,0xA7,0x9B,0xBD,0xBD,0xBD,0xDB,0xE7,0xFF,0xFF,
/*-- 文字: 7 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
0xFF,0xFF,0xFF,0x81,0xBB,0xBB,0xF7,0xF7,0xEF,0xEF,0xEF,0xEF,0xEF,0xEF,0xFF,0xFF,
/*-- 文字: 8 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
0xFF,0xFF,0xFF,0xC3,0xBD,0xBD,0xBD,0xDB,0xE7,0xDB,0xBD,0xBD,0xBD,0xC3,0xFF,0xFF,
/*-- 文字: 9 --*/
/*-- 宋体12; 此字体下对应的点阵为:宽x高=8x16 --*/
0xFF,0xFF,0xFF,0xE7,0xDB,0xBD,0xBD,0xBD,0xD9,0xE5,0xFD,0xFD,0xDB,0xC7,0xFF,0xFF,
};
unsigned char code ku[]={
/*-- 调入了一幅图像:这是哭的的图像 --*/
/*-- 宽度x高度=16x16 --*/
0x03,0xC0,0x0C,0x30,0x10,0x08,0x20,0x04,0x40,0x02,0x52,0x4A,0x8C,0x31,0x80,0x01,
0x80,0x01,0x81,0x81,0x42,0x42,0x44,0x22,0x20,0x04,0x10,0x08,0x0C,0x30,0x03,0xC0,
/*-- 调入了一幅图像:这是笑的的图像 --*/
/*-- 宽度x高度=16x16 --*/
0x03,0xC0,0x0C,0x30,0x10,0x08,0x20,0x04,0x40,0x02,0x40,0x02,0x8C,0x31,0x92,0x49,
0x80,0x01,0x80,0x01,0x44,0x22,0x42,0x42,0x21,0x84,0x10,0x08,0x0C,0x30,0x03,0xC0
};
/*****************************************************************************
**字库结束
******************************************************************************/
void day();
void time();
void begin();
void display(unsigned char *cpoint,unsigned char x_line,unsigned char y_line,unsigned char s_l);
unsigned char hex_bcd(unsigned char hex);
void display_begin();
void display_time(struct time_str time,struct day_str day);
bit leapf(unsigned int year);
void main()
{
begin();
display_begin();
while (1)
{
while (1)
{
time();
day();
if (display_flag)
{
display_flag=0;
display_time(time_now,day_now);
}
if (display_flag)
continue;
if((day_now.month==2)&leap)
{
leap=0;
day_temp=29;
}
}
}
}
/******************************************************
**time()子函数
**参数:second_flay
**
******************************************************/
void time(){
if(second_flag){
display_flag=1;
second_flag=0;
// day_flag=1;
time_now.second++;
if(time_now.second==60){
time_now.second=0;
time_now.minute++;
if(time_now.minute==60){
time_now.minute=0;
time_now.hour++;
if(time_now.hour==24){
time_now.hour=0;
day_flag=1;
}
}
}
}
}
/*********************************************************
**day()子函数
**参数:day_flag日标志;leap润年标志
**
**********************************************************/
void day(){
if(day_flag){
day_flag=0;
day_now.day++;
if(day_now.day==day_temp+1){
day_now.month++;
if((day_now.month==2)&leap){
leap=0;
day_temp=29;
}
else day_temp=day_num[day_now.month-1];
day_now.day=1;
if(day_now.month==13){
day_now.month=1;
day_temp=day_num[day_now.month-1];
day_now.year++;
leap=leapf(day_now.year);
}
}
}
}
/****************************************************************
**begin()子函数系统参数初始化
**
****************************************************************/
void begin(){
//定时器0初始化
TMOD=0x21; //50ms int 12M
TH0=0x3c;
TL0=0xba;
TR0=1;
IE=0x92;
SCON|=0x40;
//状态标志位初始化
//参量初始化
time_now.second=0;
time_now.minute=0;
time_now.hour=0;
day_now.year=1980;
day_now.month=6;
day_now.day=27;
leap=leapf(day_now.year);
day_temp=day_num[day_now.month-1];
}
/****************************************************************
**display()子函数
**参数说明:*cpoint:要显示的字符地址指针
** x_line:横轴坐标(1-16)
** y_line:纵轴坐标(1-16)
** s_l: 1为8*16字体;2为16*16字体
**返回值:无
****************************************************************/
void display(unsigned char *cpoint,unsigned char x_line,unsigned char y_line,unsigned char s_l){
unsigned char xdata *xpoint;
unsigned char i,j;
xpoint=0x8000+x_line+y_line*16;
for(i=0;i<16;i++){
for(j=0;j
cpoint++;
xpoint++;
}
xpoint=xpoint+16-s_l;
}
}
/****************************************************************
**display_begin()子函数显示屏幕的初始值如:
** —————————
** | 1980年09月05日|
** | 23时03分09秒|
** —————————
**调用函数:display()
** display_time()
**
****************************************************************/
//指定位置显示“年,月,日,时,分,秒”
//unsigned char code zhi_loc[]=
//{0x06,0x01,0x0a,0x01,0x0e,0x01,0x06,0x10,0x0a,0x10,0x0e,0x10};
void display_begin(){
unsigned char x_display,y_display,i,j=0;
unsigned char code *p_display;
p_display=&shi;
for (i=0;i<6;i++){
x_display=zhi_loc[j];
j++;
y_display=zhi_loc[j];
j++;
display(p_display,x_display,y_display,2);
p_display=p_display+32;
}
//指定位置显示时间
display_time(time_now,day_now);
}
/****************************************************************
** display_time()显示时间子函数
** 参数:struct time_str time
** struct day_str day
** 返回值:无
** 调用函数:display()
** hex_bcd()
****************************************************************/
void display_time(struct time_str time,struct day_str day){
unsigned char temp,temp1,year,x,y;
unsigned char *p;
//显示哭笑符
if (ku_flag)p=(&ku)+32;
else p=&ku;
x=0;
y=16;
ku_flag=!ku_flag;
display(p,x,y,2);
//显示秒
temp=time.second;
temp=hex_bcd(temp);
temp1=temp>>4;
temp=temp&0xf;
p=(&num)+16*temp;
x=13;
y=16;
display(p,x,y,1);
x=12;
p=(&num)+16*temp1;
display(p,x,y,1);
//显示分
temp=time.minute;
temp=hex_bcd(temp);
temp1=temp>>4;
temp=temp&0xf;
p=(&num)+16*temp;
x=9;
y=16;
display(p,x,y,1);
x=8;
p=(&num)+16*temp1;
display(p,x,y,1);
//显示时
temp=time.hour;
temp=hex_bcd(temp);
temp1=temp>>4;
temp=temp&0xf;
p=(&num)+16*temp;
x=5;
y=16;
display(p,x,y,1);
x=4;
p=(&num)+16*temp1;
display(p,x,y,1);
//显示日
temp=day.day;
temp=hex_bcd(temp);
temp1=temp>>4;
temp=temp&0xf;
p=(&num)+16*temp;
x=13;
y=0;
display(p,x,y,1);
x=12;
p=(&num)+16*temp1;
display(p,x,y,1);
//显示月
temp=day.month;
temp=hex_bcd(temp);
temp1=temp>>4;
temp=temp&0xf;
p=(&num)+16*temp;
x=9;
y=0;
display(p,x,y,1);
x=8;
p=(&num)+16*temp1;
display(p,x,y,1);
//显示年
year=day.year/100;
temp=day.year%100;
temp=hex_bcd(temp);
temp1=temp>>4;
temp=temp&0xf;
p=(&num)+16*temp;
x=5;
y=0;
display(p,x,y,1);
x=4;
p=(&num)+16*temp1;
display(p,x,y,1);
//显示年高位
temp=hex_bcd(year);
temp1=temp>>4;
temp=temp&0xf;
p=(&num)+16*temp;
x=3;
y=0;
display(p,x,y,1);
x=2;
p=(&num)+16*temp1;
display(p,x,y,1);
}
/****************************************************************
**润年判断leap()子函数
**参数:unsigned int year
**返回字:润年标志leap0为非润年
**内部变量:bit leap1
****************************************************************/
bit leapf(unsigned int year){
bit bdata leap1;
if(year%4==0){
if(year%100==0){
if(year%400==0){
leap1=1;
}
else leap1=0;
}
else leap1=1;
}
else leap1=0;
return(leap1);
}
/****************************************************************
**hex_bcd()子函数
**参数:hex需要转换的单字节16进制数
**返回字:转换完成的单字节bcd码,高4位为高位低4位为低位
**内部变量:bcd,temp为无符号字符型
****************************************************************/
unsigned char hex_bcd(unsigned char hex){
unsigned char bcd,temp;
bcd=hex%10;
temp=hex/10;
temp=temp<<4;
bcd=bcd|temp;
return (bcd);
}
/****************************************************************
** key()键盘子函数
** 键盘为两键式一个键
** 参数:
****************************************************************/
/****************************************************************
** time0()定时器0中断子函数,产生50ms中断
**
****************************************************************/
void time0() interrupt 1 using 1{
TH0=0x3c; //time0 50ms interrupt
TL0=0xba;
TR0=1;
msecond++;
if (msecond==20){
second_flag=1;
msecond=0;
}
}
『本文转载自网络,版权归原作者所有,如有侵权请联系删除』