Using Internal EEPROM of PIC Microcontroller

来源:http://www.cnblogs.com/harleygwak1206/archive/2016/08/30/5821557.html
-Advertisement-
Play Games

There are commonly three types of memories in a PIC Microcontroller, Flash Program Memory, Data Memory (RAM) and EEPROM Data Memory. We write Programs... ...


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/


您的分享是我們最大的動力!

-Advertisement-
Play Games
更多相關文章
  • 介紹 接下來幾篇文章會詳細介紹幾種常用的存儲引擎及適合場景。這篇文章就先來大概瞭解一下mysql的存儲引擎。 參考說明:mysql5.7 查看支持的存儲引擎 mysql> show engines \G; *************************** 1. row ************ ...
  • 利用HAProxy代理SQL Server的AlwaysOn輔助副本 公司最近資料庫升級到SQL Server2014 ,並部署了alwayson高可用集群 機房內有三套程式需要讀取資料庫 第一套:主程式,讀寫資料庫,連接主副本 第二套:報表程式,讀報表,連接輔助副本 第三套:歷史庫程式,讀歷史庫, ...
  • 1. sql語句查詢某位數字或者某幾位數字開頭的數據,欄位類型為數字類: 2. sql搜索以4開頭和含有李字的數據: ...
  • 在一些複雜的Linux維護工作中,大量重覆的輸入和交互操作不但費時費力,容易出錯.這時候就需要用到腳本。 編寫腳本的好處: 批量的處理,自動化的完成維護,減輕管理員的負擔。 linux的shell腳本是一種特殊的應用程式,常見的shell解釋器有很多種,使用不同的shell時期內部指令:cat /e ...
  • LVS介紹 lvs 核心ipvs Ipvs(IP Virtual Server)是整個負載均衡的基礎,如果沒有這個基礎,故障隔離與失敗切換就毫無意義了。Ipvs 具體實現是由ipvsadm 這個程式來完成,因此判斷一個系統是否具備ipvs 功能,只需要察看ipvsadm 程式是否被安裝。察看ipvs ...
  • 1. 安裝libevent 2. 安裝memcached 3. 安裝memagent 3-1。修改Makefile 3-2。修改ketama.h 3-3.安裝memagent 1 make 2 ln -i /usr/local/magent/magent /usr/bin/magent 4. 使用m ...
  • 1.U-Boot,全稱 Universal Boot Loader,是遵循GPL條款的開放源碼項目。U-Boot的作用是系統引導。U-Boot從FADSROM、8xxROM、PPCBOOT逐步發展演化而來。其源碼目錄、編譯形式與Linux內核很相似,事實上,不少U-Boot源碼就是根據相應的Linu ...
  • ...
一周排行
    -Advertisement-
    Play Games
  • 移動開發(一):使用.NET MAUI開發第一個安卓APP 對於工作多年的C#程式員來說,近來想嘗試開發一款安卓APP,考慮了很久最終選擇使用.NET MAUI這個微軟官方的框架來嘗試體驗開發安卓APP,畢竟是使用Visual Studio開發工具,使用起來也比較的順手,結合微軟官方的教程進行了安卓 ...
  • 前言 QuestPDF 是一個開源 .NET 庫,用於生成 PDF 文檔。使用了C# Fluent API方式可簡化開發、減少錯誤並提高工作效率。利用它可以輕鬆生成 PDF 報告、發票、導出文件等。 項目介紹 QuestPDF 是一個革命性的開源 .NET 庫,它徹底改變了我們生成 PDF 文檔的方 ...
  • 項目地址 項目後端地址: https://github.com/ZyPLJ/ZYTteeHole 項目前端頁面地址: ZyPLJ/TreeHoleVue (github.com) https://github.com/ZyPLJ/TreeHoleVue 目前項目測試訪問地址: http://tree ...
  • 話不多說,直接開乾 一.下載 1.官方鏈接下載: https://www.microsoft.com/zh-cn/sql-server/sql-server-downloads 2.在下載目錄中找到下麵這個小的安裝包 SQL2022-SSEI-Dev.exe,運行開始下載SQL server; 二. ...
  • 前言 隨著物聯網(IoT)技術的迅猛發展,MQTT(消息隊列遙測傳輸)協議憑藉其輕量級和高效性,已成為眾多物聯網應用的首選通信標準。 MQTTnet 作為一個高性能的 .NET 開源庫,為 .NET 平臺上的 MQTT 客戶端與伺服器開發提供了強大的支持。 本文將全面介紹 MQTTnet 的核心功能 ...
  • Serilog支持多種接收器用於日誌存儲,增強器用於添加屬性,LogContext管理動態屬性,支持多種輸出格式包括純文本、JSON及ExpressionTemplate。還提供了自定義格式化選項,適用於不同需求。 ...
  • 目錄簡介獲取 HTML 文檔解析 HTML 文檔測試參考文章 簡介 動態內容網站使用 JavaScript 腳本動態檢索和渲染數據,爬取信息時需要模擬瀏覽器行為,否則獲取到的源碼基本是空的。 本文使用的爬取步驟如下: 使用 Selenium 獲取渲染後的 HTML 文檔 使用 HtmlAgility ...
  • 1.前言 什麼是熱更新 游戲或者軟體更新時,無需重新下載客戶端進行安裝,而是在應用程式啟動的情況下,在內部進行資源或者代碼更新 Unity目前常用熱更新解決方案 HybridCLR,Xlua,ILRuntime等 Unity目前常用資源管理解決方案 AssetBundles,Addressable, ...
  • 本文章主要是在C# ASP.NET Core Web API框架實現向手機發送驗證碼簡訊功能。這裡我選擇是一個互億無線簡訊驗證碼平臺,其實像阿裡雲,騰訊雲上面也可以。 首先我們先去 互億無線 https://www.ihuyi.com/api/sms.html 去註冊一個賬號 註冊完成賬號後,它會送 ...
  • 通過以下方式可以高效,並保證數據同步的可靠性 1.API設計 使用RESTful設計,確保API端點明確,並使用適當的HTTP方法(如POST用於創建,PUT用於更新)。 設計清晰的請求和響應模型,以確保客戶端能夠理解預期格式。 2.數據驗證 在伺服器端進行嚴格的數據驗證,確保接收到的數據符合預期格 ...