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

I2C Communication Problem

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



Joined: 22 Jul 2013
Posts: 2

View user's profile Send private message

I2C Communication Problem
PostPosted: Mon Jul 22, 2013 8:24 am     Reply with quote

HELLO!!

I want to communicate 2 pics (16f877a) using i2c bus. Its an extremely simple code just to see if its working or not. In this code, I simply want to receive data from the slave and print it on a 2x16 LCD. However on the PROTEUS simulation i only see "FF" transferred. Here is the code for reference.

Code:
 
#include <16F877A.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES HS                       //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES WRT_50%                  //Lower half of Program Memory is Write Protected

#use delay(clock=20000000)
#use i2c(Slave,Fast,sda=PIN_C4,scl=PIN_C3,force_hw,adress=0x90)

int8 n=15,p;
#int_SSP
void  SSP_isr(void)
{  int8 state,incoming;
   
   state=i2c_isr_state();
   
   if((state >= 0x80));
   {
     i2c_write(n);
   } 


}


void main()
{

   setup_adc_ports(AN0);
   setup_adc(ADC_CLOCK_INTERNAL);
   setup_psp(PSP_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
   setup_timer_2(T2_DIV_BY_1,1,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   enable_interrupts(INT_SSP);
   enable_interrupts(GLOBAL);

   // TODO: USER CODE!!

 while(1)
 {
   
  enable_interrupts(GLOBAL);
   
 }
}



MASTER

Code:


#include <16F877A.h>
#device adc=8

#FUSES NOWDT                    //No Watch Dog Timer
#FUSES HS                       //High speed Osc (> 4mhz for PCM/PCH) (>10mhz for PCD)
#FUSES NOPUT                    //No Power Up Timer
#FUSES NOPROTECT                //Code not protected from reading
#FUSES NODEBUG                  //No Debug mode for ICD
#FUSES NOBROWNOUT               //No brownout reset
#FUSES NOLVP                    //No low voltage prgming, B3(PIC16) or B5(PIC18) used for I/O
#FUSES NOCPD                    //No EE protection
#FUSES WRT_50%                  //Lower half of Program Memory is Write Protected

#use delay(clock=20000000)
#use i2c(Master,Fast,sda=PIN_C4,scl=PIN_C3,force_hw)

#include <flex_lcd_b bacak.c>

int8 data;

void main()
{

   setup_adc_ports(NO_ANALOGS);
   setup_adc(ADC_OFF);
   setup_psp(PSP_DISABLED);
   setup_timer_0(RTCC_INTERNAL|RTCC_DIV_1);
   setup_timer_1(T1_INTERNAL|T1_DIV_BY_1);
   setup_timer_2(T2_DIV_BY_1,1,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);

   // TODO: USER CODE!!
   lcd_init();
   delay_ms(50);
   printf(lcd_putc,"\f");
   printf(lcd_putc,"yazi=\n");
   while(1)
   {
     i2c_start();
     i2c_write(0x91);
     delay_us(50);
     //i2c_write(0x05);
     //delay_us(50);
     //i2c_stop();
     //i2c_start();
     //i2c_write(0x91);
     data=i2c_read(0);
     i2c_stop();
     printf(lcd_putc,"%d",data);
     
   
   }

}



I would really appreciate if you could help
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Jul 22, 2013 10:27 am     Reply with quote

I don't know how to make it work in Proteus. Proteus has weird things
like digital and analog pull-ups. It also may not work without the i2c
debugger turned on. I don't want to solve these problems. I only
use hardware to test i2c. The following method works with two hardware
boards (or two PICs on one board):

Use the CCS example file code, Ex_Slave.c, for the Slave PIC.
It's in this directory:
Quote:

c:\Program Files\picc\Examples\Ex_Slave.c


Use the code in this post for the Master PIC:
http://www.ccsinfo.com/forum/viewtopic.php?t=32368&start=3

Use these connections:
Code:


                +5v
                 |
                 <
                 > 4.7K       
To               <         
Master PIC       |           To Slave PIC
SDA pin   ------------------ SDA pin 
(pin C4)                     (pin C4)   

                +5v
                 |
                 <
                 > 4.7K       
To               <         
Master PIC       |           To Slave PIC
SCL pin   ------------------ SCL pin 
(pin C3)                     (pin C3)   


To
Master PIC                  To slave PIC
ground    ----------------- ground (Vss pins) 
(Vss pins)       |
               -----
                ---  Ground
                 - 
blacknyellow



Joined: 22 Jul 2013
Posts: 2

View user's profile Send private message

PostPosted: Tue Jul 23, 2013 4:04 am     Reply with quote

Thank you sir. Maybe i should test my code on the board
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