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 problem between 16f873(master) and 16f690(slave)

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



Joined: 17 Aug 2006
Posts: 1

View user's profile Send private message

I2C problem between 16f873(master) and 16f690(slave)
PostPosted: Mon Aug 20, 2007 2:40 am     Reply with quote

the problem is the i2c bus can't get the right data. after the first i2c_write() , SDA and SCL will both be low, and the routine will be dead.
master code
Code:
#include <16f873a.h>

#use   I2C(master, sda=PIN_A2, scl=PIN_A5,slow)
#USE   DELAY(CLOCK=4000000)
#FUSES   XT,NOWDT,PROTECT,PUT,NOBROWNOUT,NOLVP
#USE   RS232(BAUD=19200,PARITY=N,XMIT=PIN_C6,RCV=PIN_C7)

#use        FAST_IO(A)
#use        FAST_IO(B)
#use        FAST_IO(C)

char buffer;

int8   a=0x00;
int8   b=0x00;


void main(void)
{
   ENABLE_INTERRUPTS(INT_RDA);
   ENABLE_INTERRUPTS(GLOBAL);

   while(1)
   {


      if(buffer=='0')
      {
         buffer=0x00;

         i2c_start();
         delay_us(5);
         i2c_write(0x9A);
         delay_us(5);
         i2c_write(0x00);
         delay_us(5);

                  
         i2c_start();
         delay_us(5);
         i2c_write(0x9B);
         delay_us(5);
         a=i2c_read(1);
         b=i2c_read(0);
         delay_us(5);
         i2c_stop();
         
         putc(a);
         putc(b);
         putc(0x00);
      }
   }
}

#INT_RDA
void INTRDA(void)
{
   buffer=GETC();
}


slave code
Code:
#include <16F690.h>
#fuses INTRC_IO,NOPROTECT, NOBROWNOUT, NOMCLR, NOCPD, NOWDT, PUT, NOIESO, NOFCMEN
#use delay(clock=4000000)


#use i2c(SLAVE,sda=PIN_B4, scl=PIN_B6, address=0x9a,FORCE_HW)
#byte ANSELH = 0x011F
#bit SMP = 0x94.7
BYTE a;

#INT_SSP
void ssp_interupt ()
{
   BYTE incoming, state;
   SMP=0;
   state = i2c_isr_state();

   if(state < 0x80)                     //Master is sending data
   {
      incoming = i2c_read();
      if(state == 1)
         a = incoming;
   }
   if(state == 0x80)                     //Master is requesting data
   {
      i2c_write(0x01);
      delay_us(50);
      i2c_write(0x02);
   }
}

void main ()
{
   
   ANSELH=0x00;
   
   enable_interrupts(INT_SSP);
   enable_interrupts(GLOBAL);
   SMP=0;


   while (TRUE) {SMP=0;}
}
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