|
DS3231例程,程序是在51黑論壇上找的,沒有仿真及電路圖,之后根據(jù)程序怎么做仿真都不管用,里面有錯誤,進行修改后就可以顯示了,分享給需要學(xué)習(xí)的伙伴們,數(shù)碼管為共陰 ,之前的程序為共陽,怎么連接都無法顯示,改為陰管及陰管程序后就正常顯示了。時間調(diào)整來源程序就沒有,只能程序調(diào)整
1.png (132.54 KB, 下載次數(shù): 39)
下載附件
2023-1-2 16:57 上傳
單片機源程序如下:
#include <reg51.h>
#include <intrins.h>
#define uchar unsigned char
#define uint unsigned int
sbit SDA=P3^6; //模擬I2C數(shù)據(jù)傳送位SDA
sbit SCL=P3^7; //模擬I2C時鐘控制位SCL
sbit INT=P3^2;
sbit RESET=P3^3;
sbit led0=P1^0;
sbit led1=P1^1;
sbit led2=P1^2;
sbit led3=P1^3;
sbit led4=P1^4;
sbit led5=P1^5;
sbit led6=P1^6;
sbit led7=P1^7;
bit ack; //應(yīng)答標(biāo)志位
#define DS3231_WriteAddress 0xD0 //器件寫地址
#define DS3231_ReadAddress 0xD1 //器件讀地址
#define DS3231_SECOND 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 //年
//鬧鈴1
#define DS3231_SALARM1ECOND 0x07 //秒
#define DS3231_ALARM1MINUTE 0x08 //分
#define DS3231_ALARM1HOUR 0x09 //時
#define DS3231_ALARM1WEEK 0x0A //星期/日
//鬧鈴2
#define DS3231_ALARM2MINUTE 0x0b //分
#define DS3231_ALARM2HOUR 0x0c //時
#define DS3231_ALARM2WEEK 0x0d //星期/日
#define DS3231_CONTROL 0x0e //控制寄存器
#define DS3231_STATUS 0x0f //狀態(tài)寄存器
#define BSY 2 //忙
#define OSF 7 //振蕩器停止標(biāo)志
#define DS3231_XTAL 0x10 //晶體老化寄存器
#define DS3231_TEMPERATUREH 0x11 //溫度寄存器高字節(jié)(8位)
#define DS3231_TEMPERATUREL 0x12 //溫度寄存器低字節(jié)(高2位)
uchar code dis_code[11]={0x3f,0x06,0x5b,0x4f, // 0,1,2,3
0x66,0x6d,0x7d,0x07,0x7f,0x6f, 0x00}; // 4,5,6,7,8,9,off
uchar data dis_buf[8];
uchar data dis_index;
uchar data dis_digit;
uchar BCD2HEX(uchar val) //BCD轉(zhuǎn)換為Byte
{
uchar temp;
temp=val&0x0f;
val>>=4;
val&=0x0f;
val*=10;
temp+=val;
return temp;
}
uchar HEX2BCD(uchar val) //B碼轉(zhuǎn)換為BCD碼
{
uchar i,j,k;
i=val/10;
j=val%10;
k=j+(i<<4);
return k;
}
void delayus(uint us)
{
while (us--);
}
void Start_I2C()
{
SDA=1; //發(fā)送起始條件的數(shù)據(jù)信號
delayus(1);
SCL=1;
delayus(5); //起始條件建立時間大于4.7us,延時
SDA=0; //發(fā)送起始信號
delayus(5); // 起始條件鎖定時間大于4μs
SCL=0; //鉗住I2C總線,準(zhǔn)備發(fā)送或接收數(shù)據(jù)
delayus(2);
}
void Stop_I2C()
{
SDA=0; //發(fā)送結(jié)束條件的數(shù)據(jù)信號
delayus(1); //發(fā)送結(jié)束條件的時鐘信號
SCL=1; //結(jié)束條件建立時間大于4us
delayus(5);
SDA=1; //發(fā)送I2C總線結(jié)束信號
delayus(4);
}
void SendByte(uchar c)
{
uchar BitCnt;
for(BitCnt=0;BitCnt<8;BitCnt++) //要傳送的數(shù)據(jù)長度為8位
{
if((c<<BitCnt)&0x80)
SDA=1; //判斷發(fā)送位
else
SDA=0;
delayus(1);
SCL=1; //置時鐘線為高,通知被控器開始接收數(shù)據(jù)位
delayus(5); //保證時鐘高電平周期大于4μs
SCL=0;
}
delayus(2);
SDA=1; //8位發(fā)送完后釋放數(shù)據(jù)線,準(zhǔn)備接收應(yīng)答位
delayus(2);
SCL=1;
delayus(3);
if(SDA==1)
ack=0;
else
ack=1; //判斷是否接收到應(yīng)答信號
SCL=0;
delayus(2);
}
uchar RcvByte()
{
uchar retc;
uchar BitCnt;
retc=0;
SDA=1; //置數(shù)據(jù)線為輸入方式
for(BitCnt=0;BitCnt<8;BitCnt++)
{
delayus(1);
SCL=0; //置時鐘線為低,準(zhǔn)備接收數(shù)據(jù)位
delayus(5); //時鐘低電平周期大于4.7μs
SCL=1; //置時鐘線為高使數(shù)據(jù)線上數(shù)據(jù)有效
delayus(3);
retc=retc<<1;
if(SDA==1)
retc=retc+1; //讀數(shù)據(jù)位,接收的數(shù)據(jù)位放入retc中
delayus(2);
}
SCL=0;
delayus(2);
return(retc);
}
void Ack_I2C(bit a)
{
if(a==0)
SDA=0; //在此發(fā)出應(yīng)答或非應(yīng)答信號
else
SDA=1;
delayus(3);
SCL=1;
delayus(5); //時鐘低電平周期大于4μs
SCL=0; //清時鐘線,鉗住I2C總線以便繼續(xù)接收
delayus(2);
}
uchar write_byte(uchar addr, uchar write_data)
{
Start_I2C();
SendByte(DS3231_WriteAddress);
if (ack == 0)
return 0;
SendByte(addr);
if (ack == 0)
return 0;
SendByte(write_data);
if (ack == 0)
return 0;
Stop_I2C();
delayus(10);
return 1;
}
uchar read_current()
{
uchar read_data;
Start_I2C();
SendByte(DS3231_ReadAddress);
if(ack==0)
return(0);
read_data = RcvByte();
Ack_I2C(1);
Stop_I2C();
return read_data;
}
uchar read_random(uchar random_addr)
{
Start_I2C();
SendByte(DS3231_WriteAddress);
if(ack==0)
return(0);
SendByte(random_addr);
if(ack==0)
return(0);
return(read_current());
}
void ModifyTime(uchar yea,uchar mon,uchar da,uchar hou,uchar min,uchar sec)
{
uchar temp=0;
temp=HEX2BCD(yea);
write_byte(DS3231_YEAR,temp); //修改年
temp=HEX2BCD(mon);
write_byte(DS3231_MONTH,temp); //修改月
temp=HEX2BCD(da);
write_byte(DS3231_DAY,temp); //修改日
temp=HEX2BCD(hou);
write_byte(DS3231_HOUR,temp); //修改時
temp=HEX2BCD(min);
write_byte(DS3231_MINUTE,temp); //修改分
temp=HEX2BCD(sec);
write_byte(DS3231_SECOND,temp); //修改秒
}
void TimeDisplay(uchar Dhour,uchar Dmin,uchar Dsec)
{
dis_buf[7]=dis_code[Dhour / 10]; // 時十位
dis_buf[6]=dis_code[Dhour % 10]; // 時個位
dis_buf[4]=dis_code[Dmin / 10]; // 分十位
dis_buf[3]=dis_code[Dmin % 10]; // 分個位
dis_buf[1]=dis_code[Dsec / 10]; // 秒十位
dis_buf[0]=dis_code[Dsec % 10]; // 秒個位
dis_buf[2]=0x40; // 顯示"-"
dis_buf[5]=0x40;
}
void DateDisplay(uchar Dyear,uchar Dmonth,uchar Dday)
{
dis_buf[7]=dis_code[Dyear / 10]; // 年十位
dis_buf[6]=dis_code[Dyear % 10]; // 年個位
dis_buf[4]=dis_code[Dmonth / 10]; // 月十位
dis_buf[3]=dis_code[Dmonth % 10]; // 月個位
dis_buf[1]=dis_code[Dday / 10]; // 天十位
dis_buf[0]=dis_code[Dday % 10]; // 天個位
dis_buf[2]=0x40; // 顯示"-"
dis_buf[5]=0x40;
}
void get_show_time(void)
{
uchar Htemp1,Htemp2,Mtemp1,Mtemp2,Stemp1,Stemp2;
Htemp1=read_random(DS3231_HOUR); //時 24小時制
Htemp1&=0x3f;
Htemp2=BCD2HEX(Htemp1);
Mtemp1=read_random(DS3231_MINUTE); //分
Mtemp2=BCD2HEX(Mtemp1);
Stemp1=read_random(DS3231_SECOND); //秒
Stemp2=BCD2HEX(Stemp1);
TimeDisplay(Htemp2,Mtemp2,Stemp2);
}
void get_show_date(void)
{
uchar Ytemp1,Ytemp2,Mtemp1,Mtemp2,Dtemp1,Dtemp2;
Ytemp1=read_random(DS3231_YEAR); //年
Ytemp2=BCD2HEX(Ytemp1);
Mtemp1=read_random(DS3231_MONTH); //月
Mtemp2=BCD2HEX(Mtemp1);
Dtemp1=read_random(DS3231_DAY); //日
Dtemp2=BCD2HEX(Dtemp1);
DateDisplay(Ytemp2,Mtemp2,Dtemp2);
}
void get_show_Temperature(void)
{
uchar Ttemp1,Ttemp2,Ttemp3,Ttemp4;
Ttemp1=read_random(DS3231_TEMPERATUREH); //溫度 高字節(jié)
Ttemp2=BCD2HEX(Ttemp1);
Ttemp3=read_random(DS3231_TEMPERATUREL); //溫度低字節(jié)
Ttemp4=BCD2HEX(Ttemp3);
DateDisplay(0,Ttemp2,Ttemp4);
}
void timer0() interrupt 1
{
TH0=0xFC;
TL0=0x17;
P2=0xff; // 先關(guān)閉所有數(shù)碼管
P0=dis_buf[dis_index]; // 顯示代碼傳送到P0口
P2=dis_digit;
if (dis_digit & 0x80)
dis_digit=(dis_digit << 1) | 0x1;
else
dis_digit=(dis_digit << 1);
dis_index++;
dis_index&=0x07; // 8個數(shù)碼管全部掃描完一遍之后,再回到第一個開始下一次掃描
}
void main()
{
uint ii = 0;
RESET=0x1; //DS3231復(fù)位操作,正常操作下不需要每次都復(fù)位
delayus(5000);
led0=0;
led1=0;
led2=0;
led3=0;
led4=0;
P0=0xff;
P2=0xff;
dis_digit=0xfe;
dis_index=0;
TimeDisplay(12, 5, 18);
TMOD=0x11; // 定時器0, 1工作模式1, 16位定時方式
TH0=0xFC;
TL0=0x17;
TCON=0x01;
IE=0x82; // 使能timer0,1 中斷
TR0=1;
if (write_byte(DS3231_CONTROL, 0x1C) == 0)
led0=1;
if (write_byte(DS3231_STATUS, 0x00) == 0)
led1=1;
ModifyTime(22,12,31,14,53,00); //初始化時鐘,2010/6/13,15/30/00
//小時采用24小時制
while(1)
{
//get_show_Temperature(); //顯示溫度
get_show_time(); //顯示時間
delayus(50000);
//get_show_date(); //顯示日期
}
}
|
評分
-
查看全部評分
|