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?
Change SPI Clock
Moderator: Benj
- 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
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.
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.
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Posts: 68
- Joined: Mon Oct 24, 2011 7:50 pm
- Has thanked: 11 times
- Been thanked: 3 times
- Contact:
Re: Change SPI Clock
Would this work on AVR?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.
- 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
No, This code was for a 16F877A. Which AVR are you using?
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
-
- Posts: 68
- Joined: Mon Oct 24, 2011 7:50 pm
- Has thanked: 11 times
- Been thanked: 3 times
- Contact:
Re: Change SPI Clock
I'm using the ATTiny2313.Benj wrote:No, This code was for a 16F877A. Which AVR are you using?
- 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
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.
Or you can setup the SPI rate to be software controlled using this command.
Then use this command to increment the SPI clock, each SPI transaction will require 8 clock pulses.
Let me know how you get on.
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;
Code: Select all
USICR = (USICR & 0xf1) | 0x02;
Code: Select all
USICR = (USICR & 0xfd) | 0x02;
Regards Ben Rowland - MatrixTSL
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel
Flowcode Product Page - Flowcode Help Wiki - Flowcode Examples - Flowcode Blog - Flowcode Course - My YouTube Channel