Exploring STC 8051 Microcontrollers – Coding

IAP/EEPROM

Storing calibration, configuration, setting data and some preset values are required in some devices and so they need to be stored in memories that can be later modified if needed or else left alone. For such cases, we would need either EEPROM or flash memories. Like many modern microcontrollers, STC microcontrollers don’t come equipped with built-in EEPROM memory but through coding we can store aforementioned data in internal flash memory through IAP/ISP technology.

Of course, there are ways to use external EEPROM and flash memories like AT24Cxx EERPOMS and W25X16 but that would require external wiring and the use of communication pins. Having flash/EEPROM memory embedded in the application chip allows us to simply avoid these and have our microcontroller ready for fast deployment.

In this section, we would see how to use internal flash to store data.

Code

 # include "STC8xxx.h"
#include "BSP.h"
#include "LCD.c"
#include "lcd_print.c"
 
#define BASE_ADDRESS  0x0400
 
void setup(void);
 
void main(void)
{
  unsigned char i = 0;
  setup();
  
  LCD_goto(0, 0); 
  LCD_putstr("R Addr:");
  LCD_goto(0, 1); 
  LCD_putstr("R Data:");
  
  i = IAP_read(BASE_ADDRESS);
  delay_ms(10);
  print_I(11, 0, BASE_ADDRESS);
  print_C(14, 1, i);
    
  delay_ms(2000);
  
  if(i == 0)
  {
    LCD_clear_home();
    LCD_goto(0, 0); 
    LCD_putstr("Performing Erase");
    LCD_goto(0, 1); 
    LCD_putstr("....");
    IAP_erase(BASE_ADDRESS); 
    delay_ms(1000);
  } 
  
  LCD_clear_home();
  delay_ms(100);
  
  LCD_goto(0, 0); 
  LCD_putstr("W Addr:");
  LCD_goto(0, 1); 
  LCD_putstr("W Data:");
 
  i = (P1 & 0x03);
  IAP_write(BASE_ADDRESS, i);
  print_I(11, 0, BASE_ADDRESS);    
  print_C(14, 1, i);
  delay_ms(2000);
  
  LCD_clear_home();
  delay_ms(100);
  
  LCD_goto(0, 0); 
  LCD_putstr("R Addr:");
  LCD_goto(0, 1); 
  LCD_putstr("R Data:");
  
  i = IAP_read(BASE_ADDRESS);
  delay_ms(10);
  print_I(11, 0, BASE_ADDRESS);
  print_C(14, 1, i);
    
  delay_ms(2000);
 
  while(1)
  {
  };
}
 
 
void setup(void)
{
  CLK_set_sys_clk(IRC_24M, 2, MCLK_SYSCLK_div_1, MCLK_out_P54);
 
  P10_input_mode;
  P11_input_mode;
  
  LCD_init();
  LCD_clear_home();
}

Schematic

Explanation

The code in this rudimentary demo works by reading a single fixed location of internal flash, here 0x0400. An LCD display is used to show the read location and its content. If the content is 0 then an erase is performed because it is assumed to be empty. After performing erase, the same location is written. The write value is generated by reading the logic states of the lowermost bits (i.e., bit 0 and 1) of P1 port. After writing, the location is read again as to check if the data is properly saved. If the micro is reset or if there is a power-down, the saved data is retained, confirming that indeed data has been preserved in the internal flash memory.

IAP functionality utilizes the following three functions. As can be seen that these functions must follow a sequence operation in order to enable access to internal flash.

 unsigned char IAP_read(unsigned int address)
{
  unsigned char value = 0x00;
 
  IAP_CONTR = IAP_WT;
  IAP_CMD = IAP_read_command;
  IAP_address(address);
  IAP_trigger;
  _nop_();
  value = IAP_DATA;
  IAP_clear;
 
  return value;
}
 
void IAP_write(unsigned int address, unsigned char value)
{
  IAP_CONTR = IAP_WT;
  IAP_CMD = IAP_write_command;
  IAP_address(address);
  IAP_DATA = value;
  IAP_trigger;
  _nop_();
  IAP_clear;
}
 
void IAP_erase(unsigned int address)
{
  IAP_CONTR = IAP_WT;
  IAP_CMD = IAP_erase_command;
  IAP_address(address);
  IAP_trigger;
  _nop_();
  IAP_clear;
}

Some key points to note while using internal flash as EEPROM:

  1. The flash memory of STC micro can be divided into sectors of 512 bytes.
  2. Byte write or byte erase operations are not possible as these are done at sector level. This means that to change one byte of a sector, we would have to totally erase and write that sector.
  3. Basing on point 2, it is better to use two sectors for one set of data. We can, then, maintain wear-leveling and memory ring buffer. 
  4. We must use those locations of internal flash that won’t have any part of application code. As with other microcontrollers, STC micros begin executing code from 0x0000 location of memory. Thus, it is better to use locations at the last portion of internal flash memory.
  5. Flash memory has an endurance of 100,000 cycles. Thus, frequent writes/erases must be avoided. It is better to use a RAM buffer for applications that require frequent data storage. By using such a technique, flash memory is only modified before a reset or power down event. This is just like saving all your files before shutting down your PC. Most modern solid-state drives (SSD) come with a DRAM cache and this has almost similar usage.
  6. IAP size can be set by using STC programmer GUI.

Demo

Pages: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35

Related Posts

13 comments

Leave a Reply

Your email address will not be published. Required fields are marked *