(memory storing) help needed with programming of PIC18F45K20

Thread Starter

reneeqps

Joined Dec 27, 2012
2
hi guys may i know how can i store the inputs i keyed in and still can display it after i restart the device. for example: i store ABC then i switch off, when i switch on again and press display it will show ABC without having me to key in all over again. now it would reset automatically everytime i restart the device. thank you so much!



Rich (BB code):
#include "p18f45K20.h"
#include "delays.h"

int Cases = 0;
int Letter = 0;
int Letter2 = 0;
int Store = 0;
int Seq = 0;
int Restore = 0;
int StorageArray[];
/* Delays 4Mhz
Delay1TCY (); // gives a delay of 1 us (implemented as a single NOP instruction).
Delay10TCYx (60); // gives a delay of 10 x 60 x 1us = 600us
Delay100TCYx (60); // gives a delay of 100 x 120 x 1us = 60ms 
Delay1KTCYx (60); // gives a delay of 1000 x 240 x 1us = 600ms 
Delay10KTCYx (60); // gives a delay of 10000 x 25 x 1us = 6sec
*/
//Start of Program ~ flow chart(1)

rom char Sequences[26][1] = 
{    0x01,//A
    0x02,//B
    0x04,//C
    0x08,//D
    0x10,//E
    0x20,//F
    0x40,//G
    0x80,//H
    0x03,//I
    0x06,//J
    0x0C,//K
    0x18,//L
    0x30,//M
    0x60,//N
    0xC0,//O
    0x07,//P
    0x0E,//Q
    0x1C,//R
    0x38,//S
    0x70,//T
    0xE0,//U
    0x0F,//V
    0x1E,//W
    0x3C,//X
    0x78,//Y
    0xF0,//Z
};
//Declaring All the Words
rom char Characters[52][7] = 
{        0x00, 0x00, 0x7F, 0x88, 0x88, 0x88, 0x7F, //A
        0x00, 0x00,    0xFF, 0x91, 0x91, 0x91, 0x6E, //B
        0x00, 0x00, 0x7E, 0x81, 0x81, 0x81, 0x66, //C
        0x00, 0x00, 0xFF, 0x81, 0x81, 0x42, 0x3C, //D
        0x00, 0x00, 0xFF, 0x91, 0x91, 0x91, 0x91, //E
        0x00, 0x00, 0xFF, 0x90, 0x90, 0x90, 0x90, //F
        0x00, 0x00, 0x7E, 0x81, 0x89, 0x89, 0x4E, //G
        0x00, 0x00, 0xFF, 0x10, 0x10, 0x10, 0xFF, //H
        0x00, 0x00, 0x81, 0x81, 0xFF, 0x81, 0x81, //I
        0x00, 0x00, 0x87, 0x81, 0xFF, 0x80, 0x80, //J
        0x00, 0x00, 0xFF, 0x10, 0x28, 0x44, 0x83, //K
        0x00, 0x00, 0xFF, 0x01, 0x01, 0x01, 0x01, //L
        0x00, 0x00, 0x7F, 0x80, 0x7C, 0x80, 0x7F, //M
        0x00, 0xFF, 0x40, 0x30, 0x0C, 0x02, 0xFF, //N
        0x00, 0x00, 0x7E, 0x81, 0x81, 0x81, 0x7F, //O
        0x00, 0x00, 0xFF, 0x88, 0x88, 0x88, 0x60, //P
        0x00, 0x00, 0x7C, 0x82, 0x82, 0x82, 0x7D, //Q
        0x00, 0x00, 0xFF, 0x98, 0x94, 0x92, 0x61, //R
        0x00, 0x00, 0x66, 0x91, 0x91, 0x91, 0x4E, //S
        0x00, 0x00, 0x80, 0x80, 0xFF, 0x80, 0x80, //T
        0x00, 0x00, 0xFF, 0x01, 0x01, 0x01, 0xFF, //U
        0x00, 0x00, 0xF8, 0x06, 0x01, 0x06, 0xF8, //V
        0x00, 0x00, 0xFE, 0x01, 0x1E, 0x01, 0xFE, //W
        0x00, 0x00, 0xC7, 0x28, 0x10, 0x28, 0xC7, //X
        0x00, 0x00, 0xC0, 0x30, 0x0F, 0x30, 0xC0, //Y
        0x00, 0x00, 0x83, 0x85, 0x89, 0x91, 0xE1, //Z
        0x00, 0x00, 0x17, 0x15, 0x15, 0x1F, 0x00, //a
        0x00, 0x00, 0x7F, 0x09, 0x09, 0x09, 0x09, //b
        0x00, 0x00, 0x0E, 0x09, 0x09, 0x09, 0x0A, //c
        0x00, 0x00, 0x0F, 0x09, 0x09, 0x7F, 0x00, //d
        0x00, 0x00, 0x0E, 0x15, 0x15, 0x15, 0x0D, //e
        0x00, 0x00, 0x08, 0x08, 0x7F, 0x48, 0x48, //f
        0x00, 0x00, 0x00, 0x3B, 0x29, 0x29, 0x3F, //g
        0x00, 0x00, 0x00, 0x3F, 0x04, 0x04, 0x07, //h
        0x00, 0x00, 0x00, 0x00, 0x2F, 0x00, 0x00, //i
        0x00, 0x00, 0x03, 0x01, 0x2F, 0x00, 0x00, //j
        0x00, 0x00, 0x3F, 0x08, 0x14, 0x23, 0x3F, //k
        0x00, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00, //l
        0x00, 0x00, 0x1F, 0x20, 0x18, 0x20, 0x1F, //m
        0x00, 0x00, 0x3F, 0x20, 0x20, 0x3F, 0x00, //n
        0x00, 0x00, 0x1E, 0x21, 0x21, 0x1E, 0x00, //o
        0x00, 0x00, 0x3F, 0x28, 0x28, 0x38, 0x00, //p
        0x00, 0x00, 0x38, 0x28, 0x28, 0x3F, 0x00, //q
        0x00, 0x00, 0x00, 0x1F, 0x20, 0x20, 0x20, //r
        0x00 ,0x00, 0x00, 0x1D, 0x15, 0x15, 0x17, //s
        0x00, 0x00, 0x00, 0x08, 0x3F, 0x08, 0x00, //t
        0x00, 0x00, 0x1E, 0x01, 0x01, 0x1E, 0x00, //u
        0x00, 0x00, 0x00, 0x1E, 0x01, 0x1E, 0x00, //v
        0x00, 0x00, 0x1E, 0x01, 0x06, 0x01, 0x1E, //w
        0x00, 0x00, 0x21, 0x12, 0x04, 0x12, 0x21, //x
        0x00, 0x00, 0x00, 0x38, 0x09, 0x09, 0x3F, //y
        0x00, 0x00, 0x00, 0x23, 0x25, 0x29, 0x31  //z
};
void main()
{    
INTCON2bits.RBPU = 0;
TRISB = 0b00000000;
ANSELH = 0b00000000;
TRISD = 0x00;
TRISB = 0xFF;
PORTD = 0x00;
if (PORTBbits.RB0 == 0)
PORTD = 0xFF;
else
PORTD = 0x00;
//Making All Of PORTD Outputs
Wait:
if (PORTBbits.RB3 == 0)
{
WaitA:
    if (PORTBbits.RB3 != 0)
    {
    Start();
    }
    else
    {
    goto WaitA;
    }
}
else
goto Wait;
}

