• AVR Freaks

Locked[FAQ]Program Memory / Internal EEPROM Reads and Writes

Page: < 1234 Showing page 4 of 4
Author
DavidP5
Super Member
  • Total Posts : 472
  • Reward points : 0
  • Joined: 2006/02/01 23:23:40
  • Location: New Zealand
  • Status: offline
RE: Program Memory / Internal EEPROM Reads and Writes 2006/04/05 22:01:15 (permalink)
0
You should use the code button (<&) when you post code, otherwise it is very hard to read.
 
I have reformatted you code, your main function is:
 

void main(){
 ADCON1  = 0x07;   // Set Port A & E as digital I/O
 TRISA   = 0b11110001;  // bit 1, 2 & 3 of Port A as output
 TRISD   = 0;   // Port D as output
 Init_LCD();           // Init LCD 4-bit interface, multiple line
 Init_com();           // Init Com
 while (1) {
  if (PIR1bits.RCIF) {         //if PIR1 bit is RCIF
    W_data_4bit(RCREG); //get char from hyper term and display
  }
 }
}

 
I am no expert in working with serial communcations, but I think your method of receiving serial data might be wrong. I don't know what RCREG is, or whether you can get a byte from it in this way when the PIR1bits.RCIF flag is set, but it seems to me that this should probably be done inside an interrupt service routine, and that the PIR1bits.RCIF flag should be unset after you collect the byte. You can only start to think about saving the bytes to flash when you are actually receiving them from the serial line. Once you have got the bytes coming in properly you might consider saving them into a buffer, and when you have them all in the buffer you can save them to flash using the memcpyram2flash() function that I posted. It might be a good idea for you to ask a question on another part of the forum about the use of a UART of USART for receiving bytes from a serial line. 
#61
Guest
Super Member
  • Total Posts : 80500
  • Reward points : 0
  • Joined: 2003/01/01 00:00:00
  • Location: 0
  • Status: online
RE: Program Memory / Internal EEPROM Reads and Writes 2006/04/06 00:03:01 (permalink)
0
thans for reformat my code, anywa my source code is fine i guess, cos i m able to receive characters from hyper terminal, meanin wad i type in hyper terminal is display on the LCD of the demo board. i don get wad u mean by saving it in a buffer? any example  of buffer code
#62
DavidP5
Super Member
  • Total Posts : 472
  • Reward points : 0
  • Joined: 2006/02/01 23:23:40
  • Location: New Zealand
  • Status: offline
RE: Program Memory / Internal EEPROM Reads and Writes 2006/04/06 13:50:00 (permalink)
0
I mean declare a buffer of bytes. For example, if you need to save 100 bytes to memory:
 

char buffer[100];

Then as you get the bytes, put them in the buffer:
 

char count = 0;
while (1) {
 if (PIR1bits.RCIF) {         //if PIR1 bit is RCIF
  W_data_4bit(RCREG); //get char from hyper term and display
  buffer[count++] = RCREG;
}
}


 
something like this anyway... As I said, your method of collecting the bytes seems a bit strange to me. 
Then, when you have got the 100 bytes, save them to flash:
 

memcpyram2flash(address, buffer);

 
OK?
 
 
 
#63
Guest
Super Member
  • Total Posts : 80500
  • Reward points : 0
  • Joined: 2003/01/01 00:00:00
  • Location: 0
  • Status: online
RE: Program Memory / Internal EEPROM Reads and Writes 2006/04/07 06:56:10 (permalink)
0
hmmm ok, thanx, i wil try it out and get back 2 you 4 more information
#64
Guest
Super Member
  • Total Posts : 80500
  • Reward points : 0
  • Joined: 2003/01/01 00:00:00
  • Location: 0
  • Status: online
RE: Program Memory / Internal EEPROM Reads and Writes 2006/04/09 19:57:37 (permalink)
0
tell me if this is correct,


void memcpyram2flash(unsigned addr, char buffer) {
unsigned char i;
TBLPTR = addr;
EECON1bits.EEPGD = 1; //access flash memory
EECON1bits.CFGS = 0; //access flash program or data EEPROM
EECON1bits.WREN = 1; //allow write operation
EECON1bits.FREE = 1;
INTCONbits.GIE = 0; //disable all interrupt
EECON2 = 0x55;
EECON2 = 0xAA;
EECON1bits.WR = 1; //initialise a data EEPROM erase/write cycle
Nop();
INTCONbits.GIE = 1; //enable interrupt
EECON1bits.WREN = 0; //inhibit write to the EEPROM
for(i = 0;i < 100;i++){
TABLAT = buffer[i];    // put a char into the table latch register
_asm
TBLWTPOSTINC     // write to holding register and post-increment TBLPTR
_endasm
if(((i + 1) % 8) == 0){ // after each 8-byte write to holding registers...
TBLPTR -= 8;
EECON1bits.EEPGD = 1; // ...write the holding registers to flash
EECON1bits.CFGS = 0;
EECON1bits.WREN = 1;
EECON1bits.FREE = 0;
INTCONbits.GIE = 0;
EECON2 = 0x55;
EECON2 = 0xAA;
EECON1bits.WR = 1;
Nop();
INTCONbits.GIE = 1;
EECON1bits.WREN = 0;
TBLPTR += 8;
}
}
}[/i]
[i][/i] 
[i]char buffer[100];[/i]
[i]


