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

Problem with SPI communication between 16f877a and 30f4011

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



Joined: 16 Mar 2012
Posts: 31

View user's profile Send private message

Problem with SPI communication between 16f877a and 30f4011
PostPosted: Fri Mar 16, 2012 11:08 am     Reply with quote

Hi everyone,

I am trying to communicate a pic16f877a with a dsPic30f4011 but still failed. I hope some of you can figure out my problem. Thank you in advance.
Here is my code:
For Master (dsPic30f4011)
Code:

#include <30F4011.h>
#DEVICE ADC=10
#FUSES NOWDT, HS, NOPUT, NOPROTECT, NODEBUG, BROWNOUT, NOWRT
#FUSES XT_PLL8
#use delay(clock=4000000)
#use rs232(baud=4800, parity=N,xmit=PIN_F5,rcv=PIN_F4,bits=8))

#define SDI PIN_F2
#define SDO PIN_F3
#define SCK PIN_F6
#define SS  PIN_B2

#INT_SPI1
void spi()
{
   clear_interrupt(INT_SPI1);
}
void main()
{
   int8 a = 8;
   set_tris_b(0x00);
   set_tris_f(0b00000100);
   set_tris_e(0x00);
   setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_CLK_DIV_16|SPI_XMIT_L_TO_H);
   enable_interrupts(INT_SPI1);
   enable_interrupts(INTR_GLOBAL);
   output_high(SCK);
   output_high(SS);
   while(1)
   { 
      delay_ms(100);
      output_low(SS);
      delay_ms(10);
      spi_write(a);     
      output_e(a);
      a++;
      delay_ms(100);
      output_high(SS);
      delay_ms(3000);
      if (a >= 200 ) a = 0;
   }
}


For Slave:
Code:

#include <16f877a.h>
#device *=16                   
#FUSES NOWDT, HS, NOPUT, NOPROTECT
#use delay(clock=4000000)

#include "LCD_4bit.h"

#define SDI PIN_C4
#define SDO PIN_C5
#define SCK PIN_C3
#define SS  PIN_A5

int8 in_data = 0;
#INT_SSP
void spi()
{
   in_data = spi_read();
   output_c(in_data);
   lcd_gotoxy(1,1);
   printf(lcd_putc,"%u",in_data);
   clear_interrupt(INT_SSP);
}

void main()
{
   set_tris_a(0x00100000);
   set_tris_c(0b00010000);
   output_c(0);
   setup_spi(SPI_SLAVE|SPI_L_TO_H|SPI_CLK_DIV_16|SPI_XMIT_L_TO_H);
   enable_interrupts(INT_SSP);
   enable_interrupts(GLOBAL);
   lcd_init();
   delay_ms(20); 
   while(1)
   {
     
   }
}

Thanks again Smile
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Fri Mar 16, 2012 12:33 pm     Reply with quote

Here are some SPI slave examples:

This example uses software SPI for the master and
hardware SPI for the slave (all in the same PIC):
http://www.ccsinfo.com/forum/viewtopic.php?t=26888

Here is an example of hardware SPI for master and slave (using 2 PICs
on separate boards):
http://www.ccsinfo.com/forum/viewtopic.php?t=39145
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