Using Internal EEPROM of PIC Microcontroller

There are commonly three types of memories in a PIC Microcontroller, Flash Program Memory, Data Memory (RAM) and EEPROM Data Memory. We write Programs in the Flash Program Memory of a microcontroller. Flash memory makes it possible to program a microcontroller many times before installing to device and even after the installation we can change the program. RAM Data Memory is used for storing data temporarily during program execution and it is volatile. That is, this memory is cleared when the power is gone or after CPU reset. RAM Data Memory locations are also called General Purpose Registers (GPR). These two memories have faster response time. The third memory is EEPROM memory which is an abbreviation for Electrically Erasable Programmable Read Only Memory. EEPROM memory can be read and write electrically, can be accessed through program. It is a non volatile memory but has slower response time. EEPROM memory can be used to store data such as sensor logs, device parameters which should not be loss during power loss or CPU reset. Here I using PIC 16F877A for explanation.

EEPROM Special Function Registers

The data in the EEPROM and Flash Program Memory can be read/write during normal operations (over full VDD range). These memories are not mapped in the register file space, instead of it can be accessed through the following six Special Function Registers (SFR) for read and write operations.

  • EECON1

  • EECON2
  • EEDATA
  • EEDATH
  • EEADR
  • EEADRH

EEDATA register hold 8-bit data for read/write and EEADR holds the address of EEPROM memory location to be accessed. PIC Microcontrollers usually have 128/256 bytes of data EEPROM memory with address ranging from 00h to FFh. On devices having 128 bytes, memory locations from 80h to FFh are unimplemented and will be wraparound to beginning of the data EEPROM memory. On-Chip charge pump (used while writing data to EEPROM) is turned off when writing these unimplemented locations. EEDATH and EEADRH registers are used when interfacing program memory block with Program Flash Memory. Here we deals only with  EEPROM data memory.

EEPROM data memory allows only single byte read and write. When a data byte is written in to EEPROM, automatically erases the particular location first and then writes the new data (erase before write). The write time is controlled by and on-chip timer and write/erase voltages are generated automatically by an on-chip charge pump. When the device is code protected, program or data memory will not be accessible to programmer but CPU may read or write data EEPROM memory.

EECON1 is the control register used for memory access.

EEPGD control bit is used to select Program Memory or Data Memory access. If it is set Program Memory is selected and vice versa. Control bits RD, WR are used to initiate read, write, erase operations. These bit cannot be cleared in program, only able to set. These bits are cleared in hardware on the completion of read or write operations.

The WREN control bit is the Write Enable bit, when set it enables write or erase operation. On power-up, the WREN bit will be automatically cleared. The WRERR bit is the Write Error Flag bit, it sets when a write (or erase) operation is interrupted by a MCLR or a WDT Time-out Reset during normal operation. In these situations, we can check the WRERR bit and rewrite the location as the data and address will be unchanged in the EEDATA and EEADR registers.

On the completion of write operation Interrupt flag bit, EEIF in the PIR2 register is set. It must be cleared in program.

EECON2 register is not a physical register and it is used exclusively in the EEPROM write sequence. Reading EECON2 will read all zeros.

Reading from Data EEPROM Memory

  1. Write the address of the memory location to be read to EEADR register.

  2. To select EEPROM data memory, clear the EEPGD control bit.
  3. Set the RD bit to initiate the read cycle.
  4. Then we can read data from EEDATA register.

Writing to Data EEPROM Memory

  1. Write the address of the memory location to write to EEADR register.

  2. Write the 8-bit data to be written in the EEDATA register.
  3. To select EEPROM data memory, clear the EEPGD control bit.
  4. Set the WREN control bit to enable write operations.
  5. Disable Interrupts if enabled in your program. (You may store interrupt register (INTCON) to enable interrupts)
  6. Then the special five instruction sequence is executed.
  7. Enable Interrupts if using.
  8. Disable the program operations by clearing WREN control bit.
  9. WR control bit is cleared and EEIF interrupt flag bit is set after completion of the write operation. EEIF bit must be cleared in the program.

