"); //-->
//使用请申明代码来源
//程序不包含时间校准部分 晶振为16MHz
#include <reg52.h>
#define uint unsigned int
#define uchar unsigned char
#define x1 0x80
#define x2 0x88
#define y 0x80
#define comm 0
#define dat 1
//液晶控制端口定义
sbit cs = P0^0;
sbit std = P0^1;
sbit sclk = P0^2;
sbit psb = P0^3; //H=并口; L=串口;
sbit rst = P3^7; //Reset Signal 低电平有效
//全局变量定义
bit timeflag = 0; //时间标示
bit displayflag = 1; //显示扫描
unsigned int intcounter = 0; //中断次数计数器
bit dayflag = 0;
struct calendar
{
unsigned int year; //年
unsigned char month; //月
unsigned char day; //日
unsigned char hour; //小时
unsigned char minute; //分钟
unsigned char second; //秒钟
unsigned char week; //星期
unsigned char alarmhour; //闹钟小时
unsigned char alarmminute; //闹钟分钟
}caldar;
uchar code tab1[]={
//"本系列中文模块内"
//"任意位置反白显示"
//"置二级字库,可在"
//"使用更方便更灵活"
" 烟台大学 "
"学院:计算机学院"
" 04级毕业设计 "
"专业: 自动化 "
};
//函数声明
bit LeapYear(unsigned int year); //计算是否是闰年
unsigned char GetMonthDay(unsigned int year,unsigned char month); //计算月份的天数
void Time(void); //时钟函数
void DayTime(void); //日历函数
void wr_lcd (uchar dat_comm,uchar content); //写LCD
void delay (uint us);
void init_lcd (void); //LCD初始化
void chn_disp (uchar code *chn);
void lat_disp (uchar data1,uchar data2);
void con_disp (uchar data1,uchar data2,uchar x0,uchar y0,uchar xl,uchar yl);
void wr_lcd (uchar dat_comm,uchar content);
void delay1 (uint ms);
void clrram (void); //清屏操作
void DisplayCalendar(void);
//初始化串口:串口工作方式1,允许接受REN=1,波特率=1200
void initialize_UART()
{
P1=0xFF;
TMOD=0x20;
//下面已修改为适合12MHz晶振,11.0592改成E8
TH1=0xf7;//0xe6;
TL1=0xf7;//0xe6;
SCON=0x50;
TR1=1;
}
//串口发,查询发送
void s_send(unsigned char ch)
{
SBUF=ch;
TI=0;
while(!TI);
TI=0;
}
main()
{
caldar.year = 2008;
caldar.month = 12;
caldar.day = 31;
caldar.hour = 23;
caldar.minute = 59;
caldar.second = 30;
caldar.week = 2;
initialize_UART();
init_lcd();
TMOD = TMOD | 0x01;
TMOD = TMOD & 0xfd; //16位定时器方式
EA = 1;
ET0 = 1;
TR0 = 1;
lat_disp (0x00,0x00);
clrram();
delay1 (40);
chn_disp (tab1);
delay1 (5000);
// delay1 (50000);
clrram();
for (;;)
{
Time();
DayTime();
DisplayCalendar();
}
}
/******************************************************************************
* 函数:bit LeapYear(unsigned int year)
* 出口参数:1 则是润年 0 则不是
* 入口参数:年份
* 日期: 2008.01.08
* 编者:zhangfazhong
* 状态:已测 通过 (2006 2000)
******************************************************************************/
bit LeapYear(unsigned int year)
{
bit flag = 0;
if (year%4 == 0)
{
if (year*100 != 0)
flag = 1;
}
if (year%400 == 0)
flag = 1;
return (flag);
// return(((year%4 == 0)&&(year%100 != 0))||(year%400 == 0));
}
/******************************************************************************
* 函数:unsigned char GetMonthDay(unsigned int year,unsigned char month)
* 出口参数:此月的天数
* 入口参数:年份 月份
* 日期: 2008.01.08
* 编者: zhangfazhong
* 状态:已测 通过
******************************************************************************/
unsigned char GetMonthDay(unsigned int year,unsigned char month)
{
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return (31);
break;
case 4:
case 6:
case 9:
case 11:
return(30);
break;
case 2:
if (LeapYear(year))
return (29);
else
return (28);
// return (LeapYear(year)?28:29);
break;
default: return 0;
}
}
/******************************************************************************
* 函数:void DisplayCalendar(void)
* 出口参数:无
* 入口参数:无
* 日期: 2008.01.08
* 编者: zhangfazhong
* 状态:待测
******************************************************************************/
void DisplayCalendar(void)
{
unsigned int week[8] = {0xd2bb,0xd2bb,0xb6fe,0xc8fd,0xcbc4,0xcee5,0xc1f9,0xc8d5};
//一 一 二 三 四 五 六 日
wr_lcd (comm,0x88);
wr_lcd (dat,caldar.year/1000 + 48);//
wr_lcd (dat,(caldar.year/100)%10 + 48);
wr_lcd (comm,0x89);
wr_lcd (dat,(caldar.year/10)%10 + 48);//
wr_lcd (dat,caldar.year%10 + 48);
wr_lcd (comm,0x8a); //年
wr_lcd (dat,0xc4);//:
wr_lcd (dat,0xea);
wr_lcd (comm,0x8b);
wr_lcd (dat,caldar.month/10 + 48);
wr_lcd (dat,caldar.month%10 + 48);
wr_lcd (comm,0x8c);
wr_lcd (dat,0xd4);//月
wr_lcd (dat,0xc2);
wr_lcd (comm,0x8d);
wr_lcd (dat,caldar.day/10 + 48);
wr_lcd (dat,(caldar.day%10) + 48);
wr_lcd (comm,0x8e); //日
wr_lcd (dat,0xc8);
wr_lcd (dat,0xd5);
wr_lcd (comm,0x98);
wr_lcd (dat,0xd0);//星
wr_lcd (dat,0xc7);
wr_lcd (comm,0x99);
wr_lcd (dat,0xc6);//期
wr_lcd (dat,0xda);
wr_lcd (comm,0x9a);
wr_lcd (dat,(unsigned char)(week[caldar.week]>>8));//一 二 三 ....日
wr_lcd (dat,(unsigned char)week[caldar.week]);
wr_lcd (comm,0x9c);
wr_lcd (dat,(char)(caldar.hour/10)+48);
wr_lcd (dat,caldar.hour%10+48);
wr_lcd (comm,0x9d);
wr_lcd (dat,0x3a); //:
wr_lcd (dat,(char)(caldar.minute/10)+48);
wr_lcd (comm,0x9e);
wr_lcd (dat,caldar.minute%10+48);
wr_lcd (dat,0x3a); //:
wr_lcd (comm,0x9f);
wr_lcd (dat,(char)(caldar.second/10)+48 );
wr_lcd (dat,caldar.second%10+48);
}
/*****************************************************************************
函数:void Time(void)
出口参数:无
入口参数:无
日期:2007.12.27
编者:zhangfazhong
状态:已测通过
*****************************************************************************/
void Time(void)
{
if(timeflag)
{
intcounter++;
timeflag = 0;
if (intcounter == 50) //388 180(慢) //此值待定171(12小时快3分钟)
{
intcounter = 0;
if(caldar.second == 59)
{
if (caldar.minute == 59)
{
caldar.minute = 0;
if (caldar.hour == 23)
{
caldar.hour = 0;
dayflag = 1;
if (caldar.week == 7)
{
caldar.week = 1;
}
else
caldar.week ++;
}
else
caldar.hour++;
}
else
caldar.minute++;
caldar.second = 0;
}
else
caldar.second++;
}
s_send(caldar.hour/10 + 48);
s_send(caldar.hour%10 + 48);
// s_send('H');
s_send(caldar.minute/10 + 48);
s_send(caldar.minute%10 + 48);
// s_send('M');
s_send(caldar.second/10 + 48);
s_send(caldar.second%10 + 48);
// s_send('S');
}
}
/******************************************************************************
功能:T0中断
日期:
编者:
状态:已测功能实现
******************************************************************************/
Timer0() interrupt 1 using 1
{
TH0 = 0x97;//b1; //20ms
TL0 = 0xd5;//e0;
timeflag = 1;
displayflag = 1;
TF0 = 0;
}
/*****************************************************************************
函数:void DayTime(void)
出口参数:无
入口参数:无
日期:2008.01.08
编者:zhangfazhong
状态:已测 通过
*****************************************************************************/
void DayTime(void)
{
if (dayflag)
{
dayflag = 0;
if (caldar.day < GetMonthDay(caldar.year,caldar.month))
{
caldar.day++;
}
else
{
caldar.day = 1;
if((caldar.month == 12))
{
caldar.month = 1;
caldar.year++;
}
else
{
caldar.month++;
}
}
s_send(caldar.year/1000 + 48);
s_send((caldar.year/100)%10 + 48);
s_send((caldar.year/10)%10 + 48);
s_send(caldar.year%10 + 48);
s_send('Y');
s_send(caldar.month/10 + 48);
s_send(caldar.month%10 +48);
s_send('M');
s_send(caldar.day/10 + 48);
s_send(caldar.day%10 +48);
s_send('D');
}
}
/*------------------初始化-----------------*/
void init_lcd (void)
{
rst=1;
psb=0;
wr_lcd (comm,0x30); /*30---基本指令动作*/
wr_lcd (comm,0x01); /*清屏,地址指针指向00H*/
delay (100);
wr_lcd (comm,0x06); /*光标的移动方向*/
wr_lcd (comm,0x0c); /*开显示,关游标*/
}
/*---------------显示汉字或字符----------------*/
void chn_disp (uchar code *chn)
{
uchar i,j;
wr_lcd (comm,0x30);
wr_lcd (comm,0x80);
for (j=0;j<4;j++)
{
for (i=0;i<16;i++)
wr_lcd (dat,chn[j*16+i]);
}
}
/*--------------显示点阵----------------*/
void lat_disp (uchar data1,uchar data2)
{
uchar i,j,k,x;
x=x1;
for(k=0;k<2;k++)
{
for(j=0;j<16;j++)
{
for(i=0;i<8;i++)
{
wr_lcd (comm,0x34);
wr_lcd (comm,y+j*2);
wr_lcd (comm,x+i);
wr_lcd (comm,0x30);
wr_lcd (dat,data1);
wr_lcd (dat,data1);
}
for(i=0;i<8;i++)
{
wr_lcd (comm,0x34);
wr_lcd (comm,y+j*2+1);
wr_lcd (comm,x+i);
wr_lcd (comm,0x30);
wr_lcd (dat,data2);
wr_lcd (dat,data2);
}
}
x=x2;
}
wr_lcd (comm,0x36);
}
/*-----------------------------------------------*/
//当data1=0xff,data2=0xff时,在x0,y0处反白显示16xl*yl.
void con_disp (uchar data1,uchar data2,uchar x0,uchar y0,uchar xl,uchar yl)
{
uchar i,j;
for(j=0;j<yl;j++)
{
for(i=0;i<xl;i++)
{
wr_lcd (comm,0x34);
wr_lcd (comm,y0+j);
wr_lcd (comm,x0+i);
wr_lcd (comm,0x30);
wr_lcd (dat,data1);
wr_lcd (dat,data2);
}
}
wr_lcd (comm,0x36);
}
/*--------------清DDRAM------------------*/
void clrram (void)
{
wr_lcd (comm,0x30);
wr_lcd (comm,0x01);
delay (300);
}
/*---------------------------------------*/
void wr_lcd (uchar dat_comm,uchar content)
{
uchar a,i,j;
delay (50);
a=content;
cs=1;
sclk=0;
std=1;
for(i=0;i<5;i++)
{
sclk=1;
sclk=0;
}
std=0;
sclk=1;
sclk=0;
if(dat_comm)
std=1; //data
else
std=0; //command
sclk=1;
sclk=0;
std=0;
sclk=1;
sclk=0;
for(j=0;j<2;j++)
{
for(i=0;i<4;i++)
{
a=a<<1;
std=CY;
sclk=1;
sclk=0;
}
std=0;
for(i=0;i<4;i++)
{
sclk=1;
sclk=0;
}
}
}
/*-----------------------------------*/
void delay (uint us) //delay time
{
while(us--);
}
void delay1 (uint ms)
{
uint i,j;
for(i=0;i<ms;i++)
for(j=0;j<15;j++)
delay(1);
}
*博客内容为网友个人发布,仅代表博主个人观点,如有侵权请联系工作人员删除。