[STC单片机] 读写ISL12026EEPROM失败

[复制链接]
4524|0
 楼主| 比神乐 发表于 2024-12-10 20:45 | 显示全部楼层 |阅读模式
代码:
  1. #include<reg51.h>
  2. #include"OLED.H"

  3. extern unsigned char code Dot[];
  4. #define uint unsigned int
  5. #define uchar unsigned char
  6. #define UINT8 unsigned char

  7. #define HMS_FLAG  1
  8. //--------与24C02相关的设置和子函数-------------
  9. void delay(){ ;;;;;;;;;;;;;; ;;;;;;;;;;;;;;}                 //简单短延时子函数
  10. sbit sda_24c02=P2^6;    //定义24C02的数据线与单片机的连接                     
  11. sbit scl_24c02=P2^7;
  12. void start_24c02();          //24C02开始信号
  13. void stop_24c02();           //24C02停止
  14. void respons_24c02();          //24C02应答
  15. void init_24c02();                  //24C02初始化
  16. void write_byte_24c02(uchar date);         //写一个byte到24C02
  17. uchar read_byte_24c02();                         //从24C02读一个byte
  18. void write_add_24c02(uchar address,uchar date);        //写一个字节到指定的24C02单元
  19. uchar read_add_24c02(uchar address);                        //从24C02指定单元读一个字节数据
  20. void write_add1_24c02(uchar address,uchar date);        //写一个字节到指定的24C02单元
  21. uchar read_add1_24c02(uchar address);                        //从24C02指定单元读一个字节数据
  22. void XWrite_eeprom(UINT8 Address,UINT8 eeprom_Data);
  23. UINT8  XRead_eeprom(UINT8 Address);

  24. sbit led=P1^0;

  25. unsigned char code table_num[]="0123456789";   //LCD显示的数字代码

  26. void delay_ms(uint xms);                   //ms级延时子函数

  27. unsigned char i;                        //
  28. struct ccb
  29. {
  30.         uchar second;
  31.         uchar minute;
  32.         uchar hour;
  33.         uchar day;
  34.         uchar month;
  35.         uchar year;
  36. };
  37. struct ccb CurrentTime;

  38. UINT8 Change10ToBCD(UINT8 data1)         //10->BCD数值转化
  39. {
  40.     UINT8 high,low,result;
  41.     high=data1/10;
  42.     low=data1%10;
  43.     result=(high<<4)|low;
  44.     return result;
  45. }
  46. void init_ISL12026()
  47. {
  48.     XWrite_eeprom(0x3F,0x02);
  49.     XWrite_eeprom(0x3F,0x06);
  50. }

  51. void ResetRtcTime()
  52. {
  53.     init_ISL12026();
  54.     XWrite_eeprom(0x37,Change10ToBCD(20));//世纪
  55.     XWrite_eeprom(0x36,Change10ToBCD(0));//周
  56.     XWrite_eeprom(0x35,Change10ToBCD(10));//年
  57.     XWrite_eeprom(0x34,Change10ToBCD(1));//月
  58.     XWrite_eeprom(0x33,Change10ToBCD(1));//日
  59.     XWrite_eeprom(0x32,Change10ToBCD(12)|0x80);//时
  60.     XWrite_eeprom(0x31,Change10ToBCD(12));//分
  61.     XWrite_eeprom(0x30,Change10ToBCD(12));//秒
  62. }
  63. UINT8 ChangeBCDTo10(UINT8 data1)         //BCD->10数值转化
  64. {
  65.     UINT8 temp,result;
  66.     result=data1&0x0F;
  67.     temp=data1>>4;
  68.     result+=temp*10;
  69.     return result;
  70. }
  71. void readCurrentTime()
  72. {
  73.     CurrentTime.year=ChangeBCDTo10(XRead_eeprom(0x35));
  74.     CurrentTime.month=ChangeBCDTo10(XRead_eeprom(0x34));
  75.     CurrentTime.day=ChangeBCDTo10(XRead_eeprom(0x33));
  76.     CurrentTime.hour=ChangeBCDTo10(XRead_eeprom(0x32)&0x7F);
  77.     CurrentTime.minute=ChangeBCDTo10(XRead_eeprom(0x31));
  78.     CurrentTime.second=ChangeBCDTo10(XRead_eeprom(0x30));
  79. }

  80. /**************************************************************************/
  81. //按页写入。  pBuffer数组指针首地址,WriteAddr要写入页的首地址,NumByteToWrite写入字符个数
  82. /*************************************************************************/
  83. void ISL12026_PageWrite(UINT8* pBuffer, UINT8 WriteAddr, UINT8 NumByteToWrite)
  84. {
  85.     init_ISL12026();
  86.     //XStart_iic();
  87.     start_24c02();
  88.         write_byte_24c02(0xde);
  89.     respons_24c02();
  90.     //XWriteByte(0);
  91.     //XReceiveAck();
  92.         write_byte_24c02(0);
  93.     respons_24c02();
  94.     //XWriteByte(WriteAddr);
  95.     //XReceiveAck();
  96.         write_byte_24c02(WriteAddr);
  97.     respons_24c02();
  98.     while(NumByteToWrite--)   
  99.     {
  100.         //XWriteByte(*pBuffer);
  101.         //Point to the next byte to be written
  102.                 write_byte_24c02(*pBuffer);
  103.             respons_24c02();
  104.         pBuffer++;  
  105.         //XReceiveAck();
  106.     }
  107.     //XStop_iic();
  108.     stop_24c02();
  109.         for(i=0;i<40;i++)
  110.                 delay();
  111. }

  112. /**************************************************************************/
  113. //写入时间信息,按照秒,分,时,天,月,年的顺序写入
  114. /*************************************************************************/
  115. void ISL12026_TimeWrite(UINT8 second,UINT8 minute,UINT8 hour,UINT8 day,UINT8 month,UINT8 year)
  116. {
  117.     UINT8 temp[8];
  118.     temp[0]=Change10ToBCD(second);
  119.     temp[1]=Change10ToBCD(minute);
  120.     temp[2]=Change10ToBCD(hour)|0x80;
  121.     temp[3]=Change10ToBCD(day);
  122.     temp[4]=Change10ToBCD(month);
  123.     temp[5]=Change10ToBCD(year);
  124.     temp[6]=Change10ToBCD(0);      //周,不用则写0
  125.     temp[7]=Change10ToBCD(20);
  126.     ISL12026_PageWrite(temp,0x30,8);
  127. }

  128. /*
  129. void saveX1226(UINT8 hour_year,UINT8 minute_month,UINT8 second_day,UINT8 index)
  130. {
  131.     init_X1226();
  132.     if(index==HMS_FLAG)//写时分秒
  133.     {
  134.         XWrite_eeprom(0x30,Change10ToBCD(second_day));//秒
  135.         XWrite_eeprom(0x31,Change10ToBCD(minute_month));//分
  136.         XWrite_eeprom(0x32,Change10ToBCD(hour_year)|0x80);//时
  137.     }
  138.     else//写年月日星期
  139.     {
  140.         XWrite_eeprom(0x33,Change10ToBCD(second_day));//日
  141.         XWrite_eeprom(0x34,Change10ToBCD(minute_month));//月
  142.         XWrite_eeprom(0x35,Change10ToBCD(hour_year));//年
  143.     }         
  144.     XWrite_eeprom(0x11,0x00);//按定时输出
  145.     //XWrite_eeprom(0x3F,0x00);
  146.     Delay(50);
  147. }*/

  148. /*void init_ISL12026_IQR()
  149. {
  150.     //UINT8 mRtcStatus;
  151.     init_ISL12026();
  152.     //mRtcStatus=ChangeBCDTo10(XRead_eeprom(0x11));
  153.     //mRtcStatus=mRtcStatus;
  154.     XWrite_eeprom(0x11,0x00);//0x18, 1秒一个频冲
  155.     //Delay(100);
  156.         for(i=0;i<80;i++)
  157.                 delay();
  158. }  */
  159.    
  160. void init_ISL12026_Alarm(UINT8 Hour_Week,UINT8 Minute_Month,UINT8 Second_Day,UINT8 index) // 设置报警时间
  161. {
  162.     init_ISL12026();
  163.    
  164.     if(index==HMS_FLAG)//写时分秒
  165.     {
  166.         XWrite_eeprom(0x00,Change10ToBCD(Second_Day));//秒
  167.         XWrite_eeprom(0x01,Change10ToBCD(Minute_Month));//分
  168.         XWrite_eeprom(0x02,Change10ToBCD(Hour_Week)|0x80);//时
  169.     }
  170.     else//写月日星期
  171.     {
  172.         XWrite_eeprom(0x03,Change10ToBCD(Second_Day));//日
  173.         XWrite_eeprom(0x04,Change10ToBCD(Minute_Month));//月
  174.         XWrite_eeprom(0x06,Change10ToBCD(Hour_Week));//年
  175.     }
  176.     XWrite_eeprom(0x11,0xA0);//按定时输出
  177.     //Delay(50);
  178.         for(i=0;i<40;i++)
  179.                 delay();     
  180. }

  181. void PowerInitTime(void)
  182. {
  183.     struct ccb DownTime;
  184.     UINT8 mRtcStatus;
  185.    
  186.     init_ISL12026();
  187.     //mRtcStatus=XRead_eeprom(0x14);
  188.     XWrite_eeprom(0x14,0x00);
  189.     mRtcStatus=XRead_eeprom(0x14);
  190.    
  191.     mRtcStatus=ChangeBCDTo10(XRead_eeprom(0x3F));
  192.     if ( mRtcStatus & 0x01 )                             //如果完全断电,设置为上次参数下载的时间
  193.     {
  194.         /*DownTime.year=ByteReadEeprom(DownLoadYearAddrOne);
  195.         DownTime.month=ByteReadEeprom(DownLoadMonthAddrOne);
  196.         DownTime.day=ByteReadEeprom(DownLoadDayAddrOne);
  197.         DownTime.hour=ByteReadEeprom(DownLoadHourAddrOne);
  198.         DownTime.minute=ByteReadEeprom(DownLoadMinuteAddrOne);
  199.         DownTime.second=ByteReadEeprom(DownLoadSecondAddrOne);
  200.           */
  201.         
  202.         ISL12026_TimeWrite(DownTime.second,DownTime.minute,DownTime.hour,DownTime.day,DownTime.month,DownTime.year);
  203.     }
  204. }


  205. void readSecond()
  206. {
  207.     //CurrentTime.minute=ChangeBCDTo10(XRead_eeprom(0x31));
  208.     CurrentTime.second=ChangeBCDTo10(XRead_eeprom(0x30));
  209. }
  210. void main(void)
  211. {
  212.         unsigned int i;
  213.         unsigned char temp;
  214.         unsigned char second;
  215.         unsigned char bai,shi,ge;
  216.         unsigned char year,month,day,hour,minute;
  217.         LCD_Init();
  218.      LCD_CLS();
  219.         //LCD_P8x16Str(45,5,(u8*)"EASEAR");
  220.         //LCD_P6x8Str(0,7,(u8*)"www.holteksupport.com");
  221.         //Draw_BMP(0,0,100,3,(u8*)Dot);
  222.         init_ISL12026();
  223.                 //XWrite_eeprom(0x3F,0x82);
  224.                 //delay2();
  225.             //XWrite_eeprom(0x3F,0x86);
  226.                 XWrite_eeprom(0x50,209);//
  227.                 for(i=0;i<60000;i++);
  228.                 XWrite_eeprom(0x52,126);//
  229.                 for(i=0;i<60000;i++);
  230.                 XWrite_eeprom(0x54,158);//
  231.                 for(i=0;i<60000;i++);
  232.                 temp=XRead_eeprom(0x52);
  233.                 bai=temp/100+48;
  234.                 shi=temp%100/10+48;
  235.                 ge=temp%10+48;
  236.                 LCD_P8x16Char(0,0,bai);
  237.                 LCD_P8x16Char(8,0,shi);
  238.                 LCD_P8x16Char(16,0,ge);

  239.                 temp=XRead_eeprom(0x54);
  240.                 bai=temp/100+48;
  241.                 shi=temp%100/10+48;
  242.                 ge=temp%10+48;
  243.                 LCD_P8x16Char(32,0,bai);
  244.                 LCD_P8x16Char(40,0,shi);
  245.                 LCD_P8x16Char(48,0,ge);

  246.                 temp=XRead_eeprom(0x50);
  247.                 bai=temp/100+48;
  248.                 shi=temp%100/10+48;
  249.                 ge=temp%10+48;
  250.                 LCD_P8x16Char(64,0,bai);
  251.                 LCD_P8x16Char(72,0,shi);
  252.                 LCD_P8x16Char(80,0,ge);
  253.                 //XWrite_eeprom(0x10,0x00);//去除写保护
  254.                 /*XWrite_eeprom(0x37,0x06);//
  255.                 second=XRead_eeprom(0x3f);
  256.                 bai=second/100;
  257.                 shi=second%100/10;
  258.                 ge=second%10;
  259.                 lcd_display_char(12,0,bai+0x30);
  260.                 lcd_display_char(13,0,shi+0x30);
  261.                 lcd_display_char(14,0,ge+0x30);
  262.                 //XWrite_eeprom(0x3f,0x00);//
  263.                 XWrite_eeprom(0x14,0x00);//        */
  264.                 //XWrite_eeprom(0x10,0x00);//去除写保护
  265.                 LCD_P8x16Str(0,2,(u8*)"  :  :  ");
  266.                 LCD_P8x16Str(0,4,(u8*)"20  -  -  ");
  267.                 ISL12026_TimeWrite(50,59,23,31,12,23);
  268.         while(1)
  269.         {
  270. //                readSecond();
  271.                
  272.                 readCurrentTime();
  273.                 shi=(CurrentTime.second/10)+0x30;
  274.                 ge=(CurrentTime.second%10)+0x30;
  275.                 LCD_P8x16Char(48,2,shi);
  276.                 LCD_P8x16Char(56,2,ge);
  277.                 shi=(CurrentTime.minute/10)+0x30;
  278.                 ge=(CurrentTime.minute%10)+0x30;
  279.                 LCD_P8x16Char(24,2,shi);
  280.                 LCD_P8x16Char(32,2,ge);
  281.                 shi=(CurrentTime.hour/10)+0x30;
  282.                 ge=(CurrentTime.hour%10)+0x30;
  283.                 LCD_P8x16Char(0,2,shi);
  284.                 LCD_P8x16Char(8,2,ge);
  285.                 shi=(CurrentTime.day/10)+0x30;
  286.                 ge=(CurrentTime.day%10)+0x30;
  287.                 LCD_P8x16Char(64,4,shi);
  288.                 LCD_P8x16Char(72,4,ge);
  289.                 shi=(CurrentTime.month/10)+0x30;
  290.                 ge=(CurrentTime.month%10)+0x30;
  291.                 LCD_P8x16Char(40,4,shi);
  292.                 LCD_P8x16Char(48,4,ge);
  293.                 shi=(CurrentTime.year/10)+0x30;
  294.                 ge=(CurrentTime.year%10)+0x30;
  295.                 LCD_P8x16Char(16,4,shi);
  296.                 LCD_P8x16Char(24,4,ge);
  297.                 for(i=0;i<20000;i++);
  298.         }
  299. }
  300. void delay_ms(uint xms)        //ms级延时子程序
  301.         {        unsigned int aa,bb;
  302.                 for(aa=xms;aa>0;aa--)
  303.                         for(bb=4000;bb>0;bb--);        //调整数值以使本句运行时间为1ms
  304.         }
  305. //--------与24C02相关的设置和子函数-------------
  306. void start_24c02()  //24c02开始信号
  307. {        sda_24c02=1;        delay();
  308.         scl_24c02=1;        delay();
  309.         sda_24c02=0;        delay(); }

  310. void stop_24c02()   //24c02停止
  311. {        sda_24c02=0;        delay();
  312.         scl_24c02=1;        delay();
  313.         sda_24c02=1;        delay(); }

  314. void respons_24c02()  //24c02应答
  315. {        uchar i;
  316.         scl_24c02=1;
  317.         delay();
  318.         while((sda_24c02==1)&&(i<250))i++;
  319.         scl_24c02=0;
  320.         delay(); }

  321. void init_24c02()                 //24c02初始化
  322. {        sda_24c02=1;        delay();
  323.         scl_24c02=1;        delay(); }

  324. void write_byte_24c02(uchar date) //写8个
  325. {        uchar i,temp;
  326.         temp=date;
  327.         for(i=0;i<8;i++)
  328.         {        temp=temp<<1;
  329.                 scl_24c02=0;            delay();
  330.                 sda_24c02=CY;                delay();
  331.                 scl_24c02=1;                delay();
  332.         }
  333.         scl_24c02=0;        delay();
  334.         sda_24c02=1;        delay();  }

  335. uchar read_byte_24c02()
  336. {        uchar i,k;
  337.         scl_24c02=0;        delay();
  338.         sda_24c02=1;        delay();
  339.         for(i=0;i<8;i++)
  340.         {        scl_24c02=1;                delay();        
  341.                 k=(k<<1)|sda_24c02;
  342.                 scl_24c02=0;                 delay();        
  343.         }
  344.         return k;  }

  345. void write_add_24c02(uchar address,uchar date)//将某数据写入24C02某个单元中
  346. {        start_24c02();
  347.         write_byte_24c02(0xa0);         //24c02的写地址(1010 0000)
  348.         respons_24c02();
  349.         write_byte_24c02(address);
  350.         respons_24c02();
  351.         write_byte_24c02(date);
  352.         respons_24c02();
  353.         stop_24c02();         }

  354. uchar read_add_24c02(uchar address)//读出24c02某个单元数据到变量中
  355. {        uchar date;
  356.         start_24c02();
  357.         write_byte_24c02(0xa0);           //24c02的读地址(1010 0001)
  358.         respons_24c02();
  359.         write_byte_24c02(address);
  360.         respons_24c02();
  361.         start_24c02();
  362.         write_byte_24c02(0xa1);
  363.         respons_24c02();
  364.         date=read_byte_24c02();
  365.         stop_24c02();
  366.         return date; }
  367. //--------与24C02相关的设置和子函数  结束-----------
  368. void write_add1_24c02(uchar address,uchar date)//将某数据写入24C02某个单元中
  369. {        start_24c02();
  370.         
  371.         write_byte_24c02(address<<1);
  372.         respons_24c02();
  373.         write_byte_24c02(date);
  374.         respons_24c02();
  375.         stop_24c02();         }

  376. uchar read_add1_24c02(uchar address)//读出24c02某个单元数据到变量中
  377. {        uchar date;
  378.         start_24c02();
  379.         
  380.         write_byte_24c02((address<<1)+1);
  381.         respons_24c02();
  382.         //start_24c02();
  383.         //write_byte_24c02(0xa1);
  384.         //respons_24c02();
  385.         date=read_byte_24c02();
  386.         stop_24c02();
  387.         return date; }

  388. void XWrite_eeprom(UINT8 Address,UINT8 eeprom_Data)
  389. {
  390.         //XStart_iic();
  391.         start_24c02();
  392.     //XWriteByte(0xDE);
  393.         write_byte_24c02(0xde);
  394.     //XReceiveAck();
  395.         respons_24c02();
  396.     /*XWriteByte(0);
  397.     XReceiveAck();
  398.     XWriteByte(Address);
  399.     XReceiveAck();
  400.     XWriteByte(eeprom_Data);
  401.     XReceiveAck();       
  402.     XStop_iic();*/
  403.         write_byte_24c02(0);
  404.     //XReceiveAck();
  405.         respons_24c02();
  406.         write_byte_24c02(Address);
  407.     //XReceiveAck();
  408.         respons_24c02();
  409.         write_byte_24c02(eeprom_Data);
  410.     //XReceiveAck();
  411.         respons_24c02();
  412.         stop_24c02();
  413. }
  414. UINT8  XRead_eeprom(UINT8 Address)
  415. {
  416.     UINT8 rdata;

  417.     //XStart_iic();
  418.     //XWriteByte(0xDE);
  419.     //XReceiveAck();
  420.         start_24c02();
  421.    
  422.         write_byte_24c02(0xde);
  423.    
  424.         respons_24c02();
  425.     //XWriteByte(0);
  426.     //XReceiveAck();
  427.     //XWriteByte(Address);
  428.     //XReceiveAck();
  429.         write_byte_24c02(0);
  430.    
  431.         respons_24c02();
  432.         write_byte_24c02(Address);
  433.    
  434.         respons_24c02();

  435.     /*XStart_iic();
  436.     XWriteByte(0xDF);
  437.     XReceiveAck();
  438.     rdata=XReadByte();
  439.     XNo_Acknowledge();
  440.     XStop_iic(); */
  441.         start_24c02();
  442.    
  443.         write_byte_24c02(0xdf);
  444.    
  445.         respons_24c02();
  446.         rdata=read_byte_24c02();
  447.         stop_24c02();
  448.     return(rdata);       
  449. }
读写时间都没问题。读写EEPROM,读出来都是158,也就是最后写进去的值。
请问高手,哪里有错?谢谢
您需要登录后才可以回帖 登录 | 注册

本版积分规则

470

主题

3537

帖子

7

粉丝
快速回复 在线客服 返回列表 返回顶部