DS3231最高精度时钟模块,IIC,C51 8051单片机I2C 测试程序 【开源】

实物图

 

 

 

 

原理图

效果图

程序烧录:

 

DS3231时序图

 

 

【图】写数据

 

 

【图】读数据

 

/***************************************************************************** 
*文件名称:main.c
*版    本:Keil uVision4
*控 制 器:STC89C52RC/12M
功能:显示时间到串口
 
*说    明:
1,DS3231实时时钟模块测试程序 
2,1T的单片机用不了
3,晶振12M
4,串口波特率2400
 
编译结果:
Rebuild target 'Demo_DS3231'
compiling main.c...
linking...
*** WARNING L16: UNCALLED SEGMENT, IGNORED FOR OVERLAY PROCESS
    SEGMENT: ?PR?_UART_SENDBYTE?MAIN
Program Size: data=61.2 xdata=0 code=2059
creating hex file from "Demo_DS3231"...
"Demo_DS3231" - 0 Error(s), 1 Warning(s).
 
 
*****************************************************************************/
 
#include <reg52.h>
#include <stdio.h>
#include "intrins.h"
 
//#define  Effective                  0x01
//#define  Ineffectiveness            0x00
#define  DS3231WriteAddress         0xd0
#define  DS3231ReadAddress          0xd1
//#define  DS3231_SecondRegister      0x00
#define  DS3231_TimeFirstRegister   0x00
#define  DS3231_MinuteRegister      0x01
#define  DS3231_HourRegister        0x02
#define  DS3231_WeekRegister        0x03
#define  DS3231_DayRegister         0x04
#define  DS3231_MonthRegister       0x05
#define  DS3231_YearRegister        0x06
#define  DS3231_InterruptRegister   0x0e
#define  DS3231_AlarmRegister       0x0f
#define  DS3231_ResetSCL()     DS3231_scl = 0   
#define  DS3231_SetSCL()       DS3231_scl = 1
#define  DS3231_ResetSDA()     DS3231_sda = 0
#define  DS3231_SetSDA()       DS3231_sda = 1
#define  DS3231_ReadPinSDA()   DS3231_sda
#define Time0_TH0  0xec          //定义计数器0计数寄存器装载的高8位值
#define Time0_TL0  0x78          //定义计数器0计数寄存器装载的低8位值
 
//--------------------------秒-分-时-星期-日-月-年
unsigned int   SetTime[] = {12,12,12,1,1,1,15};
unsigned int   CurrentT[7];
bit            Flag_Collect = 0;         //定义采集扫描标志变量
unsigned char   SweepInterval_Collect;       //定义采集扫描时间累加变量
sbit    DS3231_scl = P2^1;  //DS3231 clock 
sbit    DS3231_sda = P2^0;  //DS3231 data 
 
void DS3231_Delay(void) {    //DS3231通信速率延时,延时5微秒  12T单片机@12M
    unsigned char Number = 8;
    while (Number--)    {
        _nop_();
        _nop_();
    } 
}
 
void DS3231_DelayForWrite(void){  //DS3231写字节延时,延时5毫秒 12T单片机@12M
    unsigned int Number = 2500;
    while (Number--){
            _nop_();
      _nop_();
            _nop_();
            _nop_();
            _nop_();
    } 
}
 
void DS3231_Start(void) { //模拟DS3231通信开始信号,SCL=1期间,在SDA上产生一个下降沿
    DS3231_SetSDA();
    DS3231_SetSCL();DS3231_Delay(); 
    DS3231_ResetSDA();DS3231_Delay(); 
} 
 
  • 9
    点赞
  • 51
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在IIC通信的OLED上显示DS1302可调时钟,需要编写一个程序来读取DS1302的时间,并将其显示在OLED上。以下是一个简单的示例程序: ``` #include <Wire.h> // include the Wire library for I2C communication #include <U8g2lib.h> // include the U8g2 library for OLED display #include <DS1302.h> // include the DS1302 library for RTC #define RTC_CE 10 // RTC chip enable pin #define RTC_IO 11 // RTC data pin #define RTC_CLK 12 // RTC clock pin DS1302 rtc(RTC_CE, RTC_IO, RTC_CLK); // create an instance of the DS1302 library U8G2_SSD1306_128X64_NONAME_F_SW_I2C u8g2(U8G2_R0, /* clock=*/ SCL, /* data=*/ SDA, /* reset=*/ U8X8_PIN_NONE); // create an instance of the U8g2 library void setup() { u8g2.begin(); // initialize the OLED display rtc.halt(false); // start the RTC rtc.writeProtect(false); // disable write protection on RTC } void loop() { rtc.read(); // read the current time from the RTC u8g2.clearBuffer(); // clear the OLED buffer u8g2.setFont(u8g2_font_6x10_tf); // set the font for the display u8g2.setCursor(0, 10); // set the cursor position for the display u8g2.print(rtc.getHours()); // display the hours u8g2.print(":"); if (rtc.getMinutes() < 10) { // if the minutes are less than 10, add a leading zero u8g2.print("0"); } u8g2.print(rtc.getMinutes()); // display the minutes u8g2.print(":"); if (rtc.getSeconds() < 10) { // if the seconds are less than 10, add a leading zero u8g2.print("0"); } u8g2.print(rtc.getSeconds()); // display the seconds u8g2.sendBuffer(); // send the buffer to the OLED display delay(1000); // wait for 1 second before updating the display again } ``` 该程序使用Wire库进行I2C通信,并使用U8g2库控制OLED显示。它还使用DS1302库从RTC芯片读取当前时间,并将其显示在OLED上。在设置中,程序启动RTC并禁用了RTC的写保护。在主循环中,程序读取当前时间,并将其格式化为hh:mm:ss的格式。然后,它将时间显示在OLED上,并在每次更新显示之间等待1秒钟。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值