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 Between PIC18f46k22 and PIC18F13K22

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



Joined: 02 Jul 2013
Posts: 1
Location: London

View user's profile Send private message

SPI Between PIC18f46k22 and PIC18F13K22
PostPosted: Tue Jul 02, 2013 9:51 am     Reply with quote

Hi All


I am new to this forum.

I trying to transfer 8 bit data from PIC18F46K22 to PIC18F13K22 using SPI Communication, both PIC have their own internal SPI hardware.

Master code
Code:
 #include <18F46K22.h>
#device ADC=8
#use delay(int=64Mhz)

int8 x;

void main()
{
setup_spi(SPI_MASTER | SPI_H_TO_L | SPI_XMIT_L_TO_H  | SPI_CLK_DIV_4  );

setup_adc( ADC_CLOCK_INTERNAL );
SETUP_ADC_PORTS(sAN0|VSS_VDD);

while(1)
 {
set_adc_channel( 0 );
delay_ms(20);
x = read_adc();

 spi_write(x);

 }

}



slave code(PIC18f13k22)
Code:
#include <18F13K22.h>
#use delay(clock=8M)
#use rs232(baud=9600, xmit=PIN_B7)//, rcv=PIN_B5, ERRORS)

void main(void)
{
setup_spi(spi_slave);
int8 x;

while(1)
{
x=spi_read();
printf("spi=%u\n\r",x);
}
}

I want to send analog data from pic18f46k22 to pic18f13k22.

I Tested so many time the code, seem to build correctly but when tested in ISIS doesn't seem to work. SCK of master is not generating clock, SDO pin of master blink when the signal applied to AN0 changes. Slave (PIC18f13k22) should display analog values coming from master but this is displaying random data.


free screenshot
Mike Walne



Joined: 19 Feb 2004
Posts: 1785
Location: Boston Spa UK

View user's profile Send private message

PostPosted: Tue Jul 02, 2013 10:24 am     Reply with quote

Quote:
I Tested so many time the code, seem to build correctly but when tested in ISIS doesn't seem to work.
You've just answered your own question.

Read what other forum users have to say about Proteus/ISIS.

Work with REAL hardware, then ask again, you will likely get help.

Mike
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Wed Jul 03, 2013 1:19 am     Reply with quote

Setup_spi, and #use SPI, are _different_, and _not always compatible_ commands. You should only use one or the other. Note in the manual, if you read 'setup_spi', it makes no mention at all of #use spi, and similarly #use spi, makes no mention of setup_spi.

Start by using one or the other.
Get rid of the #use spi in the slave device.

The slave needs to also have the clock edges specification.

setup_spi(SPI_SLAVE | SPI_H_TO_L | SPI_XMIT_L_TO_H);

Only 'master' specifies the clock rate, but the edges to use, need to be specified at both ends, and match.

I'd suggest dropping the clock rate. Though SPI is 'master clocked', with a slave PIC using just an 8MHz clock is a problem. Note that the SSP slave mode clock high time, and clock low time, are both specified at Tcy+20nSec. Tcy, is the instruction cycle time (just 2MHz on an 8Mhz clock.....). You need your SSP clock rate to be lower, with a slave PIC running just 1/8th the speed of the master chip....

Best Wishes
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