Example C language program to read and write 24LCxx series EEPROM

Publisher:静雅心灵Latest update time:2018-03-09 Source: eefocus Reading articles on mobile phones Scan QR code
Read articles on your mobile phone anytime, anywhere

    PIC: Example C language program to read and write 24LCxx series EEPROM

    //*********************************************************  
//*            Using I2C Master Mode for aCCess Slave (EEPRM)  
 //*                
 //* Written by:  Richard Yang                                
 //*              Sr. Corporate Application Engineer            
 //*              MicroChip Technology Inc.                
 //* Date:        Oct. 3nd '2002                            
 //* Revision: 1.00 
//* Language tools : MPLAB-C18 v2.09.13 
//*      MPLINK v3.10
//*      MPLAB-IDE v6.00.17 & ICD2                                  
 //***********************************************************
/* Include Header files  */

    #i nclude
#i nclude   // Load I2C Header file from defult direct
#i nclude
#i nclude "P18LCD.h"  // Load P18LCD Header file form current working direct

    /* Declare the Function Prototype */

    void Initialize_I2C_Master(void);
void EE_Page_Write(unsigned char,unsigned char,unsigned char,unsigned char *);
void EE_SEQU_Read(unsigned char,unsigned char,unsigned char,unsigned char *);
void EEPROM_Write(unsigned char,unsigned char,unsigned char);
void EEPROM_ACK(unsigned char);
unsigned char EEPROM_Read(unsigned char,unsigned char);
void I2C_Done(void);
void Initialize_Timer2(void);
void isr_high_direct(void);
void isr_high(void);

    #pragma romdata My_romdata=0x1000
const rom far unsigned char LCD_MSG1[]="SW2: Byte Write ";
const rom far unsigned char LCD_MSG2[]="SW6: Random Read";
const rom far unsigned char LCD_MSG3[]="Byte Write Mode ";
const rom far unsigned char LCD_MSG4[]="Random Read Mode";
const rom far unsigned char LCD_MSG5[]="Sended:        ";
const rom far unsigned char LCD_MSG6[]="Send:          ";
const rom unsigned char I2C_Write_Buffer[]="Microchip Technology";
#pragma romdata

    /* Define following array in data memory */

    unsigned char I2C_Read_Buffer [32];

    /* define following variable in data memory at Access Bank */
#pragma udata access My_RAM
near unsigned char Debounce;
near unsigned char Send_Addr;
near unsigned char Send_Data;
near unsigned char Send_Length;
near unsigned char Read_Data;
near unsigned char P_SW2;
near unsigned char P_SW6;
#pragma udata

    #define Page_Length 8
#define SW2  PORTAbits.RA4
#define SW6  PORTEbits.RE1
#define Bounce_Time 6
#define EE_CMD 0xA0

    //***********************************************************
