|
|
View previous topic :: View next topic |
Author |
Message |
jake1272
Joined: 15 Mar 2021 Posts: 37
|
"*Solved*"writing/reading external EEPROM |
Posted: Mon Mar 15, 2021 6:12 am |
|
|
The external EEPROM is - Microchip 24LC512
The compiler is CCS v5.015
The IDE is MPLAB X IDE v5.40
Using teraterm as terminal.
Hi, everyone
I am trying to input a string from the user and write it to EEPROM.
Then read and write the string back to the PC terminal and print the number of characters in the string on the terminal.
I have setup software I2C with the code below.
Any help will be appreciated.
Thanks,
Jake
Code: | #include <16F1847.h>
#fuses INTRC_IO,NOWDT,NOPROTECT,MCLR,NOLVP
#use delay(clock=8000000)
#use rs232(Baud=9600, UART1) //Set max BAUD rate on RS232
#define EEPROM_SDA PIN_A0 //Define I2C pins for EEPROM
#define EEPROM_SCL PIN_A1 //Add 2K resistor from each pin to +5V
#include <24512.c> //Call EEPROM driver file
#use I2C(Master, SDA=eeprom_SDA, SCL=eeprom_SCL)
void TestEeprom (){ //Function to write 255 to EEPROM memory
unsigned long c=0; //locations 750 to 1000 the rest are set to 0
printf("\nInitialising Eeprom..");
init_ext_eeprom(); //Driver function to initialise EEPROM
printf("..Ok.Now writing...\n");
while(c<1000){ //Long variable counter from 1 to 1000
if(c<750) write_ext_eeprom (c, 0); //Write 0 to all locations under address 750
if(c>=750) write_ext_eeprom (c, 255); //Write 255 to the remaining addresses up to 1000
printf("\r\nWritten :%Lu-Read:%u", c, read_ext_eeprom (c));
c++ ; ///Print written and read values to RS232
}
printf("\r\n\nAll Done.\n");
}
void main(){
TestEeprom ();//Call on TestEeprom () subroutine
} |
Last edited by jake1272 on Wed Mar 31, 2021 10:46 am; edited 2 times in total |
|
|
temtronic
Joined: 01 Jul 2010 Posts: 9229 Location: Greensville,Ontario
|
|
Posted: Mon Mar 15, 2021 8:00 am |
|
|
so... does your test program actually work ?? |
|
|
jake1272
Joined: 15 Mar 2021 Posts: 37
|
|
Posted: Mon Mar 15, 2021 8:21 am |
|
|
temtronic wrote: | so... does your test program actually work ?? |
yes it does |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19518
|
|
Posted: Mon Mar 15, 2021 8:38 am |
|
|
In which case, what are you actually asking?.
However a couple of comments. Currently you are using the basic 24512
driver. This only supports single byte writes to the EEPROM, so 5mSec/byte.
The 24LC512 driver supports page writes, so the whole 'string' when
you have it could be written with probably just one write. Hundreds of
times faster.
So instead of your current setup, use:
Code: |
#include <16F1847.h>
#fuses INTRC_IO,NOWDT,NOPROTECT,MCLR,NOLVP
#use delay(clock=8000000)
#use rs232(Baud=9600, UART1) //Set max BAUD rate on RS232
#define EEPROM_SDA PIN_A0 //Define I2C pins for EEPROM
#define EEPROM_SCL PIN_A1 //Add 2K resistor from each pin to +5V
#include <24LC512.c> //Load EEPROM driver file
|
Add 'ERRORS' to your RS232 #use. This should _always_ be used when
using the hardware UART, unless you add your own error handling.
Then you are going to have to read your 'string' from the UART. Look
possibly at using input.c, which has a nice simple routine 'get_string',
which allows you to specify a maximum length for your string, and
you can input this into a character array. Then instead of the byte based
write operation, use
write_ext_eeprom_bytes(ADDRESS, string_buffer, strlen(string_buffer)+1);
which will use a block write to write the entire buffer to the EEPROM. |
|
|
jake1272
Joined: 15 Mar 2021 Posts: 37
|
|
Posted: Wed Mar 17, 2021 10:56 am |
|
|
Thank you. I have tried this instead but I could not read back the value. Any idea what I am missing ?
Many Thanks
Code: | #include <16F1847.h>
#fuses INTRC_IO,NOWDT,NOPROTECT,MCLR,NOLVP
#use delay(clock=8000000)
#use rs232(Baud=9600, UART1) //Set max BAUD rate on RS232
#define EEPROM_SDA PIN_A0 //Define I2C pins for EEPROM
#define EEPROM_SCL PIN_A1 //Add 2K resistor from each pin to +5V
#include <24512.c> //Call EEPROM driver file
#include <STRING.h>
void WriteRead (){ //Function to write 255 to EEPROM memory
unsigned char inString[30]; //locations 750 to 1000 the rest are set to 0
printf("\n\r Enter your string\n\r");
gets(inString);
printf("\n\r Input collect :%s" ,inString);
int size = 0;
while (inString[size]!= '\0')
{
size++ ;
}
printf("\n\r Size of Input String is %d\n\r" , size);
init_ext_eeprom();
printf("\n\r ... Now writing %s to eeprom ..... \n\r" , inString);
for (int i=0; i<size ; i++);
{
write_ext_eeprom (i, inString[i]);
}
printf("\n\r ... writing complete ... \n\r");
printf("\n\r ...Reading back ... \n\r");
printf(" Readback = :");
for (int i2=0; i2 <size +1 ; i2++);
{
printf("%c " , read_ext_eeprom(i2));
}}
void main(){
WriteRead ();//Call on TestEeprom () subroutine
}
|
|
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19518
|
|
Posted: Thu Mar 18, 2021 2:36 am |
|
|
Rewritten to show what I was saying:
Code: |
#include <16F1847.h>
#fuses INTRC_IO,NOWDT,NOPROTECT,MCLR,NOLVP
#use delay(internal=8MHz)
#use rs232(Baud=115200, UART1, ERRORS) //Set max BAUD rate on RS232
//you _must_ always use ERRORS, unless you are writing your own error
//handler.
//Not having this here will mean that if any character arrives on the serial
//while printing or in the EEPROM write, will mean that the UART will
//become hung...
#define EEPROM_SDA PIN_A0 //Define I2C pins for EEPROM
#define EEPROM_SCL PIN_A1 //Add 2K resistor from each pin to +5V
#include <24LC512.c> //Load, not 'call' the EEPROM driver
#include <string.h> //String handling
#include <stdlib.h> //standard IO functions
#include <input.c> //and input.c
#define STORE_AT 0L //location to store data at
#define BUFFLEN 32
void WriteRead ()
{ //Function to read string from serial and write to EEPROM
unsigned char inString[BUFFLEN]; //storage for input string
printf("\n\r Enter your string\n\r");
//gets(inString);
//gets, is terrifyingly dangerous. If you type one character more than
//the input length you have allocated, will result in complete disaster.
//Do not use. Use get_string instead, which allows maximum length to
//be limited
get_string(inString, 32); //Max 31 characters with this
printf("\n\r Input collect :%s" ,inString);
printf("\n\r Size of Input String is %d\n\r" , strlen(inString));
init_ext_eeprom();
printf("\n\r ... Now writing %s to eeprom ..... \n\r" , inString);
write_ext_eeprom_bytes(STORE_AT, inString, strlen(inString)+1);
printf("\n\r ... writing complete ... \n\r");
printf("\n\r ...Reading back ... \n\r");
//Read is fast. So just readback the 32bytes max allowed in the buffer
//this then will include the null terminator.
read_ext_eeprom_bytes(inString, STORE_AT, BUFFLEN);
printf(" Readback = :%s\n\r", inString);
}
void main()
{
WriteRead (); //Call on TestEeprom () subroutine
while (TRUE) //prevent dropping off end, otherwise serial TX will lose last byte.
;
}
|
Be aware that this does require the EEPROM has
WP=Vss
A0=Vss
A1=Vss
A2=Vss |
|
|
jake1272
Joined: 15 Mar 2021 Posts: 37
|
|
Posted: Thu Mar 18, 2021 4:20 am |
|
|
Thanks very much, Ttelmah.
My question might sound stupid but How can I load the 24LC512.c driver file?
I am getting the following errors
C:\Users\chris\MPLABXProjects\eeprom.X\newmain.c:20:10: Error#18 File can not be opened
Not in "C:\Program Files\PICC\devices\24LC512.c"
Not in "C:\Program Files\PICC\drivers\24LC512.c"
Not in local "24LC512.c"
Not in local "C:\Users\chris\MPLABXProjects\eeprom.X\24LC512.c"
Not in last "C:\Program Files\PICC\devices\24LC512.c"
Not in project "C:\Users\chris\MPLABXProjects\eeprom.X\24LC512.c"
C:\Users\chris\MPLABXProjects\eeprom.X\newmain.c:44:19: Error#12 Undefined identifier -- init_ext_eeprom
C:\Users\chris\MPLABXProjects\eeprom.X\newmain.c:46:26: Error#12 Undefined identifier -- write_ext_eeprom_bytes
C:\Users\chris\MPLABXProjects\eeprom.X\newmain.c:53:25: Error#12 Undefined identifier -- read_ext_eeprom_bytes
4 Errors, 0 Warnings.
Last edited by jake1272 on Thu Mar 18, 2021 4:28 am; edited 1 time in total |
|
|
Ttelmah
Joined: 11 Mar 2010 Posts: 19518
|
|
Posted: Thu Mar 18, 2021 4:26 am |
|
|
The problem there is the age of your compiler. 5.015, is a very early V5 compiler. The LC driver was added in early 2016. |
|
|
jake1272
Joined: 15 Mar 2021 Posts: 37
|
|
Posted: Fri Mar 19, 2021 5:45 am |
|
|
Ttelmah wrote: | The problem there is the age of your compiler. 5.015, is a very early V5 compiler. The LC driver was added in early 2016. |
Thank you the code is working now |
|
|
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|