0

I have some difficulty with EEprom. Any help is highly appreciated. I am using MPLABX ide with XC8 v2.10 compiler. I wrote a simple code that gets some text message content (Up to 50 characters) via UART and write them to EEprom for later use. I am attaching the code below.

This is my main code.

#include <xc.h>
#include <string.h>
#include <pic18f4520.h>
#include <stdio.h>
#include <stdlib.h>
#include "config.h"
#include "uart.h"
#include "EEPROM_RW.h"


#define LED PORTDbits.RD0
#define Buzzer PORTBbits.RB0

#include <xc.h>


void Configuration();
void Timer_IE();
void Timer_ID();
void UART_Flush();
int ADC_Read(int Ch);

unsigned char UART_RX[100]='\r';
unsigned char UART_Dummy=0,loop_pointer=0,EE_Pointer=0,EE_Dummy;
int counter=0;
bit LED_Blink=0,Modem_Attached=0,UART_Data_Ready;

void main(void) {
    Configuration();
    Timer_IE();
    LED=0;
    Buzzer=0;
    
    UART_Close();
    UART_Open(9600);
    UART_Flush();
    
    while(1){
        if(Modem_Attached==0){
            LED_Blink=1;
            if(UART_Data_Ready==1){
                for(UART_Dummy=0;UART_Dummy<=99;UART_Dummy++){
                    if(UART_RX[UART_Dummy]=='O'){
                        if(UART_RX[UART_Dummy+1]=='K'){
                            Modem_Attached=1;
                            UART_Data_Ready=0;
                            UART_Flush();
                        }
                    }
                    if(UART_RX[UART_Dummy]=='A'){
                        if(UART_RX[UART_Dummy+1]=='T'){
                            if(UART_RX[UART_Dummy+2]=='+'){
                                loop_pointer=UART_Dummy+3;
                                EE_Pointer=205;
                                while(1){
                                    if(UART_RX[loop_pointer]=='\0'){
                                        EEPROM_WriteByte(EE_Pointer,UART_RX[loop_pointer]);
                                        loop_pointer=0;
                                        EE_Pointer=0;
                                        UART_Write("Alarm Text Saved\r");
                                        UART_Data_Ready=0;
                                        UART_Flush();
                                        break;
                                    }
                                    else{
                                        EEPROM_WriteByte(EE_Pointer,UART_RX[loop_pointer]);
                                        loop_pointer++;
                                        EE_Pointer++;
                                        
                                    }
                                    if(EE_Pointer==255){
                                        UART_Write("Alarm Text is too long\r");
                                        EE_Pointer=0;
                                        UART_Data_Ready=0;
                                        UART_Flush();
                                        break;
                                    }
                                }
                            }
                            if(UART_RX[UART_Dummy+2]=='?'){
                                EE_Pointer=205;
                                UART_Write("Alarm Text = ");
                                while(1){
                                    EE_Dummy=EEPROM_ReadByte(EE_Pointer);
                                    if(EE_Dummy!='\0'){
                                        UART_Write_Byte(EE_Dummy);
                                        EE_Pointer++;
                                    }
                                    else{
                                        EE_Pointer=0;
                                        UART_Data_Ready=0;
                                        UART_Flush();
                                        break;
                                    }
                                    if(EE_Pointer==255){
                                        UART_Data_Ready=0;
                                        UART_Flush();
                                        break;
                                    }
                                }
                                UART_Write_Byte('\r');
                            }
                        }
                    }
                }
            }
        }
        if(Modem_Attached==1){
            LED_Blink=0;
            LED=0;
        }
    }
    
    return;
}
void Configuration()
{
    //setting the Oscillator Freq. 8MHz
    OSCCONbits.IRCF0=1;
    OSCCONbits.IRCF1=1;
    OSCCONbits.IRCF2=1;  
    
    //ADC Module Settings
    ADCON0=0x00;
    ADCON1=0b00001010; 
    ADCON2=0b10010000;
    
    //Timer 0 Interrupt Parameters     
    T0CONbits.T08BIT=1;
    T0CONbits.T0CS=0;
    T0CONbits.PSA=1;
        
    TRISD=0x00;
    TRISBbits.RB0=0;
}
void Timer_IE()
{
    INTCONbits.GIE=1; 
    INTCONbits.TMR0IE=1;     
    T0CONbits.TMR0ON=1;
}
void Timer_ID()
{
    INTCONbits.TMR0IE=0;     
    T0CONbits.TMR0ON=0;
}
void UART_Flush()
{
    unsigned char dummy, i;
    dummy=UART_RX[0];
    i=0;
    while(dummy!=0){
        UART_RX[i]=0;
        dummy=UART_RX[i+1];
        i++;
    }
    UART_RX[i+1]=0;
    i=0; 
}
int ADC_Read(int Ch)
{  
    if(Ch==0)    
        ADCON0=0x03;
    else if(Ch==1)
        ADCON0=0x07;
    else if(Ch==2)
        ADCON0=0x0B;
    else if(Ch==3)
        ADCON0=0x0F;
    else if(Ch==4)
        ADCON0=0x13;
    else
        return 0;
    while(GODONE);
    ADON=0;    
    return ADRES; 
}
__interrupt (low_priority) void tmr0(void)
{
    if(INTCONbits.TMR0IF == 1){        
            INTCONbits.TMR0IF = 0;
            if(Modem_Attached==0){
                counter++;
            }
            else{
                counter=0;
            }
    }
    if(counter>=15000){
        counter=0;
        if(Modem_Attached==0){
            UART_Write("ATZ\r");
        }
        if(LED_Blink==1){
            if(LED==0)
                LED=1;
            else
                LED=0;
        }
    }
    if(UART_RX_Ready()==1){
        Timer_ID();
        UART_Read_kemal(&UART_RX);
        UART_Data_Ready=1;
        Timer_IE();
    }
}


