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

30f4013 as i2c slave (solved and another weird problem)

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



Joined: 05 Jun 2006
Posts: 41
Location: Belgium

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

30f4013 as i2c slave (solved and another weird problem)
PostPosted: Sat Jan 24, 2009 2:34 pm     Reply with quote

hey,
can anybody provide me with a example to set up a 30f4013 as i2c slave?

how to set up the interrupt routine ?

Thanks
on7nh



Joined: 05 Jun 2006
Posts: 41
Location: Belgium

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

PostPosted: Sun Jan 25, 2009 5:56 am     Reply with quote

i have now communication,
my master code :
Code:

//   DS_plasma voeding date 07/12/2008
#include <30f4013.h>
#device adc=12
#FUSES NOWDT // Watch Dog Timer
#FUSES HS2_PLL16
#FUSES PR_PLL //Promiary Oscillator
#FUSES NOCKSFSM //Clock Switching is disabled, fail Safe clock monitor is disabled
#FUSES BROWNOUT //Reset when brownout detected
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOWRT //Program memory not write protected
#FUSES DEBUG //No Debug mode for ICD
#FUSES ICS0 //ICD communication channel 0


#use delay(clock=128000000)
#use rs232(baud=19200, UART1A)
#use i2c(Master,sda=PIN_F2,scl=PIN_F3,FORCE_SW,SLOW) // hw I2C
#include <math.h>
void main(void)
{
   setup_adc_ports(ALL_ANALOG);
    setup_adc(adc_clock_internal);
     printf("test\n");
   Prnt_display();
   
while(TRUE)
  {
    // Inlezen Potentiometer Freq    
         set_adc_channel(0);
         delay_ms(10);
         freq=read_adc();
   // Inlezen Potentiometer Power   
         set_adc_channel(1);
         delay_ms(10);
      //   dutycyl=read_adc();
         power=read_adc();
   // Inlezen Gemeten Spanning   
         set_adc_channel(2);
         delay_ms(10);
      //   dutycyl=read_adc();
         MeasU=read_adc();
   // Inlezen Gemeten Stroom   
         set_adc_channel(3);
         delay_ms(10);
      //   dutycyl=read_adc();
         MeasI=read_adc();



      Prnt_display();
                       
}

}

////////////////////////////////////////////////////////////////////////////////////////////////////
void Prnt_display()
{

         //write ingestelde frequentie
i2c_stop();   
               i2c_start();
            i2c_write(0xb0);
               i2c_write(200);
            i2c_write(205);
            i2c_write(104);
            i2c_stop();   



}





code slave :
Code:

//   DS_plasma voeding date 07/12/2008
#include <30f4013.h>
#device adc=12
#FUSES NOWDT // Watch Dog Timer
#FUSES HS2_PLL16
#FUSES PR_PLL //Promiary Oscillator
#FUSES NOCKSFSM //Clock Switching is disabled, fail Safe clock monitor is disabled
#FUSES BROWNOUT //Reset when brownout detected
#FUSES NOPROTECT //Code not protected from reading
#FUSES NOWRT //Program memory not write protected
#FUSES DEBUG //No Debug mode for ICD
#FUSES ICS0 //ICD communication channel 0

#use delay(clock=128000000)
#use rs232(baud=115200, UART1A)
#use i2c(SLAVE, SDA=PIN_F2, SCL=PIN_F3, address=0xb0)
#include <math.h>
////////////////////////////////////////////////////////////////////////////////////////////////////
   unsigned int16  measU,   measI;
   unsigned int16   Power, bargraph;
   unsigned int16 freq;
   unsigned int dyc;
   void Prnt_display();
   int16 keyw;
      int incoming, state;
   BYTE B1, B2, B3, B4;

float dutycyl;
////////////////////////////////////////////////////////////////////////////////////////////////////


#INT_SI2C
void SI2C_interupt ()
{
      state = i2c_isr_state();
//printf("state %u\n\r  ",state);
    if(state <= 0x80)                     //Master is sending data
   {
      incoming = i2c_read();
         if(state == 1)                 
            b1 = incoming;
printf("Inc1 %u\n\r  ",incoming);
         if(state == 2)                 
            b2 = incoming;
printf("Inc2 %u\n\r  ",incoming);
         if(state == 3)                   
            keyw = incoming;
printf("Inc3 %u\n\r  ",incoming);
      // toekennen i2c data
       if(keyw == 101)         
            {
         power = make16(b2,b1);
         keyw = 0;
         }
       if(keyw == 102)       
            {
         MeasU = make16(b2,b1);
         keyw = 0;
         }
       if(keyw == 103)         
            {
         MeasI = make16(b2,b1);
         keyw = 0;
         }
       if(keyw == 104)             
            {
         Freq = make16(b2,b1);
//printf("Inc %u\n\r  ",incoming);
      //   printf("I2c freq  %u\n\r  ",freq);
         keyw = 0;
         }
       if(keyw == 105)         
            {
         bargraph = make16(b2,b1);
         keyw = 0;
         }

   }
   if(state == 0x80)                     //Master is requesting data
   {
//      i2c_write(buffer[address]);
   }
}
/////////////////////////////////////////////////////////////////////////////////////

void main(void)
{
   
      char selection;
      setup_compare(1, COMPARE_PWM | COMPARE_TIMER2);
   setup_adc_ports(NO_ANALOGS);
//    setup_adc(adc_clock_internal);
   enable_interrupts(intr_GLOBAL);
   enable_interrupts(int_SI2C);
   enable_interrupts(int_MI2C);
    
   dutycyl = 10;

while(TRUE)
  {

         dutycyl = freq;
         setup_timer2(TMR_INTERNAL | TMR_DIV_BY_1, 2000);
         //dutycyl = 1000;
         dyc =(ceil(2000-(2000*dutycyl/4000)));
      //   printf("dyc  %u         freq  %u\n\r  "dyc,freq);
         set_pwm_duty(1,dyc); // This sets the time the pulse is
                            
}

}

////////////////////////////////////////////////////////////////////////////////////////////////////


After the I2C_read command I print now for debugging,
address is 0Xb0 because there is also another slave with address 0xa0 (pic18f452)
Isr_state is incrementing every time I write to the slave.
but I receive always 255 in I2C_read.
Shocked Shocked Shocked
Any suggestions?

ps to CCS.....why is in the PCD manual still wrong about the interrupts ?
Int_SSP is not possible in 30F Confused .....
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