找回密码
 立即注册

QQ登录

只需一步,快速开始

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

DS3231的单片机源码

[复制链接]
跳转到指定楼层
楼主
ID:241609 发表于 2017-10-21 16:40 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
  1. #define DS3231_WriteAddress 0xD0    //
  2. #define DS3231_ReadAddress  0xD1    //
  3. bit  ack;
  4. void Delay6us(void)                //@11.0592MHz
  5. {
  6.         unsigned char i;

  7.         i = 14;
  8.         while (--i);
  9. }
  10. void Delay1us(void)                //@11.0592MHz
  11. {
  12.         _nop_();
  13.         _nop_();
  14.         _nop_();
  15. }

  16. void Start_I2C()
  17. {
  18.     SDA=1;                  
  19.     Delay1us();
  20.     SCL=1;
  21.     Delay6us();            

  22.     SDA=0;                 
  23.     Delay6us();            

  24.     SCL=0;  
  25.     Delay1us();
  26.                 Delay1us();
  27. }
  28. void Stop_I2C()
  29. {
  30.     SDA=0;                  
  31.     Delay1us();
  32.     SCL=1;                 
  33.     Delay6us();

  34.     SDA=1;                 
  35.     Delay6us();
  36. }
  37. void SendByte(uchar c)
  38. {
  39.     uchar BitCnt;

  40.     for(BitCnt=0;BitCnt<8;BitCnt++)   
  41.     {
  42.         if((c<<BitCnt)&0x80)
  43.             SDA=1;         
  44.         else
  45.             SDA=0;            
  46.           Delay1us();
  47.           SCL=1;                           
  48.           Delay6us();                          
  49.           SCL=0;
  50.     }

  51.     Delay1us();
  52.     SDA=1;                                
  53.     Delay1us();  
  54.     SCL=1;
  55.                 Delay6us();
  56.     if(SDA==1)
  57.         ack=0;   
  58.     else
  59.         ack=1;         
  60.     SCL=0;
  61.     Delay1us();
  62.                 Delay1us();
  63. }
  64. uchar RcvByte(void)
  65. {
  66.    uchar retc;
  67.    uchar BitCnt;

  68.    retc=0;
  69.    SDA=1;                           
  70.    for(BitCnt=0;BitCnt<8;BitCnt++)
  71.    {
  72.         Delay1us();  
  73.         SCL=0;                     

  74.         Delay6us();               

  75.         SCL=1;                     
  76.         Delay1us();
  77.                                 Delay1us();
  78.         retc=retc<<1;
  79.         if(SDA==1)
  80.             retc=retc+1;
  81.         Delay1us();
  82.                                 Delay1us();
  83.    }
  84.    SCL=0;
  85.          Delay1us();
  86.          Delay1us();
  87.    return(retc);
  88. }

  89. void Ack_I2C(bit a)
  90. {

  91.     if(a==0)
  92.         SDA=0;            
  93.     else
  94.         SDA=1;
  95.     Delay1us();
  96.                 Delay1us();   
  97.     SCL=1;

  98.     Delay6us();            

  99.     SCL=0;                  
  100.     Delay1us();
  101.                 Delay1us();   
  102. }
  103. uchar write_byte(uchar addr, uchar write_data)
  104. {
  105.     Start_I2C();
  106.     SendByte(DS3231_WriteAddress);
  107.     if (ack == 0)
  108.         return 0;

  109.     SendByte(addr);   
  110.     if (ack == 0)
  111.         return 0;

  112.     SendByte(write_data);
  113.     if (ack == 0)
  114.         return 0;

  115.     Stop_I2C();
  116.     Delay6us();
  117.     Delay6us();                             
  118.     return 1;
  119. }

  120. uchar read_current()
  121. {
  122.     uchar read_data;
  123.     Start_I2C();
  124.     SendByte(DS3231_ReadAddress);
  125.     if(ack==0)
  126.         return(0);

  127.     read_data = RcvByte();
  128.     Ack_I2C(1);
  129.     Stop_I2C();
  130.     return read_data;
  131. }

  132. uchar read_random(uchar random_addr)
  133. {
  134.                 uchar ret = 0;
  135.     Start_I2C();
  136.     SendByte(DS3231_WriteAddress);
  137.     if(ack==0)
  138.                 {        
  139.         return ret;
  140.                 }
  141.     SendByte(random_addr);
  142.     if(ack==0)
  143.                 {        
  144.         return ret;
  145.                 }
  146.                 ret = read_current();
  147.                 return ret;
  148. }

  149. #define MBCD(n) (((n/10)<<4)|(n%10))

  150. void time_init(void)
  151. {
  152.         write_byte(0x00,MBCD(00));
  153.         write_byte(0x01,MBCD(36));
  154.         write_byte(0x02,MBCD(18));
  155.         write_byte(0x03,MBCD(3));
  156.         write_byte(0x04,MBCD(18));
  157.         write_byte(0x05,MBCD(10));
  158.         write_byte(0x06,MBCD(17));
  159.         write_byte(0x0E,0);
  160. }

  161. void DS3231_test(void)
  162. {
  163.         uchar i = 0, tmp = 0;

  164.         for(i = 0; i < 7; i++)
  165.         {
  166.                 times[i] = read_random(i);
  167. //                uart1_send(times[i]);
  168.                 tmp = (times[i]>>4)*10+(times[i]&0x0F);
  169.                 times[i] = tmp;
  170.         }
  171.         timenum = mktime(times[6]+2000,times[5],times[4],times[2],times[1],times[0]);
  172.         timenum = timenum - 28800;
  173. }
复制代码
分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏1 分享淘帖 顶 踩
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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