then in the main;
 

char count = 0;
while (1) {
if (PIR1bits.RCIF) {         //if PIR1 bit is RCIF
W_data_4bit(RCREG); //get char from hyper term and display
buffer[count++] = RCREG;
}
}


after that save it to flash;

memcpyram2flash(address, buffer);

post edited by boi^boi - 2006/04/09 20:06:09
#65
DavidP5
Super Member
  • Total Posts : 472
  • Reward points : 0
  • Joined: 2006/02/01 23:23:40
  • Location: New Zealand
  • Status: offline
RE: Program Memory / Internal EEPROM Reads and Writes 2006/04/10 12:54:47 (permalink)
0
No, it doesn't look correct to me. There needs to be some way of detecting that the required number of bytes has been collected, then this needs to somehow call the function that writes to flash. You are missing some program logic. However, this thread is a FAQ about writing to EEPROM and FLASH, but your question is about how to fill a buffer from the serial port, so you are definitely off the topic. I suggest that you post a new topic in a more appropriate place (perhaps the USART, or C18 forum) if you want help with this. Given your previous posts I would like to suggest that you use proper English (no "wads" instead of what etc), you give a full explanation of what you are trying to do (spend some time off-line composing your question in a text editor then just cut and paste into the forum message editor), and be polite (the word "please" goes down well). If you do these things then I think you might get all the help you need.
#66
Guest
Super Member
  • Total Posts : 80500
  • Reward points : 0
  • Joined: 2003/01/01 00:00:00
  • Location: 0
  • Status: online
RE: Program Memory / Internal EEPROM Reads and Writes 2006/04/11 05:39:50 (permalink)
0
sorry if i were rude, thanks for all the help provided. i will bear what i said in mind. thanks again
#67
DavidP5
Super Member
  • Total Posts : 472
  • Reward points : 0
  • Joined: 2006/02/01 23:23:40
  • Location: New Zealand
  • Status: offline
RE: Program Memory / Internal EEPROM Reads and Writes 2006/04/28 15:37:02 (permalink)
0
I have tried to modify the code that I posted in post #36 of this thread to write to a PIC18F4520 memory. The data sheet says that this PIC writes to flash in 64 bytes chunks, but otherwise the procedure appears to be the same. I modified the code as follows:
 