/*                */
/*      Main  Program        */
/*                */
//***********************************************************

   

     


    
void main(void)
{
 ADCON1=0b00000110;  // DISAble A/D Function
 TRISAbits.TRISA4=1;  // Set SW2 for input
 TRISEbits.TRISE1=1;  // Set SW6 for Input 
 Initialize_Timer2( );
 Initialize_I2C_Master( );
 OpenLCD( );
 
 if (SW2 & SW6)Debounce=0;
 else Debounce = Bounce_Time; 
 
 while(1)
 {
  LCD_Set_Cursor(0,0);    // Put LCD Cursor on (0,0)
  putrsLCD(LCD_MSG1); 
  LCD_Set_Cursor(1,0);    // Put LCD Cursor on (1,0)
  putrsLCD(LCD_MSG2);
 
  P_SW2=P_SW6=0; 
  Send_Addr=0;
  
  while(1)
  {
  if (P_SW2) 
  {
    P_SW2=0;
    Debounce = Bounce_Time;
    LCD_Set_Cursor(0,0);    // Put LCD Cursor on (0,0)
    putrsLCD(LCD_MSG3); 
    LCD_Set_Cursor(1,0);    // Put LCD Cursor on (0,0)
    putrsLCD(LCD_MSG5);
    
    do
    {
    while (!P_SW2);
    P_SW2=0;
    LCD_Set_Cursor(1,8);
    Send_Data=I2C_Write_Buffer[Send_Addr];
    EEPROM_Write(EE_CMD,Send_Addr,Send_Data);
    puthexLCD(EE_CMD);
    putcLCD(' ');
    puthexLCD(Send_Addr);
    putcLCD(' ');
    puthexLCD(Send_Data);
    EEPROM_ACK(EE_CMD);
    Send_Addr++;
    } while (I2C_Write_Buffer[Send_Addr]!=0x00);
  break;
  }
  
   if (P_SW6) 
  {
    P_SW6=0;
    Debounce = Bounce_Time;
    LCD_Set_Cursor(0,0);    // Put LCD Cursor on (0,0)
    putrsLCD(LCD_MSG4); 
    LCD_Set_Cursor(1,0);    // Put LCD Cursor on (0,0)
    putrsLCD(LCD_MSG6);
    
  
     while(1)
    {
    if (P_SW6)
    {
    P_SW6=0;
    LCD_Set_Cursor(1,5);
    Read_Data = EEPROM_Read(EE_CMD,Send_Addr);
    puthexLCD(EE_CMD);
    putcLCD(' ');
    puthexLCD(Send_Addr);
    putcLCD(' ');
    puthexLCD(EE_CMD);
    putcLCD(' ');
    puthexLCD(Read_Data);
    Send_Addr++;
    }
    if (P_SW2) break;
    }
    if (P_SW2) break;
  }
  if (P_SW2)
  {
    P_SW2=0;
    break;
  }
  } 
 }
}
//************************************************
//*      #pragma Interrupt Declarations        *
//*            *
//* Function: isr_high_direct                    *
//*  - Direct execution to the actual          *
//*    high-priority interrupt code.            *
//************************************************
#pragma code isrhighcode = 0x0008

    void isr_high_direct(void) 
{          
    _asm      //begin in-line assembly
    goto isr_high  //go to isr_high function
    _endasm    //end in-line assembly
}

#pragma code

     

    //************************************************
//* Function: isr_high(void)                    *    
 //*  High priority interrupt for Timer2        *        
 //************************************************
#pragma interrupt isr_high

    void isr_high(void)
{
 PIR1bits.TMR2IF=0;    // Clear Timer2 interrupt Flag
 
 if (Debounce==0)
 {
  if (!SW2)
  { P_SW2=1; Debounce =Bounce_Time; } 
  if (!SW6) 
  { P_SW6=1; Debounce =Bounce_Time; } 
 }  
 else if (SW2 & SW6)Debounce--; 
  else Debounce =Bounce_Time;    
}        
#pragma code

    //***********************************************
//*        Write a Byte to EEPROM              
 //*  - CTRl : Control Byte of EEPROM    
//*  - addr : LOCation of EEPROM    
//*  - data : Data Byte of EEPROM    
 //***********************************************

    void Initialize_Timer2(void)
{

      RCONbits.IPEN=1;  // Enable Interrupt Priority bit
  IPR1bits.TMR2IP=1;  // Set Timer2 for High Priority
  INTCONbits.GIEH=1;  // Enable High Priority Interrupt

      OpenTimer2 (TIMER_INT_ON  // Turn On the Timer2 with Interrupt
      & T2_PS_1_4  // (4MHz/4) [4*10*(99+1)] = 4mS */
      & T2_POST_1_10);

      PR2 = 99;
}

    //***********************************************
