Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
    • Design Guides
      • WiFi & the IOT Design Guide
      • Microcontrollers Design Guide
      • State of the Art Inductors Design Guide
  • Women in Engineering

How to use inbuilt EEPROM of PIC18F4550 Microcontroller- (Part 18/25)

By Amit Joshi

The EEPROM (ELECTRICALLY ERASABLE PROGRAMMABLE READ ONLY MEMORY) is a very useful memory which can be used for storing data. The data storing and retrieving from any EEPROM memory is very simple compared to other kind of memories. As the name suggest the memory is electrically programmable and hence the data will be sustained in the memory until it is electrically erased or reprogrammed.

There are lots of EEPROM chips available most of them are easy to interface with a microcontroller. It would be even better if the microcontroller itself has a built-in EEPROM. The microcontroller used in this project is PIC18F4550 and it also has an inbuilt EEPROM memory other than the flash memory. The data can be easily stored into or retrieve using simple codes.

 

 

 

 


 

The project mainly has two parts, a serial communication part and EEPROM accessing part. The data which should be written into the EEPROM is received from the serial port. The internal hardware module USART is used for this purpose. The data which is read from the EEPROM is also send back to the serial port using the same hardware module.

 

Block Diagram OF communication between USART and EEPROM in PIC18F4550 Microcontroller

Fig. 2: Block Diagram OF communication between USART and EEPROM in PIC18F4550 Microcontroller

Hence two internal modules of the PIC18F4550 are used in this project namely EEPROM and USART. These hardware modules can be accessed and controller by simply writing into or reading from their corresponding registers.

The important control registers associated with the USART and their details are given below:

Name and Details of control Registers asociated with USART

Fig. 3: Name and Details of control Registers asociated with USART

 TXSTA: TRANSMIT STATUS AND CONTROL REGISTER

This register has the bits which controls the serial transmission features like enable or disable the transmission, whether it should be synchronous asynchronous etc.

For a simple asynchronous serial transmission only two bits are significant, which are TXEN and SYNC

TXEN is the transmission enable/disable bit and SYNC sets the synchronous/asynchronous mode.

TXEN: Transmit Enable

1 = Transmit enabled

0 = Transmit disabled

SYNC: EUSART Mode Select bit

1 = Synchronous mode

0 = Asynchronous mode

RCSTA: RECEIVE STATUS AND CONTROL REGISTER

This register has the bits which controls the serial reception features like enable/disable the serial port, reception should be 8 bit or 9 bit, whether it should be synchronous asynchronous, enable/disable continuous reception etc.

For a simple asynchronous serial reception the significant bits are SPEN, RX9 and CREN.

SPEN: Serial Port Enable bit

1 = Serial port enabled

0 = Serial port disabled

RX9: 9-Bit Receive Enable bit

1 = Selects 9-bit reception

0 = Selects 8-bit reception

CREN: Continuous Receive Enable bit

1 = Enables receiver

0 = Disables receiver

SPBRG: EUSART BAUD RATE GENERATOR REGISTER

This is a 16 bit register into which the value corresponding to the required baud-rate can be written into. The value according to a particular baud-rate can be calculated from the CPU clock frequency according to the following equation;

SPBRG = ((FOSC/Desired Baud Rate)/64) – 1

Where; FOSC is the CPU clock frequency

The UART hardware module can be initialized to a particular baud-rate with transmission and reception enabled can be done in the following steps:

 

Flow Chart for Initializing UART hardware module in PIC Microcontroller

Fig. 4: Flow Chart for Initializing UART hardware module in PIC Microcontroller

 Once the UART module has been initialized then the data can be read or write from the RCREG and TXREG registers respectively.

The steps required to receive data from the serial port is shown in the following figure:

  

Flow Chart to Receive Data From Serial Port to use inbuilt EEPROM in PIC18F4550

Fig. 5: Flow Chart to Receive Data From Serial Port to use inbuilt EEPROM in PIC18F4550

 The steps required to transmit data to the serial port is shown in the following figure:

 

Flow Chart to Transmit Data From Serial Port

Fig. 6: Flow Chart to Transmit Data From Serial Port

 

The important control associated with the EEPROM is the EECON1register and its details of the important bits are given below;

 

EECON1 Register and important bits of EEPROM in PIC18F4550

Fig. 7: EECON1 Register and important bits of EEPROM in PIC18F4550

 EEPGD: Flash Program or Data EEPROM Memory Select bit

1 = Access Flash program memory

0 = Access data EEPROM memory

CFGS: Flash Program/Data EEPROM or Configuration Select bit

1 = Access Configuration registers

0 = Access data EEPROM memory

WREN: Flash Program/Data EEPROM Write Enable bit

1 = Allows data write into the EEPROM

