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 communication

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



Joined: 03 May 2010
Posts: 2
Location: Turkey

View user's profile Send private message MSN Messenger

spi communication
PostPosted: Mon May 03, 2010 1:53 pm     Reply with quote

I am communicating over SPI between two pics 16f877a. For some reason the data does not transfer correctly. I send the same byte out every 100ms. No matter what I sent. What can be wrong? Is it a software problem or can it be in the hardware problem?

Code:

 transmitter code

#if defined(__PCM__)
#include <16F877A.h>
#device ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#use delay(clock=40000000)
#use rs232(baud=19200,xmit=PIN_c6,rcv=PIN_c7)
//#use fast_io(B)
#use fast_io(C)
#use fast_io(D)
#org 0x1F00,0x1FFF {}
#opt 9
#include <LCD.C >


#define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1 (SPI_L_TO_H)
#define SPI_MODE_2 (SPI_H_TO_L)
#define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H)

#define SS_PIN PIN_C5         //data shhetten bakıldı

void main()
{
   int8 val;
   set_tris_d(0b00000000);
   set_tris_c(0b10000000);
   set_tris_b(0b00000000);
   set_tris_a(0b00000000);
   
   printf("maindeyim\n\r");
   
   setup_adc_ports(AN0);
   setup_adc(ADC_CLOCK_INTERNAL);
   set_adc_channel(0);


   setup_spi(SPI_MASTER | SPI_MODE_1 | SPI_CLK_DIV_64);
   
   while(true) {
       val = read_adc();   
       
       output_low(SS_PIN);   
       delay_us(10);         
       
       spi_write(val);       
       printf("%i gonderildi\n\r",val);
       delay_ms(2000);         
       output_high(SS_PIN);   
       printf("high yapildi\n\r");
       delay_ms(10);
   }
}


reciever code
Code:

#if defined(__PCM__)
#include <16F877A.h>
#device ADC=10
#fuses HS,NOWDT,NOPROTECT,NOLVP
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#use delay(clock=40000000)
#use rs232(baud=19200,xmit=PIN_c6,rcv=PIN_c7)
//#use fast_io(B)
#use fast_io(C)
#use fast_io(D)
#org 0x1F00,0x1FFF {}
#opt 9
#include <LCD.C >

#define SPI_MODE_0 (SPI_L_TO_H | SPI_XMIT_L_TO_H)
#define SPI_MODE_1 (SPI_L_TO_H)
#define SPI_MODE_2 (SPI_H_TO_L)
#define SPI_MODE_3 (SPI_H_TO_L | SPI_XMIT_L_TO_H)
unsigned int8 val;
void main()
{
    set_tris_d(0b00000000);
   set_tris_c(0b10000000);
   set_tris_b(0b00000000);
   set_tris_a(0b00000000);
   setup_spi(SPI_SLAVE | SPI_MODE_1 );
   //setup_spi(SPI_MASTER | SPI_MODE_1 | SPI_CLK_DIV_64);
   lcd_init();
   printf("slave in mainideyim\n\r"); 
   while(true) {
      printf("while -slave\n\r");
       val = spi_read();

       
       printf("slave okuma yapildi\n\r %i",val);
       lcd_gotoxy(1, 1);
       printf(lcd_putc, "Pot at: %u   ", val);
   }
}
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 03, 2010 1:59 pm     Reply with quote

The TRIS is wrong. You have set most pins to be outputs. A '0' means
the pin is an output, and yet you want to receive data on the SDI pin,
which must be set as an input.

With CCS, you don't actually have to set the TRIS. If you use CCS
functions and use Standard i/o mode, CCS will take care of it for you.
It will do it all correctly. You don't have to worry about it. Standard i/o
mode is the default mode. You don't have to specify it.

My advice is to delete all the #use fast_io lines, and delete all the
set_tris_x lines.

Also, look at this CCS example code. Notice how they call a function
to check if data has been received by the Slave, before they read it.
Quote:
c:\program files\picc\examples\ex_spi_slave.c
eco_985



Joined: 03 May 2010
Posts: 2
Location: Turkey

View user's profile Send private message MSN Messenger

i did your advise but it doesn't work
PostPosted: Mon May 03, 2010 2:54 pm     Reply with quote

PCM programmer wrote:
The TRIS is wrong. You have set most pins to be outputs. A '0' means
the pin is an output, and yet you want to receive data on the SDI pin,
which must be set as an input.

With CCS, you don't actually have to set the TRIS. If you use CCS
functions and use Standard i/o mode, CCS will take care of it for you.
It will do it all correctly. You don't have to worry about it. Standard i/o
mode is the default mode. You don't have to specify it.

My advice is to delete all the #use fast_io lines, and delete all the
set_tris_x lines.

Also, look at this CCS example code. Notice how they call a function
to check if data has been received by the Slave, before they read it.
Quote:
c:\program files\picc\examples\ex_spi_slave.c
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon May 03, 2010 2:59 pm     Reply with quote

Make sure your connections are correct. SDO on the master connects
to SDI on the slave. SDO on the slave goes to SDI on the master.
SS from the master connects to the /SS pin on the slave.

Try an SPI slave example for two PICs:
http://www.ccsinfo.com/forum/viewtopic.php?t=39145
Here's an SPI slave test program for a single PIC:
http://www.ccsinfo.com/forum/viewtopic.php?t=26888
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