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

PIC24 I2C Read System Freeze

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



Joined: 08 Oct 2009
Posts: 3

View user's profile Send private message

PIC24 I2C Read System Freeze
PostPosted: Thu Oct 08, 2009 8:35 am     Reply with quote

I am using a PIC24FJ16GA002. For my I2C communication I am using pins 17 and 18 (I2C1). I have tried using SW mode and it gives me incorrect I2C timing, the clock transitions appear at the same time as data transitions. But no hangs occur. When I attempt to use HW mode my timing works well with the transitions on both lines where they are supposed to be. But in this mode I hang after my first read command. After this hang I can not simply reset the pic I have to physically disconnect my ICD3 from my usb and reconnect it. I am using MPLAB.
Code:
#rom 0x2BFC = {0x79C7}
#rom 0x2BFE = {0x321F}

#use I2C(I2C1, MASTER, SLOW, FORCE_HW)
#pin_select IC1=PIN_B0

unsigned int8 X, Y = 0;
 
void main()
{   
   setup_oscillator( OSC_INTERNAL, 32000000);

   setup_timer2(TMR_INTERNAL | TMR_DIV_BY_1);
   setup_timer4(TMR_INTERNAL | TMR_DIV_BY_1);
   setup_timer3(TMR_INTERNAL | TMR_DIV_BY_1);
   setup_timer5(TMR_INTERNAL | TMR_DIV_BY_1);
   
   setup_spi(SPI_SS_DISABLED);
   setup_spi2(SPI_SS_DISABLED);

   setup_wdt(WDT_ON);

   setup_timer1(TMR_INTERNAL|TMR_DIV_BY_1);

   while(1){
      X = 0x10;
      //   Write value
      i2c_start();
      i2c_write(0X58);   //   I2C Address with read bit
      i2c_write(1);      //   Memory Address
      i2c_write(X);      //   Value to write
      i2c_stop();
      delay_us(10);
      //   Read Value
      i2c_start();
      i2c_write(0X59);   //   I2C Address with write bit
      Y = i2c_read();      //   Read last memory written
      i2c_stop();
      delay_us(1000);
   }
}
Ttelmah
Guest







PostPosted: Thu Oct 08, 2009 9:26 am     Reply with quote

Er.
Check your addresses.
On I2C, the R/W bit is the bottom bit of the address, and is 'high' for a read. You have 0x58, commented as a 'read' address. This cannot be.
You appear to have your read, and write addresses reversed.....
The bottom bit, is R/!W.
High for a read, low for a Write.
Since you are trying to read, when the slave expects a write, it is not surprising you hang...

Best Wishes
natan



Joined: 08 Oct 2009
Posts: 3

View user's profile Send private message

PostPosted: Thu Oct 08, 2009 9:36 am     Reply with quote

Sorry about that I put my commenting in last minute to make it easier for the people on the forum to help be find the bug. My commenting is wrong but you will notice my usage case is correct. I am using 0x58 before my writes and 0x59 before my read command. Here is the code with the correct comments.
Code:
#rom 0x2BFC = {0x79C7}
#rom 0x2BFE = {0x321F}

#use I2C(I2C1, MASTER, SLOW, FORCE_HW)
#pin_select IC1=PIN_B0

unsigned int8 X, Y = 0;
 
void main()
{   
   setup_oscillator( OSC_INTERNAL, 32000000);

   setup_timer2(TMR_INTERNAL | TMR_DIV_BY_1);
   setup_timer4(TMR_INTERNAL | TMR_DIV_BY_1);
   setup_timer3(TMR_INTERNAL | TMR_DIV_BY_1);
   setup_timer5(TMR_INTERNAL | TMR_DIV_BY_1);
   
   setup_spi(SPI_SS_DISABLED);
   setup_spi2(SPI_SS_DISABLED);

   setup_wdt(WDT_ON);

   setup_timer1(TMR_INTERNAL|TMR_DIV_BY_1);

   while(1){
      X = 0x10;
      //   Write value
      i2c_start();
      i2c_write(0X58);   //   I2C Address with write bit
      i2c_write(1);      //   Memory Address
      i2c_write(X);      //   Value to write
      i2c_stop();
      delay_us(10);
      //   Read Value
      i2c_start();
      i2c_write(0X59);   //   I2C Address with read bit
      Y = i2c_read();      //   Read last memory written
      i2c_stop();
      delay_us(1000);
   }
}
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