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 pic24hj23gp202

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



Joined: 02 Nov 2013
Posts: 1

View user's profile Send private message

spi pic24hj23gp202
PostPosted: Sat Nov 02, 2013 11:37 am     Reply with quote

hi
i want contact two pic24hj32gp202 with spi but does not work Please help me :
master :

Code:

#include <24hj32gp202.h>
#fuses hs
#use delay (clock=20m)

#PIN_SELECT SDI1=PIN_B5
#PIN_SELECT SDO1=PIN_B6
#PIN_SELECT SCK1OUT=PIN_B8
#PIN_SELECT SS1OUT=PIN_B9
 int16 i ;
main(){
setup_spi(SPI_MASTER|SPI_H_TO_L|SPI_CLK_DIV_16|SPI_MODE_8B|SPI_SS_DISABLED);
while (1){
for(i=0;i<100; i++){
spi_write(i);
delay_ms(500);
}
}



}


slave :
Code:

#include <24hj32gp202.h>
#fuses hs
#use delay (clock=20m)



#define LCD_ENABLE_PIN  PIN_B13                                   
#define LCD_RS_PIN      PIN_B15                                   
#define LCD_RW_PIN      PIN_B14                                   
#define LCD_DATA4       PIN_B0                                   
#define LCD_DATA5       PIN_B1                                   
#define LCD_DATA6       PIN_B2                                   
#define LCD_DATA7       PIN_B3   
 
#PIN_SELECT SDI1=PIN_B5
#PIN_SELECT SDO1=PIN_B6
#PIN_SELECT SCK1OUT=PIN_B8
#PIN_SELECT SS1OUT=PIN_B9
 
 
#define LCD_TYPE 2

#include <lcd.c>
#include <stdlib.h>
void lcd_clear () {
lcd_send_byte(0,0x01);
lcd_send_byte(0,0x02);
}

char str[16];
int16 i ;

main(){
lcd_init ();
setup_spi(SPI_SLAVE|SPI_SS_DISABLED|SPI_MODE_8B);

while (1){
lcd_clear();

i=spi_read();

lcd_gotoxy(1,1);
itoa (i,10,str);

printf(lcd_putc, "i=%s"str);

delay_ms(400);

}

}

Ttelmah



Joined: 11 Mar 2010
Posts: 19535

View user's profile Send private message

PostPosted: Sat Nov 02, 2013 12:06 pm     Reply with quote

A couple of comments without looking too far:

SPI_SS_DISABLED, should normally _only_ ever be used on a slave....

Get rid of the delay in the slave. It will wait for the data to arrive, and this should be what synchronises the loop. Clear the lcd after the byte arrives, then you have the whole wait time to see it (see below).
You may be falling out of sync, since clearing an LCD, takes a long time.

Add a delay before starting in the master. Unless you are very lucky, and the slave starts first, the link will never synchronise.

Generally, SPI is _much_ better used with slave select. Without this, you have to do something to ensure the connection starts synchronised....

why fiddle around with itoa. Printf, can format the data for you:

printf(lcd_putc,"\fi=%ld",i);

This will also clear the LCD for you....

Then, depending on your compiler version, you may need to set the TRIS on the pins for the SPI. CCS does this automatically for 'fixed' peripheral pins, but most versions do not do this when the pin is relocatable.

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