找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 3565|回复: 7
打印 上一主题 下一主题
收起左侧

关于时钟芯片DS1302全部RAM读写方法!

[复制链接]
跳转到指定楼层
楼主
ID:963743 发表于 2021-9-3 23:01 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
各位前辈师傅们好!
       我是初学者请多包涵!这里恳请前辈们能否指导我写一个利用c51单片机挂接DS1302时钟芯片 然后设计按一下k1 单片机一次性读出DS1302内部全部RAM的数据 然后再按一下k2 再一次性写入DS1302内部全部RAM寄存器。手里有一套普中A4-1开发板套装,利用这个套装读写 感谢前辈指导!
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:624769 发表于 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 为 你自己声明的数组变量
回复

使用道具 举报

板凳
ID:963743 发表于 2021-9-4 07:12 | 只看该作者
感谢指导,我试一下!
回复

使用道具 举报

地板
ID:584814 发表于 2021-9-4 08:43 | 只看该作者
用A4-1开发板套装自带的DEMO就可以,随机光盘上有,光盘丢失可以去官网下载。
回复

使用道具 举报

5#
ID:963743 发表于 2021-9-4 11:17 | 只看该作者
我下载的光盘资料里面是DS1302+LCD1602程序只是时钟寄存器我就是想麻烦您 能写一个简单的读取DS1302时钟寄存器和31个RAM寄存器的数据 比如 按k1 一次批量读取所以寄存器数据 按k2一次批量写入所以寄存器数据!这样程序小一点好理解些!感谢!
回复

使用道具 举报

6#
ID:624769 发表于 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 就可以了。
回复

使用道具 举报

