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

PIC24FJ256GB110 SPI Problem !

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



Joined: 22 Aug 2011
Posts: 2

View user's profile Send private message

PIC24FJ256GB110 SPI Problem !
PostPosted: Mon Aug 22, 2011 5:02 am     Reply with quote

Hey everyone!

I have a problem and I don't know how I can solve. I have used spi communication for PIC18f2550 and 18f4550 before. But now, I'm using PIC24FJ256GB110 and I don't know how to use spi on this pic. I guess, I have to find spi header file because I can configure pic24fj256gb110's spi register, but I couldn't find it. Could you share spi header file for pic24fj256gb110 or have any idea ?

Thank you in advance...
abafin



Joined: 22 Aug 2011
Posts: 2

View user's profile Send private message

PostPosted: Tue Aug 23, 2011 5:52 am     Reply with quote

my code is:
Code:

#include <24fj256gb110.h>
#include <stdlib.h>
#include <spi_register.h>
#FUSES HS,NOWDT,NOJTAG,NOPROTECT,NOWRT,DEBUG,ICSP1,NOWDT,NOIESO,PR,NOCKSFSM,NOOSCIO
#PIN_SELECT SDO1=PIN_C3
#PIN_SELECT SDI1=PIN_C2
#PIN_SELECT SCK1OUT=PIN_C1     
#use delay(clock=20000000) 

#use spi(SPI1, MODE=3, MASTER, BITS=8, stream=SPIA)   

void main()
{
setup_spi2(FALSE);
setup_spi3(FALSE);
setup_adc(ADC_OFF);
setup_adc_ports(NO_ANALOGS);

set_tris_a(0);
set_tris_c(4);
output_a(0);
output_c(0);

SPI1CON1=4414;
RPINR20=9767;
SPIROV=0;
SPIEN=1;
SRMPT=1;

while(1)
{
spi_write(0x66);
output_high(pin_a1);
delay_ms(500);
output_low(pin_a1);
delay_ms(500);
}
}

spi_register.h is;
Code:

#WORD SPI1STAT = 0x0240
#WORD SPI1CON1 = 0x0242
#WORD SPI1BUF = 0x0248
#WORD RPINR20 = 0x06A8
#WORD RPINR21 = 0x06AA
#BIT SPIEN =SPI1STAT.15
#BIT SRMPT =SPI1STAT.7
#BIT SPIROV =SPI1STAT.6
#BIT DISSCK = SPI1CON1.12
#BIT DISSDO = SPI1CON1.11
#BIT MODE16 = SPI1CON1.10
#BIT SMP = SPI1CON1.9
#BIT CKE = SPI1CON1.8
#BIT SSEN = SPI1CON1.7
#BIT CKP = SPI1CON1.6
#BIT MSTEN = SPI1CON1.5
#BIT SPRE2 = SPI1CON1.4
#BIT SPRE1 = SPI1CON1.3
#BIT SPRE0 = SPI1CON1.2
#BIT PPRE1 = SPI1CON1.1
#BIT PPRE0 = SPI1CON1.0

where am i wrong ?
miketwo



Joined: 04 Aug 2010
Posts: 24

View user's profile Send private message

PostPosted: Tue Aug 23, 2011 11:25 am     Reply with quote

I'm not an expert by any stretch, but I've messed around with SPI stuff before.

It seems like you're mixing and matching different methods. You have some #pin_selects, but no setup_spi(). And you have a #use spi, but then you're trying to set registers directly.

I wouldn't bother with trying to set the registers directly, unless you need to use all 3 SPI modules. It's a pain because the PIC24 requires an unlocking sequence to set them. See page 128 (Section 9.4.4.1) of the datasheet.

I would try using just the #use spi(), no pin selects or register-meddling, with the spi_xfer() function, and see if you have any luck with that.
FvM



Joined: 27 Aug 2008
Posts: 2337
Location: Germany

View user's profile Send private message

PostPosted: Tue Aug 23, 2011 11:36 am     Reply with quote

As a first point, consult the compiler help file. You're mixing two different SPI access methods provided by PCD, and are adding various SFR accesses. I didn't check, if your code possibly works though, but SPI access can be done easier.

#USE_SPI and setup_spi/spi_read/spi_write are mutual exclusive. In my opinion, setup_spi is the better method for hardware access, allowing various things, that can't be done with #USE_SPI directly, e.g. changing the frequency (required for SD_Card) or mode (partly required for multi device access).

You normally won't need SFR accesses when using the CCS SPI functions. A somewhat confusing point is the mapping of CCS mode syntax to standard SPI modes, because it has changed between PCD versions. There have been also problems with spi_write() functionality in previous PCD versions. You can review forum posts related to PIC24 spi operation, or ask specifically. It won't be bad, if you are able to verify the operation of your SPI code, either by looking at the hardware signals or the register settings.

Below an SPI example, that works with old and new PCD versions
Code:
#IF  getenv("VERSION")>=4.109
#define SPI_MODE_0 0x100
#define SPI_MODE_1 0
#define SPI_MODE_2 0x40
#define SPI_MODE_3 0x140
#ELSE
#define SPI_MODE_0 0x100
#define SPI_MODE_1 0
#define SPI_MODE_2 0x140
#define SPI_MODE_3 0x40
#ENDIF

setup_spi(SPI_MASTER | SPI_MODE_0 |SPI_CLK_DIV_1);
SPI_nCS_LAT = 0;
// spi_read() is used for compatibility with previous PCD versions
spi_read(0xAB);
spi_read(0);
spi_read(0);
spi_read(0);
h=spi_read(0);
SPI_nCS_LAT = 1;
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