码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2019-07-05 19:18:35      阅读:119      评论:0      收藏:0      [点我收藏+]

标签:turn   mission   elb   ecif   search   lun   dia   stuck   dsp   

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

标签:turn   mission   elb   ecif   search   lun   dia   stuck   dsp   

原文地址:https://www.cnblogs.com/MCSFX/p/11140130.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!