0 = Inhibits data write into the EEPROM

WR: Write Control bit

1 = Initiates an EEPROM write

0 = When read as 0 it indicates that the write cycle to the EEPROM is complete

RD: Read Control bit

1 = Initiates an EEPROM read

0 = When read as 0 it indicates that the read cycle to the EEPROM is complete

The steps required to read a data from the EEPROM is shown in the following figure:

  

Flow Chart For Reading Data From EEPROM in PIC18F4550 Microcontroller

Fig. 8: Flow Chart For Reading Data From EEPROM in PIC18F4550 Microcontroller

The exact steps which are shown in the below figure should be followed to get a successful EEPROM write;

 

Flow Chart Of EEPROM Write in PIC18F4550 Microcontroller

Fig. 9: Flow Chart Of EEPROM Write in PIC18F4550 Microcontroller

 

Things to care:

· The baud rate of the serial communication depends on the clock frequency that the CPU receives. The calculation for the value to be written into the    â€˜SPBRG’ should be made based on that exact frequency.

       SPBRG = ( ( FOSC / Desired Baud Rate) / 64 ) – 1

Always make sure that the frequency received by the CPU is a round figure, like 4 MHz, 8 MHz, and 12 MHz etc. If using an external crystal, a slight shift in the frequency or a bad connection with the crystal and decoupling capacitor etc. can cause a data error in transmission or reception. Hence it is recommended to use the internal oscillator of a microcontroller which will reduce the circuit and provide a stable round figured frequency; in case of PIC18F455 the maximum internal frequency is 8 Mhz.

· Disable all the interrupts of the microcontroller before initiating an EEPROM read

· The EEPROM writing is possible only when the register EECON2  is first written with a value of 0x55 and then with a value of 0xAA, before initiating an EEPROM write. No delay is necessary between these writing steps.

· The EEPROM writing will be successful only through the steps given in the flow diagram of EEPROM write.

Project Source Code

###




#include <p18f4550.h>


//======================= chip config ===================//

#pragma config PLLDIV = 1          

#pragma config CPUDIV = OSC1_PLL2                              

#pragma config FOSC = INTOSC_HS    

#pragma config USBDIV = 1          

#pragma config IESO = OFF        

#pragma config PWRT = OFF        

#pragma config BOR = OFF        

#pragma config VREGEN = OFF  

#pragma config WDT = OFF     

#pragma config WDTPS = 32768     

#pragma config CCP2MX = ON       

#pragma config PBADEN = OFF      

#pragma config LPT1OSC = OFF         

#pragma config MCLRE = ON            

#pragma config STVREN = ON       

#pragma config LVP = OFF     

#pragma config ICPRT = OFF       

#pragma config XINST = OFF       

#pragma config DEBUG = OFF

#pragma config WRTD = OFF

//======================= chip config ===================//


void tx_data(unsigned char data);

unsigned char rx_data ( void );

void delay_ms ( int delay );

void eeprom_write ( unsigned char data, int address );

unsigned char eeprom_read ( int address );


unsigned char read_address = 0;

unsigned char write_address = 0;

unsigned char write_data;

unsigned char read_data;

unsigned int i=0;


#define FREQ 8000000     // CPU Frequency 

#define baud 9600

#define spbrg_value (((FREQ/64)/baud)-1)     // Refer to the formula for Baud rate calculation in Description tab


void main()

{

OSCCON = 0x72; // set CPU Frequency as 8 MHz

TRISB = 0x00; // Set PORTB as output PORT

LATB = 0xFF;      // Set PORTB high initially (All LEDs on)


    SPBRG = spbrg_value;                          // Fill the SPBRG register to set the Baud Rate

    RCSTA |= 0x80;                                      // To activate Serial port (TX and RX pins)

    TXSTA |= 0x20;                                      // To enable transmission

    RCSTA |= 0x10;                                      // To enable continuous reception

    

while(1)

{

   write_data = rx_data ();                     // Receive data from PC

            tx_data ( write_data );                     // Transmit the same data back to PC

        

            if ( write_data == 0x1A )

            {

     delay_ms ( 100 );

              for ( read_address = 0; read_address < 256; read_address ++ ) // read out all the data from 0 to 256

              {

                  read_data =  eeprom_read ( read_address ); 

                  tx_data ( read_data );          // Transmit the same data back to PC  

                  delay_ms ( 200 );

              }

              while ( 1 );

          }

          else

          {

              eeprom_write ( write_data, write_address ); // store the received data into the eeprom

              write_address ++;

          }

    }


}


void eeprom_write ( unsigned char data, int address )