This is the EEPROM_RW.h

unsigned char EEPROM_ReadByte(unsigned char eepromAddress)
{
    EECON1bits.CFGS=0;
    
    while(EECON1bits.RD || EECON1bits.WR);           // check the WR&RD bit to see if a RD/WR is in progress
    EEADR=eepromAddress;       // Write the address to EEADR.
    EECON1bits.RD = 1;                    // Set the RD bit to trigger the eeprom read operation.
    return(EEDATA);            // Return the data read form eeprom.
}

void EEPROM_WriteByte(unsigned char eepromAddress, unsigned char eepromData)
{
    signed char gie_Status;
    while(EECON1bits.WR==1);            // check the WR bit to see if a previous Write operation is in progress
    EEADR=eepromAddress;  // Write the address to EEADR.
    EEDATA=eepromData;    // load the 8-bit data value to be written in the EEDATA register.
    EECON1bits.EEPGD = 0;       // select "EEPROM"
    EECON1bits.CFGS = 0;        // not the config-registers
    EECON1bits.WREN = 1;

    gie_Status = GIE;     // Copy the current Interrupt state
    GIE = 0;              // Disable the interrupts
    EECON2=0x55;          // Execute the special instruction sequence
    EECON2=0xaa;          // Refer the datasheet for more info
    EECON1bits.WR=1;                 // Set the WR bit to trigger the eeprom write operation.
    
    while(EECON1bits.WR==1);
    PIR2.EEIF = 0;
    EECON1bits.WREN = 0;
    GIE = gie_Status;     // Restore the interrupts
    
}

this is Configuration bits.


// PIC18F4520 Configuration Bit Settings

// 'C' source line config statements

// CONFIG1H
#pragma config OSC = INTIO67    // Oscillator Selection bits (Internal oscillator block, port function on RA6 and RA7)
#pragma config FCMEN = OFF      // Fail-Safe Clock Monitor Enable bit (Fail-Safe Clock Monitor disabled)
#pragma config IESO = OFF       // Internal/External Oscillator Switchover bit (Oscillator Switchover mode disabled)

// CONFIG2L
#pragma config PWRT = ON        // Power-up Timer Enable bit (PWRT enabled)
#pragma config BOREN = ON       // Brown-out Reset Enable bits (Brown-out Reset enabled and controlled by software (SBOREN is enabled))
#pragma config BORV = 3         // Brown Out Reset Voltage bits (Minimum setting)

