My question Why must I set portb.2 as an output? This took me hours to figure out. The demos use portb.2 as the CS line but the code below did not - I used a shield and the shield exposes portd.4.
I had to set portb.2/Digital 10 as an output - if I did not hardware SPI failed - why?
Ideas? Comments?
Sample Code Commentry
This shows how the MEGA328p can support hardware and software SPI. To use hardware make sure the #define SPI_HardwareSPI is not commented out.
To use software comment out #define SPI_HardwareSPI.
To use the hardware SPI the Data Out, Data In and Clock (DO/DI and SCK) cannot be moved but the optional Data Command, Chip Select and Reset are all moveable.
To use the hardware SPI the Data Out, Data In and Clock (DO/DI and SCK), Data Command, Chip Select and Reset are all moveable.
I have called InitSPIMode to call SPIMode if needed, and set the lines.
I have also define a sub called SendByteviaSPI to handle whether to call the Hardware or use Software (bit-banging) SPI.
'ChipSettings.#chip mega328p, 16#option explicit#include<UNO_mega328p.h >#define SPI_HardwareSPI 'comment this out to make into Software SPI but, you may have to change clock lines'PinmappingsforSPI-thisSPIdriversupportsHardwareSPI#define SPI_DC DIGITAL_8 ' Data command line#define SPI_CS DIGITAL_4 ' Chip select line#define SPI_RESET DIGITAL_9 ' Reset line'mandatedforhardware#define SPI_DI DIGITAL_12 ' Data in | MISO #define SPI_DO DIGITAL_11 ' Data out | MOSI#define SPI_SCK DIGITAL_13 ' Clock LinedirSPI_DCoutdirSPI_CSoutdirSPI_RESEToutdirSPI_DOOutdirSPI_DIIndirSPI_SCKOutdirDIGITAL_10Out'neededandIamnotsurewhy!DIMbyte1AsbyteDIMbyte2AsbyteDIMbyte3AsbyteDIMttasbyteDIMSPISendByteasbytebyte1=100'tempvalues(willcomefrompotentiometerlater)byte2=150byte3=200InitSPIModedoforeversetSPI_CSOFF;setSPI_DCOFF;SendByteviaSPI(byte1)setSPI_CSON;setSPI_DCONsetSPI_CSOFF;setSPI_DCOFF;SendByteviaSPI(byte2)setSPI_CSON;setSPI_DCONsetSPI_CSOFF;setSPI_DCOFF;SendByteviaSPI(byte3)setSPI_CSON;setSPI_DCONwait100msloopsubInitSPIMode#ifdef SPI_HardwareSPISPIMode(MasterFast,SPI_CPOL_0+SPI_CPHA_0)#endifsetSPI_DOOFF;setSPI_CSON;thereforeCPOL=0setSPI_DCON;deselectEndsubsubSendByteviaSPI(inSPISendByte)setSPI_CSOFFsetSPI_DCOFF;#ifdef SPI_HardwareSPIFastHWSPITransferSPISendBytesetSPI_CSON;exitsub#endif#ifndef SPI_HardwareSPIrepeat8ifSPISendByte.7=ONthensetSPI_DOON;elsesetSPI_DOOFF;endifSETSPI_SCKOn;;thereforeCPOL=0==ON,and,whereCPOL=1==ONrotateSPISendByteleftsetSPI_SCKOff;;thereforeCPOL=0=OFF,and,whereCPOL=1==OFFendrepeatsetSPI_CSON;setSPI_DOOFF;#endifendSub
~~~
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
*Master Mode
When the SPI is configured as a Master the user can determine the direction of the SS pin (this is DIGITAL10). If SS is configured as an output, the pin is a general output pin which does not affect the SPI system.
Typically, the pin will be driving the SS pin of the SPI Slave.
If SS is configured as an input, it must be held high to ensure Master SPI operation. If the SS pin is driven low by peripheral circuitry when the SPI is configured as a Master with the SS pin defined as an input, the SPI system interprets this as another master selecting the SPI as a slave and starting to send data to it.
*
So, when we initialise the Mega328p it is an input. The input left floating will be interpreted as another master selecting the SPI and all communications cease. Therefore, if I do not use Digital 10 as SS/Chip Select either set as an output or pull high.
Case Closed - RTFM
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
I justed posted the code below.
My question Why must I set portb.2 as an output? This took me hours to figure out. The demos use portb.2 as the CS line but the code below did not - I used a shield and the shield exposes portd.4.
I had to set portb.2/Digital 10 as an output - if I did not hardware SPI failed - why?
Ideas? Comments?
Sample Code Commentry
This shows how the MEGA328p can support hardware and software SPI. To use hardware make sure the #define SPI_HardwareSPI is not commented out.
To use software comment out #define SPI_HardwareSPI.
To use the hardware SPI the Data Out, Data In and Clock (DO/DI and SCK) cannot be moved but the optional Data Command, Chip Select and Reset are all moveable.
To use the hardware SPI the Data Out, Data In and Clock (DO/DI and SCK), Data Command, Chip Select and Reset are all moveable.
I have called InitSPIMode to call SPIMode if needed, and set the lines.
I have also define a sub called SendByteviaSPI to handle whether to call the Hardware or use Software (bit-banging) SPI.
~~~
Read the datasheet!!
*Master Mode
When the SPI is configured as a Master the user can determine the direction of the SS pin (this is DIGITAL10). If SS is configured as an output, the pin is a general output pin which does not affect the SPI system.
Typically, the pin will be driving the SS pin of the SPI Slave.
If SS is configured as an input, it must be held high to ensure Master SPI operation. If the SS pin is driven low by peripheral circuitry when the SPI is configured as a Master with the SS pin defined as an input, the SPI system interprets this as another master selecting the SPI as a slave and starting to send data to it.
*
So, when we initialise the Mega328p it is an input. The input left floating will be interpreted as another master selecting the SPI and all communications cease. Therefore, if I do not use Digital 10 as SS/Chip Select either set as an output or pull high.
Case Closed - RTFM