//使用请声明代码来源
#include <reg52.h>
#include <PORTSET.h>
#include <math.h>
#include <intrins.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 低电平有效
//ISD1420端口定义
sbit ADDRESS0 = P3^0; //录放音地址A3
sbit ADDRESS1 = P3^1; //录放音地址A4
sbit ADDRESS2 = P3^2; //录放音地址A5
sbit ADDRESS3 = P3^4; //录放音地址A6
sbit PLAY = P3^3; //放音控制口
//全局变量定义
bit timeflag = 0; //时间标示
bit displayflag = 1; //显示扫描
unsigned int intcounter = 0; //中断次数计数器
bit dayflag = 0;
unsigned char keycounter1 = 0; //记录第一个建按的次数
unsigned char keycounter2 = 0; //记录第二个建按的次数
unsigned char keycounter3 = 0; //记录第三个建按的次数
unsigned char keycounter4 = 0; //记录第四个建按的次数
bit keyflag = 0; //控制键盘扫描时间
unsigned char ch1data[10] = {0x00}; //数据缓冲区
bit clockflag = 1; //闹钟开启标志
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级生产实习 "
"专业: 自动化 "
};
uchar code tab2[]={
//"本系列中文模块内"
//"任意位置反白显示"
//"置二级字库,可在"
//"使用更方便更灵活"
" 鸣谢 "
"学院创新实验室 "
"烟大创新实验室 "
"指导教师:刘兆伟"
};
uchar code tab3[]={
//"本系列中文模块内"
//"任意位置反白显示"
//"置二级字库,可在"
//"使用更方便更灵活"
"实习小组:LanMan"
"让生活变得更懒。"
"LanMan 懒人——"
"LanMan WorkGroup"
};
//函数声明
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); //显示日历
void ADCInitial(unsigned char chanal,unsigned char speed); //初始化AD
void StartADC(void); //启动AD转换
void StopADC(void); //停止AD转换
unsigned char GetKeyNum(void); //取键盘值
void ChangTime(void); //校准时间
void ChangCalendar(void); //日历校准
void ChangClock(void); //闹铃修改
void Alarm(void); //闹铃函数
void ISD1420_Play(unsigned char playrecord); //放音函数
//初始化串口:串口工作方式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()
{
unsigned char keynum = 0;
caldar.year = 2008;
caldar.month = 1;
caldar.day = 8;
caldar.hour = 12;
caldar.minute = 30;
caldar.second = 30;
caldar.week = 2;
caldar.alarmhour = 6;
caldar.alarmminute = 30;
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 (50000);
delay1 (50000);
clrram();
lat_disp (0x00,0x00);
clrram();
delay1 (40);
chn_disp (tab2);
delay1 (50000);
delay1 (50000);
clrram();
lat_disp (0x00,0x00);
clrram();
delay1 (40);
chn_disp (tab3);
delay1 (50000);
delay1 (50000);
delay1 (50000);
clrram();
for (;;)
{
Time();
DayTime();
if (clockflag)
Alarm();
else
PLAY = 1;
if (keyflag)
{
if (keynum != GetKeyNum())
{
clrram (); //清出上一个界面
// delay(400);
keynum = GetKeyNum();
if (keynum == 1)
keycounter1++;
if (keynum == 2)
keycounter2++;
if (keynum ==3)
//keycounter3 = 1;
keycounter3++;
if (keynum ==4)
//keycounter4 = 1;
keycounter4++;
// s_send(keycounter1);
}
keyflag = 0;
}
switch(keycounter1) //菜单
{
case 0: //主界面显示
if(displayflag)
{
displayflag = 0;
DisplayCalendar();
}
break;
case 1: //调时间
ChangTime();
break;
case 2: //调日历
ChangCalendar();
break;
case 3: //调闹铃
ChangClock();
break;
case 4: //返回主界面
keycounter1 = 0;
keycounter2 = 0;
keycounter3 = 0;
keycounter4 = 0;
break;
default:keycounter1 = 0;break; //返回主界面
}
}
}
/******************************************************************************
* 函数: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,0x80);
wr_lcd (dat,0x2a);//
wr_lcd (dat,0x2a);
// wr_lcd (dat,0xcf);//现
// wr_lcd (dat,0xd6);
wr_lcd (comm,0x82);
wr_lcd (dat,0x2a);//室
wr_lcd (dat,0x2a);
// wr_lcd (dat,0xcf);//现
// wr_lcd (dat,0xd6);
wr_lcd (comm,0x81);
wr_lcd (dat,0x2a);//室
wr_lcd (dat,0x2a);
wr_lcd (comm,0x83);
wr_lcd (dat,0xc8);//日
wr_lcd (dat,0xd5);
// wr_lcd (dat,0xd4);//在
// wr_lcd (dat,0xda);
wr_lcd (comm,0x84);
wr_lcd (dat,0xc0);//历
wr_lcd (dat,0xfa);
// wr_lcd (dat,0xca);//是
// wr_lcd (dat,0xc7);
wr_lcd (comm,0x85);
wr_lcd (dat,0x2a);//境
wr_lcd (dat,0x2a);
wr_lcd (comm,0x86);
wr_lcd (dat,0x2a);//境
wr_lcd (dat,0x2a);
wr_lcd (comm,0x87);
wr_lcd (dat,0x2a);//境
wr_lcd (dat,0x2a);
// wr_lcd (comm,0x84);
// wr_lcd (dat,0xa3);//:
// wr_lcd (dat,0xba);
wr_lcd (comm,0x90);
wr_lcd (dat,caldar.year/1000 + 48);//
wr_lcd (dat,(caldar.year/100)%10 + 48);
wr_lcd (comm,0x91);
wr_lcd (dat,(caldar.year/10)%10 + 48);//
wr_lcd (dat,caldar.year%10 + 48);
wr_lcd (comm,0x92); //年
wr_lcd (dat,0xc4);//:
wr_lcd (dat,0xea);
wr_lcd (comm,0x93);
wr_lcd (dat,caldar.month/10 + 48);
wr_lcd (dat,caldar.month%10 + 48);
wr_lcd (comm,0x94);
wr_lcd (dat,0xd4);//月
wr_lcd (dat,0xc2);
wr_lcd (comm,0x95);
wr_lcd (dat,caldar.day/10 + 48);
wr_lcd (dat,(caldar.day%10) + 48);
wr_lcd (comm,0x96); //日
wr_lcd (dat,0xc8);
wr_lcd (dat,0xd5);
if (clockflag)
{
wr_lcd (comm,0x97); //日
wr_lcd (dat,0x20);
wr_lcd (dat,0x02);
}
else
{
wr_lcd (comm,0x97); //日
wr_lcd (dat,0x20);
wr_lcd (dat,0x20);
}
wr_lcd (comm,0x88);
wr_lcd (dat,0x2a);//
wr_lcd (dat,0x2a);
// wr_lcd (dat,0xcf);//现
// wr_lcd (dat,0xd6);
wr_lcd (comm,0x89);
wr_lcd (dat,0x2a);//室
wr_lcd (dat,0x2a);
// wr_lcd (dat,0xcf);//现
// wr_lcd (dat,0xd6);
wr_lcd (comm,0x8a);
wr_lcd (dat,0x2a);//室
wr_lcd (dat,0x2a);
wr_lcd (comm,0x8b);
wr_lcd (dat,0xca);//时
wr_lcd (dat,0xb1);
// wr_lcd (dat,0xd4);//在
// wr_lcd (dat,0xda);
wr_lcd (comm,0x8c);
wr_lcd (dat,0xd6);//钟
wr_lcd (dat,0xd3);
// wr_lcd (dat,0xca);//是
// wr_lcd (dat,0xc7);
wr_lcd (comm,0x8d);
wr_lcd (dat,0x2a);//境
wr_lcd (dat,0x2a);
wr_lcd (comm,0x8e);
wr_lcd (dat,0x2a);//境
wr_lcd (dat,0x2a);
wr_lcd (comm,0x8f);
wr_lcd (dat,0x2a);//境
wr_lcd (dat,0x2a);
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++;
}
}
}
/******************************************************************************
功能:T0中断
日期:
编者:
状态:已测功能实现
******************************************************************************/
Timer0() interrupt 1 using 1
{
TH0 = 0x97;//b1; //20ms
TL0 = 0xd5;//e0;
timeflag = 1;
displayflag = 1;
keyflag = 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);
}