- #include<reg51.h>
- #include"OLED.H"
- extern unsigned char code Dot[];
- #define uint unsigned int
- #define uchar unsigned char
- #define UINT8 unsigned char
- #define HMS_FLAG 1
- //--------与24C02相关的设置和子函数-------------
- void delay(){ ;;;;;;;;;;;;;; ;;;;;;;;;;;;;;} //简单短延时子函数
- sbit sda_24c02=P2^6; //定义24C02的数据线与单片机的连接
- sbit scl_24c02=P2^7;
- void start_24c02(); //24C02开始信号
- void stop_24c02(); //24C02停止
- void respons_24c02(); //24C02应答
- void init_24c02(); //24C02初始化
- void write_byte_24c02(uchar date); //写一个byte到24C02
- uchar read_byte_24c02(); //从24C02读一个byte
- void write_add_24c02(uchar address,uchar date); //写一个字节到指定的24C02单元
- uchar read_add_24c02(uchar address); //从24C02指定单元读一个字节数据
- void write_add1_24c02(uchar address,uchar date); //写一个字节到指定的24C02单元
- uchar read_add1_24c02(uchar address); //从24C02指定单元读一个字节数据
- void XWrite_eeprom(UINT8 Address,UINT8 eeprom_Data);
- UINT8 XRead_eeprom(UINT8 Address);
- sbit led=P1^0;
- unsigned char code table_num[]="0123456789"; //LCD显示的数字代码
- void delay_ms(uint xms); //ms级延时子函数
- unsigned char i; //
- struct ccb
- {
- uchar second;
- uchar minute;
- uchar hour;
- uchar day;
- uchar month;
- uchar year;
- };
- struct ccb CurrentTime;
- UINT8 Change10ToBCD(UINT8 data1) //10->BCD数值转化
- {
- UINT8 high,low,result;
- high=data1/10;
- low=data1%10;
- result=(high<<4)|low;
- return result;
- }
- void init_ISL12026()
- {
- XWrite_eeprom(0x3F,0x02);
- XWrite_eeprom(0x3F,0x06);
- }
- void ResetRtcTime()
- {
- init_ISL12026();
- XWrite_eeprom(0x37,Change10ToBCD(20));//世纪
- XWrite_eeprom(0x36,Change10ToBCD(0));//周
- XWrite_eeprom(0x35,Change10ToBCD(10));//年
- XWrite_eeprom(0x34,Change10ToBCD(1));//月
- XWrite_eeprom(0x33,Change10ToBCD(1));//日
- XWrite_eeprom(0x32,Change10ToBCD(12)|0x80);//时
- XWrite_eeprom(0x31,Change10ToBCD(12));//分
- XWrite_eeprom(0x30,Change10ToBCD(12));//秒
- }
- UINT8 ChangeBCDTo10(UINT8 data1) //BCD->10数值转化
- {
- UINT8 temp,result;
- result=data1&0x0F;
- temp=data1>>4;
- result+=temp*10;
- return result;
- }
- void readCurrentTime()
- {
- CurrentTime.year=ChangeBCDTo10(XRead_eeprom(0x35));
- CurrentTime.month=ChangeBCDTo10(XRead_eeprom(0x34));
- CurrentTime.day=ChangeBCDTo10(XRead_eeprom(0x33));
- CurrentTime.hour=ChangeBCDTo10(XRead_eeprom(0x32)&0x7F);
- CurrentTime.minute=ChangeBCDTo10(XRead_eeprom(0x31));
- CurrentTime.second=ChangeBCDTo10(XRead_eeprom(0x30));
- }
- /**************************************************************************/
- //按页写入。 pBuffer数组指针首地址,WriteAddr要写入页的首地址,NumByteToWrite写入字符个数
- /*************************************************************************/
- void ISL12026_PageWrite(UINT8* pBuffer, UINT8 WriteAddr, UINT8 NumByteToWrite)
- {
- init_ISL12026();
- //XStart_iic();
- start_24c02();
- write_byte_24c02(0xde);
- respons_24c02();
- //XWriteByte(0);
- //XReceiveAck();
- write_byte_24c02(0);
- respons_24c02();
- //XWriteByte(WriteAddr);
- //XReceiveAck();
- write_byte_24c02(WriteAddr);
- respons_24c02();
- while(NumByteToWrite--)
- {
- //XWriteByte(*pBuffer);
- //Point to the next byte to be written
- write_byte_24c02(*pBuffer);
- respons_24c02();
- pBuffer++;
- //XReceiveAck();
- }
- //XStop_iic();
- stop_24c02();
- for(i=0;i<40;i++)
- delay();
- }
- /**************************************************************************/
- //写入时间信息,按照秒,分,时,天,月,年的顺序写入
- /*************************************************************************/
- void ISL12026_TimeWrite(UINT8 second,UINT8 minute,UINT8 hour,UINT8 day,UINT8 month,UINT8 year)
- {
- UINT8 temp[8];
- temp[0]=Change10ToBCD(second);
- temp[1]=Change10ToBCD(minute);
- temp[2]=Change10ToBCD(hour)|0x80;
- temp[3]=Change10ToBCD(day);
- temp[4]=Change10ToBCD(month);
- temp[5]=Change10ToBCD(year);
- temp[6]=Change10ToBCD(0); //周,不用则写0
- temp[7]=Change10ToBCD(20);
- ISL12026_PageWrite(temp,0x30,8);
- }
- /*
- void saveX1226(UINT8 hour_year,UINT8 minute_month,UINT8 second_day,UINT8 index)
- {
- init_X1226();
- if(index==HMS_FLAG)//写时分秒
- {
- XWrite_eeprom(0x30,Change10ToBCD(second_day));//秒
- XWrite_eeprom(0x31,Change10ToBCD(minute_month));//分
- XWrite_eeprom(0x32,Change10ToBCD(hour_year)|0x80);//时
- }
- else//写年月日星期
- {
- XWrite_eeprom(0x33,Change10ToBCD(second_day));//日
- XWrite_eeprom(0x34,Change10ToBCD(minute_month));//月
- XWrite_eeprom(0x35,Change10ToBCD(hour_year));//年
- }
- XWrite_eeprom(0x11,0x00);//按定时输出
- //XWrite_eeprom(0x3F,0x00);
- Delay(50);
- }*/
- /*void init_ISL12026_IQR()
- {
- //UINT8 mRtcStatus;
- init_ISL12026();
- //mRtcStatus=ChangeBCDTo10(XRead_eeprom(0x11));
- //mRtcStatus=mRtcStatus;
- XWrite_eeprom(0x11,0x00);//0x18, 1秒一个频冲
- //Delay(100);
- for(i=0;i<80;i++)
- delay();
- } */
-
- void init_ISL12026_Alarm(UINT8 Hour_Week,UINT8 Minute_Month,UINT8 Second_Day,UINT8 index) // 设置报警时间
- {
- init_ISL12026();
-
- if(index==HMS_FLAG)//写时分秒
- {
- XWrite_eeprom(0x00,Change10ToBCD(Second_Day));//秒
- XWrite_eeprom(0x01,Change10ToBCD(Minute_Month));//分
- XWrite_eeprom(0x02,Change10ToBCD(Hour_Week)|0x80);//时
- }
- else//写月日星期
- {
- XWrite_eeprom(0x03,Change10ToBCD(Second_Day));//日
- XWrite_eeprom(0x04,Change10ToBCD(Minute_Month));//月
- XWrite_eeprom(0x06,Change10ToBCD(Hour_Week));//年
- }
- XWrite_eeprom(0x11,0xA0);//按定时输出
- //Delay(50);
- for(i=0;i<40;i++)
- delay();
- }
- void PowerInitTime(void)
- {
- struct ccb DownTime;
- UINT8 mRtcStatus;
-
- init_ISL12026();
- //mRtcStatus=XRead_eeprom(0x14);
- XWrite_eeprom(0x14,0x00);
- mRtcStatus=XRead_eeprom(0x14);
-
- mRtcStatus=ChangeBCDTo10(XRead_eeprom(0x3F));
- if ( mRtcStatus & 0x01 ) //如果完全断电,设置为上次参数下载的时间
- {
- /*DownTime.year=ByteReadEeprom(DownLoadYearAddrOne);
- DownTime.month=ByteReadEeprom(DownLoadMonthAddrOne);
- DownTime.day=ByteReadEeprom(DownLoadDayAddrOne);
- DownTime.hour=ByteReadEeprom(DownLoadHourAddrOne);
- DownTime.minute=ByteReadEeprom(DownLoadMinuteAddrOne);
- DownTime.second=ByteReadEeprom(DownLoadSecondAddrOne);
- */
-
- ISL12026_TimeWrite(DownTime.second,DownTime.minute,DownTime.hour,DownTime.day,DownTime.month,DownTime.year);
- }
- }
- void readSecond()
- {
- //CurrentTime.minute=ChangeBCDTo10(XRead_eeprom(0x31));
- CurrentTime.second=ChangeBCDTo10(XRead_eeprom(0x30));
- }
- void main(void)
- {
- unsigned int i;
- unsigned char temp;
- unsigned char second;
- unsigned char bai,shi,ge;
- unsigned char year,month,day,hour,minute;
- LCD_Init();
- LCD_CLS();
- //LCD_P8x16Str(45,5,(u8*)"EASEAR");
- //LCD_P6x8Str(0,7,(u8*)"www.holteksupport.com");
- //Draw_BMP(0,0,100,3,(u8*)Dot);
- init_ISL12026();
- //XWrite_eeprom(0x3F,0x82);
- //delay2();
- //XWrite_eeprom(0x3F,0x86);
- XWrite_eeprom(0x50,209);//
- for(i=0;i<60000;i++);
- XWrite_eeprom(0x52,126);//
- for(i=0;i<60000;i++);
- XWrite_eeprom(0x54,158);//
- for(i=0;i<60000;i++);
- temp=XRead_eeprom(0x52);
- bai=temp/100+48;
- shi=temp%100/10+48;
- ge=temp%10+48;
- LCD_P8x16Char(0,0,bai);
- LCD_P8x16Char(8,0,shi);
- LCD_P8x16Char(16,0,ge);
- temp=XRead_eeprom(0x54);
- bai=temp/100+48;
- shi=temp%100/10+48;
- ge=temp%10+48;
- LCD_P8x16Char(32,0,bai);
- LCD_P8x16Char(40,0,shi);
- LCD_P8x16Char(48,0,ge);
- temp=XRead_eeprom(0x50);
- bai=temp/100+48;
- shi=temp%100/10+48;
- ge=temp%10+48;
- LCD_P8x16Char(64,0,bai);
- LCD_P8x16Char(72,0,shi);
- LCD_P8x16Char(80,0,ge);
- //XWrite_eeprom(0x10,0x00);//去除写保护
- /*XWrite_eeprom(0x37,0x06);//
- second=XRead_eeprom(0x3f);
- bai=second/100;
- shi=second%100/10;
- ge=second%10;
- lcd_display_char(12,0,bai+0x30);
- lcd_display_char(13,0,shi+0x30);
- lcd_display_char(14,0,ge+0x30);
- //XWrite_eeprom(0x3f,0x00);//
- XWrite_eeprom(0x14,0x00);// */
- //XWrite_eeprom(0x10,0x00);//去除写保护
- LCD_P8x16Str(0,2,(u8*)" : : ");
- LCD_P8x16Str(0,4,(u8*)"20 - - ");
- ISL12026_TimeWrite(50,59,23,31,12,23);
- while(1)
- {
- // readSecond();
-
- readCurrentTime();
- shi=(CurrentTime.second/10)+0x30;
- ge=(CurrentTime.second%10)+0x30;
- LCD_P8x16Char(48,2,shi);
- LCD_P8x16Char(56,2,ge);
- shi=(CurrentTime.minute/10)+0x30;
- ge=(CurrentTime.minute%10)+0x30;
- LCD_P8x16Char(24,2,shi);
- LCD_P8x16Char(32,2,ge);
- shi=(CurrentTime.hour/10)+0x30;
- ge=(CurrentTime.hour%10)+0x30;
- LCD_P8x16Char(0,2,shi);
- LCD_P8x16Char(8,2,ge);
- shi=(CurrentTime.day/10)+0x30;
- ge=(CurrentTime.day%10)+0x30;
- LCD_P8x16Char(64,4,shi);
- LCD_P8x16Char(72,4,ge);
- shi=(CurrentTime.month/10)+0x30;
- ge=(CurrentTime.month%10)+0x30;
- LCD_P8x16Char(40,4,shi);
- LCD_P8x16Char(48,4,ge);
- shi=(CurrentTime.year/10)+0x30;
- ge=(CurrentTime.year%10)+0x30;
- LCD_P8x16Char(16,4,shi);
- LCD_P8x16Char(24,4,ge);
- for(i=0;i<20000;i++);
- }
- }
- void delay_ms(uint xms) //ms级延时子程序
- { unsigned int aa,bb;
- for(aa=xms;aa>0;aa--)
- for(bb=4000;bb>0;bb--); //调整数值以使本句运行时间为1ms
- }
- //--------与24C02相关的设置和子函数-------------
- void start_24c02() //24c02开始信号
- { sda_24c02=1; delay();
- scl_24c02=1; delay();
- sda_24c02=0; delay(); }
- void stop_24c02() //24c02停止
- { sda_24c02=0; delay();
- scl_24c02=1; delay();
- sda_24c02=1; delay(); }
- void respons_24c02() //24c02应答
- { uchar i;
- scl_24c02=1;
- delay();
- while((sda_24c02==1)&&(i<250))i++;
- scl_24c02=0;
- delay(); }
- void init_24c02() //24c02初始化
- { sda_24c02=1; delay();
- scl_24c02=1; delay(); }
- void write_byte_24c02(uchar date) //写8个
- { uchar i,temp;
- temp=date;
- for(i=0;i<8;i++)
- { temp=temp<<1;
- scl_24c02=0; delay();
- sda_24c02=CY; delay();
- scl_24c02=1; delay();
- }
- scl_24c02=0; delay();
- sda_24c02=1; delay(); }
- uchar read_byte_24c02()
- { uchar i,k;
- scl_24c02=0; delay();
- sda_24c02=1; delay();
- for(i=0;i<8;i++)
- { scl_24c02=1; delay();
- k=(k<<1)|sda_24c02;
- scl_24c02=0; delay();
- }
- return k; }
- void write_add_24c02(uchar address,uchar date)//将某数据写入24C02某个单元中
- { start_24c02();
- write_byte_24c02(0xa0); //24c02的写地址(1010 0000)
- respons_24c02();
- write_byte_24c02(address);
- respons_24c02();
- write_byte_24c02(date);
- respons_24c02();
- stop_24c02(); }
- uchar read_add_24c02(uchar address)//读出24c02某个单元数据到变量中
- { uchar date;
- start_24c02();
- write_byte_24c02(0xa0); //24c02的读地址(1010 0001)
- respons_24c02();
- write_byte_24c02(address);
- respons_24c02();
- start_24c02();
- write_byte_24c02(0xa1);
- respons_24c02();
- date=read_byte_24c02();
- stop_24c02();
- return date; }
- //--------与24C02相关的设置和子函数 结束-----------
- void write_add1_24c02(uchar address,uchar date)//将某数据写入24C02某个单元中
- { start_24c02();
-
- write_byte_24c02(address<<1);
- respons_24c02();
- write_byte_24c02(date);
- respons_24c02();
- stop_24c02(); }
- uchar read_add1_24c02(uchar address)//读出24c02某个单元数据到变量中
- { uchar date;
- start_24c02();
-
- write_byte_24c02((address<<1)+1);
- respons_24c02();
- //start_24c02();
- //write_byte_24c02(0xa1);
- //respons_24c02();
- date=read_byte_24c02();
- stop_24c02();
- return date; }
- void XWrite_eeprom(UINT8 Address,UINT8 eeprom_Data)
- {
- //XStart_iic();
- start_24c02();
- //XWriteByte(0xDE);
- write_byte_24c02(0xde);
- //XReceiveAck();
- respons_24c02();
- /*XWriteByte(0);
- XReceiveAck();
- XWriteByte(Address);
- XReceiveAck();
- XWriteByte(eeprom_Data);
- XReceiveAck();
- XStop_iic();*/
- write_byte_24c02(0);
- //XReceiveAck();
- respons_24c02();
- write_byte_24c02(Address);
- //XReceiveAck();
- respons_24c02();
- write_byte_24c02(eeprom_Data);
- //XReceiveAck();
- respons_24c02();
- stop_24c02();
- }
- UINT8 XRead_eeprom(UINT8 Address)
- {
- UINT8 rdata;
- //XStart_iic();
- //XWriteByte(0xDE);
- //XReceiveAck();
- start_24c02();
-
- write_byte_24c02(0xde);
-
- respons_24c02();
- //XWriteByte(0);
- //XReceiveAck();
- //XWriteByte(Address);
- //XReceiveAck();
- write_byte_24c02(0);
-
- respons_24c02();
- write_byte_24c02(Address);
-
- respons_24c02();
- /*XStart_iic();
- XWriteByte(0xDF);
- XReceiveAck();
- rdata=XReadByte();
- XNo_Acknowledge();
- XStop_iic(); */
- start_24c02();
-
- write_byte_24c02(0xdf);
-
- respons_24c02();
- rdata=read_byte_24c02();
- stop_24c02();
- return(rdata);
- }
读写时间都没问题。读写EEPROM,读出来都是158,也就是最后写进去的值。
请问高手,哪里有错?谢谢