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

24c32 losing data(Solved)

 
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

24c32 losing data(Solved)
PostPosted: Wed May 20, 2015 1:22 am     Reply with quote

hi ..
I used 2432.c library for writing and reading from 24c32 by pic 18f4580.
I wrote a value in an address and its supposed to be kept in that address even when power removed. I wrote another program with only read operation from that address and uploaded it on pic but i found 0 there. I didnt find the value i wrote.

First i uploaded this program to do writing:
Code:

#include "D:\mohammet\eprom test.h"
#ifndef EEPROM_SDA   
   
#define EEPROM_SDA  PIN_C4   
#define EEPROM_SCL  PIN_C3   
   
#endif   
   

#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>
#define hi(x)  (*(&x+1))
#use i2c(MASTER, SCL=PIN_C3, SDA=PIN_C4)

#define CAN_USE_EXTENDED_ID FALSE
#include <can-18xxx8.c>

#define EEPROM_ADDRESS long int   
#define EEPROM_SIZE    4096   
void init_ext_eeprom() {   
   output_float(EEPROM_SCL);   
   output_float(EEPROM_SDA);   
}   
   
BOOLEAN ext_eeprom_ready() {   
   int1 ack;   
   i2c_start();            // If the write command is acknowledged,   
   ack = i2c_write(0xa0);  // then the device is ready.   
   i2c_stop();   
   return !ack;   
}   
   
void write_ext_eeprom(long int address, BYTE data) {   
   while(!ext_eeprom_ready());   
   i2c_start();   
   i2c_write(0xa0);   
   i2c_write(hi(address));   
   i2c_write(address);   
   i2c_write(data);   
   i2c_stop();   
}   
   
   
BYTE read_ext_eeprom(long int address) {   
   BYTE data;   
   
   while(!ext_eeprom_ready());   
   i2c_start();   
   i2c_write(0xa0);   
   i2c_write(hi(address));   
   i2c_write(address);   
   i2c_start();   
   i2c_write(0xa1);   
   data=i2c_read(0);   
   i2c_stop();   
   return(data);   
}   


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();
   
   BYTE counter,f,read;
   long int loc = 0x16;
 
   write_ext_eeprom(loc,114);
   read=read_ext_eeprom(loc);
   printf(lcd_putc,"\f%d",read);
 
 while(1);
}


and after i uploaded it on pic and connect to power the address hold the value 114.
After that i removed writing instruction and kept reading and uploaded it on pic, but the value was 0 not 114. I thought that the 114 will stay in that address.

what's wrong with it guys .....


Last edited by moha-affa on Wed May 20, 2015 3:52 am; edited 1 time in total
Ttelmah



Joined: 11 Mar 2010
Posts: 19499

View user's profile Send private message

PostPosted: Wed May 20, 2015 2:25 am     Reply with quote

1) A test program should _just_ test one thing. Get rid of all the can etc.. Just talk to the EEPROM.

2) Is SPI_SS_DISABLED, how to disable the SPI?. Since the SPI is on the I2C pins, having this setup incorrectly is not a good start. The #USE I2C has already setup the MSSP peripheral. This may well be setting it up incorrectly....

3) You are using a complex way to get the high byte, that is slow, and does not necessarily work. It does not necessarily work, since it relies on &x, being a byte pointer, which it is not guaranteed to be, unless you cast it to be this, and complex, since the compiler has the make8 function, that can return the high byte in a single instruction (both in C, and in the processor instruction set). The address being used may well be wrong because of this.

4) Specify a baud rate with the #USE I2C. Never assume defaults will be right for your chip.

5) Test the I2C first. Use the bus scanner in the code library and verify the chip is being seen.
moha-affa



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

View user's profile Send private message

PostPosted: Wed May 20, 2015 3:50 am     Reply with quote

thank you very much man its working now ...

the problem was in the second or the third point of what you said ..
i've changed both of them and now its working ....
thank you again
jeremiah



Joined: 20 Jul 2010
Posts: 1345

View user's profile Send private message

PostPosted: Wed May 20, 2015 5:49 am     Reply with quote

An additional note to hopefully save you later:

You have
Code:

   BYTE counter,f,read;
   long int loc = 0x16;

declared in the middle of code instead of at the beginning of the function. While the compiler won't complain about this, it sometimes leads to really horrible bugs that are very difficult to trace (variables overwriting variables, etc.).

You should really move those variable declarations to the top of the function.
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