MikroC Programming

Functions Developed by Us

MikroC Function to Read Data from Internal EEPROM :
unsigned char readEEPROM(unsigned char address)
{
  EEADR = address; //Address to be read
  EECON1.EEPGD = 0;//Selecting EEPROM Data Memory
  EECON1.RD = 1; //Initialise read cycle
  return EEDATA; //Returning data
}
MikroC Function to Write Data to Internal EEPROM :
void writeEEPROM(unsigned char address, unsigned char datas)
{
  unsigned char INTCON_SAVE;//To save INTCON register value
  EEADR = address; //Address to write
  EEDATA = datas; //Data to write
  EECON1.EEPGD = 0; //Selecting EEPROM Data Memory
  EECON1.WREN = 1; //Enable writing of EEPROM
  INTCON_SAVE=INTCON;//Backup INCON interupt register
  INTCON=0; //Diables the interrupt
  EECON2=0x55; //Required sequence for write to internal EEPROM
  EECON2=0xAA; //Required sequence for write to internal EEPROM
  EECON1.WR = 1; //Initialise write cycle
  INTCON = INTCON_SAVE;//Enables Interrupt
  EECON1.WREN = 0; //To disable write
  while(PIR2.EEIF == 0)//Checking for complition of write operation
  {
    asm nop; //do nothing
  }
  PIR2.EEIF = 0; //Clearing EEIF bit
}

To read or write data to EEPROM, you may use built-in MikroC Libraries or user defined functions as following.

Using MikroC EEPROM Libraries

MikroC PRO for PIC Microcontrollers provides library to work with Internal EEPROM. We can easily read/write form EEPROM using the following library functions.

  • Eeprom_Read

  • Eeprom_Write
Eeprom_Read

Prototype: unsigned short EEPROM_Read(unsigned int address);

Eeprom_Read function reads data from a specified address. Note that parameter address is of integer type, which implies that this functions supports microcontrollers with more than 256 bytes of EEPROM.  There must me atleast 20ms delay between successive using of these routines.

Eeprom_Write

Prototype: void EEPROM_Write(unsigned int address, unsigned short data);

EEPROM_Write function write data to the specified address. As I said above, since the address parameter is of integer type, it supports microcontrollers with more than 256 bytes of EEPROM. There must me atleast 20ms delay between successive using of these routines. Beware that all Interrupts will be disabled during the execution of this function.

Note: Address ranges from 00h to FFh for devices having 256 bytes while for 128 bytes devices it is 00h to 7Fh.

Mikro Code

void main()
{
  unsigned int a, i;
  TRISC = 0;
  do
  {
    for(i=0,a=1;i<8;i++)
    {
      EEPROM_Write(i, a);
      a = a<<1;
    }

    for(i=0;i<8;i++)
    {
      PORTC = EEPROM_Read(i);
      Delay_ms(1000);
    }
  }while(1);
}

Circuit Diagram

Note: VDD and VSS of the pic microcontroller is not shown in the circuit diagram. VDD should be connected to +5V and VSS to GND.

In this example we writes 00000001 to the first memory location, 00000010 to second, 000000100 to third etc sequentially up to 10000000. Then it is read sequentially and output through PORTC.

Download Here

You can download the hex file, MikroC source code, Proteus files etc here.

文章来源: https://electrosome.com/internal-eeprom-pic-microcontroller/

时间: 2024-11-13 08:12:09

Using Internal EEPROM of PIC Microcontroller的相关文章

RFID 克隆/仿真/模拟/监控/拦截/检测/嗅探器

Demo: Cloning a Verichip Yourself Download I have: schematics as PDF BOM, with Digikey part numbers board artwork (Gerbers and drill file) PIC source code (assembly for MPASM / IHEX binary) Theory of Operation Please refer to the schematic. It's impo

RFID emulator

RFID emulator The underground waste containers in my neighborhood used to have an access system based on Texas Instruments’ TIRIS RFID tags. The tags contain a 64-bit read-only identifier that is read using a low-frequency (LF) electromagnetic field

