已打包成库方便大家使用 所有功能用法见main.c
注意h.h mhz.h mhz.c 3231.h 3231.c
程序适配机型 stc8a8k64s4a12
非1t单片机或其他低速机型需移植使用
单片机源程序如下:
- #include "stc8.h"
- #include "intrins.h"
- #include "delay.h"
- #define DS3231WriteAddress 0xd0
- #define DS3231ReadAddress 0xd1
- #define DS3231_Second 0x00
- #define DS3231_TimeFirst 0x00
- #define DS3231_Minute 0x01
- #define DS3231_Hour 0x02
- #define DS3231_Week 0x03
- #define DS3231_Day 0x04
- #define DS3231_Month 0x05
- #define DS3231_Year 0x06
- #define DS3231_Interrupt 0x0e
- #define DS3231_Status 0x0f
- //--------------------------秒-分-时-星期-日-月-年
- unsigned int SetTime[7];
- unsigned int CurrentT[7];
- sbit DS3231_scl = P2^1; //DS3231 clock
- sbit DS3231_sda = P2^0; //DS3231 data
- char get_time(int i){
- return CurrentT[i];
- }
- void DS3231_st(char i) {
- if(i=='f'){
- DS3231_sda = 0;
- delay_us(5);
- DS3231_scl = 1;
- delay_us(5);
- DS3231_sda = 1;
- delay_us(5);
- }
- if(i=='o'){
- DS3231_sda = 1;
- DS3231_scl = 1;
- delay_us(5);
- DS3231_sda = 0;
- delay_us(5);
- }
- }
- unsigned char DS3231_WriteByte(unsigned char SendByte)
- { //向DS3231设备写一字节数据及8为二进制数据,高位在前
- unsigned char i=8;
- DS3231_scl = 0;
- for(i=0; i<8; i++) {
- if(SendByte&0x80){DS3231_sda = 1;}
- else {DS3231_sda = 0;}
-
- DS3231_scl = 0;
- delay_us(5);
- DS3231_scl = 1;
- delay_us(5);
- SendByte=SendByte<<1;
- DS3231_scl = 0;
- delay_us(5);
- }
- DS3231_sda = 1;
- delay_us(5);
- DS3231_scl = 0;
- delay_us(5);
- DS3231_scl = 1;
- delay_us(5);
- i = DS3231_sda;
- delay_us(5);
- DS3231_scl = 0;
- delay_us(5);
- return i;
- }
- unsigned char DS3231_ReceiveByte(unsigned char Response)
- { //接收DS3231发送的数据
- unsigned char i=8;
- unsigned char ReceiveByte=0;
- DS3231_sda = 1;
- delay_us(5);
- DS3231_scl = 0;
- delay_us(5);
- for(i=0; i<8; i++){
- DS3231_scl = 1;
- delay_us(5);
- ReceiveByte = ReceiveByte << 1;
- ReceiveByte=ReceiveByte|(unsigned char)DS3231_sda;
- DS3231_scl = 0;
- delay_us(5);
- }
- if(Response) DS3231_sda = 1;
- else DS3231_sda = 0;
- delay_us(5);
- DS3231_scl = 1;
- delay_us(5);
- DS3231_scl = 0;
- delay_us(5);
- DS3231_sda = 1;
- delay_us(5);
- return ReceiveByte;
- }
- void DS3231_ReadTime(unsigned int *Pointer){ //从DS3231中读出当前时间
- unsigned char Number = 7;
- unsigned char Time = 0x00;
- DS3231_st('o'); //DS3231芯片IIC通信开始信号
- DS3231_WriteByte(DS3231WriteAddress); //写入芯片IIC写地址
- DS3231_WriteByte(DS3231_Status); //写入状态寄存器首地址
- DS3231_WriteByte(0x00); //关闭闹钟中断标志位
- DS3231_st('f'); //DS3231芯片IIC通信停止信号
-
- DS3231_st('o'); //DS3231芯片IIC通信开始信号
- DS3231_WriteByte(DS3231WriteAddress); //写入芯片IIC写地址
- DS3231_WriteByte(DS3231_TimeFirst); //写入时间寄存器首地址
- DS3231_st('o'); //DS3231芯片IIC通信开始信号
- DS3231_WriteByte(DS3231ReadAddress); //写入芯片IIC读地址
- for(Number=0; Number<6; Number++) {
- Time = DS3231_ReceiveByte(0x00);
- *Pointer++ = (Time/16*10+Time%16);
- }
- Time = DS3231_ReceiveByte(0x01);
- *Pointer++ = (Time/16*10+Time%16);
- DS3231_st('f'); //DS3231芯片IIC通信停止信号
- }
- void DS3231_SetTime(unsigned int *Pointer){ //向DS3231写入设置时间信息
- unsigned char Number = 0x00;
- unsigned char TransitionData = 0x00;
- DS3231_st('o'); //DS3231芯片IIC通信开始信号
- DS3231_WriteByte(DS3231WriteAddress); //写入芯片IIC写地址
- DS3231_WriteByte(DS3231_TimeFirst); //写入时间寄存器首地址
- for(Number=0; Number<7; Number++) {
- TransitionData = *Pointer++;
- DS3231_WriteByte((TransitionData/10)*16+TransitionData%10); //向DS3231写入设置时间信息
- }
- DS3231_st('f'); //DS3231芯片IIC通信停止信号
- delay_us(5);
- }
- void DS3231_Initialization(){ //初始化时钟芯片DS3231,先选择要写入的寄存器,在写入需要设置的指令
- DS3231_st('o'); //IIC通信起始信号
- DS3231_WriteByte(DS3231WriteAddress); //写入芯片IIC写地址
- DS3231_WriteByte(DS3231_Hour); //选择时寄存器为写入地址
- DS3231_WriteByte(0x00); //写入指令,时钟范围为0-23,即24小时制式
- DS3231_st('f');
-
- DS3231_st('o'); //IIC通信起始信号
- DS3231_WriteByte(DS3231WriteAddress); //写入芯片IIC写地址
- DS3231_WriteByte(DS3231_Interrupt); //选择中断寄存器为写入地址
- DS3231_WriteByte(0x04); //中断寄存器初始化,关闭方波信号,关闭闹钟中断
- DS3231_WriteByte(0x00); //状态寄存器初始化,失效32KHz信号输出,不匹配闹钟
- DS3231_st('f');
- }
- void fountion(char fountion,int year,int month,int day,int week,int hour,int min,int seconds)
- {
- //读/写时间
- if(fountion=='s'){
- SetTime[0]=seconds;
- SetTime[1]=min;
- SetTime[2]=hour;
- SetTime[3]=week;
- SetTime[4]=day;
- SetTime[5]=month;
- SetTime[6]=year;
- DS3231_SetTime(SetTime);
- DS3231_Initialization();
- }
- if(fountion=='r')
- {DS3231_ReadTime(CurrentT);}
- }
复制代码 借用移植修改自https://my.oschina.net/u/3776585/blog/1617326 特此感谢
所有资料51hei提供下载:
ds3231+lcd1602.rar
(79.62 KB, 下载次数: 79)
|