Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

PIC16F886 eeprom code

Status
Not open for further replies.

ajit_nayak87

Member level 5
Member level 5
Joined
Oct 30, 2017
Messages
86
Helped
1
Reputation
2
Reaction score
1
Trophy points
8
Activity points
981
Dear all,

I am looking for example sample code to read and write internal eeprom using PIC16F886. Kindly suggest me sample code here.
I have used Mplab IDE 8.86 with hitech compiler v 9.83. Here is my code I could able to read only 1 from memory location 0X10 in debug mode
Code:
#include <pic.h>
#include <htc.h>
#include "delay.h"
__CONFIG(WDTE_OFF & PWRTE_ON & CP_OFF & BOREN_ON & LVP_OFF & CPD_OFF & DEBUG_ON);
unsigned char Prg_setting[10];
unsigned char INTYPE[2];
unsigned char OUTTYPE[2];
#define LED RC2

unsigned char Write_flag=0;

void Timer1_Interrupt() {
 INTCON = 0b00000000;
 PIE1=0b00000001;
 PIR1=0x01;
 TMR1H=0xFD;
 TMR1L=0xE8;
 T1CON=0x31;

}



void eeprom_readvalue() {

 Prg_setting[0]=eeprom_read(0x10);
 Prg_setting[1]=eeprom_read(0x11);
 Prg_setting[2]=eeprom_read(0x12);
 Prg_setting[3]=eeprom_read(0x13);
}



void Init_Controller() {

 OSCCON=0b01110001;
 PIE2=0b00010000;
 PIR2=0b00000000;
 CM1CON0 = 0b00000000;
 CM2CON0 = 0b00000000;
 TRISC = 0x00;
 PORTC =0x00;

 TRISB=0X00;
 PORTB = 0X00;

 TRISA=0x10;
 PORTA=0x00;

 ADCON0= 0x00;
 ANSEL = 0b00000000;
 ANSELH=0b00000000;
 IOCB=0b00000000;
 WPUB=0b00000000;

 Timer1_Interrupt();

}




void WRITE_EEPROM() {
 GIE=0;
 Prg_setting[0]=0X01;
 Prg_setting[1]=0X0A;
 Prg_setting[2]=0X0B;
 Prg_setting[3]=0X0C;
 eeprom_write(0X10,Prg_setting[0]);
 eeprom_write(0X11,Prg_setting[1]);
 eeprom_write(0X12,Prg_setting[2]);
 eeprom_write(0X13,Prg_setting[3]);
 GIE=1;
}




void interrupt isr(void) {

 asm("CLRWDT");

 if(TMR1IF == 1) {
  TMR1IF =0;
  TMR1H=0xFD;
  TMR1L=0xE8;
  LED=!LED;
  // Write_flag=0;


 }
}


void main(void) {
 Init_Controller();
 GIE=1;
 PEIE=1;
 TMR1IE=1;
 eeprom_readvalue();
 Write_flag=1;
 while(1) {
  if(Write_flag==1) {
   Prg_setting[0]=0X01;
   Prg_setting[1]=0X0A;
   Prg_setting[2]=0X0B;
   Prg_setting[3]=0X0C;

   eeprom_write(0X10,Prg_setting[0]);
   eeprom_write(0X11,Prg_setting[1]);
   eeprom_write(0X12,Prg_setting[2]);
   eeprom_write(0X13,Prg_setting[3]);
   Write_flag=0;
   eeprom_readvalue();
  }
 }

}
 

When eeprom writing or reading has to be done the GIE bit has to be cleared or global interrupt should be disabled.
 

Hi,

Reading/writing EEPROM is a very basic task, only the microcontroller (no external device) is involved.
Thus the first you need to do is: Read the datasheet.
It explains what you have to take care for.
It even gives code examples.
Code:
BCF INTCON, GIE ;Disable INTs.
BTFSC INTCON, GIE ;SEE AN576
This is from datasheet. Even if you don't write your code in assembler you should be able to understand it.
--> GIE bit
--> AN576
I assume the manufacturer gives additional information at their internet sites. In best case C# examples or even libraries.

Did you look for those informations?

Klaus
 

You don't need to disable interrupts when reading EEPROM but you must do it while writing. When reading there is no time delay, the EEPROM contents are available at the next instruction cycle but the timing during a write operation must be maintained and of course an interrupt could mess with that.

I'm not sure what the compiler internal library routines do, the versions mentioned are quite old and out of date.

Brian.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top