Page 1 of 1

Change SPI Clock

Posted: Thu Jul 05, 2012 3:54 pm
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?

Re: Change SPI Clock

Posted: Thu Jul 05, 2012 4:20 pm
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.

Re: Change SPI Clock

Posted: Thu Jul 05, 2012 6:39 pm
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?

Re: Change SPI Clock

Posted: Fri Jul 06, 2012 10:33 am
by Benj
No, This code was for a 16F877A. Which AVR are you using?

Re: Change SPI Clock

Posted: Fri Jul 06, 2012 2:57 pm
by rocket200
Benj wrote:No, This code was for a 16F877A. Which AVR are you using?
I'm using the ATTiny2313.

Re: Change SPI Clock

Posted: Fri Jul 06, 2012 5:04 pm
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.