我在用eeprom读取数据的时候一直在变,ch4的位置应该是5,但是实际上数码管在5,4,0之间跳变,这是为什么?
#include "iic.h"
#include "hardware.h"
#include <STC15F2K60S2.H>
unsigned char ch1,ch2,ch3,ch4=0;
unsigned long int old_sec=123456;
unsigned long int new_sec=654321;
void Delay5ms() //@12.000MHz
{
unsigned char i, j;
i = 59;
j = 90;
do
{
while (--j);
} while (--i);
}
void Write_eeprom(unsigned char addr,unsigned char dat)
{
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();
I2CSendByte(dat);
I2CWaitAck();
I2CStop();
Delay5ms();
}
//------------------------------------------------------------------------
unsigned char Read_eeprom(unsigned char addr)
{
unsigned char tmp;
I2CStart();
I2CSendByte(0xa0);
I2CWaitAck();
I2CSendByte(addr);
I2CWaitAck();
I2CStart();
I2CSendByte(0xa1);
I2CWaitAck();
tmp=I2CReceiveByte();
I2CSendAck(1);
I2CStop();
return tmp;
}
void SMG_display()
{
SMG_buf[0]=17;
SMG_buf[1]=17;
SMG_buf[2]=ch3;//new_sec/100000;
SMG_buf[3]=ch4;//new_sec%100000/10000;
SMG_buf[4]=new_sec%10000/1000;
SMG_buf[5]=new_sec%1000/100;
SMG_buf[6]=new_sec%100/10;
SMG_buf[7]=new_sec%10;
}
void tt()
{
ch3=Read_eeprom(0x00);
ch4=Read_eeprom(0x01);
//ch3=Read_eeprom(0x02);
//ch4=Read_eeprom(0x03);
//new_sec=(ch1<<24)|(ch2<<16)|(ch3<<8)|ch4;
}
void main()
{
Timer1Init();
ch1=9;//old_sec/(256*3);
ch2=5;//(old_sec<<8)/(256*2);
//ch3=(old_sec<<16)/256;
//ch4=old_sec<<24;
Write_eeprom(0x00,ch1);
Write_eeprom(0x01,ch2);
//Write_eeprom(0x02,ch3);
//Write_eeprom(0x03,ch4);
while(1)
{
SMG_display();
if(timer200ms>=200)
{
timer200ms=0;
tt();
}
}
}