int Start()
{
Selections:
switch(Cases)
{
case 0: Letter = 0; goto StartOfProgram;
case 1: Letter = 1; goto StartOfProgram;
case 2: Letter = 2; goto StartOfProgram;
case 3: Letter = 3; goto StartOfProgram;
case 4: Letter = 4; goto StartOfProgram;
case 5: Letter = 5; goto StartOfProgram;
case 6: Letter = 6; goto StartOfProgram;
case 7: Letter = 7; goto StartOfProgram;
case 8: Letter = 8; goto StartOfProgram;
case 9: Letter = 9; goto StartOfProgram;
case 10: Letter = 10; goto StartOfProgram;
case 11: Letter = 11; goto StartOfProgram;
case 12: Letter = 12; goto StartOfProgram;
case 13: Letter = 13; goto StartOfProgram;
case 14: Letter = 14; goto StartOfProgram;
case 15: Letter = 15; goto StartOfProgram;
case 16: Letter = 16; goto StartOfProgram;
case 17: Letter = 17; goto StartOfProgram;
case 18: Letter = 18; goto StartOfProgram;
case 19: Letter = 19; goto StartOfProgram;
case 20: Letter = 20; goto StartOfProgram;
case 21: Letter = 21; goto StartOfProgram;
case 22: Letter = 22; goto StartOfProgram;
case 23: Letter = 23; goto StartOfProgram;
case 24: Letter = 24; goto StartOfProgram;
case 25: Letter = 25; goto StartOfProgram; 
}
StartOfProgram:
//Start
PORTD = Sequences[Letter][Letter2];
goto Int1;


//Int1 Start
Int1:
if (PORTBbits.RB2 == 0)
{
Int1A:
    if (PORTBbits.RB3 != 0)
    {
        if (PORTBbits.RB2 != 0)
        {
            Cases++;
            if (Cases >    25)
            {
            Cases = 0;
            goto Selections;
            }
            else
            goto Selections;
        }
        else
        goto Int1A;
    }
    else if (PORTBbits.RB3 == 0)
    {
Int1A1:
        if (PORTBbits.RB3 != 0)
        {
            if (Store != 0)
            {
            Store--;
            Seq--;
            goto Selections;
            }
        }
        else
        goto Int1A1;
    }
}
else if (PORTBbits.RB3 == 0)
{
Int1B:
if (PORTBbits.RB3 != 0)
{
    Cases --;
    if (Cases < 0)
        {
            Cases = 25;
            goto Selections;
        }
    else
    {
    goto Selections;
    }
}
else
goto Int1B;
}
else if(PORTBbits.RB1 == 0)
{
Int2A:
    if (PORTBbits.RB1 != 0)
    {
        if (PORTBbits.RB3 == 0)
        {
            StorageArray[Store] = Letter +26; //array holding data
            Store++; //array1d value increase
            Seq++; //increase total letter by 1    
Wait5:
            if (PORTBbits.RB3 != 0)
            {
            goto StartOfProgram;
            }
            else
            goto Wait5;
        }
        else
        {    
            StorageArray[Store] = Letter; //array holding data
            Store++; //array1d value increase
            Seq++; //increase total letter by 1    
            goto StartOfProgram;
        }
    }
    else 
    goto Int2A;
    
}
else
{
goto Int3A;

}
Int3A:
if(PORTBbits.RB0 == 0)
{
Int3B:
    if(PORTBbits.RB0 != 0)
    {
    Restore = Store;
    Display();
    }
    else
    {
    goto Int3B;
    }
}
else
goto StartOfProgram;
}

