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

SPI comm

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



Joined: 31 Mar 2011
Posts: 51
Location: Mexico

View user's profile Send private message

SPI comm
PostPosted: Thu Jun 15, 2017 9:05 am     Reply with quote

I'm new in SPI comms. I need to communicate two PICs:
16F887 Master
16F886 Slave

I've written the following two programs:

MASTER:
Code:
#include<16f887.h>
#use delay (clock = 20 MHz)
#include<LCD.c>
#fuses HS, NOWDT, NOMCLR, NOBROWNOUT, NOLVP, NOPROTECT, NOFCMEN, NODEBUG
#use fast_io(E)
#use standard_io(C)
#use standard_io(A)

void main()
{
   int8 valor;
   lcd_init();
   setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_16);
   lcd_gotoxy(1,1);
   printf(lcd_putc, "SPI maestro");
   while(true)
   {
      output_bit(PIN_B0, 1);      // SS
      lcd_gotoxy(1,2);
      printf(lcd_putc, "Recibiendo");
      valor=spi_read();
      delay_ms(2);
      lcd_gotoxy(1,2);         
      printf(lcd_putc, "Valor = %u", valor);
      output_bit(PIN_B0, 0);
      delay_ms(1000);
   }
}

SLAVE
Code:

#include<16f886.h>
#use delay (clock = 8 MHz)
#include<LCD.c>
#fuses INTRC_IO, NOWDT, NOMCLR, NOBROWNOUT, NOLVP, NOPROTECT, NOFCMEN, NODEBUG
#use fast_io(E)
#use standard_io(C)
#use standard_io(A)
/*    RC3   SPI CLK
   RC4   SPI   SDI
   RC5   SPI SDO
   RC0   SPI SS   
   */

void main()
{
   int8 valor;
   lcd_init();
   setup_spi(SPI_SLAVE | SPI_H_TO_L);
   lcd_gotoxy(1,1);
   printf(lcd_putc, "SPI esclavo");
   setup_oscillator(OSC_8MHZ);
   while(true)
   {
      if(input(PIN_C0)==1)      // entrada SS
      {
         lcd_gotoxy(1,2);
         printf(lcd_putc, "Enviando");
         spi_write(input_b());
      }
      lcd_gotoxy(1,2);
      printf(lcd_putc, "Terminado");
   }
}

But I have nothing in SCK, SDO, SDI.
May somebody help me?

Regards
Ricardo
alan



Joined: 12 Nov 2012
Posts: 349
Location: South Africa

View user's profile Send private message

PostPosted: Thu Jun 15, 2017 9:12 am     Reply with quote

If you read the manual your use of spi_read are wrong for master.

Quote:
Return a value read by the SPI. If a value is passed to the spi_read() the data will be clocked out and the data received will be returned. If no data is ready, spi_read() will wait for the data is a SLAVE or return the last DATA clocked in from spi_write().

If this device is the MASTER then either do a spi_write(data) followed by a spi_read() or do a spi_read(data). These both do the same thing and will generate a clock. If there is no data to send just do a spi_read(0) to get the clock.
rfjhh



Joined: 31 Mar 2011
Posts: 51
Location: Mexico

View user's profile Send private message

PostPosted: Thu Jun 15, 2017 10:11 pm     Reply with quote

I've modified the code as follows:
MASTER
Code:

#include<16f887.h>
#use delay (clock = 20 MHz)
#include<LCD.c>
#fuses HS, NOWDT, NOMCLR, NOBROWNOUT, NOLVP, NOPROTECT, NOFCMEN, NODEBUG

#use fast_io(E)
#use standard_io(C)
#use standard_io(A)

void main()
{
   int8 valor;
   set_tris_e(0xfe);
   lcd_init();
   setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_CLK_DIV_16);
   lcd_gotoxy(1,1);
   printf(lcd_putc, "SPI maestro");
   while(true)
   {
      lcd_gotoxy(1,2);
      printf(lcd_putc, "Recibiendo");
      output_high(PIN_E0);      // SS      
      valor=spi_read(0x96);
      delay_ms(2);
      lcd_gotoxy(1,2);         
      printf(lcd_putc, "Valor = %u", valor);
      output_low(PIN_E0);
      delay_ms(100);
   }
}

SLAVE:
Code:

#include<16f886.h>
#use delay (clock = 8 MHz)
#include<LCD.c>
#fuses INTRC_IO, NOWDT, NOMCLR, NOBROWNOUT, NOLVP, NOPROTECT, NOFCMEN, NODEBUG

#use fast_io(B)
#use fast_io(C)
#use standard_io(A)
/*    RC3   SPI CLK
   RC4   SPI   SDI
   RC5   SPI SDO
   RC0   SPI SS   
   */

void main()
{
   int8 valor;
   set_tris_b(0xff);
   set_tris_c(0xdf);
   setup_spi(SPI_SLAVE | SPI_H_TO_L | SPI_CLK_DIV_16);
   setup_oscillator(OSC_8MHZ);
   while(true)
   {
      valor = input_b();
      if(input(PIN_C0)==1)      // entrada SS
      {
         spi_write(valor);
      }   
   }
}

Now I have the clock signal, but nothing happens from the slave to the master.
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