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

i2c troubles - again

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



Joined: 05 Jan 2008
Posts: 10

View user's profile Send private message

i2c troubles - again
PostPosted: Thu May 08, 2008 5:50 pm     Reply with quote

I'm having i2c troubles again, with a very similar setup as last time.

Currently I'm using a 16F88 as the master device, and an 18F2431 as a slave device. Both running from the same supply. I have tried both 4k7 & 2k7 pullups on the i2c bus. The circuit seems to be ok.

As you can see in the code below, a led should turn on if the ISR on the slave is called. It never turns on. The printf in the master code returns 'ff'.
Both leds successfully blink, however, the slave seems to lock up sometimes.

Kind regards,

Erik

PS: compiler version 4.057 (poor student - can't upgrade)

Slave:

Code:
#include <18f2431.h>
#fuses INTRC_IO,NOWDT,NOPROTECT,NOLVP
#use delay(clock=8000000)
#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C5, address=0xa0,force_hw)

BYTE address, buffer[0x10];

#INT_SSP
void ssp_interupt ()
{
   BYTE incoming, state;
   output_low(pin_a2); //turn on a LED
 

   state = i2c_isr_state();

   if(state < 0x80)                     //Master is sending data
   {
      incoming = i2c_read();
      if(state == 1)                     //First received byte is address
         address = incoming;
      if(state == 2)                     //Second received byte is data
         buffer[address] = incoming;
   }
   if(state == 0x80)                     //Master is requesting data
   {
      i2c_write(buffer[address]);
   }
}

void main ()
{
   enable_interrupts(GLOBAL);
   enable_interrupts(INT_SSP);

 
   while (TRUE) {
      //blink led
      output_low(pin_a3);
      delay_ms(1000);
      output_high(pin_a3);
      delay_ms(1000);
      }
}


Master:

Code:

#include <16f88.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=25000000)
#use i2c(MASTER,slow, SDA=PIN_b1, SCL=PIN_b4,force_sw)

#use rs232(baud=9600, xmit=PIN_b5, rcv=PIN_B2)


void main()
{
   // Write the letter 'B' to the slave board.
   int8 data;
   printf("init");
   
   i2c_start();
   i2c_write(0xA0);
   i2c_write(0x00);
   i2c_write('B');
   i2c_stop();

   delay_ms(500);
   // Read from the slave board and display the data.
   i2c_start();
   i2c_write(0xA0);
   i2c_write(0x00);
   i2c_start();
   i2c_write(0xA1);
   data = i2c_read(0);
   i2c_stop();
   printf("read %x \n\r", data);
   
   while(TRUE){
      //blink
      output_low(pin_a0);
      delay_ms(1000);
      output_high(pin_a0);
      delay_ms(1000);
   }
}


Last edited by erik006 on Thu May 08, 2008 7:03 pm; edited 1 time in total
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu May 08, 2008 6:16 pm     Reply with quote

Quote:
#use i2c( SDA=PIN_C4, SCL=PIN_C5, address=0xa0,force_hw)

This statement is missing the 'Slave' parameter.
erik006



Joined: 05 Jan 2008
Posts: 10

View user's profile Send private message

Re
PostPosted: Thu May 08, 2008 7:05 pm     Reply with quote

Good catch, PCM programmer, although that's not the problem. In trying to eliminate possibilities I tried a version without "SLAVE" although there's probably not much sense in that. It doesn't work with slave added either. (I edited my main post)

Erik
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu May 08, 2008 7:21 pm     Reply with quote

Quote:
#include <16f88.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=25000000)
#use i2c(MASTER,slow, SDA=PIN_b1, SCL=PIN_b4,force_sw)

The 16F88 data sheet doesn't say that it will run at 25 MHz.
It says the limit is 20 MHz:
Quote:

• Three Crystal modes:
- LP, XT, HS: up to 20 MHz
• Two External RC modes
• One External Clock mode:
- ECIO: up to 20 MHz
erik006



Joined: 05 Jan 2008
Posts: 10

View user's profile Send private message

Re:
PostPosted: Thu May 08, 2008 7:25 pm     Reply with quote

Good point,but I just didn't have a 20 mhz available. I did try the internal oscillator, an 8mhz crystal & a 12mhz crystal which had the same result.
rnielsen



Joined: 23 Sep 2003
Posts: 852
Location: Utah

View user's profile Send private message

PostPosted: Fri May 09, 2008 9:37 am     Reply with quote

It's possible you are having timing issues. The master might be sending commands faster than the slave can process them.

If you have an o-scope available, try setting an output as soon as the ISR is entered and then reset the output just before the ISR is exited. Monitor the I2C bus and this output and you will be able to see if commands are being sent too quickly. It's possible that you need to add delays to allow the slave time to do it's thing.

Ronald
_________________
Never take a sleeping pill and a laxitive at the same time.
erik006



Joined: 05 Jan 2008
Posts: 10

View user's profile Send private message

Re:
PostPosted: Mon May 12, 2008 12:53 pm     Reply with quote

Mielsen, that's very possible, as the slave is using an internal osc (8Mhz), while the master was running at 12Mhz or more. Here are several things I tried.
-Using the 16F88 as the slave, everything is fine & works. This implies the master code is fine.
-Using another 18f2431 does not work.
-When I use a 20Mhz crystal as the clock source, using i2c_poll() seems to work, however, no interrupt occurs.

Thanks for your help so far!

Erik
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