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

Basic Read/Write Operation at SPI module

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



Joined: 21 Feb 2010
Posts: 17

View user's profile Send private message

Basic Read/Write Operation at SPI module
PostPosted: Tue Jan 25, 2011 1:46 am     Reply with quote

I want to use SPI module of PIC 18f14k50. And first of all I wrote basic read/write commands for testing but I didn't achieve. My aim is sending a byte (data=15) with SPI module of 18f14k50. But I see wrong value at the serial port monitor. My compiler verison is 4.114. My codes:
Master
Code:

#include <18f14k50.h>
#include <PIC18f14k50_registers.h>
#FUSES NOWDT,XT,NOPUT,NOBROWNOUT,NOLVP,NOCPD,NOMCLR
#use delay(clock=4000000)
int16 timeout;
int temp,data;
void main(void)
{
data=21;
TRISC=0x00;
TRISB=0b00110000;
sspcon1=0b00100000;
sspstat=0b00100000;
temp=SSPBUF;
while(1)
{
            SSPBUF=data;           
            while(!BF&&timeout<5000){
            delay_us(1);
            timeout++;
            }
            timeout=0;
            temp=SSPBUF;
            delay_ms(100);
}
}

SLAVE
Code:

#include <18f14k50.h>
#include <PIC18f14k50_registers.h>
#FUSES NOWDT,XT,NOPUT,NOBROWNOUT,NOLVP,NOCPD,NOMCLR
#use delay(clock=4000000)
#use rs232(baud=9600, xmit=PIN_B7,rcv=PIN_B5)
int16 timeout;
int temp,data;
void main(void)
{
TRISB=0b01110000;
TRISC=0x00;
SSPCON1=0b00100101;
SSPSTAT=0b01000000;
temp=SSPBUF;
while(1)
{
data=SSPBUF;
while(!BF&&timeout<5000){
delay_us(1);
timeout++;
}
timeout=0;
printf("%d\r",data);
}
}

I just want basic read/write operation in SPI modes. Could you propose anything? I tried same codes for PIC16f877 and it is worked but I couldn't do it for 18f14k50.
Thanks
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Tue Jan 25, 2011 3:03 pm     Reply with quote

I don't know why you are writing directly to registers. I tested the
setup_spi() and spi_write() functions with vs. 4.114 and they work OK.
I see pulses on SDO (pin C7) and SCK (pin B6). Here is the test program:
Code:

#include <18F14K50.H>
#fuses INTRC_IO, NOWDT, PUT, BROWNOUT, NOLVP, CPUDIV1
#use delay(clock=4000000)

#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)

//=================================
void main()
{

setup_spi(SPI_MASTER | SPI_MODE_0 | SPI_CLK_DIV_16);

while(1)
  {
   spi_write(0x33);
   delay_us(100);
  }
}


This thread has an example of an SPI Master/Slave program 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