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 problems

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



Joined: 24 Oct 2009
Posts: 12

View user's profile Send private message

i2c problems
PostPosted: Sat Oct 24, 2009 7:49 pm     Reply with quote

Hi guys,
I'd like to send data from master (in one PIC 16f877a) and receive the data from slave (in another PIC 16f877a). I don't know why the code does not work right. I check the output from the master and clk and sda look fine, but when I connect two lines to slave then check the signal again, the clock is low (0) and the data is high (1). Here is my code:


Master:
Code:

#include <16F877A.h>
#device adc=8
#fuses NOWDT,RC, NOPUT, NOPROTECT, NODEBUG, BROWNOUT, LVP, NOCPD, NOWRT
#use delay(clock=20000000)

#use I2C(master, sda=PIN_C4, scl=PIN_C3)

void sendData(BYTE data);
void main()
{

   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_spi(FALSE);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

    while (1){
      delay_ms (5);
      sendData(0b11111111);
     }

}

void sendData(BYTE data){
   i2c_start();    // Start condition
   delay_cycles(5);
   i2c_write(0xa0);// Device address
   delay_cycles(5);
   i2c_write(data);// Low byte of command
   delay_cycles(5);
   i2c_stop();
}


Slave:
Code:

#include <16F877A.h>
#device adc=8
#fuses NOWDT,RC, NOPUT, NOPROTECT, NODEBUG, BROWNOUT, LVP, NOCPD, NOWRT
#use delay(clock=20000000)

#use i2c(SLAVE, SDA=PIN_C4, SCL=PIN_C3, address=0xa0)

unsigned int dat;

#INT_SSP
void ssp_interupt ()
{
   //clear_interrupt(int_SSP);
   //while(!i2c_poll()) ;
   dat = i2c_read();
   i2c_write(0);
}


void main()
{
   set_tris_c(0xFF);

   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_spi(FALSE);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

   enable_interrupts(INT_SSP);      //enable I2C interrupts
   enable_interrupts(GLOBAL);

   //dat=0;

   while(1){
     // while(!i2c_poll()) ;
     // dat = i2c_read();
      if (dat==0b11111111) {
         output_high(PIN_A0);
      } else {
         output_low(PIN_A0);
      }
   }
}

I want to compare the received data and sending data and ensure that it is same.

Thanks.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Sat Oct 24, 2009 9:15 pm     Reply with quote

Try the test given in this post:
http://www.ccsinfo.com/forum/viewtopic.php?t=37876&start=1
Remember to include the pull-up resistors. If it doesn't work, then post
your PICs (master and slave) and your compiler version.

More information:
http://www.ccsinfo.com/forum/viewtopic.php?t=39242
ncvnmarket



Joined: 24 Oct 2009
Posts: 12

View user's profile Send private message

PostPosted: Sun Oct 25, 2009 4:11 am     Reply with quote

I followed the code you gave me and modified it and It works...
Thanks so much
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