管脚配置:
MOT: 接地
CS: 接地 地址:0xFF00
AS: 接单片机 ALE
R/W: 接 RW
DS: 接 RD
RESET: 接高
IRQ: 空
SQW: 空
*/
#i nclude
#i nclude
/*************************/
#define TM_SEC XBYTE[0xFF00]
#define TM_MIN XBYTE[0xFF02]
#define TM_HOU XBYTE[0xFF04]
#define DAY XBYTE[0xFF06]
#define DATE XBYTE[0xFF07]
#define MONTH XBYTE[0xFF08]
#define YEAR XBYTE[0xFF09]
#define AM_SEC XBYTE[0xFF01]
#define AM_MIN XBYTE[0xFF03]
#define AM_HOU XBYTE[0xFF05]
#define REG_A XBYTE[0xFF0a]
#define REG_B XBYTE[0xFF0b]
#define REG_C XBYTE[0xFF0c]
#define REG_D XBYTE[0xFF0d]
/********************************************************************/
#define LCDIO P2
#define LINE1 0
#define LINE2 1
#define HIGH 1
#define LOW 0
#define CLEARSCREEN LCD_en_command(0x01)
#define LCD_DELAY_TIME 40
/*******************************************************************/
sbit RS=P0^7; //数据、命令选择 1:数据 0:指令 4脚
sbit RW=P0^6; //读、写操作选择 1:读 0:写 5脚
sbit E=P0^5; //使能信号 6脚
/********************************************************************/
void LCD_delay(void);
void LCD_en_command(unsigned char command);
void LCD_en_dat(unsigned char temp);
void LCD_set_xy( unsigned char x, unsigned char y );
void LCD_write_char( unsigned x,unsigned char y,unsigned char dat);
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s);
void LCD_init(void);
/********************************************************************/
void Set_calendar(void);
void Read_calendar(void);
void Start_calendar(void);
void Display_calendar(void);
void Stop_calendar(void);
/********************************************************************/
void delay_nms(unsigned int n);
/********************************************************************/
code unsigned char calendar_day[7][3]={"MON","TUE","WEN","THU","FRI","SAT","SUN"};
unsigned char calendar_time[7]={05,8,16,18,58,55,2}; /* 05/8/16 18:58:00 星期二*/
code unsigned char at[7]={9,8,7,4,2,0,6}; /* 年、月、日、时、分、秒 、星期*/
unsigned char xdata *calendar_address=0xff00;
void main(void)
{
LCD_init();
//Set_calendar();
Start_calendar();
Stop_calendar();
while(1)
{
Read_calendar();
Display_calendar();
}
}
/********************************************************************/
/******************** LCD PART *************************************/
void LCD_delay(void)//延时子函数
{
unsigned char i;
for(i=LCD_DELAY_TIME;i>0;i--); //保证lcd复位的最小延时
}
void LCD_en_command(unsigned char command)//写命令函数
{
LCDIO=command;
RS=LOW;
RW=LOW;
E=LOW;
LCD_delay();
E=HIGH;
}
void LCD_en_dat(unsigned char dat)//写数据函数
{
LCDIO=dat;
RS=HIGH;
RW=LOW;
E=LOW;
LCD_delay();
E=HIGH;
}
void LCD_set_xy( unsigned char x, unsigned char y ) //设置地址函数
{
unsigned char address;
if (y == 0)
address = 0x80 + x;
else
address = 0xc0 + x;
LCD_en_command(address);
}
void LCD_write_char( unsigned x,unsigned char y,unsigned char dat)//写字符函数
{
LCD_set_xy( x, y );
LCD_en_dat(dat);
}
void LCD_write_string(unsigned char X,unsigned char Y,unsigned char *s)//写字符串函数
{
LCD_set_xy( X, Y ); //写地址
while (*s) // 写显示字符
{
LCDIO=*s;
LCD_en_dat(*s);
s ++;
}
}
void LCD_init(void)//初始化子函数
{
CLEARSCREEN;//清屏
LCD_en_command(0x38);//设置8位串数据格式
LCD_en_command(0x0c);//开显示器
LCD_en_command(0x80);//显示起始地址
CLEARSCREEN;//清屏
}
/********************************************************************/
void Set_calendar(void) //设置系统时间
{
unsigned char i;
REG_B=0x80;
for(i=0;i<9;i++)
*(calendar_address+at[i])=calendar_time[i];
}
void Read_calendar(void) //读取系统时间
{
unsigned char temp,i;
REG_B=0x06;
do{ temp=REG_A;}
while(temp&0x80);
for(i=0;i<7;i++)
calendar_time[i]=*(calendar_address+at[i]);
}
void Start_calendar(void) //启动时钟
{
REG_A=0x20;
REG_B=0x06;
}
/********************************************************************/
void Display_calendar(void)
{
LCD_write_string(0,LINE1," ");
LCD_write_string(0,LINE1,"20 - - ");
LCD_write_char(0x02,LINE1,(calendar_time[0]/10)|0X30);
LCD_write_char(0x03,LINE1,(calendar_time[0]%10)|0X30);
LCD_write_char(0x05,LINE1,(calendar_time[1]/10)|0X30);
LCD_write_char(0x06,LINE1,(calendar_time[1]%10)|0X30);
LCD_write_char(0x08,LINE1,(calendar_time[2]/10)|0X30);
LCD_write_char(0x09,LINE1,(calendar_time[2]%10)|0X30);
if(calendar_time[6]==1)
LCD_write_string(0x0d,LINE1,"Mon");
if(calendar_time[6]==2)
LCD_write_string(0x0d,LINE1,"Tue");
if(calendar_time[6]==3)
LCD_write_string(0x0d,LINE1,"Wen");
if(calendar_time[6]==4)
LCD_write_string(0x0d,LINE1,"Thu");
if(calendar_time[6]==5)
LCD_write_string(0x0d,LINE1,"Fri");
if(calendar_time[6]==6)
LCD_write_string(0x0d,LINE1,"Sat");
if(calendar_time[6]==7)
LCD_write_string(0x0d,LINE1,"Sun");
LCD_write_string(0,LINE2," : :");
LCD_write_char(0x08,LINE2,(calendar_time[3]/10)|0X30);
LCD_write_char(0x09,LINE2,(calendar_time[3]%10)|0X30);
LCD_write_char(0x0b,LINE2,(calendar_time[4]/10)|0X30);
LCD_write_char(0x0c,LINE2,(calendar_time[4]%10)|0X30);
LCD_write_char(0x0e,LINE2,(calendar_time[5]/10)|0X30);
LCD_write_char(0x0f,LINE2,(calendar_time[5]%10)|0X30);
}
void Stop_calendar(void)
{
REG_A=0x70;
}
上一篇:ds1868(数字电位器) c 驱动程序
下一篇:max532 驱动程序
推荐阅读最新更新时间:2024-03-16 15:18