main.c
#include <reg52.h>
#include "./lcd/lcd.h"
#define ds1302_sec_add 0x80 //
#define ds1302_min_add 0x82 //
#define ds1302_hr_add 0x84 //
#define ds1302_date_add 0x86 //
#define ds1302_month_add 0x88 //
#define ds1302_day_add 0x8a //
#define ds1302_year_add 0x8c //
#define ds1302_control_add 0x8e //
#define ds1302_charger_add 0x90
#define ds1302_clkburst_add 0xbe
sbit RST = P1^0;
sbit SCK = P1^1;
sbit IO = P1^2;
unsigned char timebuf[7] = {18,40,55,16,8,17,3}; //时,分,秒,年,月,日,星期
unsigned char tempbuf[7];
unsigned char readbuf[7];
unsigned char disbuf[7];
void ds1302_write_byte(unsigned char addr, unsigned char byte)
{
unsigned char i;
addr = addr & 0xfe; //最低位清0
SCK = 0;
RST = 0;
RST = 1;
for(i = 0; i < 8; i++)
{
IO = addr & 0x01; //最低位
SCK = 0; //上升沿
SCK = 1; //上升沿
addr >>= 1; //次低位
}
for(i = 0; i < 8; i++)
{
IO = byte & 0x01;
SCK = 0;
SCK = 1;
byte >>= 1;
}
SCK = 0;
RST = 0;
}
unsigned char ds1302_read_byte(unsigned char addr)
{
unsigned char i;
unsigned char temp;
addr = addr & 0xfe; //最低位清0
SCK = 0;
RST = 0;
RST = 1;
addr = addr + 1;
for(i = 0; i < 8; i++)
{
IO = addr & 0x01; //最低位
SCK = 0; //上升沿
SCK = 1; //上升沿
addr >>= 1; //次低位
}
for(i = 0; i < 8; i++)
{
SCK = 1;
SCK = 0;
temp >>= 1;
if(IO)
{
temp += 0x80;
}
}
RST = 0;
return temp;
}
void ds1302_write_time()
{
unsigned char temp;
unsigned char i;
for(i = 0; i < 7; i++)
{
temp = timebuf[i] / 10;
tempbuf[i] = timebuf[i] % 10;
tempbuf[i] = (temp << 4) | tempbuf[i];
}
ds1302_write_byte(ds1302_control_add,0x00); //关闭写保护
ds1302_write_byte(ds1302_hr_add,tempbuf[0]);
ds1302_write_byte(ds1302_min_add,tempbuf[1]);
ds1302_write_byte(ds1302_sec_add,tempbuf[2]);
ds1302_write_byte(ds1302_year_add,tempbuf[3]);
ds1302_write_byte(ds1302_month_add,tempbuf[4]);
ds1302_write_byte(ds1302_date_add,tempbuf[5]);
ds1302_write_byte(ds1302_day_add,tempbuf[6]);
ds1302_write_byte(ds1302_control_add,0x80); //打开写保护
}
void ds1302_read_time()
{
unsigned char i;
unsigned char high;
unsigned char low;
readbuf[0] = ds1302_read_byte(ds1302_hr_add);
readbuf[1] = ds1302_read_byte(ds1302_min_add);
readbuf[2] = ds1302_read_byte(ds1302_sec_add);
readbuf[3] = ds1302_read_byte(ds1302_year_add);
readbuf[4] = ds1302_read_byte(ds1302_month_add);
readbuf[5] = ds1302_read_byte(ds1302_date_add);
readbuf[6] = ds1302_read_byte(ds1302_day_add);
for(i = 0; i < 7; i++)
{
high = (readbuf[i] >> 4);
low = (readbuf[i] & 0x0f);
disbuf[i] = high * 10 + low;
}
}
void lcd_dis_time()
{
unsigned char lcd_distime[9] = {0};
unsigned char lcd_time[9] = {0};
lcd_distime[0] = (disbuf[0] / 10) + 0x30;
lcd_distime[1] = (disbuf[0] % 10) + 0x30;
lcd_distime[2] = ':';
lcd_distime[3] = (disbuf[1] / 10) + 0x30;
lcd_distime[4] = (disbuf[1] % 10) + 0x30;
lcd_distime[5] = ':';
lcd_distime[6] = (disbuf[2] / 10) + 0x30;
lcd_distime[7] = (disbuf[2] % 10) + 0x30;
lcd_time[0] = (disbuf[3] / 10) + 0x30;
lcd_time[1] = (disbuf[3] % 10) + 0x30;
lcd_time[2] = '.';
lcd_time[3] = (disbuf[4] / 10) + 0x30;
lcd_time[4] = (disbuf[4] % 10) + 0x30;
lcd_time[5] = '.';
lcd_time[6] = (disbuf[5] / 10) + 0x30;
lcd_time[7] = (disbuf[5] % 10) + 0x30;
lcd_dis_str(1,1,"DAY:20");
lcd_dis_str(1,7,lcd_time);
lcd_dis_str(2,6,lcd_distime);
switch(readbuf[6])
{
case 1:
{
lcd_dis_str(2,1,"MON");
break;
}
case 2:
{
lcd_dis_str(2,1,"TUE");
break;
}
case 3:
{
lcd_dis_str(2,1,"WED");
break;
}
case 4:
{
lcd_dis_str(2,1,"THU");
break;
}
case 5:
{
lcd_dis_str(2,1,"FRI");
break;
}
case 6:
{
lcd_dis_str(2,1,"SAT");
break;
}
case 7:
{
lcd_dis_str(2,1,"SUN");
break;
}
}
}
void main()
{
lcd_init();
ds1302_write_time();
while(1)
{
ds1302_read_time();
lcd_dis_time();
}
}
lcd.c
#include <reg52.h>
#include "../delay/delay.h"
#include "lcd.h"
/*====================================================
判断写入命令/数据函数
=====================================================*/
void lcd_write(unsigned char byte, unsigned char flag)
{
if(flag)
{
RS = 1; //选择数据寄存器
}
else
{
RS = 0; //选择指令寄存器
}
RW = 0; //写
E = 1; //选中LCD
LCDPORT = byte;
delay_us(5); //稳定
E = 0; //失能LCD
}
/*====================================================
LCD初始化函数
=====================================================*/
void lcd_init()
{
delay_ms(15);
lcd_write(0x38,LCD_WRITE_COM); //显示8位数据接口,两行显示,5*7点阵字符
delay_ms(5);
lcd_write(0x38,LCD_WRITE_COM);
delay_ms(5);
lcd_write(0x38,LCD_WRITE_COM);
delay_ms(5);
lcd_write(0x38,LCD_WRITE_COM);
delay_ms(5);
lcd_write(0x08,LCD_WRITE_COM); //关闭显示,关闭光标的闪烁
delay_ms(5);
lcd_write(0x01,LCD_WRITE_COM); //清屏
delay_ms(5);
lcd_write(0x06,LCD_WRITE_COM); //设置指针方式,画面不平移
delay_ms(5);
lcd_write(0x0c,LCD_WRITE_COM); //打开显示
delay_ms(5);
}
/*====================================================================================
写入字符函数
函数原型:void lcd_dis_byte(unsigned char x, unsigned char y, unsigned char byte);
说明:x为行(1——2),y为列(1——8)。
=====================================================================================*/
void lcd_dis_byte(unsigned char x, unsigned char y, unsigned char byte)
{
unsigned char add;
if(((0 == x) || (x > 2)) || ((0 == y) || (y > 16)))
{
return ;
}
add = 0x80 + (x - 1) * 0x40 + (y - 1);
lcd_write(add,LCD_WRITE_COM);
lcd_write(byte,LCD_WRITE_DATA);
}
/*====================================================================================
写入字符串函数
函数原型:void lcd_dis_str(unsigned char x, unsigned char y, unsigned char *disdata);
说明:x为行(1——2),y为列(1——8)。
=====================================================================================*/
void lcd_dis_str(unsigned char x, unsigned char y, unsigned char *disdata)
{
unsigned char add;
if(((0 == x) || (x > 2)) || ((0 == y) || (y > 16)))
{
return ;
}
add = 0x80 + (x - 1) * 0x40 + (y - 1);
lcd_write(add,LCD_WRITE_COM);
while(*disdata != '\0')
{
lcd_write(*disdata,LCD_WRITE_DATA);
disdata++;
}
}
#if 0
/*===========================================================================
时间显示函数
============================================================================*/
void lcd_dis_time()
{
lcd_write(0x80 + 4,LCD_WRITE_COM);
lcd_write((hour / 10) + 0x30,LCD_WRITE_DATA);
lcd_write((hour % 10) + 0x30,LCD_WRITE_DATA);
lcd_write((min / 10) + 0x30,LCD_WRITE_DATA);
lcd_write((min % 10) + 0x30,LCD_WRITE_DATA);
lcd_write((sec / 10) + 0x30,LCD_WRITE_DATA);
lcd_write((sec % 10) + 0x30,LCD_WRITE_DATA);
}
#endif