Skip to main content
AAraf.1
Associate
May 27, 2020
Question

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);

}

}

}

}

}

}

    This topic has been closed for replies.

    1 reply

    AAraf.1
    AAraf.1Author
    Associate
    May 27, 2020

    here is the missing part of the code

    void GPIO_conf(void)

    {

    GPIO_DeInit(GPIOF);

    GPIO_DeInit(GPIOE);

    GPIO_DeInit(GPIOD);

    GPIO_DeInit(GPIOC);

    GPIO_DeInit(GPIOB);

    GPIO_DeInit(GPIOA);

    GPIO_Init(GPIOA, (GPIO_Pin_TypeDef)(GPIO_PIN_2), GPIO_MODE_OUT_PP_LOW_SLOW);

    GPIO_Init(GPIOB, (GPIO_Pin_TypeDef)(GPIO_PIN_0)|(GPIO_PIN_1)|(GPIO_PIN_2)|(GPIO_PIN_3)|(GPIO_PIN_6)|(GPIO_PIN_7),GPIO_MODE_OUT_PP_LOW_SLOW);

    GPIO_Init(GPIOC, (GPIO_Pin_TypeDef)(GPIO_PIN_1)|(GPIO_PIN_2)|(GPIO_PIN_7),GPIO_MODE_OUT_PP_LOW_SLOW);

    GPIO_Init(GPIOD, (GPIO_Pin_TypeDef)(GPIO_PIN_0)|(GPIO_PIN_2)|(GPIO_PIN_4)|(GPIO_PIN_7), GPIO_MODE_OUT_PP_LOW_SLOW);

    GPIO_Init(GPIOE, GPIO_PIN_5, GPIO_MODE_OUT_PP_LOW_SLOW);

    GPIO_Init(GPIOF, GPIO_PIN_4, GPIO_MODE_OUT_PP_LOW_SLOW);

    GPIO_Init(GPIOD, GPIO_PIN_6, GPIO_MODE_IN_FL_NO_IT);

    //GPIO_Init(GPIOA, GPIO_PIN_1, GPIO_MODE_IN_FL_NO_IT);

    //GPIO_Init(GPIOC, GPIO_PIN_5, GPIO_MODE_OUT_OD_HIZ_SLOW);

    //GPIO_Init(GPIOB, GPIO_PIN_5, GPIO_MODE_OUT_OD_HIZ_SLOW);

    GPIO_Init(GPIOB, GPIO_PIN_4, GPIO_MODE_OUT_PP_LOW_SLOW);

    }

    void clk_conf()

    {

    CLK_DeInit();

    CLK_HSECmd(DISABLE);

    CLK_LSICmd(DISABLE);

    CLK_HSICmd(ENABLE);

    while(CLK_GetFlagStatus(CLK_FLAG_HSIRDY) == FALSE);

    CLK_ClockSwitchCmd(ENABLE);

    CLK_HSIPrescalerConfig(CLK_PRESCALER_HSIDIV4); // 16MHz frequency

    CLK_SYSCLKConfig(CLK_PRESCALER_CPUDIV1);

    CLK_ClockSwitchConfig(CLK_SWITCHMODE_AUTO,CLK_SOURCE_HSI,DISABLE,

    CLK_CURRENTCLOCKSTATE_ENABLE);

    CLK_PeripheralClockConfig(CLK_PERIPHERAL_SPI, DISABLE);

    CLK_PeripheralClockConfig(CLK_PERIPHERAL_I2C, DISABLE);

    CLK_PeripheralClockConfig(CLK_PERIPHERAL_ADC, DISABLE);

    CLK_PeripheralClockConfig(CLK_PERIPHERAL_AWU, DISABLE);

    CLK_PeripheralClockConfig(CLK_PERIPHERAL_UART1, DISABLE);

    CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER1, DISABLE);

    CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER2, ENABLE);

    CLK_PeripheralClockConfig(CLK_PERIPHERAL_TIMER4, DISABLE);

    }

    void Tim2_conf(unsigned short T_counts)

    {

    TIM2_DeInit();

    // @ 16MHZ rescaler 128 and 1250 counts gives 10m sec

    TIM2_TimeBaseInit(TIM2_PRESCALER_32, T_counts); 

    TIM2_ITConfig(TIM2_IT_UPDATE, ENABLE);

    TIM2_Cmd(ENABLE);

    enableInterrupts();

    }

    void EEPROM_conf(void)

    {

    FLASH_DeInit();

    }