View previous topic :: View next topic |
Author |
Message |
Ruprecht
Joined: 21 Jul 2005 Posts: 88 Location: Czech Republic
|
Posted: Fri Dec 19, 2014 4:55 pm Post subject: SPI - HW or SW is better? |
|
|
Are there an advantages of HW SPI compared to SW SPI?
I´m thinking about my first SPI communication with MCP4922 and simply I want know about possible complications...
(BASCOM-AVR version : 2.0.7.7 ) |
|
Back to top |
|
|
JC
Joined: 15 Dec 2007 Posts: 584 Location: Cleveland, OH
|
Posted: Fri Dec 19, 2014 7:01 pm Post subject: |
|
|
Hardware SPI is faster, and once the data is loaded the uC can be busy running other code.
Software SPI works fine.
The trade off is slower operation, and the uC must do the work, so it uses clock cycles.
For some projects this is an issue, for most it doesn't matter.
The other nice thing about the SW SPI is that you can configure it for any I.O pins you have available, in case the hardware pins are already being used for other purposes.
JC |
|
Back to top |
|
|
Ruprecht
Joined: 21 Jul 2005 Posts: 88 Location: Czech Republic
|
Posted: Fri Dec 19, 2014 7:27 pm Post subject: |
|
|
Thanks for your answer.
I need read buffered RX (uses interrupt) and send SPI.
In code will be some another interrupts.
HW SPI can be configured with or without interrupt, should I use interrupt for SPI too? |
|
Back to top |
|
|
JC
Joined: 15 Dec 2007 Posts: 584 Location: Cleveland, OH
|
Posted: Sat Dec 20, 2014 5:17 am Post subject: |
|
|
That is a hard question to answer without knowing a lot more about the project.
The USART does better with interrupts.
It has an exact timing that needs to be met.
If you have the USART and other interrupts running, you might initially try without SPI interrupts.
SPI is less time dependent, as the Master provides the clock for clocking the data in and out.
Small delays from other interrupts won't effect it.
Adding to many unnecessary interrupts, however, can impact the USART and other interrupts that are time critical.
JC |
|
Back to top |
|
|
Ruprecht
Joined: 21 Jul 2005 Posts: 88 Location: Czech Republic
|
Posted: Tue Dec 30, 2014 11:04 am Post subject: |
|
|
This works
Code: |
$regfile = "m8adef.dat"
$crystal = 8000000
$hwstack = 48
$swstack = 32
$framesize = 32
$baud = 31250
Config Portd = Output
Config Spi = Soft , Din = Pind.2 , Dout = Portd.7 , Ss = Portd.5 , Clock = Portd.6 , Mode = 0
Dim I As Byte
Dim A(2) As Byte
A(1) = &B0011_0000 'config bits
Spiinit 'Init SPI
'-----------------------------------------------------------------------------
Do
For I = 0 To 255
A(2) = I
Spiout A(1) , 2 'send 2 bytes
Next I
Loop
|
and this not works
Code: |
$regfile = "m8adef.dat"
$crystal = 8000000
$hwstack = 48
$swstack = 32
$framesize = 32
$baud = 31250
Config Portd = Output
Config Spi = Soft , Din = Pind.2 , Dout = Portd.7 , Ss = Portd.5 , Clock = Portd.6 , Mode = 0
Dim I As Byte
Dim Var As Word , Var_lsb As Byte At Var Overlay , Var_msb As Byte At Var + 1 Overlay
Var_msb = &B0011_0000 'config bits
Spiinit 'Init SPI
'-----------------------------------------------------------------------------
Do
For I = 0 To 255
Var_lsb = I
Spiout Var , 2 'send 2 bytes
Next I
Loop |
I want to use one word variable rather than two-element array but I cannot see mistake. This has to do with LSB/MSB sending order?
edit:
this works too
Code: | Do
For I = 0 To 255
Var_lsb = I
Swapped_var = Var
Swap Swapped_var
Spiout Swapped_var , 2 'send 2 bytes in reverted order
Next I
Loop |
Is there an easier way to change MSB/LSB order sent to SW SPI? |
|
Back to top |
|
|
|