...
 for(i = 0;i < 64;i++){
  TABLAT = mem[i];    // put a char into the table latch register
  _asm
   TBLWTPOSTINC     // write to holding register and post-increment TBLPTR
  _endasm
  if(((i + 1) % 64) == 0){ // MOD for 4520 after each 64-byte write to holding registers...
...

The code before the ... and after the ... is the same as in post #36, except I decremented TBLPTR by 64 and the incremented by 64 before and after the actual write (intead of 8 as in post #36). But this doesn't seem to work properly. When I run the program and load the program memory into MPLAB I get the following at memory address 0x5000:
 
 
 

Address   00   02   04   06   08   0A   0C   0E       ASCII     
   5000   0404 0404 0404 0404 0404 0404 0404 0404 ........ ........
   5010   0404 0404 0404 0404 0404 0404 0404 0404 ........ ........
   5020   FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF ........ ........
   5030   FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF ........ ........
   5040   FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF ........ ........


 
Can anyone explain this behaviour to me please?
 
 
#68
DavidP5
Super Member
  • Total Posts : 472
  • Reward points : 0
  • Joined: 2006/02/01 23:23:40
  • Location: New Zealand
  • Status: offline
RE: Program Memory / Internal EEPROM Reads and Writes 2006/04/28 19:35:51 (permalink)
0
As stated in my most recent post I am trying to write to flash in the
PIC18F4520. The PIC18F4520 data sheet says that flash should be written in
chunks of 64 bytes:
Section 6.5.1 FLASH PROGRAM MEMORY WRITE SEQUENCE
...
5. Load Table Pointer register with address of first
byte being written.
6. Write the 64 bytes into the holding registers with
auto-increment.
7. Set the EECON1 register for the write operation:
- set EEPGD bit to point to program memory;
...
Because I had problems I tried the following program to try different chunk
sizes:

#include <p18F4520.h>

void memcpyram2flash(unsigned addr, char * mem) {
int chunkSize = 64;
unsigned char i;
TBLPTR = addr;
EECON1bits.EEPGD = 1;
EECON1bits.CFGS = 0;
EECON1bits.WREN = 1;
EECON1bits.FREE = 1;
INTCONbits.GIE = 0;
EECON2 = 0x55;
EECON2 = 0xAA;
EECON1bits.WR = 1;
Nop();
INTCONbits.GIE = 1;
EECON1bits.WREN = 0;
for(i = 0;i < 64;i++){
   TABLAT = mem[i];        // put a char into the table latch register
   _asm
     TBLWTPOSTINC          // write to holding register and post-increment
   _endasm
   if(((i + 1) % chunkSize) == 0){ // write to flash after each chunkSize bytes
     TBLPTR --;    //put TBLPTR in holding register address range
     EECON1bits.EEPGD = 1; // ...write the holding registers to flash
     EECON1bits.CFGS = 0;
     EECON1bits.WREN = 1;
     EECON1bits.FREE = 0;
     INTCONbits.GIE = 0;
     EECON2 = 0x55;
     EECON2 = 0xAA;
     EECON1bits.WR = 1;
     Nop();
     INTCONbits.GIE = 1;
     EECON1bits.WREN = 0;
     TBLPTR ++;
}
}
void main(void) {
char mem[64];
char i;
// write test string to address 0x5000 in program memory
for (i = 0; i < 64; i++)
   mem[i] = i;
memcpyram2flash(0x5000, mem);
Nop();
while(1);
}


When I use a 64 byte chunk size I get the following (incorrect) result:

Address   00   02   04   06   08   0A   0C   0E       ASCII
  4FF0   FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF ........ ........
  5000   FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF ........ ........
  5010   FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF ........ ........
  5020   2120 2322 2524 2726 2928 2B2A 2D2C 2F2E  !"#$%&' ()*+,-./
  5030   3130 3332 3534 3736 3938 3B3A 3D3C 3F3E 01234567 89:;<=>?
  5040   FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF ........ ........
  5050   FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF ........ ........

When I use a 32 byte chunk size I get the following (expected) result:

Address   00   02   04   06   08   0A   0C   0E       ASCII
  4FF0   FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF ........ ........
  5000   0100 0302 0504 0706 0908 0B0A 0D0C 0F0E ........ ........
  5010   1110 1312 1514 1716 1918 1B1A 1D1C 1F1E ........ ........
  5020   2120 2322 2524 2726 2928 2B2A 2D2C 2F2E  !"#$%&' ()*+,-./
  5030   3130 3332 3534 3736 3938 3B3A 3D3C 3F3E 01234567 89:;<=>?
  5040   FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF ........ ........
  5050   FFFF FFFF FFFF FFFF FFFF FFFF FFFF FFFF ........ ........

The procedure seems to work OK for chunk sizes of 8 and 16 too.
Is the datasheet wrong, or am I misunderstanding something?

post edited by DavidP5 - 2006/04/28 19:44:56
#69
Guest
Super Member
  • Total Posts : 80500
  • Reward points : 0
  • Joined: 2003/01/01 00:00:00
  • Location: 0
  • Status: online
RE: Program Memory / Internal EEPROM Reads and Writes 2006/05/07 19:04:49 (permalink)
0
Datasheet aside, the 18F4520 writes 32 bytes of memory at a time, not 64.  (It's in an errata somewhere...)


#70
plugwash
Super Member
  • Total Posts : 332
  • Reward points : 0
  • Joined: 2006/04/07 13:45:54
  • Status: offline
RE: Internal EEPROM Reads and Writes--Solved the problem 2006/06/15 15:44:40 (permalink)
0

I finally guessed that some register bit somewhere was being set during programming, and needed to be cleared, which the cold reboot accomplished. The most likely was bit 6 of the EECON1 register. By adding the line

yes that would make sense, most programmers program configuration bits last so the registers would have been left set up for that.
#71
Guest
Super Member
  • Total Posts : 80500
  • Reward points : 0
  • Joined: 2003/01/01 00:00:00
  • Location: 0
  • Status: online
RE: Program Memory / Internal EEPROM Reads and Writes 2006/07/25 22:22:27 (permalink)
0
hi guys
 
i'm having the same problem,my progarm which need to store a 16bit integer to the internal eeprom of pic18f452.
In the Mplab library i have found some function that can be use to move data to the Rom but i have no idea how it works, so sorry to say that i'm new in this.sad
any idea or sample that i can read up..
 
 
 
cheer...
best regard.art
#72
abhaysingh
New Member
  • Total Posts : 13
  • Reward points : 0
  • Joined: 2006/06/08 23:50:25
  • Location: 0
  • Status: offline
RE: Program Memory / Internal EEPROM Reads and Writes 2006/08/04 07:07:14 (permalink)
0
Hello everybody..!
First of all i would like to thankx for all who are indirectly responsible for solving my problems..!
I wanted make one thing clear that is it necessary to clear(Erase) the memory (Flash) before writing to it?
or can i directly write my data to flash..it will be more easier and efficient.. if i am write.
#73
Page: < 1234 Showing page 4 of 4
Jump to:
© 2019 APG vNext Commercial Version 4.5