//*        Write a Byte to EEPROM              *
//*  - ctrl : Control Byte of EEPROM  *
//*  - addr : Location of EEPROM  *
//*  - data : Data Byte of EEPROM    *
//***********************************************
void EEPROM_Write(unsigned char ctrl,unsigned char addr,unsigned char data)
{
 IdleI2C();                // ensure module is idle
  StartI2C();      // Start condition
 I2C_Done();      // Wait Start condition completed and clear SSPIF flag   

     WriteI2C(ctrl);    // Write Control+Write to EEPROM & Check BF flag
 while(SSPCON2bits.ACKSTAT);  // wait until received the AcknowLEDge from EEPROM
 I2C_Done();      // Clear SSPIF flag

     WriteI2C(addr);    // Write Address to EEPROM
 while(SSPCON2bits.ACKSTAT);  // wait until received the Acknowledge from EEPROM
 I2C_Done();

     WriteI2C(data);    // Write Data to EEPROM
 while(SSPCON2bits.ACKSTAT);  // wait until received the Acknowledge from EEPROM
 I2C_Done();

     StopI2C();      // Stop condition
 I2C_Done();      // Wait the Stop condition completed
}

    //***********************************************
//*        Pae Write to EEPROM                
//*
//*  - ctrl : Control Byte of EEPROM  
//*  - addr : Location of EEPROM    
//*  - length : Write counter
//*  - *dptr : RAM point --> EEPROM
//* 
//***********************************************
void EE_Page_Write(unsigned char ctrl,unsigned char addr,unsigned char length,unsigned char *dptr)
{
 IdleI2C();                // ensure module is idle
  StartI2C();      // Start condition
 I2C_Done();      // Wait Start condition completed
 
 WriteI2C(ctrl);    // Write Control+Write to EEPROM & Check BF flag
 while(SSPCON2bits.ACKSTAT);  // wait until received the Acknowledge from EEPROM
 I2C_Done();      // Clear SSPIF flag

     WriteI2C(addr);    // Write Address to EEPROM
 while(SSPCON2bits.ACKSTAT);  // wait until received the Acknowledge from EEPROM
 I2C_Done();

     while (length!=0)    // Check write completed ?
 {
  WriteI2C(*dptr);  // Write data to EEPROM
  while(SSPCON2bits.ACKSTAT); // wait until received the Acknowledge from EEPROM
  I2C_Done(); 
  dptr++;      // Point to next byte
  length--;
 }
 
 StopI2C();      // Stop condition
 I2C_Done();      // Wait the Stop condition completed

 
//***********************************************
//*        EEPROM Acknowledge Polling          *
//*  --  The routine will polling the ACK  *
//*      response from EEPROM      *
//*  --  ACK=0 return        *
//*  --  ACK=1 send Restart & loop check  *
//***********************************************
void EEPROM_ACK(unsigned char ctrl)
{
 unsigned char i;

     IdleI2C();                // ensure module is idle
  StartI2C();      // Start condition
 I2C_Done();      // Wait Start condition completed

     WriteI2C(ctrl);    // Write Control to EEPROM (WRITE)
 I2C_Done();      // Clear SSPIF flag

     while (SSPCON2bits.ACKSTAT)  // test for Acknowledge from EEPROM
    {
  for (i=0;i<100;i++);  // Delay for next Repet-Start

          RestartI2C();              // initiate Repet-Start condition
  I2C_Done();          // Wait Repet-Start condition completed

      WriteI2C(ctrl);    // Write Control to EEPROM (WRITE)
  I2C_Done();      // Clear SSPIF flag 
    }  
  StopI2C();                      // send STOP condition
 I2C_Done();      // wait until stop condition is over 
}

    //***********************************************
//*    Random Read a Byte from EEPROM          *
//*  - ctrl : Control Byte of EEPROM (Write) *
//*    (Ctrl +1 ) : Read Command    *
//*  - addr : Address Byte of EEPROM  *
//*  - Return : Read Data from EEPROM  *
//***********************************************
unsigned char EEPROM_Read(unsigned char ctrl,unsigned char addr)
{
 unsigned char f;
 
 IdleI2C();                // ensure module is idle
  StartI2C();      // Start condition
 I2C_Done();      // Wait Start condition completed

     WriteI2C(ctrl);    // Write Control to EEPROM    
    while(SSPCON2bits.ACKSTAT);    // test for ACK condition, if received
 I2C_Done();      // Clear SSPIF flag

     WriteI2C(addr);    // Write Address to EEPROM        
     while(SSPCON2bits.ACKSTAT);  // test for ACK condition, if received  
 I2C_Done();      // Clear SSPIF flag

        RestartI2C();                // initiate Restart condition
 I2C_Done();

     WriteI2C(ctrl+1);    // Write Control to EEPROM              
 while(SSPCON2bits.ACKSTAT);    // test for ACK condition, if received
 I2C_Done();      // Clear SSPIF flag

     f=ReadI2C();    // Enable I2C Receiver & wait BF=1 until received data
 I2C_Done();      // Clear SSPIF flag

     NotAckI2C();    // Genarate Non_Acknowledge to EEPROM
 I2C_Done();    
                    
     StopI2C();                  // send STOP condition
 I2C_Done();      // wait until stop condition is over

     return(f);      // Return Data from EEPROM 
}

    //***********************************************
//*        Sequential Read from EEPROM                
//*
//*  - ctrl : Control Byte of EEPROM  
//*  - addr : Location of EEPROM    
//*  - length : Read counter
//*  - *dptr : Store EEPROM data to RAM

//* 
//***********************************************
void EE_SEQU_Read(unsigned char CTRl,unsigned char addr,unsigned char length,unsigned char *dptr)
{
 IdleI2C();                // ensure module is idle
  StartI2C();      // Start condition
 I2C_Done();      // Wait Start condition completed

     

     WriteI2C(ctrl);    // Write Control to EEPROM    
    while(SSPCON2bits.ACKSTAT);    // test for ACK condition, if received
 I2C_Done();      // Clear SSPIF flag

     WriteI2C(addr);    // Write Address to EEPROM        
     while(SSPCON2bits.ACKSTAT);  // test for ACK condition, if received  
 I2C_Done();      // Clear SSPIF flag

        RestartI2C();                // initiate Restart condition
 I2C_Done();

     WriteI2C(ctrl+1);    // Write Control to EEPROM              
 while(SSPCON2bits.ACKSTAT);    // Test for ACK condition, if received
 I2C_Done();      // Clear SSPIF flag

     while (length!=0)
 {
  *dptr=ReadI2C();  // Enable I2C Receiver & Store EEPROM data to Point buffer 
  I2C_Done(); 
  dptr++; 
  length--;
    
  if (length==0) NotAckI2C();
  else AckI2C();    // Continue read next data, send a acknowLEDge to EEPROM
  
  I2C_Done();
 }

        StopI2C();                  // send STOP condition
 I2C_Done();      // wait until stop condition is over 
}

    //***********************************************
//*  Check I2C action that is completed        *
//***********************************************
void I2C_Done(void)
{
 while (!PIR1bits.SSPIF);  // Completed the action when the SSPIF is Hi.
 PIR1bits.SSPIF=0;      // Clear SSPIF
}

    //************************************************
//* Initial I2C Master Mode with 7 bits Address  *
//* CLOCk Speed : 100KHz @4MHz      *
//************************************************
void Initialize_I2C_Master(void)
{
 OpenI2C(MASTER,SLEW_ON);
 SSPADD= 9;
}


Reference address:Example C language program to read and write 24LCxx series EEPROM

Previous article:Application of PWM applet on PIC16F877A
Next article:PIC12C508 read and write 93LC4 source program based on HI-TECH C

Recommended ReadingLatest update time:2024-11-16 14:26

I2C Communication EEPROM
1 EEPROM and its background knowledge 1.1 EEPROM (1) Some concepts: ROM, RAM, PROM (Programmable ROM), EPROM (Erasable ROM), EEPROM (Electrically Erasable ROM). (2) Why do we need EEPROM? (The ROM inside the microcontroller can only be erased and rewritten when the program is downloaded, but the program itself cannot
[Microcontroller]
EEPROM 24c02 [Read storage multiple bytes]
First store the data, then read it out and display it on the digital tube. In addition to the connections defined in the code, p0 must also be connected to pin 8 of the 8-bit digital tube.   1 /*-----------------------------------------------   2 Name: IIC protocol EEPROM24c02   3 Content: This program is used to test
[Microcontroller]
AT24CXX serial EEPROM chip reading and writing program
;Description: ;Entry conditions: data is placed in ADAT_I2C, page is placed in APAG_ IC 2, byte address is placed in AADR_I2C, if you want to write value 0FH to byte address 03H of the first page of AT24CXX, then set APAG_IC2=01H, AADR_I2C=03H, ADAT_I2C=0FH. ;Exit information: the read data is in accumulator A. ;Affec
[Microcontroller]
AVR Eeprom C language programming
//***************FileName:Capature.C***************// //***************ICCAVR compile*****************// #include io8535v.h                   #define uchar unsigned char #define uint unsigned int //Digital tube font table, corresponding to 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, F // uchar Table ={0x3f, 0x
[Microcontroller]
51 MCU IIC & EEPROM driver
#include reg52.h #include "./delay/delay.h" sbit SCL = P2^0; sbit SDA = P2^1; bit ack = 0;   unsigned char flag = 1; #define LCDPORT P0 #define LCD_WRITE_DATA 1 #define LCD_WRITE_COM 0 sbit RS = P2^4; sbit RW = P2^5; sbit E = P2^6;   #define SUCC 0 #define ERR 1   void iic_start() { SDA = 1; //Operate SDA first, th
[Microcontroller]
Application of EEPROM inside 51 MCU
STC89C51 and 52 have 2K bytes of EEPROM, 54, 55 and 58 have 16K bytes of EEPROM. STC microcontrollers use IAP technology to implement EEPROM. The internal Flash can be erased and written more than 100,000 times. Let's first introduce the differences and characteristics of ISP and IAP.     ISP: In System Programable me
[Microcontroller]
STM8S_010_I2C read and write EEPROM (hardware method)
Ⅰ Written in front I believe that those who have read the previous article "A Deeper Understanding of I2C Bus, Protocol and Application" have a certain understanding of I2C. That article is about using microcontroller IO to simulate I2C to implement read and write operations. This article will describe the hardware
[Microcontroller]
I2C bus structure EEPROM
Commonly used chip AT24C02  AT24C02 is a 2K-bit serial CMOS E2PROM, which contains 256 8-bit bytes internally. AT24C02 has an 8-byte page write buffer. The device is operated through the IIC bus interface and has a dedicated write protection function      pin  SCL serial clock  AT24C02 serial clock input pin is used t
[Microcontroller]
Latest Microcontroller Articles
  • Download from the Internet--ARM Getting Started Notes
    A brief introduction: From today on, the ARM notebook of the rookie is open, and it can be regarded as a place to store these notes. Why publish it? Maybe you are interested in it. In fact, the reason for these notes is ...
  • Learn ARM development(22)
    Turning off and on interrupts Interrupts are an efficient dialogue mechanism, but sometimes you don't want to interrupt the program while it is running. For example, when you are printing something, the program suddenly interrupts and another ...
  • Learn ARM development(21)
    First, declare the task pointer, because it will be used later. Task pointer volatile TASK_TCB* volatile g_pCurrentTask = NULL;volatile TASK_TCB* vol ...
  • Learn ARM development(20)
    With the previous Tick interrupt, the basic task switching conditions are ready. However, this "easterly" is also difficult to understand. Only through continuous practice can we understand it. ...
  • Learn ARM development(19)
    After many days of hard work, I finally got the interrupt working. But in order to allow RTOS to use timer interrupts, what kind of interrupts can be implemented in S3C44B0? There are two methods in S3C44B0. ...
  • Learn ARM development(14)
  • Learn ARM development(15)
  • Learn ARM development(16)
  • Learn ARM development(17)
Change More Related Popular Components

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

About Us Customer Service Contact Information Datasheet Sitemap LatestNews


Room 1530, 15th Floor, Building B, No.18 Zhongguancun Street, Haidian District, Beijing, Postal Code: 100190 China Telephone: 008610 8235 0740

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京ICP证060456号 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号