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

PIC24FJ128GA204 SPI

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



Joined: 18 Nov 2008
Posts: 281
Location: Athens, Greece.

View user's profile Send private message

PIC24FJ128GA204 SPI
PostPosted: Fri May 22, 2020 12:56 am     Reply with quote

Hello!

I am using PIC24FJ128GA204 with SPI but I have some problems.

Code:

#include <24FJ128GA204.h>

#fuses HS,NOWDT,DEBUG,NOPROTECT,NOPR,FRC_PLL,OSCIO

#use delay(clock=32M,int)

#pin_select U1TX = PIN_C9
#pin_select U1RX = PIN_C8

#pin_select SDO1 = PIN_B15
#pin_select SCK1OUT = PIN_B13
#pin_select SDI1 = PIN_B12

#use rs232(BAUD=115200,UART1)

#use    fast_io(A)
#use    fast_io(B)
#use    fast_io(C)

#word PORTA=getenv("SFR:PORTA")
#word PORTB=getenv("SFR:PORTB")
#word PORTC=getenv("SFR:PORTC")

#word LATA=getenv("SFR:LATA")
#word LATB=getenv("SFR:LATB")
#word LATC=getenv("SFR:LATC")

#word TRISA=getenv("SFR:TRISA")
#word TRISB=getenv("SFR:TRISB")
#word TRISC=getenv("SFR:TRISC")

#word SPI1BRGL=getenv("SFR:SPI1BRGL")


void Main(){

   TRISA=0b0000000000000000;
   TRISB=0b0001000100000000;
   TRISC=0b0000000100000000;

   LATA=0;
   LATB=0;
   LATC=0;   

   setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_XMIT_L_TO_H);
   SPI1BRGL=0;   

   while(1){
     spi_read(85);
    delay_us(100);
   }
   
}



The code above works and the SPI clock is according to the SPI1BRGL register.

But if I try this:
Code:


setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_XMIT_L_TO_H SPI_CLK_DIV_2);



No matter the SPI_CLK_DIV_ X value, I see no clock output!

Compiler version 5.049

Any ideas?
_________________
George.
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Fri May 22, 2020 1:01 am     Reply with quote

| 'or' missing in what you post:
georpo wrote:
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_XMIT_L_TO_H SPI_CLK_DIV_2);

Needs to be:
Code:
setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_XMIT_L_TO_H | SPI_CLK_DIV_2);

Note the extra |
georpo



Joined: 18 Nov 2008
Posts: 281
Location: Athens, Greece.

View user's profile Send private message

PostPosted: Fri May 22, 2020 1:11 am     Reply with quote

Yes of course.

I typed the code here as an example and made a typo.
Of course it is:
Code:

 setup_spi(SPI_MASTER | SPI_L_TO_H | SPI_XMIT_L_TO_H | SPI_CLK_DIV_2);


It does not work.
_________________
George.
georpo



Joined: 18 Nov 2008
Posts: 281
Location: Athens, Greece.

View user's profile Send private message

PostPosted: Fri May 22, 2020 1:25 am     Reply with quote

Also just for the record this works:

Code:

CNPU2|=0x20;            //enable PIN_B9 (CN21) pull-up


This does not work.
Code:

SET_PULLUP(PIN_B9);      //enable PIN_B9 (CN21) pull-up

_________________
George.
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Fri May 22, 2020 1:39 am     Reply with quote

OK.
The reason is simple. Those options don't exist....

If you look at the datasheet for that PIC, it doesn't have a prescaler
option for the SPI. The #defines were removed almost immediately
afterwards (5.053 doesn't have them). The bit patterns being loaded
by the incorrect entries do things like disable the clock output....

You can't use a prescaler on this clock.
The reason is that the chip has a 16bit BRG, so shouldn't need a
prescaler. The people setting up the files at CCS made a mistake
including this option, and realised it soon afterwards.
georpo



Joined: 18 Nov 2008
Posts: 281
Location: Athens, Greece.

View user's profile Send private message

PostPosted: Fri May 22, 2020 1:52 am     Reply with quote

As always Ttelmah you know everything and I thank you for this Very Happy

So for my case I should just use the SPI1BRGL register. Right?
_________________
George.
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Fri May 22, 2020 2:56 am     Reply with quote

Or just use setup_spi.

setup_spi(1000000);

Gives you 1Mbps on the SPI (if the hardware supports it).
Etc. etc., for whatever baud rate you want.

Means you don't have to be working out BRG values yourself.
georpo



Joined: 18 Nov 2008
Posts: 281
Location: Athens, Greece.

View user's profile Send private message

PostPosted: Fri May 22, 2020 3:14 am     Reply with quote

This did not work.
_________________
George.
Ttelmah



Joined: 11 Mar 2010
Posts: 19504

View user's profile Send private message

PostPosted: Fri May 22, 2020 4:05 am     Reply with quote

Yes, just checked with your compiler, and this makes the same mistake
as for the SPI_CLK_DIV_2. It tried to write a prescaler to the SPI1CON1
register. A couple of versions later, both faults are fixed... Sad
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