標題:
單片機ds3231+lcd1602庫 源程序下載
[打印本頁]
作者:
i66580
時間:
2020-3-3 23:52
標題:
單片機ds3231+lcd1602庫 源程序下載
已打包成庫方便大家使用 所有功能用法見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設(shè)備寫一字節(jié)數(shù)據(jù)及8為二進制數(shù)據(jù),高位在前
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發(fā)送的數(shù)據(jù)
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); //寫入狀態(tài)寄存器首地址
DS3231_WriteByte(0x00); //關(guān)閉鬧鐘中斷標志位
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寫入設(shè)置時間信息
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寫入設(shè)置時間信息
}
DS3231_st('f'); //DS3231芯片IIC通信停止信號
delay_us(5);
}
void DS3231_Initialization(){ //初始化時鐘芯片DS3231,先選擇要寫入的寄存器,在寫入需要設(shè)置的指令
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); //中斷寄存器初始化,關(guān)閉方波信號,關(guān)閉鬧鐘中斷
DS3231_WriteByte(0x00); //狀態(tài)寄存器初始化,失效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, 下載次數(shù): 79)
2020-3-3 23:51 上傳
點擊文件名下載附件
ds3231+lcd1602.rar
下載積分: 黑幣 -5
作者:
hainanyingcong
時間:
2020-3-4 08:32
謝謝樓主分享學習的好資料
作者:
i66580
時間:
2020-3-4 09:25
DS3231_SetTime(SetTime); DS3231_Initialization();這兩個程序調(diào)換一下上下位置
作者:
hzyong
時間:
2020-3-19 14:18
i66580 發(fā)表于 2020-3-4 09:25
DS3231_SetTime(SetTime); DS3231_Initialization();這兩個程序調(diào)換一下上下位置
你好,樓主。壓縮文件下載后解壓報錯,不能解壓。
作者:
胡鬧鬧
時間:
2021-3-13 13:31
p55=0是啥
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1