基于DS1302的实时时钟系统

简介: 1、原理图 2、DS1302手册 3、源代码 ************************************************************************* ds1302控制程序 *****************************...
1、原理图


2、DS1302手册

3、源代码
*************************************************************************
ds1302控制程序
*************************************************************************
#include

unsigned char time[] = __TIME__;
unsigned char date[] = __DATE__;

#define uchar unsigned char
#define uint  unsigned int




#define WRITE_SECOND 0x80
#define WRITE_MINUTE 0x82
#define WRITE_HOUR 0x84

#define WRITE_DAY 0x86
#define WRITE_MONTH 0x88
#define WRITE_YEAR 0x8C

#define READ_SECOND 0x81
#define READ_MINUTE 0x83
#define READ_HOUR 0x85


#define READ_DAY 0x87
#define READ_MONTH 0x89
#define READ_YEAR 0x8D

sbit SCLK = P1^7;
sbit DATA = P1^2;

uint second, minute, hour, day, month, year;
uchar str[3];
uchar rdata;

void write_cmd(uchar addr);
void write_data(uchar dat);
void read_data();
void write_ds1302(uchar addr, uchar dat);
uint read_ds1302(uchar addr);
void init_ds1302();


void lcd_1602_init();
void lcd_1602_write_string(uchar x, uchar y, uchar *string);
void lcd_1602_write_char(uchar x, uchar y, uchar dat);
void delay_10us();
uchar *itoa(uchar dat);

void main()
{
init_ds1302();
lcd_1602_init();

second = read_ds1302(READ_SECOND);
minute = read_ds1302(READ_MINUTE);
hour = read_ds1302(READ_HOUR);




day = read_ds1302(READ_DAY);
month = read_ds1302(READ_MONTH);
year = read_ds1302(READ_YEAR);

str[0] = month/10 + '0';
str[1] = month%10 + '0';
str[2] = '\0';
// lcd_1602_write_string(1, 12, str);
lcd_1602_write_char(1, 14, str[1]);


lcd_1602_write_string(0, 0, "time:");
lcd_1602_write_string(0, 6, itoa(hour));
lcd_1602_write_string(0, 8, ":");
lcd_1602_write_string(0, 9, itoa(minute));
lcd_1602_write_string(0, 11, ":");
lcd_1602_write_string(0, 12, itoa(second));

lcd_1602_write_string(1, 0, "date:");
lcd_1602_write_string(1, 6, itoa(year));
lcd_1602_write_string(1, 8, "-");
lcd_1602_write_string(1, 9, itoa(month));
lcd_1602_write_string(1, 11, "-");
lcd_1602_write_string(1, 12, itoa(day));
}

void get_time()
{
hour = (time[0]-'0')*10 + time[1] - '0';
minute = (time[2]-'0')*10 + time[3] - '0';
second = (time[4]-'0')*10 + time[5] - '0';
}

void get_date()
{
year = (date[0]-'0')*1000 + (date[1]-'0')*100 + (date[2]-'0')*10 + (date[3]-'0');
month = (date[4]-'0')*10 + (date[5]-'0');
day = (date[6]-'0')*10 + (date[7]-'0');
}