7#
ID:155507 发表于 2021-9-4 16:33 | 只看该作者
程序分享出来给需要的人作参考

  1. /*****************************************************************************************/
  2. /****************************************main.c********************************************/
  3. /*****************************************************************************************/

  4. #include "reg52.h"
  5. #include "DS1302.h"
  6. #include "LCD1602.h"

  7. sbit K1 = P3^1;
  8. sbit K2 = P3^0;
  9. sbit K3 = P3^2;

  10. uchar TIMEtemp1[7];
  11. uchar TIMEtemp2[7];
  12. uchar code RAMreadaddr[7] = {0xc1,0xc3,0xc5,0xc7,0xc9,0xcb,0xcd};
  13. uchar code RAMwriteaddr[7] = {0xc0,0xc2,0xc4,0xc6,0xc8,0xca,0xcc};

  14. void LCD1602display();
  15. void delay10ms(void);
  16. void keypress();

  17. void main()
  18. {
  19.         LCD1602Init();
  20.         DS1302Init();
  21.         while(1)
  22.         {
  23.                 DS1302readtime();
  24.                 LCD1602display();
  25.                 keypress();
  26.         }
  27. }

  28. void LCD1602display()
  29. {
  30.         LCDwriteorder(0x80);
  31.         LCDwritedata('2');
  32.         LCDwritedata('0');
  33.         LCDwritedata('0'+TIME[6]/16);    //year
  34.         LCDwritedata('0'+(TIME[6]&0x0f));
  35.         LCDwritedata('-');
  36.         LCDwritedata('0'+TIME[4]/16);         //month
  37.         LCDwritedata('0'+(TIME[4]&0x0f));
  38.         LCDwritedata('-');
  39.         LCDwritedata('0'+TIME[3]/16);         //day
  40.         LCDwritedata('0'+(TIME[3]&0x0f));
  41.         LCDwriteorder(0x8D);
  42.         LCDwritedata('0'+(TIME[5]&0x07));//week

  43.         LCDwriteorder(0x80+0x40);
  44.         LCDwritedata('0'+TIME[2]/16);    //hour
  45.         LCDwritedata('0'+(TIME[2]&0x0f));
  46.         LCDwritedata('-');
  47.         LCDwritedata('0'+TIME[1]/16);         //minute
  48.         LCDwritedata('0'+(TIME[1]&0x0f));
  49.         LCDwritedata('-');
  50.         LCDwritedata('0'+TIME[0]/16);         //second
  51.         LCDwritedata('0'+(TIME[0]&0x0f));
  52. }

  53. void delay10ms(void)   //10  ms 0  mission
  54. {
  55.         unsigned char a,b,c;
  56.         for(c=1;c>0;c--)
  57.         for(b=38;b>0;b--)
  58.         for(a=130;a>0;a--);
  59. }

  60. void keypress()
  61. {
  62.         uchar n;
  63.         if(K1==0)
  64.         {
  65.                 delay10ms();
  66.                 if(K1==0)
  67.                 {
  68.                         for (n=0; n<7; n++)//读取七个字节的时钟信号:分秒时日月周年
  69.                         {
  70.                                 TIMEtemp1[n] = DS1302read(RTCreadaddr[n]);
  71.                         }
  72.                         DS1302write(0x8E,0x00);
  73.                         for(n=0;n<7;n++)
  74.                         {
  75.                                 DS1302write(RAMwriteaddr[n],TIMEtemp1[n]);
  76.                         }
  77.                         DS1302write(0x8E,0x80);               
  78.                 }
  79.                 while(!K1);      
  80.         }

  81.         if(K2==0)
  82.         {
  83.                 delay10ms();
  84.                 if(K2==0)
  85.                 {
  86.                         for(n=0;n<7;n++)
  87.                         {
  88.                                 TIMEtemp2[n]=DS1302read(RAMreadaddr[n]);
  89.                         }


  90.                         while(1)
  91.                         {
  92.                                 LCDwriteorder(0x80);
  93.                                 LCDwritedata('2');
  94.                                 LCDwritedata('0');
  95.                                 LCDwritedata('0'+TIMEtemp2[6]/16);    //year
  96.                                 LCDwritedata('0'+(TIMEtemp2[6]&0x0f));
  97.                                 LCDwritedata('-');
  98.                                 LCDwritedata('0'+TIMEtemp2[4]/16);         //month
  99.                                 LCDwritedata('0'+(TIMEtemp2[4]&0x0f));
  100.                                 LCDwritedata('-');
  101.                                 LCDwritedata('0'+TIMEtemp2[3]/16);         //day
  102.                                 LCDwritedata('0'+(TIMEtemp2[3]&0x0f));
  103.                                 LCDwriteorder(0x8D);
  104.                                 LCDwritedata('0'+(TIMEtemp2[5]&0x07));//week

  105.                                 LCDwriteorder(0x80+0x40);
  106.                                 LCDwritedata('0'+TIMEtemp2[2]/16);    //hour
  107.                                 LCDwritedata('0'+(TIMEtemp2[2]&0x0f));
  108.                                 LCDwritedata('-');
  109.                                 LCDwritedata('0'+TIMEtemp2[1]/16);         //minute
  110.                                 LCDwritedata('0'+(TIMEtemp2[1]&0x0f));
  111.                                 LCDwritedata('-');
  112.                                 LCDwritedata('0'+TIMEtemp2[0]/16);         //second
  113.                                 LCDwritedata('0'+(TIMEtemp2[0]&0x0f));


  114.                                 if(K3==0)
  115.                                 {
  116.                                         delay10ms();
  117.                                         if(K3==0)
  118.                                         {
  119.                                                 LCD1602display();
  120.                                                 break;
  121.                                         }
  122.                                 }         
  123.                         }
  124.                 }               
  125.         }
  126. }

  127. /*****************************************************************************************/
  128. /****************************************DS1302.h*****************************************/
  129. /*****************************************************************************************/


  130. #define __DS1302_H_

  131. #include "reg52.h"
  132. #include "intrins.h"

  133. #ifndef uchar
  134. #define uchar unsigned char
  135. #endif

  136. #ifndef uint
  137. #define uint unsigned int
  138. #endif

  139. sbit DSIO = P3^4;
  140. sbit RST = P3^5;
  141. sbit SCLK = P3^6;

  142. void DS1302write(uchar addr,uchar dat);
  143. uchar DS1302read(uchar addr);
  144. void DS1302Init();
  145. void DS1302readtime();

  146. extern uchar TIME[7];
  147. extern uchar code RTCreadaddr[7];
  148. extern uchar code RTCwriteaddr[7];

  149. #endif

  150. /*****************************************************************************************/
  151. /****************************************DS1302.c*****************************************/
  152. /*****************************************************************************************/
  153. #include "DS1302.h"

  154. uchar code RTCreadaddr[7] = {0x81,0x83,0x85,0x87,0x89,0x8b,0x8d};
  155. uchar code RTCwriteaddr[7] = {0x80,0x82,0x84,0x86,0x88,0x8a,0x8c};

  156. uchar TIME[7]={0,0,0,0x14,0x02,0x04,0x19};// 00:00:00   14-2-19

  157. void DS1302write(uchar addr,uchar dat)
  158. {
  159.         uchar n;
  160.         RST = 0;    //CE
  161.         _nop_();

  162.         SCLK = 0 ;
  163.         _nop_();
  164.         RST = 1;
  165.         _nop_();

  166.         for(n=0;n<8;n++)            //address
  167.         {
  168.                 DSIO = addr & 0x01;                  //低位开始传送
  169.                 addr >>= 1;
  170.                 SCLK = 1;
  171.                 _nop_();
  172.                 SCLK = 0;
  173.                 _nop_();
  174.         }

  175.         for(n=0;n<8;n++)                  //order
  176.         {
  177.                 DSIO = dat & 0x01;
  178.                 dat >>= 1;
  179.                 SCLK = 1;
  180.                 _nop_();
  181.                 SCLK = 0;
  182.                 _nop_();
  183.         }

  184.         RST = 0;
  185.         _nop_();
  186. }

  187. uchar DS1302read(uchar addr)
  188. {
  189.         uchar n,dat,dat1;
  190.         RST = 0;
  191.         _nop_();

  192.         SCLK = 0;
  193.         _nop_();
  194.         RST = 1;
  195.         _nop_();

  196.         for(n=0;n<8;n++)
  197.         {
  198.                 DSIO = addr & 0x01;
  199.                 addr >>= 1;
  200.                 SCLK = 1;
  201.                 _nop_();
  202.                 SCLK = 0;
  203.                 _nop_();
  204.         }

  205.         _nop_();

  206.         for(n=0;n<8;n++)
  207.         {
  208.                 dat1 = DSIO;
  209.                 dat = (dat>>1)|(dat1<<7);
  210.                 //data1读入数据只有2个值,0x01和0x00,只看0x01那么<<7后变成0x80,
  211.                 //再看dat假如dat=0x80,>>1后变成0x40, 0x40. /0x80=1100 0000B.
  212.                 //简单说就是一位一位读入串行数据。
  213.                 SCLK = 1;
  214.                 _nop_();
  215.                 SCLK = 0;
  216.                 _nop_();
  217.         }

  218.         RST = 0;
  219.         _nop_();

  220.         SCLK = 1;
  221.         _nop_();
  222.         DSIO = 0;
  223.         _nop_();
  224.         DSIO = 1;
  225.         _nop_();

  226.         return dat;
  227. }

  228. void DS1302Init()
  229. {
  230.         uchar n;
  231.         DS1302write(0x8E,0x00); //禁止写保护
  232.         for(n=0;n<7;n++)
  233.         {
  234.                 DS1302write(RTCwriteaddr[n],TIME[n]);      
  235.         }
  236.         DS1302write(0x8E,0x80); //开启写保护
  237. }

  238. void DS1302readtime()
  239. {
  240.         uchar n;
  241.         for(n=0;n<7;n++)
  242.         {
  243.                 TIME[n] = DS1302read(RTCreadaddr[n]);
  244.         }
  245. }


  246. /*****************************************************************************************/
  247. /****************************************LCD1602.h*****************************************/
  248. /*****************************************************************************************/
  249. #ifndef __LCD_H_
  250. #define __LCD_H_

  251. #include "reg52.h"

  252. #ifndef uchar
  253. #define uchar unsigned char
  254. #endif

  255. #ifndef uint
  256. #define uint unsigned int
  257. #endif

  258. #define LCD1602_DATAPINS P0
  259. sbit LCD1602_E = P2^7;
  260. sbit LCD1602_RW = P2^5;
  261. sbit LCD1602_RS = P2^6;

  262. void LCD1602_delay1ms(uint c);
  263. void LCDwriteorder(uchar order);
  264. void LCDwritedata(uchar dat);
  265. void LCD1602Init();

  266. #endif

  267. /*****************************************************************************************/
  268. /****************************************LCD1602.c*****************************************/
  269. /*****************************************************************************************/

  270. #include "LCD1602.h"

  271. void LCD1602_delay1ms(uint c)   //1ms , 0 mission      
  272. {
  273.         uchar a,b;
  274.         for (; c>0; c--)
  275.         {
  276.                 for (b=199;b>0;b--)
  277.                 {
  278.                         for(a=1;a>0;a--);
  279.                 }      
  280.         }         
  281. }

  282. void LCDwriteorder(uchar order)
  283. {
  284.         LCD1602_E = 0;             //power  open
  285.         LCD1602_RS = 0;            //send order
  286.         LCD1602_RW = 0;            //choose write

  287.         LCD1602_DATAPINS = order;
  288.         LCD1602_delay1ms(1);   

  289.         LCD1602_E = 1;
  290.         LCD1602_delay1ms(5);
  291.         LCD1602_E = 0;
  292. }

  293. void LCDwritedata(uchar dat)
  294. {
  295.         LCD1602_E = 0;
  296.         LCD1602_RS = 1; //send data
  297.         LCD1602_RW = 0;

  298.         LCD1602_DATAPINS = dat;
  299.         LCD1602_delay1ms(1);

  300.         LCD1602_E = 1;
  301.         LCD1602_delay1ms(5);
  302.         LCD1602_E = 0;
  303. }

  304. void LCD1602Init()
  305. {
  306.         LCDwriteorder(0x38); //display open
  307.         LCDwriteorder(0x0c); // no flash sign
  308.         LCDwriteorder(0x06); // one date one point
  309.         LCDwriteorder(0x01); // rush screen
  310.         LCDwriteorder(0x80); // set point start space
  311. }

  312. /*****************************************************************************************/
  313. /****end********end*********end******end*****end****end*****end********end**********end****/
  314. /*****************************************************************************************/
复制代码
回复

使用道具 举报

8#
ID:963743 发表于 2021-9-4 18:16 | 只看该作者
谢谢指导,我再测试一下看看!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表