找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3872|回復(fù): 4
收起左側(cè)

單片機(jī)ds3231+lcd1602庫 源程序下載

[復(fù)制鏈接]
ID:370473 發(fā)表于 2020-3-3 23:52 | 顯示全部樓層 |閱讀模式
已打包成庫方便大家使用 所有功能用法見main.c
注意h.h         mhz.h  mhz.c      3231.h 3231.c

程序適配機(jī)型 stc8a8k64s4a12     
非1t單片機(jī)或其他低速機(jī)型需移植使用

單片機(jī)源程序如下:
  1. #include "stc8.h"
  2. #include "intrins.h"
  3. #include "delay.h"


  4. #define  DS3231WriteAddress 0xd0  
  5. #define  DS3231ReadAddress  0xd1
  6. #define  DS3231_Second      0x00  
  7. #define  DS3231_TimeFirst   0x00
  8. #define  DS3231_Minute      0x01
  9. #define  DS3231_Hour        0x02
  10. #define  DS3231_Week        0x03
  11. #define  DS3231_Day         0x04
  12. #define  DS3231_Month       0x05
  13. #define  DS3231_Year        0x06
  14. #define  DS3231_Interrupt   0x0e
  15. #define  DS3231_Status      0x0f



  16. //--------------------------秒-分-時-星期-日-月-年
  17. unsigned int  SetTime[7];
  18. unsigned int   CurrentT[7];
  19. sbit    DS3231_scl = P2^1;  //DS3231 clock
  20. sbit    DS3231_sda = P2^0;  //DS3231 data

  21. char get_time(int i){
  22. return CurrentT[i];
  23. }

  24. void DS3231_st(char i) {
  25.         if(i=='f'){
  26.         DS3231_sda = 0;
  27.         delay_us(5);
  28.     DS3231_scl = 1;
  29.         delay_us(5);
  30.     DS3231_sda = 1;
  31.         delay_us(5);
  32.         }
  33.         if(i=='o'){
  34.         DS3231_sda = 1;
  35.     DS3231_scl = 1;
  36.         delay_us(5);
  37.     DS3231_sda = 0;
  38.         delay_us(5);
  39.         }
  40. }

  41. unsigned char DS3231_WriteByte(unsigned char SendByte)
  42. { //向DS3231設(shè)備寫一字節(jié)數(shù)據(jù)及8為二進(jìn)制數(shù)據(jù),高位在前
  43.     unsigned char i=8;
  44.     DS3231_scl = 0;   
  45.     for(i=0; i<8; i++) {
  46.         if(SendByte&0x80){DS3231_sda = 1;}
  47.         else {DS3231_sda = 0;}
  48.          
  49.         DS3231_scl = 0;
  50.                 delay_us(5);                  
  51.         DS3231_scl = 1;
  52.                 delay_us(5);
  53.         SendByte=SendByte<<1;
  54.         DS3231_scl = 0;
  55.                 delay_us(5);
  56.     }
  57.     DS3231_sda = 1;
  58.         delay_us(5);
  59.     DS3231_scl = 0;
  60.         delay_us(5);
  61.     DS3231_scl = 1;
  62.         delay_us(5);
  63.     i = DS3231_sda;
  64.         delay_us(5);
  65.     DS3231_scl = 0;
  66.         delay_us(5);
  67.     return i;      
  68. }

  69. unsigned char DS3231_ReceiveByte(unsigned char Response)
  70. { //接收DS3231發(fā)送的數(shù)據(jù)
  71.     unsigned char i=8;
  72.     unsigned char ReceiveByte=0;
  73.     DS3231_sda = 1;
  74.         delay_us(5);   
  75.     DS3231_scl = 0;
  76.         delay_us(5);     
  77.     for(i=0; i<8; i++){
  78.         DS3231_scl = 1;
  79.                 delay_us(5);
  80.         ReceiveByte = ReceiveByte << 1;      
  81.         ReceiveByte=ReceiveByte|(unsigned char)DS3231_sda;
  82.         DS3231_scl = 0;
  83.                 delay_us(5);
  84.     }
  85.     if(Response) DS3231_sda = 1;   
  86.     else DS3231_sda = 0;   
  87.     delay_us(5);
  88.     DS3231_scl = 1;
  89.         delay_us(5);
  90.     DS3231_scl = 0;
  91.         delay_us(5);
  92.     DS3231_sda = 1;
  93.         delay_us(5);
  94.     return ReceiveByte;
  95. }   

  96. void DS3231_ReadTime(unsigned int *Pointer){       //從DS3231中讀出當(dāng)前時間
  97.     unsigned char Number  = 7;
  98.   unsigned char Time    = 0x00;
  99.     DS3231_st('o');                                  //DS3231芯片IIC通信開始信號
  100.     DS3231_WriteByte(DS3231WriteAddress);            //寫入芯片IIC寫地址
  101.     DS3231_WriteByte(DS3231_Status);                 //寫入狀態(tài)寄存器首地址
  102.     DS3231_WriteByte(0x00);                          //關(guān)閉鬧鐘中斷標(biāo)志位
  103.     DS3231_st('f');                                   //DS3231芯片IIC通信停止信號

  104.     DS3231_st('o');                                 //DS3231芯片IIC通信開始信號
  105.     DS3231_WriteByte(DS3231WriteAddress);            //寫入芯片IIC寫地址
  106.     DS3231_WriteByte(DS3231_TimeFirst);              //寫入時間寄存器首地址
  107.     DS3231_st('o');                                  //DS3231芯片IIC通信開始信號
  108.     DS3231_WriteByte(DS3231ReadAddress);             //寫入芯片IIC讀地址
  109.   for(Number=0; Number<6; Number++) {
  110.         Time = DS3231_ReceiveByte(0x00);
  111.     *Pointer++ = (Time/16*10+Time%16);   
  112.   }
  113.     Time = DS3231_ReceiveByte(0x01);
  114.     *Pointer++ = (Time/16*10+Time%16);
  115.   DS3231_st('f');                                 //DS3231芯片IIC通信停止信號
  116. }

  117. void DS3231_SetTime(unsigned int *Pointer){    //向DS3231寫入設(shè)置時間信息
  118.     unsigned char Number = 0x00;
  119.   unsigned char TransitionData = 0x00;
  120.     DS3231_st('o');                             //DS3231芯片IIC通信開始信號
  121.     DS3231_WriteByte(DS3231WriteAddress);       //寫入芯片IIC寫地址
  122.     DS3231_WriteByte(DS3231_TimeFirst);         //寫入時間寄存器首地址
  123.     for(Number=0; Number<7; Number++)  {
  124.     TransitionData = *Pointer++;
  125.     DS3231_WriteByte((TransitionData/10)*16+TransitionData%10);   //向DS3231寫入設(shè)置時間信息
  126.   }
  127.   DS3231_st('f');                                   //DS3231芯片IIC通信停止信號
  128.   delay_us(5);
  129. }

  130. void DS3231_Initialization(){            //初始化時鐘芯片DS3231,先選擇要寫入的寄存器,在寫入需要設(shè)置的指令
  131.     DS3231_st('o');                                             //IIC通信起始信號
  132.     DS3231_WriteByte(DS3231WriteAddress);  //寫入芯片IIC寫地址
  133.     DS3231_WriteByte(DS3231_Hour);         //選擇時寄存器為寫入地址
  134.     DS3231_WriteByte(0x00);                //寫入指令,時鐘范圍為0-23,即24小時制式
  135.     DS3231_st('f');
  136.      
  137.     DS3231_st('o');                                                      //IIC通信起始信號
  138.     DS3231_WriteByte(DS3231WriteAddress);      //寫入芯片IIC寫地址
  139.     DS3231_WriteByte(DS3231_Interrupt);        //選擇中斷寄存器為寫入地址     
  140.     DS3231_WriteByte(0x04);                    //中斷寄存器初始化,關(guān)閉方波信號,關(guān)閉鬧鐘中斷
  141.     DS3231_WriteByte(0x00);                    //狀態(tài)寄存器初始化,失效32KHz信號輸出,不匹配鬧鐘
  142.     DS3231_st('f');
  143. }

  144. void fountion(char fountion,int year,int month,int day,int week,int hour,int min,int seconds)
  145. {  
  146.         //讀/寫時間       
  147.    if(fountion=='s'){
  148.    SetTime[0]=seconds;
  149.    SetTime[1]=min;
  150.    SetTime[2]=hour;
  151.    SetTime[3]=week;
  152.    SetTime[4]=day;
  153.    SetTime[5]=month;
  154.    SetTime[6]=year;
  155.    DS3231_SetTime(SetTime);
  156.    DS3231_Initialization();
  157.    }

  158.    if(fountion=='r')
  159.         {DS3231_ReadTime(CurrentT);}

  160. }
