Lesson 35--Microduino EEPROM Reading and Writing Experiment

From Microduino Wiki
Jump to: navigation, search
Language: English  • 中文

Objective

This tutorial will teach you how to use EEPROM, the value stored in the EEPROM will be displayed on the Microduino-OLED module.

Equipment

EEPROM introduction

EEPROM (E2PROM - Electrically-Erasable Programmable Read-Only Memory) is a kind of semiconductor storage device which can electronically autotype for many times.

Microduino control chip has EEPROM, it is used widely in more area. After power off need to save the information tjat can be stored here. Such as electronic password safe.

Schematic

第三十五课-Microduino EEPROM读写实验原理图.jpg

Program

#include <avr/eeprom.h>
#include "U8glib.h"
#define EEPROM_write(address, var) eeprom_write_block((const void *)&(var), (void *)(address), sizeof(var))
#define EEPROM_read(address, var) eeprom_read_block((void *)&(var), (const void *)(address), sizeof(var))

U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE);//Define OLED connection method

//Define a config_type structure contains a int variable
struct config_type
{
  int num;
};


void draw() {
  // 1) Define structure variable config and set the initial value
  config_type config;
  config.num = random(3000);//Select a  random number in 0~3000
  // 2) Store the config to EEPROM, from the address 0 to write
  EEPROM_write(0, config);
  // 3) Read the value from address 0, then stored into config_readback
  config_type config_readback;
  EEPROM_read(0, config_readback);
  u8g.setFont(u8g_font_unifont);//font 1
  u8g.drawStr( 0, 16, "Num:");
  u8g.setFont(u8g_font_7x13);//font 2
  u8g.setPrintPos(0, 32);
  u8g.print(config_readback.num);

}

void setup()
{

}

void loop()
{
  u8g.firstPage();  
  do {
    draw();
  } 
  while( u8g.nextPage() );
  
  delay(2000);//Delay 2s
  
}

Function description:

EEPROMwrite(addr,data) writes the data to specific address. Addr is that write address, data is the written data.

EEPROM_read(addr,data) read the data from specific address. Addr is that read address, data is the read data.

random(num) uses to generate random number, the num is the range for random number.

Debug

Step 1: Copy the code to IDE and compile it

Step 2: Run program

Step 3: Observe Microduino-OLED

第三十五课-Microduino OLED显示EEPROM读写结果连接图.jpg


Result

Microduino-OLED will display the value of EEPROM, change the number every 2s.

Video

http://v.youku.com/v_show/id_XNjgxMjQ0OTIw.html