久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 4250|回復: 2
打印 上一主題 下一主題
收起左側

請問怎么給DS3231時鐘寫入數(shù)據(jù),單片機函數(shù)已經(jīng)聲明好

[復制鏈接]
跳轉到指定樓層
樓主
ID:404720 發(fā)表于 2018-11-28 22:32 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
99黑幣
我買了個TM1637的數(shù)碼管,目前已經(jīng)驅動完成,可以顯示數(shù)字了。
然后我開始操刀DS3231,準備用單片機發(fā)送&寫入數(shù)據(jù),然后改變TM1637來顯示時間。
目前已經(jīng)把各個函數(shù),比如iic開始、結束、響應、非響應、寫一個字節(jié)、讀一個字節(jié)、寫入數(shù)據(jù)、讀數(shù)據(jù)等函數(shù)寫好,按照教程來的,但是不知道應該按照怎么樣的規(guī)則寫入數(shù)據(jù)&怎么檢查是否寫入成功。(我想把年改為2011年)根據(jù)手冊如圖


好像不是太對,還請大佬指明。
我的ds3231的默認地址為0x57,在此附上我的源文件及賣家給的數(shù)據(jù)手冊,源文件請看名叫mainn.c的文件,函數(shù)在叫iic.文件中,我并且聲明了個iic.h的文件,包含了各定義。希望大佬檢查下,謝謝各位大佬幫助。 DS3231時鐘模塊.rar (408.03 KB, 下載次數(shù): 23)
probjext.zip (14 KB, 下載次數(shù): 11)






最佳答案

查看完整內容

我給你來個程序試試 DS3231.h DS3231.c
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

沙發(fā)
ID:155507 發(fā)表于 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)//指定地址寫一個字節(jié)數(shù)據(jù)
  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. //指定地址讀一個字節(jié)數(shù)據(jù)
  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 發(fā)表于 2018-11-29 11:21 | 只看該作者
angmall 發(fā)表于 2018-11-29 08:26
我給你來個程序試試

大佬這代碼我有點難看懂,回家做琢磨下
回復

使用道具 舉報

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網(wǎng)

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 日本中文字幕日韩精品免费 | 91在线资源 | 成人国产精品免费观看 | 在线免费观看色 | 亚洲视频免费在线看 | 精品国产免费人成在线观看 | 欧洲毛片 | 日韩成人免费视频 | 草樱av | 天堂在线免费视频 | 亚洲精品视频在线 | 精品久久久久久久久久久久久久 | 永久看片 | 日韩国产欧美一区 | 人人亚洲 | 91久久综合 | 国产成人一区二区三区电影 | 欧美中文 | 91在线色视频 | 国产一区二区三区在线 | 99热免费在线 | 999久久精品 | 精品一区二区三区不卡 | 日本在线观看视频 | 成年人黄色小视频 | 一二三在线视频 | 欧美二三区 | 99福利视频 | 精品欧美乱码久久久久久 | 国精产品一区一区三区免费完 | 欧美一区二区三区久久精品视 | av免费在线播放 | 户外露出一区二区三区 | a级毛片免费高清视频 | 久久久久久国产免费视网址 | 精品久久久久香蕉网 | 日日摸日日碰夜夜爽亚洲精品蜜乳 | 亚洲精品自拍视频 | 日韩中文在线 | 亚洲国产自产 | 伊人精品视频 |