原文来自:https://www.avrfreaks.net/comment/2236256
I'm writing code for an embedded chip that consists of an Atmega16U2 connected to two devices via the port B SPI pins and the Port D USART pins (which can be used for SPI comms as well).
I'm a bit confused: is the SPCR register a shared resource that will be used for communications to both my Port B and Port D SPI slaves?
Also, i'm a bit stuck on the nitty gritty of how to send data to a specific slave. Since I have two SS lines (DDD4 and DDB0) connected to the uC do I just bit shift one of them low and start calling my writeSPI() function? What would my readSPI() function look like?
C Code:
void SetupSPIHardware(void) { /* Set MOSI and SCK output, all others input */ DDRB = (1 << DDB2)|(1 << DDB1); DDRD = (1 << DDD3)|(1 << DDD5); /* Enable SPI, Master, set clock rate fck/16 */ SPCR = (1 << SPE)|(1 << MSTR)|(1 << SPR0); } void writeSPI(char cData) { /* Start transmission */ SPDR = cData; /* Wait for transmission complete */ while(!(SPSR & (1<<SPIF))) ; }
Why do you want to use both the spi and usart for the same thing? You can have a number of slaves on the one spi master, but they must have individual slave selects.
uint8_t SPI(uint8_t cData) { /* Start transmission */ SPDR = cData; /* Wait for transmission complete */ while(!(SPSR & (1<<SPIF))); return SPDR; }
SPI needs to send a byte to receive a byte, so you really only need one function.
'bit shift' is not quite the right term. Basically if you want to talk to device #1, make it's SS pin low. Make it high when finished. Similarly for device #2, make it's SS pin low when you want to talk to it. High when finished.
Regarding SPI and USART - the SPDR register is NOT shared. The USART has it's own registers.
Also, i'm a bit stuck on the nitty gritty of how to send data to a specific slave
Do some research and try some things before asking questions.
There surely are plenty datasheets, application notes, libs, examples, tutorials about SPI.
And then, when you get stuck, ask more specific questions.
There is even a tutorial about how to ask questions on a forum and why asking "help me" is not helping anybody.
https://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus#Independent_slave_configuration
Addendum - highlighting the different Slave-Select (SS) lines:
#SPISlaveSelect
Ah interesting, it sounds like I may have made a mistake in trying to utilize the USART pins for SPI, seems like I don't want that after all.
Given my schematic below you will see that i've connected my CAP1188 slave to the PORT D USART pins that also support Master SPI Mode. If I was to simplify my design I would connect the MISO/MOSI/SCK lines at PD2,3,5 to those SPI pins on PortB. I'm a little confused though; what line would be good for my CAP1188s SS? Could I just choose any digital I/O pin?
The High Septon wrote:Also, i'm a bit stuck on the nitty gritty of how to send data to a specific slaveDo some research and try some things before asking questions.
There surely are plenty datasheets, application notes, libs, examples, tutorials about SPI.
And then, when you get stuck, ask more specific questions.
There is even a tutorial about how to ask questions on a forum and why asking "help me" is not helping anybody.
That's why I didn't say "help me"... I gave a very clear question with background explanation and a code example. Relax dude.