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

interfacing 2 Pic16f877 via spi

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



Joined: 25 Mar 2010
Posts: 5

View user's profile Send private message

interfacing 2 Pic16f877 via spi
PostPosted: Thu Mar 25, 2010 1:13 am     Reply with quote

I m using MpLab v7.01 & want to interface 2 pic16f877 controller in master/slave mode via spi. Wanted source codes for master & slave of transferring any data between them.
hayee



Joined: 05 Sep 2007
Posts: 252

View user's profile Send private message

PostPosted: Thu Mar 25, 2010 3:38 am     Reply with quote

Here is an example for SPI communication. One master and one slave.
In this code master reads the analog data and send to the slave using SPI.

For master
Code:

void main()
{
   float adc1,adc2,value1;
   int b0,b1,b2,b3;
   setup_adc_ports(ALL_ANALOG);
   setup_adc(ADC_CLOCK_INTERNAL);
   setup_psp(PSP_DISABLED);
   setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_CLK_DIV_4);
   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);

   // TODO: USER CODE!!
  while(1)
  {
   if (input(PIN_A5)==0)
   {
   set_adc_channel(0);
   delay_ms(10);
   adc1=read_adc();
   adc2=(adc1*5)/1024;
   value1=adc2;
   printf("value is: %f \r\n",value1);
   {
   union convert
    {
     int8 b[4];
     float fval;
    }
    val;
   
    val.fval=value1;
   
    b0=val.b[3];
    b1=val.b[2];
    b2=val.b[1];
    b3=val.b[0];
   }
    spi_write(b0);
    spi_write(b1);
    spi_write(b2);
    spi_write(b3);
   }
   else
      {
         delay_cycles(1);
      }
  }
}


For slave
Code:

float readnum(void);
void main()
{
   float adc;
   setup_adc_ports(ALL_ANALOG);
   setup_adc(ADC_CLOCK_INTERNAL);
   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);
   SET_TRIS_B (0xF0);
   // TODO: USER CODE!!
   output_high(PIN_A5);
   setup_spi(SPI_SLAVE | SPI_L_TO_H | SPI_CLK_DIV_4);
   {
   while(1)
    {
    adc=readnum();
    printf(" %f ",adc);
    delay_ms(100);
    }
   }


float readnum(void)
{
   int8 b0,b1,b2,b3;
   float result,value;
   int32 x;
   output_low(PIN_A5);
   delay_us(100);
   b0=spi_read(0);
   b1=spi_read(0);
   b2=spi_read(0);
   b3=spi_read(0);
   
   x=make32(b0,b1,b2,b3);
   {
   union convert
   {
   int32 y;
   float fval;
   }
   val;
   
   val.y=x;
   result=val.fval;
   }
   
   output_high(PIN_A5);
   return result;
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu Mar 25, 2010 11:59 am     Reply with quote

See this thread for more SPI slave examples:
http://www.ccsinfo.com/forum/viewtopic.php?t=41025
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