找回密码
 立即注册

QQ登录

只需一步,快速开始

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

单片机ds3231+lcd1602库 源程序下载

[复制链接]
跳转到指定楼层
楼主
ID:370473 发表于 2020-3-3 23:52 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
已打包成库方便大家使用 所有功能用法见main.c
注意h.h         mhz.h  mhz.c      3231.h 3231.c

程序适配机型 stc8a8k64s4a12     
非1t单片机或其他低速机型需移植使用

单片机源程序如下:
  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设备写一字节数据及8为二进制数据,高位在前
  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发送的数据
  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中读出当前时间
  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);                 //写入状态寄存器首地址
  102.     DS3231_WriteByte(0x00);                          //关闭闹钟中断标志位
  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写入设置时间信息
  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写入设置时间信息
  126.   }
  127.   DS3231_st('f');                                   //DS3231芯片IIC通信停止信号
  128.   delay_us(5);
  129. }

  130. void DS3231_Initialization(){            //初始化时钟芯片DS3231,先选择要写入的寄存器,在写入需要设置的指令
  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);                    //中断寄存器初始化,关闭方波信号,关闭闹钟中断
  141.     DS3231_WriteByte(0x00);                    //状态寄存器初始化,失效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. }
复制代码
借用移植修改自https://my.oschina.net/u/3776585/blog/1617326 特此感谢

所有资料51hei提供下载:
ds3231+lcd1602.rar (79.62 KB, 下载次数: 78)

评分

参与人数 1黑币 +50 收起 理由
admin + 50 共享资料的黑币奖励!

查看全部评分

分享到:  QQ好友和群QQ好友和群 QQ空间QQ空间 腾讯微博腾讯微博 腾讯朋友腾讯朋友
收藏收藏4 分享淘帖 顶 踩
回复

使用道具 举报

沙发
ID:135809 发表于 2020-3-4 08:32 | 只看该作者
谢谢楼主分享学习的好资料
回复

使用道具 举报

板凳
ID:370473 发表于 2020-3-4 09:25 | 只看该作者
DS3231_SetTime(SetTime);    DS3231_Initialization();这两个程序调换一下上下位置
回复

使用道具 举报

地板
ID:39824 发表于 2020-3-19 14:18 | 只看该作者
i66580 发表于 2020-3-4 09:25
DS3231_SetTime(SetTime);    DS3231_Initialization();这两个程序调换一下上下位置

你好,楼主。压缩文件下载后解压报错,不能解压。
回复

使用道具 举报

5#
ID:93502 发表于 2021-3-13 13:31 | 只看该作者
p55=0是啥
回复

使用道具 举报

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

本版积分规则

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

Powered by 单片机教程网

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