復(fù)制代碼
借用移植修改自https://my.oschina.net/u/3776585/blog/1617326 特此感謝

所有資料51hei提供下載:
ds3231+lcd1602.rar (79.62 KB, 下載次數(shù): 79)

評分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

回復(fù)

使用道具 舉報

ID:135809 發(fā)表于 2020-3-4 08:32 | 顯示全部樓層
謝謝樓主分享學(xué)習(xí)的好資料
回復(fù)

使用道具 舉報

ID:370473 發(fā)表于 2020-3-4 09:25 | 顯示全部樓層
DS3231_SetTime(SetTime);    DS3231_Initialization();這兩個程序調(diào)換一下上下位置
回復(fù)

使用道具 舉報

ID:39824 發(fā)表于 2020-3-19 14:18 | 顯示全部樓層
i66580 發(fā)表于 2020-3-4 09:25
DS3231_SetTime(SetTime);    DS3231_Initialization();這兩個程序調(diào)換一下上下位置

你好,樓主。壓縮文件下載后解壓報錯,不能解壓。
回復(fù)

使用道具 舉報

ID:93502 發(fā)表于 2021-3-13 13:31 | 顯示全部樓層
p55=0是啥
回復(fù)

使用道具 舉報

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

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

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

快速回復(fù) 返回頂部 返回列表