STM8S001J EEPROM PROBLEMS
Hello
I'm using STM8S001j in my project
the Project is simply an output on PB4 and output on PD6
I needed to store in the EEPROM data every time the input is getting low
I noticed that some times the data is recorded and some times not
I monitored the EEPROM using the programmer to read the data and had seen that some times data written some times erased
I wrote the write code several times to make sure that it works but not
Here's the code
unsigned short tim2_count;
bool t_flag ;
unsigned int DF_count = 1;
unsigned int EE_add_RW = EE_add_start;
void GPIO_conf(void);
void clk_conf(void);
void Tim2_conf(unsigned short T_counts);
void EEPROM_conf(void);
void EE_R_V(void);
main()
{
unsigned char delay_count = 0;
unsigned short temp_count = 0;
GPIO_conf();
clk_conf();
EEPROM_conf();
Tim2_conf(124); // 1msec
GPIO_WriteLow(GPIOB, GPIO_PIN_4);
{
unsigned char EE_temp = 0;
unsigned char temp1 = 0;
unsigned char temp2 = 0;
// unsigned int add = 0x4003;
EE_temp = FLASH_ReadByte(0x4000);
EE_temp = FLASH_ReadByte(0x4000);
EE_temp = FLASH_ReadByte(0x4000);
EE_temp = FLASH_ReadByte(0x4000);
EE_temp = FLASH_ReadByte(0x4000);
if(EE_temp == 0)
{
FLASH_Unlock(FLASH_MEMTYPE_DATA);
FLASH_ProgramByte(0x4000, 0x02);
FLASH_ProgramByte(0x4002, 0xB0);
FLASH_ProgramByte(0x4003, 0x04);
temp1 = FLASH_ReadByte(0x4002);
temp2 = FLASH_ReadByte(0x4003);
if((temp1 == 0xb0) && (temp2 == 0x04))
FLASH_Lock(FLASH_MEMTYPE_DATA);
else
{
FLASH_ProgramByte(0x4000, 0x02);
FLASH_ProgramByte(0x4002, 0xB0);
FLASH_ProgramByte(0x4003, 0x04);
}
if((temp1 == 0xb0) && (temp2 == 0x04))
FLASH_Lock(FLASH_MEMTYPE_DATA);
else
{
FLASH_ProgramByte(0x4000, 0x02);
FLASH_ProgramByte(0x4002, 0xB0);
FLASH_ProgramByte(0x4003, 0x04);
}
}
else
{
EE_add_RW = 0x4000 + EE_temp;
temp1 = FLASH_ReadByte(0x4002);
temp2 = FLASH_ReadByte(0x4003);
temp1 = FLASH_ReadByte(0x4002);
temp2 = FLASH_ReadByte(0x4003);
temp1 = FLASH_ReadByte(0x4002);
temp2 = FLASH_ReadByte(0x4003);
DF_count = (temp2 << 8) | temp1;
}
}
while(1)
{
if(DF_count < 1801)
GPIO_WriteLow(GPIOB, GPIO_PIN_4);
else if(DF_count < 1831)
GPIO_WriteHigh(GPIOB, GPIO_PIN_4);
else
DF_count = 1;
if(tim2_count == 0X03E8)
{
DF_count ++;
t_flag ^= 1;
tim2_count = 0;
}
if(GPIO_ReadInputPin(GPIOD, GPIO_PIN_6) == FALSE)
{
if((temp_count <= 100) )
{
temp_count ++;
}
else
{
unsigned char low = 0;
unsigned char high = 0;
unsigned char temp1 = 0;
unsigned char temp2 = 0;
low = DF_count & 0xff;
high = DF_count >> 8;
FLASH_Unlock(FLASH_MEMTYPE_DATA);
FLASH_ProgramByte(0x4002, low);
FLASH_ProgramByte(0x4003, high);
temp1 = FLASH_ReadByte(0x4002);
temp2 = FLASH_ReadByte(0x4003);
if((temp1 == low) && (temp2 == high))
FLASH_Lock(FLASH_MEMTYPE_DATA);
else
{
FLASH_ProgramByte(0x4002, low);
FLASH_ProgramByte(0x4003, high);
temp1 = FLASH_ReadByte(0x4002);
temp2 = FLASH_ReadByte(0x4003);
if((temp1 == low) && (temp2 == high))
FLASH_Lock(FLASH_MEMTYPE_DATA);
else
{
FLASH_ProgramByte(0x4002, low);
FLASH_ProgramByte(0x4003, high);
temp1 = FLASH_ReadByte(0x4002);
temp2 = FLASH_ReadByte(0x4003);
FLASH_Lock(FLASH_MEMTYPE_DATA);
}
}
}
}
}
}
