当前位置:首页 > DIY高精度时钟、温度显示器Ds3231+12864+ds18b20+89c51 - 图文
delay(5); //时钟低电平周期大于4μs
SCL=0; //清时钟线,钳住I2C总线以便继续接收 delay(2); }
uchar I2CRead()
/************从DS3231当前地址
读一个字节************/ {
uchar read_data; Start();
SendByte(ADDRTD);
if(ack==0) {
return(0); }
read_data = RcvByte(); I2CACK(1); Stop();
return read_data; }
uchar I2CReadAdd(uchar addr) 读一个字节************/ { Start(); SendByte(ADDRTW);
if(ack==0) { return(0); } SendByte(addr); if(ack==0) { return(0); } return(I2CRead()); }
void Readtime() 时间**********************/ {
/************从DS3231指定地址
/*********************读取
uchar temp;
temp=I2CReadAdd(DS3231_SEC);//秒 sec=BCD2HEX(temp);
temp=I2CReadAdd(DS3231_MIN);//分 min=BCD2HEX(temp);
temp=I2CReadAdd(DS3231_HOUR); //时 hour=BCD2HEX(temp);
temp=I2CReadAdd(DS3231_DATE); //日 date=BCD2HEX(temp);
temp=I2CReadAdd(DS3231_MONTH); //月 month=BCD2HEX(temp);
temp=I2CReadAdd(DS3231_YEAR); //年 year=BCD2HEX(temp); }
void InitDS3231()
{SCL=1; delay(5); SDA=1; delay(5); }
void TimeDisplay(uchar Dhour,uchar Dmin,uchar Dsec) { b[0]=numbr[Dhour / 10]; // 时十位 b[1]=numbr[Dhour % 10]; // 时个位 b[4]=numbr[Dmin / 10]; // 分十位 b[5]=numbr[Dmin % 10]; // 分个位 b[8]=numbr[Dsec / 10]; // 秒十位 b[9]=numbr[Dsec % 10]; // 秒个位 } void DateDisplay(uchar Dyear,uchar Dmonth,uchar Dday) { a[2]=numbr[Dyear / 10]; // 年十位 a[3]=numbr[Dyear % 10]; // 年个位 a[6]=numbr[Dmonth / 10]; // 月十位 a[7]=numbr[Dmonth % 10]; // 月个位 a[10]=numbr[Dday / 10]; // 天十位 a[11]=numbr[Dday % 10]; // 天个位 }
//ds3231初始化
//时分秒数组赋值//年月天数组赋值
void Start_I2C() {
SDA=1; //发送起始条件的数据信号 delay(1); SCL=1;
delay(5); //起始条件建立时间大于4.7us,延时
SDA=0; //发送起始信号
delay(5); // 起始条件锁定时间大于4μs
SCL=0; //钳住I2C总线,准备发送或接收数据 delay(2); }
void Stop_I2C() {
SDA=0; //发送结束条件的数据信号 delay(1); //发送结束条件的时钟信号
SCL=1; //结束条件建立时间大于4us delay(5);
SDA=1; //发送I2C总线结束信号 delay(4); }
uchar write_byte(uchar addr, uchar write_data) {
Start_I2C();
SendByte(ADDRTW); //////*******************************************************************///////////
if (ack == 0) return 0;
SendByte(addr); if (ack == 0) return 0;
SendByte(write_data); if (ack == 0) return 0;
Stop_I2C();
delay1ms(10);
return 1; }
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_DATE,temp); //修改日
temp=HEX2BCD(hou);
write_byte(DS3231_HOUR,temp); //修改时
temp=HEX2BCD(min);
write_byte(DS3231_MIN,temp); //修改分
temp=HEX2BCD(sec);
write_byte(DS3231_SEC,temp); //修改秒 }
void xianshi(void) { { uint i; TimeDisplay(hour,min,sec); lcd_pos(1,1); //时间 i=0; while(b[i]!='\\0') { delay1ms(1); write_dat(b[i]); delay1ms(1); i++;
/////////////////////
共分享92篇相关文档