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

Using setup_spi

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



Joined: 02 Apr 2022
Posts: 97

View user's profile Send private message

Using setup_spi
PostPosted: Sun Apr 09, 2023 10:04 pm     Reply with quote

We have an old program that uses the PICF45K20 MCU and has the following statement:
Code:
setup_spi(SPI_MASTER | SPI_CLK_DIV_4 | SPI_SS_DISABLED | SPI_L_TO_H);

Immediately after the statement, we set a breakpoint to check the SSPCON1 register setting, which shows 00100001.

This setting suggests that the SPI is running at a clock of FOSC/16 instead of the FOSC/4 set in the setup_spi() function?

Is it correct that the SPI_SS_DISABLED parameter should not be there?

We compiled the program using CCS C compiler v5.115 on MPLAB IDE v8.92.

I will appreciate any comment or advice. Thank you.
PCM programmer



Joined: 06 Sep 2003
Posts: 21708

View user's profile Send private message

PostPosted: Mon Apr 10, 2023 6:01 am     Reply with quote

SPI_SS_DISABLED is only used as a setting for an SPI Slave. You are using
the PIC as an SPI Master, so SPI_SS_DISABLED should not be there.
Remove it.
Ttelmah



Joined: 11 Mar 2010
Posts: 19513

View user's profile Send private message

PostPosted: Mon Apr 10, 2023 10:41 am     Reply with quote

and on the clock, read the data sheet.
The SPI peripheral is fed with the master oscillator/4.
The setup sets the prescaler _from this_.
So selecting a prescaler of /4, sets the SPI to run off FOSC/16
kgng97ccs



Joined: 02 Apr 2022
Posts: 97

View user's profile Send private message

PostPosted: Tue Apr 11, 2023 1:20 am     Reply with quote

Thank you, PCM Programmer and Ttelmah, for your input.

Ttelmah wrote:
The SPI peripheral is fed with the master oscillator/4.
The setup sets the prescaler _from this_.
So selecting a prescaler of /4, sets the SPI to run off FOSC/16

When we examined the #define constants provided in the CCS 18F45K20.h file for the setup_spi() function, we can see that the constants make the x in SPI_CLK_DIV_x match the intended divisor.

For example, SPI_CLK_DIV_4 is defined as 0, and gives the resulting SSPM bits of the SSPCON1 register as 0000, which refers to an SPI clock of FOSC/4. Similarly, SPI_CLK_DIV_16 is defined as 1, and SPI_CLK_DIV_64 is defined as 2, and both give the intended clocks FOSC/16 and FOSC/64 respectively, as confirmed by the resulting SSPM bits.

In addition, Figure 17-1 (page 180) of the PIC18F45K20 datasheet indicates Tosc as the input to the MSSP (followed by a prescalar of 4, 16, or 64), and we understand that to mean FOSC.

I wonder if our understanding is correct.
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