Sunday, August 23, 2020

Storing and Reading Data From Flash Memory of ATMega32

ATMega32 contains 32K bytes of internal Flash memory for program storage. The Flash is organized as 16K x 16 because all AVR instructions are 16 or 32 bits wide. For software security, the Flash Program memory space is divided into two sections, Boot Program section and Application Program section.

The Flash Memory has an endurance of at least 10,000 write/erase cycles. The ATMega32 Program Counter (PC) is 14-bit wide, thus addressing the 16K program memory locations. 

ATMega32 has 2K bytes of internal SRAM. It's far smaller than its internal Flash. For any situation that the user needs to store a large amount of constant data, it is effective to store and read those data from internal Flash. For example we need to store font data at the amount of 1K bytes. This example project uses a lot of internal SRAM since the program store font and graphic data in SRAM instead of Flash.

The AVR LibC "pgmspace.h" contains routine that declare data into Flash Memory, reading byte data from Flash Memory, etc.

Storing and Reading Data From Flash Memory of ATMega32
Program Simulation
In this example, we store 7-Segments display data in Flash memory. At run-time the program will read 7-Segment data from internal Flash. It will display on a single 7-Segment display connects to PORTC.

  1. /*
  2.  * Example_2.c
  3.  *
  4.  * Created: 7/19/2023 1:52:49 PM
  5.  * Author : Admin
  6.  */
  7.  
  8. #include <avr/io.h>
  9. #include <avr/pgmspace.h>
  10. #define F_CPU 8000000UL
  11. #include "util/delay.h"
  12.  
  13. /*Declare constant data that will store in program Flash Memory*/
  14. const char cCathode[] PROGMEM =
  15. {0x3F,0x06,0x5B,0x4F,0x66,0x6D,0x7D,0x07,0x7F,0x6F,0x77,0x7C,0x39,0x5E,0x79,0x71};
  16. const char cAnode[] PROGMEM =
  17. {0xC0,0xF9,0xA4,0xB0,0x99,0x92,0x82,0xF8,0x80,0x90,0x88,0x83,0xC6,0xA1,0x86,0x8E};
  18.  
  19. int main(void)
  20. {
  21. DDRC = 0xFF;
  22. while (1)
  23. {
  24. for (char i=0;i<16;i++)
  25. {
  26. /*Read Flash Data and assign to PORTC*/
  27. PORTC = pgm_read_byte(&cCathode[i]);
  28. _delay_ms(500);
  29. }
  30. }
  31. }
  32.  
  33.  

 

Click here to download its zip file.

3 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. did you make the 100v voltmeter?

    send me the link to see how you did it

    great video!!

    ReplyDelete
    Replies
    1. Oh, next time I will make a DVM to measure a higher voltage.

      Delete