游戏开发中,图片资源的精简

在游戏开发中,包的大小总是与图片资源的大小密切相关,而图片资源中,大多为带有透明度信息的png图像. 那么,如何精简png图片资源呢? 1.图像压缩是一种方法,然而随着压缩率的增大.图片品质也越来越差.(舍弃) 2.我们另辟蹊径,采用png图像拆分.(近乎无损,资源精简) 一.原理:将png图像转化为两张jpeg图像进行存储 pngSplit下载 pngSplit使用说明 二.使用方法: 1.LibGdx中,通过Pixmap使用 // 如工程目录assets/texture/0_1.jpeg下:

译android framework层的资源文件

1.将资源放入 frameworks/base/core/res/res/ 中的相应目录,假设要添加的资源是 drawable 类型,文件名为 test(后缀可能为xml或者png等等),则将文件放入 frameworks/base/core/res/res/drawable*/ 下. 2.仿照已有的同类型系统资源修改 framework/base/core/res/res/values/public.xml,public.xml中有两种类型的资源描述,一种是<java-symbol/>系统私

atmega128 bootloader程序在IAR-AVR下 linker文件的配置及原因

第一步:atmega128的片内flash分区描述在atmega128 datasheet的275页中有分区描述 对应的在284页中,有具体的应用区与boot区的大小设置 注意:Byte address = word address * 2这里的BOOT区首地址,$F000 = 0x1E000 根据手册中的描述,我们使用JTAG MKII 烧写器通过软件 Avr Studio 4,配置熔丝位 BOOTSZ为00 注意:这里面的4096 words = 8K bytes 第二步:说明一下linke

How to solve Digiprog3 Odometer Programmer Can&#39;t Read

Yesterday, I just got the newest Digiprog III for BW X5 to diagnose LCM, EWS & Key Programming Via EWS . Well packed, but unfortunately, it cannot be used to read EEPROM. I used OBD ST01 & ST04 cables (contained in Digiprog III odometer master ent

PIC XC8 EEPROM操作

要做一个报警功能的东东,要求可以通过遥控来改变遥控内容.由于对系统的稳定性要求很高,所以用了看门狗. 可是看门狗复位会引起所有寄存器重置,恢复到默认状态.遥控要改变的内容也被复位了,所以只能借助EEPROM来存储报警标志了. 我用的是MPLAB X IDE +XC8 编译器,XC8在语法上跟PICC有点不一样 EEPROM操作有两种方法,在该存储空间中定义命名变量,或者使用块访问程序来读取或写入EEPROM. 1. EEPROM 对象 __eeprom限定符可用于指示应将变量定位到EEPROM中

PIC振荡器配置与时钟切换

单片机编程就是C语言+振荡器配置+寄存器设置. 以前对PIC振荡器的配置都是拿来主义,把别人的代码拿过来用就行了.这两天特意研究下振荡器的配置与时钟切换.在mplab IDE和C30编译器下,针对PIC24FJxx系列单片机完成的测试. 配置振荡器最主要的目的就是为了设置机械时钟Fosc,此时钟给CPU和外设提供时钟源.但为了降低功耗又不中断外设正常通信,此系列PIC保证CPU与外设的时钟同步情况下,增加了打盹模式,用于降低CPU运行时钟速度,以达到节能效果. 个人是这么理解的,CPU时钟就是代

PIC单片机该怎么加密

见过很多初学者对PIC16系列单片机的ID码的读和写犯迷糊.说实话,这方面的资料也不怎么全.有些教材可能会涉及ID区域的介绍,可是往往是一代而过.这对初学者来说是比较容易犯错的地方.今天有空,对相关的问题作了一些整理,分享给大家.可能有些地方说的不够妥当,欢迎大家多多探讨. 1.什么是ID区域? ID区域是独立于EEPROM,FLASH,RAM的区域.程序设计人员可以利用该区域存放软件的版本号,编写日期,烧录日期,产品标识等信息.不过该区域很小PIC16系列只能存放4个字节. 2.ID码怎么烧录