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

16F88 I2C master slave problem

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



Joined: 20 Mar 2005
Posts: 32
Location: Heiloo, the Netherlands

View user's profile Send private message Visit poster's website

16F88 I2C master slave problem
PostPosted: Thu Nov 13, 2008 3:17 pm     Reply with quote

Hi,

I'm using two 16F88 to communicate via i2c.
The master is reading two bytes, the slave is writing two bytes.
I'm using version 4.013.

This is the master code
Code:

#include <16F88.h>
#device *=16
#device adc=10

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#FUSES PUT                    //No Power Up Timer
#FUSES MCLR                     //Master Clear pin enabled
#FUSES BROWNOUT                 //Reset when brownout detected
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES NOWRT                    //Program memory not write protected
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOPROTECT                //Code not protected from reading
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES IESO                     //Internal External Switch Over mode enabled

#use delay(clock=8000000)
#use rs232(baud=19200,parity=N,xmit=PIN_B5,rcv=PIN_B2,bits=8)
#use i2c(Master,sda=PIN_B1,scl=PIN_B4)
int8 address;


#int_RDA
RDA_isr()
{
   char c;
   c = getc();
   putc( c );
}

#define LED_RED PIN_A4
#define LED_YELLOW PIN_A3
#define LED_GREEN PIN_A2
#define SLAVE 0xA0
void main()
{
   int byte1, byte2;
   setup_adc_ports(sAN0|VSS_VDD);
   setup_adc(ADC_CLOCK_INTERNAL);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DIV_BY_16,2,4);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   enable_interrupts(INT_RDA);
   enable_interrupts(GLOBAL);
   setup_oscillator(OSC_8MHZ);

   delay_ms( 100 );
   printf("\f");
   while(1){
      output_high( LED_GREEN );
      delay_ms( 500 );
      output_low( LED_GREEN );
      delay_ms( 500 );

      i2c_start();
      putc('S');
      i2c_write(SLAVE + 1);
      putc('W');
      byte1 = i2c_read();
      putc('R');
      byte2 = i2c_read(0);
      putc('R');
      i2c_stop();
      putc('T');
      printf( " byte1=%02x, byte2=%02x\n\r" , byte1, byte2 );
   }
}

Why MUST the second i2c_read be called with an argument 0? If I dont do that the master will hang.

This is the slave code
Code:

#include <16F88.h>
#device *=16
#device adc=10

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES INTRC_IO                 //Internal RC Osc, no CLKOUT
#FUSES PUT                    //No Power Up Timer
#FUSES NOMCLR                     //Master Clear pin enabled
#FUSES BROWNOUT                 //Reset when brownout detected
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES NOWRT                    //Program memory not write protected
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOPROTECT                //Code not protected from reading
#FUSES FCMEN                    //Fail-safe clock monitor enabled
#FUSES IESO                     //Internal External Switch Over mode enabled

#use delay(clock=8000000)
#use rs232(baud=19200,parity=N,xmit=PIN_B5,rcv=PIN_B2,bits=8)
#use i2c(Slave,sda=PIN_B1,scl=PIN_B4,address=0xA0)

#define LED_RED PIN_A4
#define LED_YELLOW PIN_A3
#define LED_GREEN PIN_A2

#int_RDA
RDA_isr()
{
   char c;
   c = getc();
   putc( c );
}

unsigned int incomming = 0, state;
#int_SSP
SSP_isr()
{
  //putc('.');
   state = i2c_isr_state();
   if( state < 0x80)
   {
   }
   if(state >= 0x80)
   {
      if(state == 0x80){
         OUTPUT_TOGGLE( LED_GREEN );
         putc('1');
         i2c_write(0x56);
      }
      else if(state == 0x81){
         OUTPUT_TOGGLE( LED_YELLOW );
         i2c_write(0x1F);
         putc('2');
      }
      else if(state == 0x82){
        OUTPUT_TOGGLE( LED_RED );
        i2c_write(0x45);
        putc('3');
      }
   }
}



void main()
{

   setup_adc_ports(sAN0|VSS_VDD);
   setup_adc(ADC_CLOCK_INTERNAL);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DIV_BY_16,2,4);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   enable_interrupts(INT_RDA);
   enable_interrupts(INT_SSP);
   enable_interrupts(GLOBAL);
   setup_oscillator(OSC_8MHZ);

   delay_ms(100);
   printf("\f");
   while(1){
      delay_ms( 1000 );
   }
}


Now the data is read fine, but in the terminal of the slave I see on every read action of the master "123". Also the three leds toggle. Indicating to
me that 3 bytes are read.

When I put a putc('.'); in the beginning of the ISR I get ".1.2" in the terminal of the slave. Indicating to me that only two bytes are read.

What am I doing wrong, can
_________________
checkout my site: www.ymoona.com/wiki
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Nov 13, 2008 3:29 pm     Reply with quote

Quote:
Why MUST the second i2c_read be called with an argument 0?

It's part of the i2c specification. You can download it here:
http://www.nxp.com/acrobat_download/literature/9398/39340011.pdf
Look in this section, on the right side of the page:
Quote:
7.2 Acknowledge

It says what an i2c master must do when it's clocking bytes out of
the slave:
Quote:
If a master-receiver is involved in a transfer, it must signal
the end of data to the slave-transmitter by not generating
an acknowledge on the last byte that was clocked out of
the slave. The slave-transmitter must release the data line
to allow the master to generate a STOP or repeated
START condition.

In CCS, a "not acknowledge" is generated by using a 0 parameter
in the i2c_read() statement.
ymoona



Joined: 20 Mar 2005
Posts: 32
Location: Heiloo, the Netherlands

View user's profile Send private message Visit poster's website

PostPosted: Thu Nov 13, 2008 3:42 pm     Reply with quote

Thanks, that makes sense.
Do you also have an answer to my other questions?
_________________
checkout my site: www.ymoona.com/wiki
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