找回密码
 立即注册

QQ登录

只需一步,快速开始

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

请问怎么给DS3231时钟写入数据,单片机函数已经声明好

[复制链接]
跳转到指定楼层
楼主
99黑币
我买了个TM1637的数码管,目前已经驱动完成,可以显示数字了。
然后我开始操刀DS3231,准备用单片机发送&写入数据,然后改变TM1637来显示时间。
目前已经把各个函数,比如iic开始、结束、响应、非响应、写一个字节、读一个字节、写入数据、读数据等函数写好,按照教程来的,但是不知道应该按照怎么样的规则写入数据&怎么检查是否写入成功。(我想把年改为2011年)根据手册如图


好像不是太对,还请大佬指明。
我的ds3231的默认地址为0x57,在此附上我的源文件及卖家给的数据手册,源文件请看名叫mainn.c的文件,函数在叫iic.文件中,我并且声明了个iic.h的文件,包含了各定义。希望大佬检查下,谢谢各位大佬帮助。 DS3231时钟模块.rar (408.03 KB, 下载次数: 23)
probjext.zip (14 KB, 下载次数: 11)






最佳答案

查看完整内容

我给你来个程序试试 DS3231.h DS3231.c
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:155507 发表于 2018-11-28 22:32 | 只看该作者
我给你来个程序试试


DS3231.h
  1. #ifndef __DS3231_H__
  2. #define __DS3231_H__

  3. #include <reg52.h>
  4. #include <intrins.h>

  5. #define uchar unsigned char
  6. #define uint  unsigned int

  7. sbit    scl = P1^1;             /* I2C pin definitions */
  8. sbit    sda = P1^0;
  9. sbit    int0 = P3^2;  //DS3231内部闹钟中断
  10. /**************************** defines *******************************/
  11. #define ADDRTC  0xd0    /* DS3231 slave address (write) */
  12. #define ACK     0
  13. #define NACK    1

  14. extern xdata   uchar   sec, min, hour, day, date, month, year,Dtemp;

  15. /*********************** Function Prototypes **************************/
  16. void    start();
  17. void    stop();
  18. void   i2cwrite(uchar d);
  19. uchar   i2cread(uchar b);
  20. void    writebyte();
  21. void    Init_DS3231();
  22. //void    frq_out_tog();
  23. void    init_alrm();
  24. void    comm_init();
  25. void        GetTime(void);
  26. void        SetTime(uchar yea,uchar mon,uchar da,uchar hou,uchar min,uchar sec);
  27. void        i2cwrite_add(uchar address,uchar dat);
  28. uchar i2cread_add(uchar address);
  29. void        InitDS3231();
  30. void    read_temp();

  31. #endif
复制代码