int Display()
{
int x;
int y;
if (Store == 0)
{
Start();
}
if (Store == 1)
{
x = 25;
y = 40;
}
else if (Store == 2)
{
x = 22;
y = 40;
}
else if (Store == 3)
{
x = 15;
y = 10;
}
else if (Store == 4)
{
x = 13;
y = 30;
}
else if (Store > 4)
{
x = 5;
y = 15;
}
//Seq is to allow m to reset to default.
    //Store is to store the letter
    //StorageArray is to store selected
Store = 0;
Repeat1:
//not yet last letter
if (Store != Seq)
{
Letter = StorageArray[Store];
Repeat2:
    if (Letter2 < 8)
    {
    Delay100TCYx (x); 
    PORTD = Characters[Letter][Letter2];
    Letter2++;
    goto IntZ;
    }
    else if (Letter2 > 7)
    {
    Store++;
    Letter2 = 0;
    goto IntZ;
    }
}
else
Store = 0;
goto Repeat1;

IntZ:
if (PORTBbits.RB3 == 0)
{

IntZ1:
    if (PORTBbits.RB3 != 0)
    {
    Letter2 = 0;
    Store = 0;
    Seq = 0;
    Cases = 0;
    Start();
    
    }
    else
    goto IntZ1;
}
else
goto Repeat1;
}
 
Last edited by a moderator:

spinnaker

Joined Oct 29, 2009
7,835
You need to store in EPROM. Search your datasheet for EPROM. You don't mention your compiler (always mention your compiler for coding questions) but there should be library functions to store and retrieve EPROM values.

And please read my signature below.
 

be80be

Joined Jul 5, 2008
2,069
Your code looks like C18 so I post you a sample of how to read and write eeprom

You really don't want to do a bunch of reads and writes to eeprom, Like don't do alot of updates to the same block you can wear it out fast

Have a look at page 99 of the data sheet to make sure you registers match what is in the code blocks I posted

Rich (BB code):
void eeWrite_byte(unsigned char address, unsigned char *_data){

    EEDATA = *_data;
    EEADR = address;
    // start write sequence as described in datasheet, page 91
    EECON1bits.EEPGD = 0;
    EECON1bits.CFGS = 0;
    EECON1bits.WREN = 1; // enable writes to data EEPROM
    INTCONbits.GIE = 0;  // disable interrupts
    EECON2 = 0x55;
    EECON2 = 0x0AA;
    EECON1bits.WR = 1;   // start writing
    while(EECON1bits.WR){
        _asm nop _endasm;}
    if(EECON1bits.WRERR){
        printf("ERROR: writing to EEPROM failed!\n");
    }
    EECON1bits.WREN = 0;
    INTCONbits.GIE = 1;  // enable interrupts
}

void eeRead_byte(unsigned char address, unsigned char *_data){
    EEADR = address;
    EECON1bits.CFGS = 0;
    EECON1bits.EEPGD = 0;
    EECON1bits.RD = 1;
    *_data = EEDATA;
}
 

ErnieM

Joined Apr 24, 2011
8,296
Since the PIC18F45k20 has no EEPROM it would be a rather futile attempt to attempt to strore anything there. It would be much better to use the flash ROM to persist variables.

