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.

How to use internal EEPROM of PIC18F45K22 to write and read data

Status
Not open for further replies.

polona1010

Member level 1
Joined
Apr 17, 2013
Messages
40
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,286
Activity points
1,524
Hello,

How to use internal eeprom memory of pic18f45k22 for reading and writing in MikroC.
 

In MikroC you can use EEPROM library, and EEPROM_READ for reading and EEPROM_WRITE for writing.

At address 0x32 write 0xAA
Code:
EEPROM_Write(0x32, 0xAA);

Read 0x3F address
Code:
unsigned short take;
...
take = EEPROM_Read(0x3F);


This is not linked just with this uC PIC18F45K22, this can be used with any other uC.
Just to mention, used PIC have only 256 bytes of EEPROM memory, there is PICs with larger EEPROM size up to 1024 bytes, and if you need more space then use external EEPROM ICs.


;-)
 
Last edited:
Can you show me whole example please?
 

For XC8 Compiler.

Turns ON LED connected to RB0 if character T is at eeprom address 0x00.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <eeprom_routines.h>
 
#define LED PORTBbits.RB0
 
void main(){
 
    unsigned char eeppdat = 0;
 
    EEPROM_WRITE(0x00, 'K');
    __delay_ms(20);
    eepdat = EEPROM_READ(0x00);
    if(eepdat == 'K')LED = 1;
 
 
}

 
How to write code A1B2C3D4E5F6G7H8 at some address in mikroc?
 

If it is a string then you can use this. If the digits are not characters then you can replace like '8' with 8 or 0x08.


Code C - [expand]
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
EEPROM_Write(0x00,'8');
Delay_ms(20);
EEPROM_Write(0x01,'H');
Delay_ms(20);
EEPROM_Write(0x02,'7');
Delay_ms(20);
EEPROM_Write(0x03,'G');
Delay_ms(20);
EEPROM_Write(0x04,'6');
Delay_ms(20);
EEPROM_Write(0x05,'F';
Delay_ms(20);
EEPROM_Write(0x06,'5');
Delay_ms(20);
EEPROM_Write(0x07,'E');
Delay_ms(20);
EEPROM_Write(0x08,'4');
Delay_ms(20);
EEPROM_Write(0x09,'D');
Delay_ms(20);
EEPROM_Write(0x0A,'3');
Delay_ms(20);
EEPROM_Write(0x0B,'C');
Delay_ms(20);
EEPROM_Write(0x0B,'2');
Delay_ms(20);
EEPROM_Write(0x0B,'B');
Delay_ms(20);
EEPROM_Write(0x0B,'1');
Delay_ms(20);
EEPROM_Write(0x0B,'A');
Delay_ms(20);

 
I need good code, optimized.

On one place when code is defined as for example

char code1[] = \"A1B2C3D4E5F6G7H8\";

I need to write A1 in 0x01, B2 in 0x02, C3 in 0x03,...
 

A1 cannot be written at 1 byte address like 0x00. You need 2 bytes one for A and one for 1.

Edit: I was wrong. Each eeprom address holds 2 bytes like 0xFF.
 
Last edited:

When I start eeprom editor in mikroc i can write A1 in one address B2 in second,...

Only G and H cannot be writen because these letters are not hex code.

One byte is represented as HEX with two HEX characters. One example FF is one one address.
 

Hi Polona,

This code write A1 8 times on 8 different addresses in internal EEPROM memory of uC.
Example for writing A1 in address range from 0x10 to 0x17 (8 address locations):

Code:
   char eeprom_address;

   eeprom_address = 0x10;                   // Starting position of eeprom address
   for (i=0; i<8; i++)                      // Loop to shift 8 bytes
     {
     EEPROM_write(eeprom_address, 0xA1);   // Writing data in eeprom
     eeprom_address = eeprom_address + 1;  // Move to one address place in eeprom
     }



This code read data from 8 different addresses from internal EEPROM of uC, and should give A1A1A1A1A1A1A1A1
For reading address location from previous example and showing on LCD use this code:

Code:
  // Reading 8 bytes from internal memory of uC, address range 0x10-0x17
  // Print on LCD display in HEX format

  char eeprom_address;
  char eeprom_value_hex;
  char eeprom_value;  
  char i;                              // Loop variable
  int colona;

  Lcd_Cmd(_LCD_CLEAR);               // Clear display
  Lcd_Out(1, 1, "HEX FORMAT");
  colona = 1;                               // Used to set starting column on lcd
  eeprom_address = 0x10;             // Starting position of eeprom address
  for (i=0; i<8; i++)                      // Loop to shift 8 bytes
     {
     eeprom_value = EEPROM_read(eeprom_address);  // Read eeprom address
     ByteToHex(eeprom_value, eeprom_value_hex);     // Convert byte value to hex format
     Lcd_Out(2, colona, eeprom_value_hex);             // Show HEX value of eeprom
     colona = colona + 2;                                      // Move to 2 places on right
     eeprom_address = eeprom_address + 1;            // Move to one address place in eeprom
     }


On end check eeprom status, read eeprom with programmer and see data in eeprom.


To delete address write use 0xFF:
Code:
EEPROM_write(0x20, 0xFF);

To write one HEX data (0xA1) on single address (0x20):
Code:
EEPROM_write(0x20, 0xA1);


You should have this picture when you read internal eeprom of PIC18F45K22 with MikroProg programmer, in different programmer softwares windows and presentation of addresses can be different:

EEPROM_A1.jpg



;-)

- - - Updated - - -

One more thing, after writing if you want to read, delay of 20ms or more is needed (20ms minimum) :

Code:
EEPROM_write(0x20, 0xA1);
Delay_ms(20);
eeprom_value = EEPROM_read(0x20);
 
Wooow thank you mr. tpeter this is nice.
 

Status
Not open for further replies.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top