Change SPI Clock

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 4.
To post in this forum you must have a registered copy of Flowcode 4 or higher. To sign up for this forum topic please use the "Online Resources" link in the Flowcode Help Menu.

Moderator: Benj

Post Reply
rocket200
Posts: 68
Joined: Mon Oct 24, 2011 7:50 pm
Has thanked: 11 times
Been thanked: 3 times
Contact:

Change SPI Clock

Post by rocket200 »

Hello,

Is it possible to set and change the SPI Clock within a program? I need to do SPI at multiple clock speeds thorough my program. Would I somehow set the SPI Clock to a variable?

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Change SPI Clock

Post by Benj »

Hello,

I am not sure if you can change the SPI clock speed on the fly. It might be that you need to disable the module first before doing the speed modification and re-enabling.

To start I would try this and see if the clock speed changes.

In your program where you want to make a speed change use a C icon with code as follows.

sspcon = (sspcon & 0xF8) | FCV_SPIVAL;

In your Flowcode variable manager add SPIVAL as a byte variable and assign this with a value before you call the line of C.

Here are the various speed settings available on a 16F877A.

0 = Fosc / 4
1 = Fosc / 16
2 = Fosc / 64
3 = Timer2 / 2

You do have the option of using timer 2 as your clock source so maybe setting up the SPI like this and then changing the timer 2 properties would give you more control over the SPI speed.

rocket200
Posts: 68
Joined: Mon Oct 24, 2011 7:50 pm
Has thanked: 11 times
Been thanked: 3 times
Contact:

Re: Change SPI Clock

Post by rocket200 »

Benj wrote:Hello,

I am not sure if you can change the SPI clock speed on the fly. It might be that you need to disable the module first before doing the speed modification and re-enabling.

To start I would try this and see if the clock speed changes.

In your program where you want to make a speed change use a C icon with code as follows.

sspcon = (sspcon & 0xF8) | FCV_SPIVAL;

In your Flowcode variable manager add SPIVAL as a byte variable and assign this with a value before you call the line of C.

Here are the various speed settings available on a 16F877A.

0 = Fosc / 4
1 = Fosc / 16
2 = Fosc / 64
3 = Timer2 / 2

You do have the option of using timer 2 as your clock source so maybe setting up the SPI like this and then changing the timer 2 properties would give you more control over the SPI speed.
Would this work on AVR?

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Change SPI Clock

Post by Benj »

No, This code was for a 16F877A. Which AVR are you using?

rocket200
Posts: 68
Joined: Mon Oct 24, 2011 7:50 pm
Has thanked: 11 times
Been thanked: 3 times
Contact:

Re: Change SPI Clock

Post by rocket200 »

Benj wrote:No, This code was for a 16F877A. Which AVR are you using?
I'm using the ATTiny2313.

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Change SPI Clock

Post by Benj »

Hello,

You can force the SPI on the ATtiny to run based on a timer 0 interrupt. Would this be any use to you?

I think to do this you would need to use the following C command.

Code: Select all

USICR = (USICR & 0xf3) | 0x04;
Or you can setup the SPI rate to be software controlled using this command.

Code: Select all

USICR = (USICR & 0xf1) | 0x02;
Then use this command to increment the SPI clock, each SPI transaction will require 8 clock pulses.

Code: Select all

USICR = (USICR & 0xfd) | 0x02;
Let me know how you get on.

Post Reply