void write_cmd(uchar addr)
{
int i;
uchar temp;
SCLK = 0;

for(i = 0; i {
SCLK = 0;
temp = addr;
DATA = (bit)(temp&0x01);
SCLK = 1;   //上升沿写入地址
addr >>= 1;   //串行输入,先输入低位
delay_10us();
}

}

void write_data(uchar dat)
{

int i;
uchar temp;
SCLK = 0;

for(i = 0; i {
SCLK = 0;
temp = dat;
DATA = (bit)(temp&0x01);
SCLK = 1; //上升沿写入数据
dat >>= 1; //串行输入,先输入低位
delay_10us();
}

}

void read_data()
{

int i;
uchar temp;
SCLK = 1;
rdata = 0;

for(i = 0; i {
SCLK = 1;
temp = DATA;
rdata |= (temp SCLK = 0;  //下降沿读出数据,先读低位
delay_10us();
}
  rdata = dat;        //将读出的数据返回
}
void write_ds1302(uchar addr, uchar dat)
{
write_cmd(addr);
write_data(dat);
}


uint read_ds1302(uchar addr)
{
write_cmd(addr);
read_data();

return rdata;
}


void init_ds1302()
{
uchar flag;

flag = read_ds1302(0x81);
if(flag & 0x80)
{
write_ds1302(0x8e, 0x00); //去除写保护
//初始化 秒、分、时
write_ds1302(WRITE_SECOND, ((44/10) write_ds1302(WRITE_MINUTE, ((33/10) write_ds1302(WRITE_HOUR, ((22/10) //初始化 日、月、年
write_ds1302(WRITE_DAY, ((26/10) write_ds1302(WRITE_MONTH, ((4/10) write_ds1302(WRITE_YEAR, ((14/10) //打开充电模式 , 增加写保护
write_ds1302(0x90, 0xa5);
write_ds1302(0x8e, 0x80);
}
}
uchar *itoa(uchar dat)  //数字转字符串函数
{
str[0] = (dat>>4)&0x07 + '0';
str[1] = dat&0x0f + '0';
str[2] = '\0';

return str;
}
****************************************************************************
lcd1602液晶显示程序
**************************************************************************
#include
#include
#define uchar unsigned char 

uchar code cmd[] = {
0x01, //清屏
0x02, //光标返回00h
0x06, //开显示,无光标,不闪烁
0x38 //8位总线,双行显示,5*7阵列
};


void delay_1ms();
void delay_10us();  


int lcd_1602_busy()
{
uchar tmp;
P0 = 0xff;
LCD_1602_RW = 1;
LCD_1602_RS = 0;


LCD_1602_EN = 1;
delay_10us();
tmp = P0;
LCD_1602_EN = 0;


return (bit)(0x80 & tmp);


}


void lcd_1602_write_cmd(uchar cmd)
{
  while(lcd_1602_busy());


LCD_1602_RW = 0;
LCD_1602_RS = 0;
P0 = cmd;


LCD_1602_EN = 1;
// P0 = cmd;
delay_10us();
LCD_1602_EN = 0;
}


void lcd_1602_write_data(uchar dat)
{
while(lcd_1602_busy());


LCD_1602_RW = 0;
LCD_1602_RS = 1;
P0 = dat;


LCD_1602_EN = 1;
// P0 = dat;
delay_10us();
LCD_1602_EN = 0;
}


void lcd_1602_init()
{
lcd_1602_write_cmd(0x01);
lcd_1602_write_cmd(0x0c);
lcd_1602_write_cmd(0x38);
// lcd_1602_write_cmd(0x05);
lcd_1602_write_cmd(0x01);
delay_1ms();
}


void lcd_1602_write_char(uchar x, uchar y, uchar dat)
{
if(x == 0)
{
lcd_1602_write_cmd(0x80 + y%16);
lcd_1602_write_data(dat);
}
else
{
lcd_1602_write_cmd(0xc0 + y%16);
lcd_1602_write_data(dat);
}
}


void lcd_1602_write_string(uchar x, uchar y, uchar *string)
{
while(*string != '\0')
{
lcd_1602_write_char(x, y++, *(string++));
}
}

相关文章
|
运维 监控 供应链
DS200DMCBG1AKG DS215DMCBG1AZZ03A
DS200DMCBG1AKG DS215DMCBG1AZZ03A
53 0
|
传感器 人工智能 供应链
DS200DCFBG1BLC GE DCS
DS200DCFBG1BLC GE DCS
53 0
|
安全
PADS VX1.2安装
关于PADS的文章都是基于PADS VX1.2进行讲解的,所以这里提供PADS VX1.2的下载及安装。下载方法:在公众号回复“PADS下载”获取下载链接(百度网盘)。下面我们来讲一下安装过程,主要提一下需要注意的环节,其它的都是正常点击“同意”或者“下一步”即可。
872 0
|
JavaScript
PADS简介及使用流程
对于PADS及其发展历史,有兴趣了解的可以上网搜一下,这里我们就简单介绍一下PADS的用途以及我们使用到的PADS组件。
376 0
PADS新建元器件
前面我们已经讲解过如何新建一个元件库,那么接下来我们在新建的ubug_lib库中新建一个元器件。在新建元器件之前,我们了解一下基本概念,即在库管理器的筛选条件下显示的:封装、元件、线、逻辑,如下图所示:
224 0
|
芯片
PADS新建多门元器件
当元器件引脚比较多或者门概念十分清晰的时候,可以将元器件做成多门元器件。比如一颗MCU的引脚很多,如果只用一个门,那么引脚太多分不清。可以按照功能分成多个门,比如电源部分作为一个门,晶振、复位等系统引脚作为一个门,IO引脚作为一个门等等。再比如多运放芯片,可以一个运放作为一个门。下面就以双运放芯片MCP6002为例,如下图所示:
241 0
PADS新建封装
前面我们已经讲解过如何新建一个电阻元器件,那么接下来我们就要新建一个该电阻元器件在现实世界中的映射——封装(Footprint)。打开PADS Layout,执行如下步骤: 1、打开库管理器,选中新建的库ubug_lib,点击“封装”按钮后“新建”按钮变为可点击,如下图所示:
478 0
|
安全 网络协议 虚拟化
安装和使用AD DS管理工具
安装和使用AD DS管理工具
363 0