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

SPI on PIC18F4580 Master and PIC16F877A Slave

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







SPI on PIC18F4580 Master and PIC16F877A Slave
PostPosted: Thu May 15, 2008 10:38 am     Reply with quote

I'm trying to demo SPI interface between 18F4580 is Master and 16F877A is slave, used 4 wire: SDI, SDO, SCK and nSS.
This is my code for Master:
Code:

#define SS_LOW (output_low(pin_A5)) //pin RA5 conected to nSS in 16F877A
#define SS_HIGH (output_high(pin_A5))

void main()
{
   byte rs;

setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_CLK_DIV_4|SPI_SAMPLE_AT_END);
   
while(1)
{
   delay_ms(3000);
   SS_LOW;       
   spi_write(0x55);
   rs = spi_read();
   SS_HIGH;
   led_perform(1);       // led on and off in 200ms when transmitting is completed
}
}


//And this my code for slave
Code:
void main()
{
   byte kq;
   setup_spi(SPI_SLAVE);
while(1)
   {   
   kq = spi_read();
   delay_us(100);
   output_d(kq);     //output kq on port D
   spi_write(0);

   /*if(spi_data_is_in())
   {
   led_perform(1);             //led on and off one time
   if(SSPBUF==0x55)
   {
   led_perform(3);          //led on and off three time
   }
   delay_ms(1000);   
   }*/

   }
}

I compiled this code completed, and loaded it in the PIC.
But the slave didn't receive any bit from Master, had not perform any thing (led on-off 0 time, and port D perform nothing)
Please tell me why???
Thanks guys.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Thu May 15, 2008 11:46 am     Reply with quote

Look at the CCS example files for an SPI slave:
Quote:
c:\Program Files\picc\Examples\Ex_spi_slave.c
c:\Program Files\picc\Examples\Ex_spi.c


This thread has code that demonstrates an SPI slave
but it's done using one board (because it's a demonstration).
http://www.ccsinfo.com/forum/viewtopic.php?t=26888
quangphongit
Guest







Experiented
PostPosted: Thu May 15, 2008 11:47 pm     Reply with quote

My kit was run very well!
The problem is, after init the SPI interface, should add delay_ms(100) and after each spi_read() should add delay_ms(1). I tried and succeeded.

the code here
Code:

//MASTER
void main()
{
   setup_spi(SPI_MASTER|SPI_L_TO_H|SPI_CLK_DIV_4|SPI_SAMPLE_AT_END);   // TODO: USER CODE!!
   delay_ms(100);
   
while(1)
{
   SS_LOW;
   rs = spi_read(0x55);
   delay_ms(1);
   SS_HIGH;
   
   if(rs==0x33)
   {
      led_perform(1);
   }
}
}



Code:

void main()
{
   setup_spi(SPI_SLAVE);   
   delay_ms(100);
   while(1)
   {
   kq = spi_read();
   delay_ms(1);
   spi_write(0x33);
   if(kq==0x55)
   {
         led_perform(1);
   }
}
}

This code is running well, SPI transfer well.
I post this reply to show the basic transfer of SPI for who don't know this, as me yesterday.

Thanks guys
art
Guest







2 wire SPI
PostPosted: Tue Feb 02, 2010 4:48 am     Reply with quote

Has anybody implemented a 2-wire SPI in a PIC16F877A or in any microcontroller as the master? I need some info for this because my slave IC which is an energy IC is configured as 2 wire SPI. All I need is to supply serial clock from the PIC to the slave so that I can get the data. The data comes from the slave.

Is it possible that a PIC can be configured as 2 wire SPI using only the SCK and SDI pin? Will the PIC (master) still supply a serial clock if it is configured as 2 wire SPI?
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