标题:
关于时钟芯片DS1302全部RAM读写方法!
[打印本页]
作者:
skcheng
时间:
2021-9-3 23:01
标题:
关于时钟芯片DS1302全部RAM读写方法!
各位前辈师傅们好!
我是初学者请多包涵!这里恳请前辈们能否指导我写一个利用c51单片机挂接DS1302时钟芯片 然后设计按一下k1 单片机一次性读出DS1302内部全部RAM的数据 然后再按一下k2 再一次性写入DS1302内部全部RAM寄存器。手里有一套普中A4-1开发板套装,利用这个套装读写 感谢前辈指导!
作者:
188610329
时间:
2021-9-3 23:45
sbit RST = P3^0;
sbit SCLK = P3^1;
sbit IO = P3^2;
/**************************************
从DS1302读1字节数据
**************************************/
unsigned char DS1302_ReadByte()
{
unsigned char i;
unsigned char dat = 0;
for (i=0; i<8; i++) //8位计数器
{
SCLK = 0; //时钟线拉低
delay(us1); //延时等待
dat >>= 1; //数据右移一位
if(IO) dat |= 0x80; //读取数据
SCLK = 1; //时钟线拉高
delay(us1); //延时等待
}
return dat;
}
/**************************************
向DS1302写1字节数据
**************************************/
void DS1302_WriteByte(unsigned char dat)
{
unsigned char i;
for (i=0; i<8; i++) //8位计数器
{
SCLK = 0; //时钟线拉低
dat >>= 1; //移出数据
IO = CY; //送出到端口
delay(us1); //延时等待
SCLK = 1; //时钟线拉高
delay(us1); //延时等待
}
}
/**************************************
开关DS1302写保护
**************************************/
void DS1302_WriteProtect(bit sw)
{
SCLK = 0;
delay(us1);
RST = 1;
delay(us2); //延时等待
DS1302_WriteByte(0x8e); //写保护地址
if(sw) DS1302_WriteByte(0x80); //写保护开
else DS1302_WriteByte(0x00); //写保护关
RST = 0;
delay(us2); //延时等待
}
/**************************************
从 DS2302 中批量读取数据
**************************************/
void DS1302_BurstRead(unsigned char addr, unsigned char n, unsigned char *p)
{
DS1302_WriteProtect(1); //读取稳定作用(避免FF,还防误操作)
SCLK = 0;
delay(us1);
RST = 1;
delay(us1); //延时等待
DS1302_WriteByte(addr); //写地址
delay(us2);
while (n--)
{
*p++ = DS1302_ReadByte();
}
RST = 0;
delay(us1); //延时等待
}
/**************************************
往 DS1302 中批量写入数据
**************************************/
void DS1302_BurstWrite(unsigned char addr, unsigned char n, unsigned char *p)
{
DS1302_WriteProtect(0); //开写保护
SCLK = 0;
delay(us1);
RST = 1;
delay(us2); //延时等待
DS1302_WriteByte(addr); //写地址
while (n--)
{
DS1302_WriteByte(*p++);
}
RST = 0;
delay(us2); //延时等待
DS1302_WriteProtect(1); //开写保护
}
调用的时候:
DS1302_BurstWrite(0xFE, 31, &Buff); // Buff 为 你自己声明的数组变量
DS1302_BurstRead(0xFF, 31, &Buff); // Buff 为 你自己声明的数组变量
作者:
skcheng
时间:
2021-9-4 07:12
感谢指导,我试一下!
作者:
man1234567
时间:
2021-9-4 08:43
用A4-1开发板套装自带的DEMO就可以,随机光盘上有,光盘丢失可以去官网下载。
作者:
skcheng
时间:
2021-9-4 11:17
我下载的光盘资料里面是DS1302+LCD1602程序只是时钟寄存器我就是想麻烦您 能写一个简单的读取DS1302时钟寄存器和31个RAM寄存器的数据 比如 按k1 一次批量读取所以寄存器数据 按k2一次批量写入所以寄存器数据!这样程序小一点好理解些!感谢!
作者:
188610329
时间:
2021-9-4 14:12
skcheng 发表于 2021-9-4 11:17
我下载的光盘资料里面是DS1302+LCD1602程序只是时钟寄存器我就是想麻烦您 能写一个简单的读取DS1302时钟寄 ...
你要简单得一键读写,我给你留的那个就是最简单的了.
无非就是判断 K1 了调用 BurstRead 判断 K2了就是调用 BurstWrite
学要保存数据的变量数组你自己声明了后 替换 &Buff
需要读 RAM 就是 0xff 读 时钟就是 0xbf
需要写 RAM 就是 0xfe 写 时钟就是 0xbe
RAM 的变量数组长度 31 , 时钟数组 变量长度 8
剩下的你自己搞,延时函数没贴,你自己写一个就好。按手册要求DS1302的时钟间隔需要1us, CE间隔需要4us,实测CE间隔2us足够了,时钟间隔 0.5us也足够了。你看着替换我代码里的 delayus 就可以了。
作者:
angmall
时间:
2021-9-4 16:33
程序分享出来给需要的人作参考
/*****************************************************************************************/
/****************************************main.c********************************************/
/*****************************************************************************************/
#include "reg52.h"
#include "DS1302.h"
#include "LCD1602.h"
sbit K1 = P3^1;
sbit K2 = P3^0;
sbit K3 = P3^2;
uchar TIMEtemp1[7];
uchar TIMEtemp2[7];
uchar code RAMreadaddr[7] = {0xc1,0xc3,0xc5,0xc7,0xc9,0xcb,0xcd};
uchar code RAMwriteaddr[7] = {0xc0,0xc2,0xc4,0xc6,0xc8,0xca,0xcc};
void LCD1602display();
void delay10ms(void);
void keypress();
void main()
{
LCD1602Init();
DS1302Init();
while(1)
{
DS1302readtime();
LCD1602display();
keypress();
}
}
void LCD1602display()
{
LCDwriteorder(0x80);
LCDwritedata('2');
LCDwritedata('0');
LCDwritedata('0'+TIME[6]/16); //year
LCDwritedata('0'+(TIME[6]&0x0f));
LCDwritedata('-');
LCDwritedata('0'+TIME[4]/16); //month
LCDwritedata('0'+(TIME[4]&0x0f));
LCDwritedata('-');
LCDwritedata('0'+TIME[3]/16); //day
LCDwritedata('0'+(TIME[3]&0x0f));
LCDwriteorder(0x8D);
LCDwritedata('0'+(TIME[5]&0x07));//week
LCDwriteorder(0x80+0x40);
LCDwritedata('0'+TIME[2]/16); //hour
LCDwritedata('0'+(TIME[2]&0x0f));
LCDwritedata('-');
LCDwritedata('0'+TIME[1]/16); //minute
LCDwritedata('0'+(TIME[1]&0x0f));
LCDwritedata('-');
LCDwritedata('0'+TIME[0]/16); //second
LCDwritedata('0'+(TIME[0]&0x0f));
}
void delay10ms(void) //10 ms 0 mission
{
unsigned char a,b,c;
for(c=1;c>0;c--)
for(b=38;b>0;b--)
for(a=130;a>0;a--);
}
void keypress()
{
uchar n;
if(K1==0)
{
delay10ms();
if(K1==0)
{
for (n=0; n<7; n++)//读取七个字节的时钟信号:分秒时日月周年
{
TIMEtemp1[n] = DS1302read(RTCreadaddr[n]);
}
DS1302write(0x8E,0x00);
for(n=0;n<7;n++)
{
DS1302write(RAMwriteaddr[n],TIMEtemp1[n]);
}
DS1302write(0x8E,0x80);
}
while(!K1);
}
if(K2==0)
{
delay10ms();
if(K2==0)
{
for(n=0;n<7;n++)
{
TIMEtemp2[n]=DS1302read(RAMreadaddr[n]);
}
while(1)
{
LCDwriteorder(0x80);
LCDwritedata('2');
LCDwritedata('0');
LCDwritedata('0'+TIMEtemp2[6]/16); //year
LCDwritedata('0'+(TIMEtemp2[6]&0x0f));
LCDwritedata('-');
LCDwritedata('0'+TIMEtemp2[4]/16); //month
LCDwritedata('0'+(TIMEtemp2[4]&0x0f));
LCDwritedata('-');
LCDwritedata('0'+TIMEtemp2[3]/16); //day
LCDwritedata('0'+(TIMEtemp2[3]&0x0f));
LCDwriteorder(0x8D);
LCDwritedata('0'+(TIMEtemp2[5]&0x07));//week
LCDwriteorder(0x80+0x40);
LCDwritedata('0'+TIMEtemp2[2]/16); //hour
LCDwritedata('0'+(TIMEtemp2[2]&0x0f));
LCDwritedata('-');
LCDwritedata('0'+TIMEtemp2[1]/16); //minute
LCDwritedata('0'+(TIMEtemp2[1]&0x0f));
LCDwritedata('-');
LCDwritedata('0'+TIMEtemp2[0]/16); //second
LCDwritedata('0'+(TIMEtemp2[0]&0x0f));
if(K3==0)
{
delay10ms();
if(K3==0)
{
LCD1602display();
break;
}
}
}
}
}
}
/*****************************************************************************************/
/****************************************DS1302.h*****************************************/
/*****************************************************************************************/
#define __DS1302_H_
#include "reg52.h"
#include "intrins.h"
#ifndef uchar
#define uchar unsigned char
#endif
#ifndef uint
#define uint unsigned int
#endif
sbit DSIO = P3^4;
sbit RST = P3^5;
sbit SCLK = P3^6;
void DS1302write(uchar addr,uchar dat);
uchar DS1302read(uchar addr);
void DS1302Init();
void DS1302readtime();
extern uchar TIME[7];
extern uchar code RTCreadaddr[7];
extern uchar code RTCwriteaddr[7];
#endif
/*****************************************************************************************/
/****************************************DS1302.c*****************************************/
/*****************************************************************************************/
#include "DS1302.h"
uchar code RTCreadaddr[7] = {0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
uchar code RTCwriteaddr[7] = {0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};
uchar TIME[7]={0,0,0,0x14,0x02,0x04,0x19};// 00:00:00 14-2-19
void DS1302write(uchar addr,uchar dat)
{
uchar n;
RST = 0; //CE
_nop_();
SCLK = 0 ;
_nop_();
RST = 1;
_nop_();
for(n=0;n<8;n++) //address
{
DSIO = addr & 0x01; //低位开始传送
addr >>= 1;
SCLK = 1;
_nop_();
SCLK = 0;
_nop_();
}
for(n=0;n<8;n++) //order
{
DSIO = dat & 0x01;
dat >>= 1;
SCLK = 1;
_nop_();
SCLK = 0;
_nop_();
}
RST = 0;
_nop_();
}
uchar DS1302read(uchar addr)
{
uchar n,dat,dat1;
RST = 0;
_nop_();
SCLK = 0;
_nop_();
RST = 1;
_nop_();
for(n=0;n<8;n++)
{
DSIO = addr & 0x01;
addr >>= 1;
SCLK = 1;
_nop_();
SCLK = 0;
_nop_();
}
_nop_();
for(n=0;n<8;n++)
{
dat1 = DSIO;
dat = (dat>>1)|(dat1<<7);
//data1读入数据只有2个值,0x01和0x00,只看0x01那么<<7后变成0x80,
//再看dat假如dat=0x80,>>1后变成0x40, 0x40. /0x80=1100 0000B.
//简单说就是一位一位读入串行数据。
SCLK = 1;
_nop_();
SCLK = 0;
_nop_();
}
RST = 0;
_nop_();
SCLK = 1;
_nop_();
DSIO = 0;
_nop_();
DSIO = 1;
_nop_();
return dat;
}
void DS1302Init()
{
uchar n;
DS1302write(0x8E,0x00); //禁止写保护
for(n=0;n<7;n++)
{
DS1302write(RTCwriteaddr[n],TIME[n]);
}
DS1302write(0x8E,0x80); //开启写保护
}
void DS1302readtime()
{
uchar n;
for(n=0;n<7;n++)
{
TIME[n] = DS1302read(RTCreadaddr[n]);
}
}
/*****************************************************************************************/
/****************************************LCD1602.h*****************************************/
/*****************************************************************************************/
#ifndef __LCD_H_
#define __LCD_H_
#include "reg52.h"
#ifndef uchar
#define uchar unsigned char
#endif
#ifndef uint
#define uint unsigned int
#endif
#define LCD1602_DATAPINS P0
sbit LCD1602_E = P2^7;
sbit LCD1602_RW = P2^5;
sbit LCD1602_RS = P2^6;
void LCD1602_delay1ms(uint c);
void LCDwriteorder(uchar order);
void LCDwritedata(uchar dat);
void LCD1602Init();
#endif
/*****************************************************************************************/
/****************************************LCD1602.c*****************************************/
/*****************************************************************************************/
#include "LCD1602.h"
void LCD1602_delay1ms(uint c) //1ms , 0 mission
{
uchar a,b;
for (; c>0; c--)
{
for (b=199;b>0;b--)
{
for(a=1;a>0;a--);
}
}
}
void LCDwriteorder(uchar order)
{
LCD1602_E = 0; //power open
LCD1602_RS = 0; //send order
LCD1602_RW = 0; //choose write
LCD1602_DATAPINS = order;
LCD1602_delay1ms(1);
LCD1602_E = 1;
LCD1602_delay1ms(5);
LCD1602_E = 0;
}
void LCDwritedata(uchar dat)
{
LCD1602_E = 0;
LCD1602_RS = 1; //send data
LCD1602_RW = 0;
LCD1602_DATAPINS = dat;
LCD1602_delay1ms(1);
LCD1602_E = 1;
LCD1602_delay1ms(5);
LCD1602_E = 0;
}
void LCD1602Init()
{
LCDwriteorder(0x38); //display open
LCDwriteorder(0x0c); // no flash sign
LCDwriteorder(0x06); // one date one point
LCDwriteorder(0x01); // rush screen
LCDwriteorder(0x80); // set point start space
}
/*****************************************************************************************/
/****end********end*********end******end*****end****end*****end********end**********end****/
/*****************************************************************************************/
复制代码
作者:
skcheng
时间:
2021-9-4 18:16
谢谢指导,我再测试一下看看!
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1