DS3231.c
  1. #include "DS3231.H"

  2. /************************* Global Variables ***************************/
  3. xdata   uchar   sec, min, hour, day, date, month, year;
  4. xdata        uchar         Dtemp;

  5. /**************************** functions ******************************/
  6. void delay()
  7. {
  8.          ;;
  9. }

  10. void start()            /* --------- Initiate start condition ---------- */
  11. {
  12.         sda = 1; delay();
  13.         scl = 1; delay();
  14.         sda = 0; delay();
  15.         scl = 0;
  16. }
  17. void stop()             /* ---------- Initiate stop condition ----------- */
  18. {
  19.         sda = 0; delay();
  20.         scl = 1; delay();
  21.         sda = 1; delay();
  22.         scl = 0;
  23. }

  24. void i2cack()
  25. {
  26.         uchar i=0;
  27.         scl=1;delay();
  28.         while(sda==1 && i<255) i++;
  29.         scl=0;delay();
  30. }
  31. void i2cwrite(uchar d)         /* ----------------------------- */
  32. {
  33. uchar i;

  34.                 scl = 0;
  35.             for (i = 0;i < 8; i++)
  36.             {
  37.             sda = d & 0x80; /* Send the msbits first */
  38.                 scl = 0;delay();
  39.                         scl = 1;delay();
  40.                 d = d << 1;     /* do shift here to increase scl high time */
  41.                 scl = 0;
  42.             }
  43.             sda = 1;delay();        /* Release the sda line */
  44.             scl = 0;delay();
  45. }

  46. uchar i2cread(uchar b)   /* ----------------------------------- */
  47. {
  48. uchar i, d;

  49.         d = 0;
  50.         sda = 1;delay();             /* Let go of sda line */
  51.         scl = 0;delay();
  52.         for (i = 0; i < 8; i++) /* read the msb first */
  53.         {
  54.                 scl = 1;delay();
  55.                                 d = d << 1;
  56.                                 d = d | (uchar)sda;
  57.                 scl = 0;delay();
  58.         }
  59.         sda = b;delay();          /* low for ack, high for nack */
  60.         scl = 1;delay();
  61.         scl = 0;delay();
  62.         sda = 1;delay();          /* Release the sda line */
  63.         return d;
  64. }
  65. void i2cwrite_add(uchar address,uchar dat)//指定地址写一个字节数据
  66. {
  67.         start();
  68.         i2cwrite(ADDRTC);
  69.         i2cack();
  70.         i2cwrite(address);
  71.         i2cack();
  72.         i2cwrite(dat);
  73.         i2cack();
  74.         stop();
  75. }
  76. uchar i2cread_add(uchar address)
  77. //指定地址读一个字节数据
  78. {
  79.         uchar dd;
  80.         start();
  81.         i2cwrite(ADDRTC);
  82.         i2cack();
  83.         i2cwrite(address);
  84.         i2cack();
  85.         start();
  86.         i2cwrite(ADDRTC);
  87.         i2cack();
  88.         dd=i2cread(0);
  89.         stop();
  90.         return dd;
  91. }

  92. void    InitDS3231()     /* ----- set time & date; user data entry ------ */
  93. {
  94.                 scl=1;delay();
  95.                 sda=1;delay();

  96.             start();
  97.             i2cwrite(ADDRTC);       /* write slave address, write 1339 */
  98.             i2cwrite(0x00);                /* write register address, 1st clock register */
  99.             i2cwrite(0x00);        //秒
  100.             i2cwrite(0x01);        //分
  101.             i2cwrite(0x01);        //时
  102.             i2cwrite(0x03);        //星期
  103.             i2cwrite(0x12);        //日
  104.             i2cwrite(0x01);        //月
  105.             i2cwrite(0x11);        //年
  106.             //i2cwrite(0x10); /* enable sqw, 1hz output */
  107.             stop();
  108. }
  109. void    GetTime()     /* --- get date/time from DS3231 --- */
  110. {
  111.         //while(int0);    /* loop until int pin goes low */
  112.         //start();
  113.         //i2cwrite(ADDRTC);
  114.         //i2cwrite(0x0f);
  115.         //i2cwrite(0);            /* clear alarm flags */
  116.         //stop();

  117.         start();
  118.         i2cwrite(ADDRTC);
  119.         i2cwrite(0);
  120.         start();
  121.         i2cwrite(ADDRTC | 1);
  122.         sec        = i2cread(ACK);
  123.         min        = i2cread(ACK);
  124.         hour= i2cread(ACK);
  125.         day        = i2cread(ACK);
  126.         date= i2cread(ACK);
  127.         month= i2cread(ACK);
  128.         year= i2cread(NACK);
  129.         stop();
  130. }

  131. void    read_temp()       /* -------- read temperature -------- */
  132. {
  133. int     itemp;
  134. float   ftemp;

  135.         do
  136.         {
  137.                 start();
  138.                 i2cwrite(ADDRTC);
  139.                 i2cwrite(0x0e);         /* address of control register */
  140.                 start();
  141.                 i2cwrite(ADDRTC + 1);   /* send the device address for read */
  142.                 itemp = i2cread(NACK);  /* get the control register value */
  143.                 stop();
  144.         }       while(itemp & 0x20);            /* wait until CNVT bit goes inactive */

  145.         start();
  146.         i2cwrite(ADDRTC);
  147.         i2cwrite(0x11);                 /* address of temperature MSB */
  148.         start();
  149.         i2cwrite(ADDRTC + 1);           /* send the device address for read */
  150.         itemp = ( (int) i2cread(ACK) << 5 );
  151.         itemp += ( i2cread(NACK) >> 3);
  152.         stop();
  153.         if(itemp & 0x1000)      itemp += 0xe000;        /* if sign bit set, make 16 bit 2's comp */

  154.         ftemp = 0.03125 * (float) itemp;        /* convert to degrees C */
  155.         /* ftemp = ftemp * 9 / 5 + 32;  /* skip this if you don't want degrees F */
  156.                 Dtemp  = (uchar) ftemp;
  157. }

  158. /*void    frq_out_tog()   // --- toggle en32khz bit to enable/disable sqw ---
  159. {
  160. uchar   val;

  161.         start();
  162.         i2cwrite(ADDRTC);
  163.         i2cwrite(0x0f);                 // control/status reg address
  164.         start();
  165.         i2cwrite(ADDRTC + 1);           //send the device address for read
  166.         val = i2cread(NACK);
  167.         stop();
  168.         val ^= 0x08;    //toggle en32khz bit
  169.         start();
  170.         i2cwrite(ADDRTC);
  171.         i2cwrite(0x0f);                 //control/status reg address
  172.         i2cwrite(val);
  173.         stop();
  174. }*/

  175. void    init_alrm()     /* --- enable alarm 1 for once-per-second --- */
  176. {
  177.         start();
  178.         i2cwrite(ADDRTC);
  179.         i2cwrite(7);            /* 1st alarm 1 reg address */
  180.         i2cwrite(0x80); /* mask alarm register */
  181.         i2cwrite(0x80);
  182.         i2cwrite(0x80);
  183.         i2cwrite(0x80);
  184.         stop();

  185.         start();
  186.         i2cwrite(ADDRTC);
  187.         i2cwrite(0x0e); /* control/status reg address */
  188.         i2cwrite(0x05); /* enable interrupts, alarm 1 output */
  189. }
  190. void    comm_init()     /* ------ reset DS3231 comm interface ------ */
  191. {
  192.         do      /* because the DS3231 I2C interface is active for both supplies */
  193.         {       /*  after a micro reset, we must get the comm into a known state */
  194.                 sda = 1;        /* make sure master has released SDA */
  195.                 scl = 1;
  196.                 if(sda) /* if sda is high, generate a start */
  197.                 {
  198.                         sda = 0;        /* The DS3231 will recognize a valid start */
  199.                         sda = 1;        /*  condition anywhere in a I2C data transfer */
  200.                 }
  201.                 scl = 0;
  202.         }       while(sda == 0);        /* if the DS3231 is holding sda low, try again */
  203. }

复制代码
回复

使用道具 举报

板凳
ID:404720 发表于 2018-11-29 11:21 | 只看该作者
angmall 发表于 2018-11-29 08:26
我给你来个程序试试

大佬这代码我有点难看懂,回家做琢磨下
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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