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

Serial EEPROM problem

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







Serial EEPROM problem
PostPosted: Fri Nov 19, 2004 2:15 pm     Reply with quote

I'm working with Serial EEPROM PIC24L256, I fist store different values in differents memory address, then i read each values for each memory address. My problem is when I read this values they are differents to the values stored.

This my code
--------------------------------------------------------------------------------------

#include <16F873.H>
#include <stdio.h>


#use delay(clock=4000000)
#use rs232(baud=9600,bits=8,parity=N,xmit=PIN_C6,rcv=PIN_C7,BRGH1OK)
#use i2c(master,sda=PIN_C4, scl=PIN_C3)

#define hi(x) (*(&x+1))
#byte PORTA = 0x05
#byte PORTC = 0x07
#byte PORTB = 0x06
#byte RTCC = 0x01


//****************************************************Rutinas del EEPROM I2C*********************************************************

void write_ext_eeprom(long int address, byte data)
{
i2c_start();
i2c_write(0xa0);
i2c_write(hi(address)&0x7f); //Storage until 32K
i2c_write(address);
i2c_write(data);
i2c_stop();
delay_ms(35);
}

byte read_ext_eeprom(long int address) {
byte data;
i2c_start();
i2c_write(0xa0);
i2c_write(hi(address)&0x7f); //Reading until 32K
i2c_write(address);
i2c_start();
i2c_write(0xa1);
data=i2c_read(0);
i2c_stop();
return(data);
}

//**************************************************Rutina de Lectura del A/D*********************************************************

unsigned ReadADC(unsigned chn){
SET_ADC_CHANNEL(chn);
DELAY_CYCLES(32);
return READ_ADC();
}

unsigned ADCchannel(unsigned ch)
{
switch(ch) {
case 0:
BIT_CLEAR(PORTB,5);
BIT_CLEAR(PORTB,6);
BIT_CLEAR(PORTB,7);
break;
case 1:
BIT_SET(PORTB,5);
BIT_CLEAR(PORTB,6);
BIT_CLEAR(PORTB,7);
break;
case 2:
BIT_CLEAR(PORTB,5);
BIT_SET(PORTB,6);
BIT_CLEAR(PORTB,7);
break;
case 3:
BIT_SET(PORTB,5);
BIT_SET(PORTB,6);
BIT_CLEAR(PORTB,7);
break;
case 4:
BIT_CLEAR(PORTB,5);
BIT_CLEAR(PORTB,6);
BIT_SET(PORTB,7);
break;
case 5:
BIT_SET(PORTB,5);
BIT_CLEAR(PORTB,6);
BIT_SET(PORTB,7);
break;
case 6:
BIT_CLEAR(PORTB,5);
BIT_SET(PORTB,6);
BIT_SET(PORTB,7);
break;
case 7:
BIT_SET(PORTB,5);
BIT_SET(PORTB,6);
BIT_SET(PORTB,7);
break; }
}

//*************************************************Inicio del Main*********************************************************************


main() {
int dat,yi, addr[50], j;
byte start;

//OUTPUT_HIGH(PIN_B1);
puts("Iniciando Adquisición de Datos");

set_tris_a (0xFF); // puerto A todo entrada
set_tris_b (0x03); // RB0(ext int) entrada y RB1, el resto salidas

set_rtcc(0);
setup_counters( RTCC_INTERNAL, RTCC_DIV_256);
enable_interrupts(RTCC_ZERO);
enable_interrupts(GLOBAL);

setup_port_a( ALL_ANALOG );
setup_adc( ADC_CLOCK_INTERNAL );

enable_interrupts(int_rda);

do {

for(j=2; j<=7; ++j)
{
ADCchannel(0);
SET_ADC_CHANNEL(0);
DELAY_CYCLES(32);
yi=READ_ADC();
printf("\n\rValor numero %d ADC Canal_0: %2X \r\n",j,yi); //Valor de RA0 en la prueba
write_ext_eeprom(addr[j],yi);
delay_ms(2000); }

printf("\nValores almacenados en EEPROM I2C\n");

for(j=0; j<=5; j++)
{
dat=read_ext_eeprom(addr[j]);
printf("\n\rValor Número %d almacenado canal 1: %2X \r\n",j,dat);
delay_ms(500);
}
printf("\n prueba finalizada \n");
}
while(true);
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Nov 19, 2004 3:21 pm     Reply with quote

I'm going to assume that you don't really intend to get your eeprom
addresses from an array. So I suggest the following:

These two lines are wrong:
Quote:
write_ext_eeprom(addr[j],yi);
dat=read_ext_eeprom(addr[j]);


Change them to this:
Code:
write_ext_eeprom(j, yi);
dat=read_ext_eeprom(j);
ckielstra



Joined: 18 Mar 2004
Posts: 3680
Location: The Netherlands

View user's profile Send private message

PostPosted: Fri Nov 19, 2004 6:36 pm     Reply with quote

Quote:
I'm working with Serial EEPROM PIC24L256

I can't find info on this chip. Do you mean the PIC24LC256?

If yes, then you can optimize your write speed by decreasing the delay_ms(35) to delay_ms(5).

Also see the other thread on the PIC24FC515 that is going on today for another code example, click here.
Mark



Joined: 07 Sep 2003
Posts: 2838
Location: Atlanta, GA

View user's profile Send private message Send e-mail

PostPosted: Fri Nov 19, 2004 10:07 pm     Reply with quote

Quote:
If yes, then you can optimize your write speed by decreasing the delay_ms(35) to delay_ms(5).


The is the wrong way of doing it. You should look for an acknowledgement when addressing the device. If it busy, say writing, you will not get the ack. There is no need for a delay and is a more efficient approach.
Don Rey
Guest







PostPosted: Sat Nov 20, 2004 6:56 am     Reply with quote

Thanks to all,

I changed from:
write_ext_eeprom(addr[j],yi);
dat=read_ext_eeprom(addr[j]);
to:
write_ext_eeprom(j, yi);
dat=read_ext_eeprom(j);

Apperently I was writting to a memory space without be define

Thanks
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