// CONFIG2H
#pragma config WDT = OFF        // Watchdog Timer Enable bit (WDT disabled (control is placed on the SWDTEN bit))
#pragma config WDTPS = 32768    // Watchdog Timer Postscale Select bits (1:32768)

// CONFIG3H
#pragma config CCP2MX = PORTC   // CCP2 MUX bit (CCP2 input/output is multiplexed with RC1)
#pragma config PBADEN = OFF     // PORTB A/D Enable bit (PORTB<4:0> pins are configured as digital I/O on Reset)
#pragma config LPT1OSC = OFF    // Low-Power Timer1 Oscillator Enable bit (Timer1 configured for higher power operation)
#pragma config MCLRE = OFF      // MCLR Pin Enable bit (RE3 input pin enabled; MCLR disabled)

// CONFIG4L
#pragma config STVREN = OFF     // Stack Full/Underflow Reset Enable bit (Stack full/underflow will not cause Reset)
#pragma config LVP = OFF        // Single-Supply ICSP Enable bit (Single-Supply ICSP disabled)
#pragma config XINST = OFF      // Extended Instruction Set Enable bit (Instruction set extension and Indexed Addressing mode disabled (Legacy mode))

// CONFIG5L
#pragma config CP0 = OFF        // Code Protection bit (Block 0 (000800-001FFFh) not code-protected)
#pragma config CP1 = OFF        // Code Protection bit (Block 1 (002000-003FFFh) not code-protected)
#pragma config CP2 = OFF        // Code Protection bit (Block 2 (004000-005FFFh) not code-protected)
#pragma config CP3 = OFF        // Code Protection bit (Block 3 (006000-007FFFh) not code-protected)

// CONFIG5H
#pragma config CPB = OFF        // Boot Block Code Protection bit (Boot block (000000-0007FFh) not code-protected)
#pragma config CPD = OFF        // Data EEPROM Code Protection bit (Data EEPROM not code-protected)

// CONFIG6L
#pragma config WRT0 = OFF       // Write Protection bit (Block 0 (000800-001FFFh) not write-protected)
#pragma config WRT1 = OFF       // Write Protection bit (Block 1 (002000-003FFFh) not write-protected)
#pragma config WRT2 = OFF       // Write Protection bit (Block 2 (004000-005FFFh) not write-protected)
#pragma config WRT3 = OFF       // Write Protection bit (Block 3 (006000-007FFFh) not write-protected)

// CONFIG6H
#pragma config WRTC = OFF       // Configuration Register Write Protection bit (Configuration registers (300000-3000FFh) not write-protected)
#pragma config WRTB = OFF       // Boot Block Write Protection bit (Boot block (000000-0007FFh) not write-protected)
#pragma config WRTD = OFF       // Data EEPROM Write Protection bit (Data EEPROM not write-protected)

// CONFIG7L
#pragma config EBTR0 = OFF      // Table Read Protection bit (Block 0 (000800-001FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR1 = OFF      // Table Read Protection bit (Block 1 (002000-003FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR2 = OFF      // Table Read Protection bit (Block 2 (004000-005FFFh) not protected from table reads executed in other blocks)
#pragma config EBTR3 = OFF      // Table Read Protection bit (Block 3 (006000-007FFFh) not protected from table reads executed in other blocks)

// CONFIG7H
#pragma config EBTRB = OFF      // Boot Block Table Read Protection bit (Boot block (000000-0007FFh) not protected from table reads executed in other blocks)

// #pragma config statements should precede project file includes.
// Use project enums instead of #define for ON and OFF.

#include <xc.h>



  • 1
    Do you get this message -> "Alarm Text Saved" from where you connected the UART output? Do you have some debug information to provide? Since embedded systems has no visual debugging screens it is hard to seek the error in code. – Kozmotronik Nov 27 '20 at 12:19
  • Yeah I get the message Alarm Text Saved and after that when I send AT? it returns the correct text message. Whenever I unplug and plug again the data is gone. – kemal gülamber Nov 28 '20 at 19:25
  • If this is the case; the EEPROM of the chip might be reached the total number of write cycles which is approximately 1 million times typically. If not may be you have some routine that clears some eeprom areas at the start up. Have you tried your code within another same chip. Does it behave same? – Kozmotronik Nov 30 '20 at 06:30

0 Answers0