{

EEADR = address; // wite the required address

EEDATA = data; // write the data

EECON1 &= 0x3F; // EEPGD = 0, CFGS = 0, all other bits are kept unchanged

EECON1 |= 0x04; // WREN = 1, all other bits are kept unchanged

INTCONbits.GIE = 0; // disable all interrupt

EECON2 = 0x55; // should write this value before initializing write

EECON2 = 0xAA; // should write this value before initializing write

EECON1 |= 0x02; // WR = 1, all other bits are kept unchanged

INTCONbits.GIE = 1; // enable interrupts

while ( EECON1 & 0x20 ); // wait till WR becomes 0

EECON1 &= 0xFB; // WREN = 0, all other bits are kept unchanged

}


unsigned char eeprom_read ( int address )

{

unsigned char data;


EEADR = address; // wite the required address

EECON1 &= 0x3F; // EEPGD = 0, CFGS = 0, all other bits are kept unchanged

EECON1 |= 0x01; // RD = 1, all other bits are kept unchanged

while ( EECON1 & 0x01 ); // wait till RD becomes 0

data = EEDATA; // read the data from the data register


return data;

}


void tx_data( unsigned char data )

{

    TXREG = data;                                              // Store data in Transmit register

    while ( ( PIR1 & 0x10 ) == 0 );                    // Wait until TXIF gets low

}



unsigned char rx_data( void )

{

    unsigned char data;


    while ( ( PIR1 & 0x20 ) == 0 ); // Wait until RXIF gets low

    data = RCREG;                                    // Retrieve data from reception register

    return data;

}


void delay_ms ( int delay )

{

int ms, i;


for ( ms = 0; ms < delay; ms ++ )

for ( i = 0; i < 10; i ++ );

}

###

 


Circuit Diagrams

Circuit-Diagram-of-How-to-use-inbuilt-EEPROM-of-PIC18F4550-Microcontroller

Project Components

  • Capacitor
  • MAX232
  • PIC18F4550
  • Resistor

Project Video

t;

 


Filed Under: PIC Microcontroller
Tagged With: max 232, microcontroller, pic18f4550
 

Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


Featured Tutorials

  • Introduction to Brain Waves & its Types (Part 1/13)
  • Understanding NeuroSky EEG Chip in Detail (Part 2/13)
  • Performing Experiments with Brainwaves (Part 3/13)
  • Amplification of EEG Signal and Interfacing with Arduino (Part 4/13)
  • Controlling Led brightness using Meditation and attention level (Part 5/13)
  • Control Motor’s Speed using Meditation and Attention Level of Brain (Part 6/13)

Stay Up To Date

Newsletter Signup

Sign up and receive our weekly newsletter for latest Tech articles, Electronics Projects, Tutorial series and other insightful tech content.

EE Training Center Classrooms

EE Classrooms

Recent Articles

  • What are the battery-selection criteria for low-power design?
  • Key factors to optimize power consumption in an embedded device
  • EdgeLock A5000 Secure Authenticator
  • How to interface a DS18B20 temperature sensor with MicroPython’s Onewire driver
  • Introduction to Brain Waves & its Types (Part 1/13)

Most Popular

5G 555 timer circuit 8051 ai Arduino atmega16 automotive avr bluetooth dc motor display Electronic Part Electronic Parts Fujitsu ic infineontechnologies integratedcircuit Intel IoT ir lcd led maximintegratedproducts microchip microchiptechnology Microchip Technology microcontroller microcontrollers mosfet motor powermanagement Raspberry Pi remote renesaselectronics renesaselectronicscorporation Research samsung semiconductor sensor software STMicroelectronics switch Technology vishayintertechnology wireless

RSS EDABOARD.com Discussions

  • Slope compensation ramp calculation for UCC38084
  • Parts storage / inventory best practices and organization
  • Unusual gap shape of ETD59 ferrite core?
  • Vco cadencd
  • WH-LTE-7S1 GSM module and SIM card problem

RSS Electro-Tech-Online.com Discussions

  • HV Diodes
  • intro to PI
  • Very logical explanation on low calue C3
  • Need help working with or replacing a ferrite tube
  • Help wanted to power an AC120v induction motor ( edited from Brushless motor - thank you @SHORTBUS= )
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • About Us
  • Contact Us
  • Advertise

Copyright © 2022 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy | Advertising | About Us

Search Engineers Garage

  • Projects and Tutorials
    • Electronic Projects
      • 8051
      • Arduino
      • ARM
      • AVR
      • PIC
      • Raspberry pi
      • STM32
    • Tutorials
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Products News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • Digi-Key Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • EE Resources
    • DesignFast
    • LEAP Awards
    • Oscilloscope Product Finder
    • White Papers
    • Webinars
  • EE Learning Center
    • Design Guides
      • WiFi & the IOT Design Guide
      • Microcontrollers Design Guide
      • State of the Art Inductors Design Guide
  • Women in Engineering