模块参数:1.尺寸:38mm(长)*22mm(宽)*14mm(高)2.重量:8g3.工作电压:3.3--5.5V4.时钟芯片:高精度时钟芯片DS32315.时钟精度:0-40℃范围内,精度2ppm,年误差约1分钟6.带2个日历闹钟7.可编程方波输出8.实时时钟产生秒、分、时、星期、日期、月和年计时,并提供有效期到2100年的闰年补偿9.芯片内部自带温度传感器,精度为±3℃10.存储芯片:AT24C32(存储容量32K)11.IIC总线接口,最高传输速度400KHz(工作电压为5V时)12.可级联其它IIC设备,24C32地址可通过短路A0/A1/A2修改,默认地址为0x5713.带可充电电池LIR2032,保证系统断电后,时钟任然正常走动接线说明,以Arduinounor3为例:SCL→A5SDA→A4VCC→5VGND→GND代码部分:#includereg51.h#includeintrins.h#defineucharunsignedchar#defineuintunsignedintsbitSDA=P3^6;//模拟I2C数据传送位SDAsbitSCL=P3^7;//模拟I2C时钟控制位SCLsbitINT=P3^2;sbitRESET=P3^3;sbitled0=P1^0;sbitled1=P1^1;sbitled2=P1^2;sbitled3=P1^3;sbitled4=P1^4;sbitled5=P1^5;sbitled6=P1^6;sbitled7=P1^7;bitack;//应答标志位#defineDS3231_WriteAddress0xD0//器件写地址#defineDS3231_ReadAddress0xD1//器件读地址#defineDS3231_SECOND0x00//秒#defineDS3231_MINUTE0x01//分#defineDS3231_HOUR0x02//时#defineDS3231_WEEK0x03//星期#defineDS3231_DAY0x04//日#defineDS3231_MONTH0x05//月#defineDS3231_YEAR0x06//年//闹铃1#defineDS3231_SALARM1ECOND0x07//秒#defineDS3231_ALARM1MINUTE0x08//分#defineDS3231_ALARM1HOUR0x09//时#defineDS3231_ALARM1WEEK0x0A//星期/日//闹铃2#defineDS3231_ALARM2MINUTE0x0b//分#defineDS3231_ALARM2HOUR0x0c//时#defineDS3231_ALARM2WEEK0x0d//星期/日#defineDS3231_CONTROL0x0e//控制寄存器#defineDS3231_STATUS0x0f//状态寄存器#defineBSY2//忙#defineOSF7//振荡器停止标志#defineDS3231_XTAL0x10//晶体老化寄存器#defineDS3231_TEMPERATUREH0x11//温度寄存器高字节(8位)#defineDS3231_TEMPERATUREL0x12//温度寄存器低字节(高2位)ucharcodedis_code[11]={0xc0,0xf9,0xa4,0xb0,//0,1,2,30x99,0x92,0x82,0xf8,0x80,0x90,0xff};//4,5,6,7,8,9,offuchardatadis_buf[8];uchardatadis_index;uchardatadis_digit;ucharBCD2HEX(ucharval)//BCD转换为Byte{uchartemp;temp=val&0x0f;val=4;val&=0x0f;val*=10;temp+=val;returntemp;}ucharHEX2BCD(ucharval)//B码转换为BCD码{uchari,j,k;i=val/10;j=val;k=j+(i4);returnk;}voiddelayus(uintus){while(us--);}voidStart_I2C(){SDA=1;//发送起始条件的数据信号delayus(1);SCL=1;delayus(5);//起始条件建立时间大于4.7us,延时SDA=0;//发送起始信号delayus(5);//起始条件锁定时间大于4μsSCL=0;//钳住I2C总线,准备发送或接收数据delayus(2);}voidStop_I2C(){SDA=0;//发送结束条件的数据信号delayus(1);//发送结束条件的时钟信号SCL=1;//结束条件建立时间大于4usdelayus(5);SDA=1;//发送I2C总线结束信号delayus(4);}voidSendByte(ucharc){ucharBitCnt;for(BitCnt=0;BitCnt8;BitCnt++)//要传送的数据长度为8位{if((cBitCnt)&0x80)SDA=1;//判断发送位elseSDA=0;delayus(1);SCL=1;//置时钟线为高,通知被控器开始接收数据位delayus(5);//保证时钟高电平周期大于4μsSCL=0;}delayus(2);SDA=1;//8位发送完后释放数据线,准备接收应答位delayus(2);SCL=1;delayus(3);if(SDA==1)ack=0;elseack=1;//判断是否接收到应答信号SCL=0;delayus(2);}ucharRcvByte(){ucharretc;ucharBitCnt;retc=0;SDA=1;//置数据线为输入方式for(BitCnt=0;BitCnt8;BitCnt++){delayus(1);SCL=0;//置时钟线为低,准备接收数据位delayus(5);//时钟低电平周期大于4.7μsSCL=1;//置时钟线为高使数据线上数据有效delayus(3);retc=retc1;if(SDA==1)retc=retc+1;//读数据位,接收的数据位放入retc中delayus(2);}SCL=0;delayus(2);return(retc);}voidAck_I2C(bita){if(a==0)SDA=0;//在此发出应答或非应答信号elseSDA=1;delayus(3);SCL=1;delayus(5);//时钟低电平周期大于4μsSCL=0;//清时钟线,钳住I2C总线以便继续接收delayus(2);}ucharwrite_byte(ucharaddr,ucharwrite_data){Start_I2C();SendByte(DS3231_WriteAddress);if(ack==0)return0;SendByte(addr);if(ack==0)return0;SendByte(write_data);if(ack==0)return0;Stop_I2C();delayus(10);return1;}ucharread_current(){ucharread_data;Start_I2C();SendByte(DS3231_ReadAddress);if(ack==0)return(0);read_data=RcvByte();Ack_I2C(1);Stop_I2C();returnread_data;}ucharread_random(ucharrandom_addr){Start_I2C();SendByte(DS3231_WriteAddress);if(ack==0)return(0);SendByte(random_addr);if(ack==0)return(0);return(read_current());}voidModifyTime(ucharyea,ucharmon,ucharda,ucharhou,ucharmin,ucharsec){uchartemp=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);//修改秒}voidTimeDisplay(ucharDhour,ucharDmin,ucharDsec){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]=0xbf;//显示-dis_buf[5]=0xbf;}voidDateDisplay(ucharDyear,ucharDmonth,ucharDday){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]=0xbf;//显示-dis_buf[5]=0xbf;}voidget_show_time(void){ucharHtemp1,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);}voidget_show_date(void){ucharYtemp1,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);}voidget_show_Temperature(void){ucharTtemp1,Ttemp2,Ttemp3,Ttemp4;Ttemp1=read_random(DS3231_TEMPERATUREH);//温度高字节Ttemp2=BCD2HEX(Ttem