51单片机,AT89S52芯片 阅读DS1302的数据手册,学会DS1302的读写操作 1.单字节写 2.单字节读 3.write in burst modle (CLOCK) 4.read in burst modle (CLOCK) 将上述功能写成函数(模块化设计,代码封装) 编写主程序验证上述功能函数 将2023年5月22号星期-18点30分00秒写到DS1302, 随后读取DS1302的日期时间1,并在数码管上进行显示 显示效果: 显示日期(年、月 日)几秒,随后显示时间(时、分、秒)秒在跳动,不断循环
时间: 2024-01-22 16:18:10 浏览: 134
51单片机控制ds1302在LCD1602上显示时间
4星 · 用户满意度95%
以下是51单片机,AT89S52芯片DS1302的读写操作的代码封装:
```c
#include <reg52.h>
#include "ds1302.h"
// DS1302写单字节数据
void ds1302_write_byte(unsigned char addr, unsigned char dat)
{
unsigned char i;
ds1302_rst = 0;
ds1302_clk = 0;
ds1302_rst = 1;
ds1302_write_byte_1(addr);
for (i = 0; i < 8; i++)
{
ds1302_io = dat & 0x01;
dat >>= 1;
ds1302_clk = 1;
ds1302_clk = 0;
}
ds1302_rst = 0;
}
// DS1302读单字节数据
unsigned char ds1302_read_byte(unsigned char addr)
{
unsigned char i, dat = 0;
ds1302_rst = 0;
ds1302_clk = 0;
ds1302_rst = 1;
ds1302_write_byte_1(addr | 0x01);
for (i = 0; i < 8; i++)
{
dat >>= 1;
if (ds1302_io)
dat |= 0x80;
ds1302_clk = 1;
ds1302_clk = 0;
}
ds1302_rst = 0;
return dat;
}
// DS1302写多字节数据
void ds1302_write_burst(unsigned char *dat)
{
unsigned char i;
ds1302_rst = 0;
ds1302_clk = 0;
ds1302_rst = 1;
ds1302_write_byte_1(DS1302_WRITE_BURST);
for (i = 0; i < 8; i++)
{
ds1302_io = *dat & 0x01;
*dat >>= 1;
ds1302_clk = 1;
ds1302_clk = 0;
}
ds1302_rst = 0;
}
// DS1302读多字节数据
void ds1302_read_burst(unsigned char *dat)
{
unsigned char i;
ds1302_rst = 0;
ds1302_clk = 0;
ds1302_rst = 1;
ds1302_write_byte_1(DS1302_READ_BURST);
for (i = 0; i < 8; i++)
{
dat[i] = 0;
if (ds1302_io)
dat[i] |= 0x80;
ds1302_clk = 1;
ds1302_clk = 0;
}
ds1302_rst = 0;
}
// 设置DS1302的时间
void ds1302_set_time(unsigned char year, unsigned char month, unsigned char date, unsigned char week, unsigned char hour, unsigned char minute, unsigned char second)
{
ds1302_write_byte(DS1302_CONTROL, 0x80); // 关闭写保护
ds1302_write_byte(DS1302_YEAR, year);
ds1302_write_byte(DS1302_MONTH, month);
ds1302_write_byte(DS1302_DATE, date);
ds1302_write_byte(DS1302_WEEK, week);
ds1302_write_byte(DS1302_HOUR, hour);
ds1302_write_byte(DS1302_MINUTE, minute);
ds1302_write_byte(DS1302_SECOND, second);
ds1302_write_byte(DS1302_CONTROL, 0x00); // 打开写保护
}
// 从DS1302读取时间
void ds1302_get_time(unsigned char *year, unsigned char *month, unsigned char *date, unsigned char *week, unsigned char *hour, unsigned char *minute, unsigned char *second)
{
unsigned char dat[8];
ds1302_read_burst(dat);
*year = dat[6];
*month = dat[4];
*date = dat[3];
*week = dat[2];
*hour = dat[1];
*minute = dat[0];
*second = dat[7];
}
```
下面是主程序:
```c
#include <reg52.h>
#include "ds1302.h"
#define uchar unsigned char
#define uint unsigned int
// 数码管共阴极的段码
uchar code smgduan[] = {
0x3f, // '0'
0x06, // '1'
0x5b, // '2'
0x4f, // '3'
0x66, // '4'
0x6d, // '5'
0x7d, // '6'
0x07, // '7'
0x7f, // '8'
0x6f, // '9'
0x77, // 'A'
0x7c, // 'b'
0x39, // 'C'
0x5e, // 'd'
0x79, // 'E'
0x71, // 'F'
};
// 数码管位选,从左到右依次为P2.0~P2.7
uchar code smgwei[] = {
0xfe, 0xfd, 0xfb, 0xf7,
0xef, 0xdf, 0xbf, 0x7f,
};
// 延时函数
void delay(uint x)
{
uint i, j;
for (i = x; i > 0; i--)
for (j = 110; j > 0; j--);
}
// 在数码管上显示日期时间
void display_datetime(unsigned char year, unsigned char month, unsigned char date, unsigned char hour, unsigned char minute, unsigned char second)
{
uchar i, j;
uchar smg[8];
// 显示日期
smg[0] = smgduan[year / 10];
smg[1] = smgduan[year % 10];
smg[2] = smgduan[month / 10];
smg[3] = smgduan[month % 10];
smg[4] = smgduan[date / 10];
smg[5] = smgduan[date % 10];
smg[6] = smgduan[16]; // '-'
smg[7] = smgduan[second % 10];
for (i = 0; i < 8; i++)
{
for (j = 0; j < 8; j++)
{
P0 = smg[j];
P2 = smgwei[i];
delay(1);
P0 = 0x00;
}
}
// 显示时间
smg[0] = smgduan[hour / 10];
smg[1] = smgduan[hour % 10];
smg[2] = smgduan[minute / 10];
smg[3] = smgduan[minute % 10];
smg[4] = smgduan[second / 10];
smg[5] = smgduan[16]; // '-'
smg[6] = smgduan[16]; // '-'
smg[7] = smgduan[16]; // '-'
while (1)
{
for (i = 0; i < 8; i++)
{
for (j = 0; j < 8; j++)
{
P0 = smg[j];
P2 = smgwei[i];
delay(1);
P0 = 0x00;
}
}
}
}
void main()
{
unsigned char year = 23, month = 5, date = 22, week = 2, hour = 18, minute = 30, second = 0;
ds1302_init(); // 初始化DS1302
ds1302_set_time(year, month, date, week, hour, minute, second); // 设置DS1302的时间
while (1)
{
ds1302_get_time(&year, &month, &date, &week, &hour, &minute, &second); // 从DS1302读取时间
display_datetime(year, month, date, hour, minute, second); // 在数码管上显示日期时间
}
}
```
在主程序中设置了初始时间为2023年5月22日星期二18点30分00秒,然后不断从DS1302读取当前时间并在数码管上显示。
阅读全文