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

pic16f877a i2c slave problem

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



Joined: 29 Jul 2011
Posts: 12

View user's profile Send private message

pic16f877a i2c slave problem
PostPosted: Fri Jul 29, 2011 8:25 am     Reply with quote

Hi All,
I have a problem; I could not simulate ex_slave example.

My master code:
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 PROTECT                  //Code protected from reads
#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 NOWRT                    //Program memory not write protected

#use delay(clock=20000000)
#use i2c(Master,Slow,sda=PIN_C4,scl=PIN_C3,force_hw)#include <2402.C>

byte gelen=0;
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_DISABLED);
   setup_timer_2(T2_DISABLED,0,1);
   setup_comparator(NC_NC_NC_NC);
   setup_vref(FALSE);
   init_ext_eeprom();

   // TODO: USER CODE!!
  gelen= read_ext_eeprom(0x03);
  while(1);
  }

My slave code is
Code:

#if defined(__PCM__)
#include <16F877.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)

#elif defined(__PCH__)
#include <18F452.h>
#fuses HS,NOWDT,NOPROTECT,NOLVP
#use delay(clock=20000000)
#use rs232(baud=9600, xmit=PIN_C6, rcv=PIN_C7)
#endif

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

BYTE address, buffer[0x10];


#INT_SSP
void ssp_interupt ()
{
   BYTE incoming, state;

   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 ()
{
int i=0;
   enable_interrupts(GLOBAL);
   enable_interrupts(INT_SSP);

for (i=0;i<16;i++)
{buffer[i]=0x37;}
   while (TRUE) {}
}

and my problem: Received data all times 80. Where is my mistake? I could not find. Here is a Proteus picture:


I am sorry for my broken English.
_________________
trainee
temtronic



Joined: 01 Jul 2010
Posts: 9163
Location: Greensville,Ontario

View user's profile Send private message

PostPosted: Fri Jul 29, 2011 8:51 am     Reply with quote

Probably a Proteus problem as it is full of bugs, errors, glitches etc.
You should use real hardware.
If you can't contact a Proteus help forum or the mfr of the simulator.

There are working code examples in the CCS examples folder you might consult, see if they work under Proteus.
mehmetem



Joined: 29 Jul 2011
Posts: 12

View user's profile Send private message

Thank you, I solved problem.
PostPosted: Wed Aug 03, 2011 12:57 am     Reply with quote

Smile
_________________
trainee
PEDE



Joined: 10 Sep 2011
Posts: 1

View user's profile Send private message

pic16f877a i2c slave problem
PostPosted: Sat Sep 10, 2011 10:29 am     Reply with quote

How did you fix your problem? Thx...
mehmetem



Joined: 29 Jul 2011
Posts: 12

View user's profile Send private message

There is not anything, Don't forget pull-up' MY WORKING CODE
PostPosted: Sat Sep 10, 2011 2:12 pm     Reply with quote

Code:

BYTE address, BufferTxt[20], buffer[20];



#INT_SSP
void ssp_interupt ()
{
   BYTE incoming, state;

   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]);
   }
}


AND MASTER

Code:
 while(1)
   {
 
  init_ext_eeprom();
 PresetNumber= read_ext_eeprom(0x09);
 
 if (PresetNumber!=PresetNumberOld)
 {

 
  CalcPhase();
 
     yerptr=&(Rpm);
     countL=*(yerPtr);
     write_ext_eeprom(0x07,CountL);

     countH=*(yerPtr+1);
     write_ext_eeprom(0x08,CountH);
     delay_ms(40);
 


 }

_________________
trainee
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