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 CCS Technical Support

pic18f4580 with external eeprom 24cf32

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



Joined: 16 May 2015
Posts: 18
Location: istanbul-turkey

View user's profile Send private message

pic18f4580 with external eeprom 24cf32
PostPosted: Tue May 19, 2015 2:55 am     Reply with quote

hi ...
I'm trying to do simple read\write operation from eeprom 24c32 by pic 18f4580 but it doesn't work. I'm kind of beginner. Would anybody give me a hand ?
This is my code:
Code:

#include "D:\mohammet\eprom.h"

#define LCD_ENABLE_PIN PIN_C5
#define LCD_RS_PIN PIN_D2
#define LCD_RW_PIN PIN_D3
#define LCD_DATA4 PIN_D4
#define LCD_DATA5 PIN_D5
#define LCD_DATA6 PIN_D6
#define LCD_DATA7 PIN_D7
#include <LCD.C>

#use i2c(MASTER, FAST, SCL=PIN_C3, SDA=PIN_C4, FAST)
#define CAN_USE_EXTENDED_ID FALSE
#include <can-18xxx8.c>
int8 EEPROM_WR = 0xA0;     
int8 EEPROM_RD = 0xA1;       

int read = 0;
int data = 128;
int16 loc;
int temp = 0;

void rand_write(int16 address, int data)
{
   i2c_start();               
   i2c_write(EEPROM_WR);   

   i2c_write(address);
   i2c_write(address>>8);       
   i2c_write(data);           
   i2c_stop();             
   delay_ms(1000);           
}

int rand_read(int16 address)
{
   i2c_start();             
   i2c_write(EEPROM_WR);   
                             
   i2c_write(address>>8);   
   i2c_write(address);       

   i2c_start();           
   i2c_write(EEPROM_RD);   
   read = i2c_read();         
   i2c_read(0);             
   i2c_stop();               
   return(read);
}




void main()
{
   can_init();
   

   can_set_mode(CAN_OP_CONFIG);
   
   BRGCON1.brp=3;
   BRGCON1.sjw=0;
   BRGCON2.prseg=2;
   BRGCON2.seg1ph=2;
   BRGCON2.sam=FALSE;
   BRGCON2.seg2phts=FALSE; 
   BRGCON3.seg2ph=2;
   BRGCON3.wakfil=TRUE;

   can_set_mode(CAN_OP_NORMAL);
   


   setup_adc_ports(NO_ANALOGS|VSS_VDD);
   setup_adc(ADC_CLOCK_DIV_2|ADC_TAD_MUL_0);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_SS_DISABLED);
   setup_wdt(WDT_OFF);
   setup_timer_0(RTCC_INTERNAL);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_ccp1(CCP_OFF);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   lcd_init();
  int r;
   
   while(true){
delay_ms(100);           
 
   loc = 0x0f;             
   temp = 5;               
   rand_write(loc, temp);   

   
   delay_ms(100);           
                     
   r = rand_read(loc);
   
   
printf(lcd_putc,"\n%d",temp);
 
}
}


Last edited by moha-affa on Tue May 19, 2015 4:08 am; edited 1 time in total
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Tue May 19, 2015 4:04 am     Reply with quote

A few tips.

1) Read the forum guidelines
2) Use the 'code' button.
3) Reduce code to a few lines which show the issue you've got.
4) Indent code.
5) Tell us what the code is doing and not doing.

Mike
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

Re: pic18f4580 with external eeprom 24cf32
PostPosted: Tue May 19, 2015 5:04 am     Reply with quote

There is CCS example code, 2432.c, that does you can use to talk to the EEPROM, or you can use as a basis for your own code.

That said, there are three things I can see wrong with your code:
1) In your write you send the low byte of the address first. You should send it second, just as you do in your read code.

2) In your read code, you have an extra unnecessary i2c_read. Change

Code:

   read = i2c_read();         
   i2c_read(0);     


to:

Code:

   read = i2c_read(0);


3) You do not wait for the EEPROM to complete its write. 2432.c shows how to do this. Your main loop code would work as it is, but wouldn't if you took out the 100ms delay between writing and reading. Note, as the example shows, you don't do this by delays, nor by polling after a write. Its better to actively poll the device to check its staus before you do any operation.

Also there are block/page modes of reading and writing. Whether you use them in your code is up to you, but personally I would expect any "driver" to support those as well; 2432.c doesn't. Thus I regard 2432.c as example code, to be the basis for your own code, rather than as a true driver, complete and ready to drop into any code.


Last edited by RF_Developer on Tue May 19, 2015 5:09 am; edited 1 time in total
temtronic



Joined: 01 Jul 2010
Posts: 9245
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Tue May 19, 2015 5:09 am     Reply with quote

As Mike says plus...
...but BEFORE you try your program....
Start with PCM P's 'I2C scanner' program located in the 'code library'. It will find all I2C devices attached to your PIC. His program works, locates all I2C chips so you KNOW you have good hardware.
Do you have he proper pullups on the I2C bus ? Something like 3k3-4k7 for 5 volt PICs should be 'OK'.

Also BEFORE you even run PCPp's pgm, have you got the PIC to run a simple '1Hz LED' program to confirm your hardware is OK ?

Jay
moha-affa



Joined: 16 May 2015
Posts: 18
Location: istanbul-turkey

View user's profile Send private message

Re: pic18f4580 with external eeprom 24cf32
PostPosted: Tue May 19, 2015 5:34 am     Reply with quote

RF_Developer wrote:
There is CCS example code, 2432.c, that does you can use to talk to the EEPROM, or you can use as a basis for your own code.

That said, there are three things I can see wrong with your code:
1) In your write you send the low byte of the address first. You should send it second, just as you do in your read code.

2) In your read code, you have an extra unnecessary i2c_read. Change

Code:

   read = i2c_read();         
   i2c_read(0);     


to:

Code:

   read = i2c_read(0);


3) You do not wait for the EEPROM to complete its write. 2432.c shows how to do this. Your main loop code would work as it is, but wouldn't if you took out the 100ms delay between writing and reading. Note, as the example shows, you don't do this by delays, nor by polling after a write. Its better to actively poll the device to check its staus before you do any operation.

Also there are block/page modes of reading and writing. Whether you use them in your code is up to you, but personally I would expect any "driver" to support those as well; 2432.c doesn't. Thus I regard 2432.c as example code, to be the basis for your own code, rather than as a true driver, complete and ready to drop into any code.



thank you very much ..
but i didnt understand your last point maybe because my bad english :( ..
would you make it more clear please ..
thank you again ...
RF_Developer



Joined: 07 Feb 2011
Posts: 839

View user's profile Send private message

PostPosted: Tue May 19, 2015 6:27 am     Reply with quote

The CCS example code is not really a "driver" because it doesn't do all the things the EEPROM can do. It just does the basic things: reading and writing bytes. Also, it only works with one address.
moha-affa



Joined: 16 May 2015
Posts: 18
Location: istanbul-turkey

View user's profile Send private message

PostPosted: Tue May 19, 2015 8:57 am     Reply with quote

thank you very much the example 2432.c worked but now i have an other problem ..

i did simple write/read program and worked but the new problem is :

when i tried to write in an address and read the value .. the value was there and this is good ... but when i uploded an other program with only read instruction(without writing) the value wasnt there !!!!
where is it ??? it has to be stored in that address ... am i wrong ????
and it has to be there forever or if i wrote again in that address
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