Sorry I don't have a link offhand I'm stuck using an iPad this week.
 

be80be

Joined Jul 5, 2008
2,069
What are you talking about read page 99 of the data sheet ErnieM

The data EEPROM is a nonvolatile memory array, separate
from the data RAM and program memory, which
is used for long-term storage of program data. It is not
directly mapped in either the register file or program
memory space but is indirectly addressed through the
Special Function Registers (SFRs). The EEPROM is
readable and writable during normal operation over the
entire VDD range.
Four SFRs are used to read and write to the data
EEPROM as well as the program memory. They are:
• EECON1
• EECON2
• EEDATA
• EEADR
• EEADRH
The data EEPROM allows byte read and write. When
interfacing to the data memory block, EEDATA holds
the 8-bit data for read/write and the EEADR:EEADRH
register pair hold the address of the EEPROM location
being accessed.
The EEPROM data memory is rated for high erase/write
cycle endurance. A byte write automatically erases the
location and writes the new data (erase-before-write).
The write time is controlled by an on-chip timer; it will
vary with voltage and temperature as well as from chipto-
chip. Please refer to parameter D122 (Table 26.10 in
Section 26.0 “Electrical Characteristics”) for exact
limits.
http://ww1.microchip.com/downloads/en/devicedoc/41303g.pdf

There 256 byte of eeprom on that chip

 
Last edited:

be80be

Joined Jul 5, 2008
2,069
There is a new kind of memory it's has better life then eeprom but I don't think it will be in a pic any time soon. MRAM
 

Thread Starter

reneeqps

Joined Dec 27, 2012
2
Your code looks like C18 so I post you a sample of how to read and write eeprom

You really don't want to do a bunch of reads and writes to eeprom, Like don't do alot of updates to the same block you can wear it out fast

Have a look at page 99 of the data sheet to make sure you registers match what is in the code blocks I posted

Rich (BB code):
void eeWrite_byte(unsigned char address, unsigned char *_data){

    EEDATA = *_data;
    EEADR = address;
    // start write sequence as described in datasheet, page 91
    EECON1bits.EEPGD = 0;
    EECON1bits.CFGS = 0;
    EECON1bits.WREN = 1; // enable writes to data EEPROM
    INTCONbits.GIE = 0;  // disable interrupts
    EECON2 = 0x55;
    EECON2 = 0x0AA;
    EECON1bits.WR = 1;   // start writing
    while(EECON1bits.WR){
        _asm nop _endasm;}
    if(EECON1bits.WRERR){
        printf("ERROR: writing to EEPROM failed!\n");
    }
    EECON1bits.WREN = 0;
    INTCONbits.GIE = 1;  // enable interrupts
}

void eeRead_byte(unsigned char address, unsigned char *_data){
    EEADR = address;
    EECON1bits.CFGS = 0;
    EECON1bits.EEPGD = 0;
    EECON1bits.RD = 1;
    *_data = EEDATA;
}
hi thank you so much for your help but my lecturer just gave me this project and ask me to do without and guide book or whatsoever. she just gave me the component and all so i'm really lost right now with no knowledge of all these.

so means i have to type in the above code into my program? thanks
 

spinnaker

Joined Oct 29, 2009
7,835
hi thank you so much for your help but my lecturer just gave me this project and ask me to do without and guide book or whatsoever. she just gave me the component and all so i'm really lost right now with no knowledge of all these.
You need to get a new lecturer. That is horrible advice. Probably the single most important skill for an engineer is to know how to find information. This would be data sheets, instruction manuals, technical papers, other peers..... Pretty much any place information can be found.
 

ErnieM

Joined Apr 24, 2011
8,296
hi thank you so much for your help but my lecturer just gave me this project and ask me to do without and guide book or whatsoever. she just gave me the component and all so i'm really lost right now with no knowledge of all these.

so means i have to type in the above code into my program? thanks
I would surely hope your lecturer did not intend for you to use a device without looking at it's data sheet.

When I use code such as this first I will copy from the PDF and paste into my program source (so at most all I need type is corrections to tabs ans spacings), and I will also add a comment noting where I obtained the code from, document and page. This way you are noting the source for future reference and NOT claiming the code as your own work...though in this case any code that does work will be doing the same things in the same order as you are working with hardware.
 

takao21203

Joined Apr 28, 2012
3,702
EEPROM is for configuration data which must be changed frequently.

If you need more memory, use serial FLASH.

Normally data is stored inside the FLASH ROM, where else?
 

be80be

Joined Jul 5, 2008
2,069
Serial Flash or EEprom would be better. There life is about the same.

EEPROM is for configuration data which must be changed frequently.
You mean infrequently, You don't want to update to EEprom every second once a day or so is what you would want.
 
Top