CCS C Software and Maintenance Offers
FAQFAQ   FAQForum Help   FAQOfficial CCS Support   SearchSearch  RegisterRegister 

ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

CCS does not monitor this forum on a regular basis.

Please do not post bug reports on this forum. Send them to support@ccsinfo.com

External EEPROM write and read

 
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion
View previous topic :: View next topic  
Author Message
Guest








External EEPROM write and read
PostPosted: Wed Mar 21, 2007 4:20 pm     Reply with quote

Hi

I´m trying to write and read an external EEPROM (24LC256), I´m entering information using a serial port, this information is added to a buffer variable until a press "Enter", Once I press enter the information is written in the external EEPROM Memory, When I read the memory addresses written I just see 0xFF, Here It´s the program:

#include <16f877.h> // PIC Selection
#fuses HS,NOWDT,NOPROTECT,NOLVP,PUT,BROWNOUT
#use delay(clock=20000000)
#use rs232(baud=1200,parity=N,xmit=PIN_B1,rcv=PIN_B0,bits=8,stream=PC)
#use rs232(baud=1200,parity=N,xmit=PIN_C6,rcv=PIN_C7,bits=8,stream=PBX)
#define EEPROM_SDA PIN_A1
#define EEPROM_SCL PIN_A0 //pines for the EEPROM
#include <24256.c> //librery eeprom 24LC256

////////////////////////////////////////////////////////////////////////////////

// Constants
int const MAXLENBUFF=68;
int commandx=0;

// Globals Variables ///////////////////////////////////////////

char buffrec[MAXLENBUFF]; // Reception Buffer
int xbuffrec=0x00; // Buffer index
int xbuffrecw=0x00;

int1 new_command=0; // Flag for writting EEPROM

void Add_buffrec(char c); // add a character to reception buffer
void write_EEPROM(void); // sub for EEPROM writing


// INTERRUPTIONS ////////////////////////////////////////////////////////////////


/******************************************************************************/
/* varibles for handlign read and write

int row=0;
int pagact=0;
int t=0;
int conta=0;

/// Funtion Declaration/////////////////////////////////////////////////

void Ini_buffrec(void); // Reception buffer
void Add_buffrec(char c); // Add a character to buffer
void read_buffrec(char s); // read EEPROM
/************************************************************
/// Interruption from PBX serial port

#int_rda
rda_isr() {
char vari;
vari=fgetc(PBX); // save character coming from PBX in vari
Add_buffrec(vari); //call sub for add to buffer variable

}
/********* Interruption from serial port (RB0)************/

#int_EXT // RB0 Interruption
ext_isr() {
char varif;
varif=fgetc(PC);
read_buffrec(varif); //call read EEPROM sub-rutine
}

//****************Add character to buufer variable

void Add_buffrec(char c){

switch(c){
case 0x0D: // Check if enter was hit
buffrec[xbuffrec++]=c; //include "enter" in buffer variable
new_command=1; // Flag to indicate end of receive
conta=conta+1; //Counter for bytes receive
break;
default:
buffrec[xbuffrec++]=c; // add character Buffer variable

conta=conta+1; //Counter for bytes receive
}
}

//****************************************************************************************

///write information in EEPROM*********************

void Escribir_EEPROM(void){
int i;
if (row>=186){ //Check if EEPROM is full
fila=0;
t=0;
pagact=0;
fprintf(PC,"row=0\n\r");
}
for(i=0;i<conta;i++){ // write information receive in EEPROM
write_ext_eeprom(row,buffrec);
row=row+1;
}
conta=0;
}

//********************************************
//Read information from the EEPROM and send it to PC serial port
void Leer_buffrec(char s){
if(s=='I'){ //Wait for "I" Characrter
for(t=pagact; t<row; t++){
fputc(read_ext_eeprom(t),PC); // read EEPROM/
}
pagact=t; //Save last EEPROM adress read and write
}
}

// Buffer Variable clear ---------------------------------------

void Ini_buff_rec(void){

int i;
for(i=0;i<MAXLENBUFF;i++){ //
buffrec=0x00; // All buffer variable to 0
}
xbuffrec=0x00; //
conta=0x00; // counter to 0
}

/*************** Main Routing************************/

void main() {

ext_int_edge(H_TO_L); // Enable interruption H_to_L Pin RB0
enable_interrupts(INT_EXT); // Enable external interruption RB0
enable_interrupts(int_rda); // Enable RDA(received Data Available)
enable_interrupts(GLOBAL); // Enable global interruption

init_ext_eeprom();
Ini_buff_rec();

fprintf(PBX,"Trying PBX\n\r");
fprintf(PC,"Trying PC\n\r");



do {

if(new_command==1){
new_command=0; // Clear variable
Escribir_EEPROM(); // Call Write EEPROM
Ini_buff_rec(); // call clear buffer
}

} while(TRUE);// LOOP

I try this program for the internal EEPROM of the 16F877 and It works (Using write_eeprom and read_eeprom functions), I`m using 3.236 PCWH compiler. Appreciate any help
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Mar 21, 2007 4:45 pm     Reply with quote

Do you have pull-up resistors on the SDA and SCL lines ?
You need them.

Do you have address pins A0, A1, and A2 on the eeprom
connected to ground ? They should be.

The WP pin should be left unconnected. It has an internal pull-down
resistor.
lexer



Joined: 21 Mar 2007
Posts: 3

View user's profile Send private message MSN Messenger

PostPosted: Wed Mar 21, 2007 5:23 pm     Reply with quote

Thanks PCM programmer, (I forgot to login when I started the post). Yes I have the SDA and the SCL lines resistors, But I dont have A0, A1 and A2 to ground let me try?
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Mar 21, 2007 5:43 pm     Reply with quote

Actually, if it's a Microchip 24LC256, the data sheet says those pins
also have internal pull-down resistors.

What brand of eeprom do you have ? Is it Microchip ?
lexer



Joined: 21 Mar 2007
Posts: 3

View user's profile Send private message MSN Messenger

PostPosted: Wed Mar 21, 2007 6:01 pm     Reply with quote

Yes I`m using a Microchip memory, I was thinking If the format of the variable for addessing the the memory could be the problem, The manual tells that It has to be a 16 bit address. The data to write has to be a char o could any type?[/i]
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Wed Mar 21, 2007 6:10 pm     Reply with quote

First you could do a simple test to see if the hardware is working.
See the test program that I've posted near the start of this thread:
http://www.ccsinfo.com/forum/viewtopic.php?t=23824
It just writes 0x55 and 0xAA to the eeprom. Then it reads it back
and displays it on a terminal window. See if that program works.

Remember to change the #define statements for the pins,
near the top of the program, to the pins that you are using (A0 and A1).
lexer



Joined: 21 Mar 2007
Posts: 3

View user's profile Send private message MSN Messenger

PostPosted: Thu Mar 22, 2007 8:44 am     Reply with quote

Thanks, I simulate your program with Proteus, I display the read addresses (0 and 1) in VTERM, I dont see the values 0x55 and 0xAA in the VTERM but If I check the EEPROM debug I see that addresses 0 and 1 have 0x55 and 0xAA, The values in VTERM arent HEX neither decimal, Any Idea?
Display posts from previous:   
Post new topic   Reply to topic    CCS Forum Index -> General CCS C Discussion All times are GMT - 6 Hours
Page 1 of 1

 
Jump to:  
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