Communicating to 2 SPI Slaves with USART & SPI ports on Atmega16U2

Posted mcsfx

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Communicating to 2 SPI Slaves with USART & SPI ports on Atmega16U2相关的知识,希望对你有一定的参考价值。

原文来自: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)))
	;

 

       

This topic has a solution.          
        Last Edited: Mon. Aug 7, 2017 - 05:26 PM     
This reply has been marked as the solution.            
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

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.

 

 

        Last Edited: Sat. Aug 5, 2017 - 06:01 AM     
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
The High Septon wrote:
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.

Doing magic with a USD 7 Logic Analyser: https://www.avrfreaks.net/comment/2421756#comment-2421756

Bunch of old projects with AVR‘s: http://www.hoevendesign.com

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

https://en.wikipedia.org/wiki/Serial_Peripheral_Interface_Bus#Independent_slave_configuration

 

技术图片

Addendum - highlighting the different Slave-Select (SS) lines:

 

技术图片

 

#SPISlaveSelect

 

 

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner‘s "Getting Started" tips: https://www.avrfreaks.net/comment...
        Last Edited: Sat. Aug 5, 2017 - 03:35 PM     
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

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?

技术图片

技术图片

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
Paulvdh wrote:

 

The High Septon wrote:
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.

 

 

That‘s why I didn‘t say "help me"... I gave a very clear question with background explanation and a code example. Relax dude.

        Last Edited: Sat. Aug 5, 2017 - 05:31 PM

 

以上是关于Communicating to 2 SPI Slaves with USART & SPI ports on Atmega16U2的主要内容,如果未能解决你的问题,请参考以下文章

failed to load class "org.sl4j.impl.StaticLoggerBinder"

org.hibernate.service.spi.ServiceException: Unable to create requested service [org.hibernate.engine

Tableau报错-An error occurred while communicating with Tableau Data Extract.

我的Android进阶之旅CMake出异常: Error occurred while communicating with CMake server.

我的Android进阶之旅CMake出异常: Error occurred while communicating with CMake server.

随手记录Unable to create requested service [org.hibernate.engine.jdbc.env.spi.JdbcEnvironment]