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

  1. void SetupSPIHardware(void)
  2. {
  3. /* Set MOSI and SCK output, all others input */
  4. DDRB = (1 << DDB2)|(1 << DDB1);
  5. DDRD = (1 << DDD3)|(1 << DDD5);
  6. /* Enable SPI, Master, set clock rate fck/16 */
  7. SPCR = (1 << SPE)|(1 << MSTR)|(1 << SPR0);
  8. }
  9.  
  10. void writeSPI(char cData)
  11. {
  12. /* Start transmission */
  13. SPDR = cData;
  14. /* Wait for transmission complete */
  15. while(!(SPSR & (1<<SPIF)))
  16. ;
  17. }
This topic has a solution. Jump to the solution.         
        Last Edited: Mon. Aug 7, 2017 - 05:26 PM     

Top      

                  Level: 10k+ Postman             
                  Joined: Thu. Dec 30, 2004             
                  Posts: 22041 View posts             
                  Location: Melbourne,Australia             
This reply has been marked as the solution.             #2
Posted by Kartman:                Sat. Aug 5, 2017 - 05:58 AM                                       
  • 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.

  1. uint8_t SPI(uint8_t cData)
  2. {
  3. /* Start transmission */
  4. SPDR = cData;
  5. /* Wait for transmission complete */
  6. while(!(SPSR & (1<<SPIF)));
  7.  
  8. return SPDR;
  9. }

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     

Top      

                  Level: Raving Lunatic             
                  Joined: Fri. Aug 3, 2001             
                  Posts: 2802 View posts             
 
Posted by Paulvdh:                Sat. Aug 5, 2017 - 02:35 PM                                       
  • 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

Top      

awneil

 
                  Level: 10k+ Postman             
                  Joined: Fri. Jul 1, 2005             
                  Posts: 15843 View posts             
                  Location: Basingstoke, Hampshire, UK             
Posted by awneil:                Sat. Aug 5, 2017 - 03:11 PM                                       
  • 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     

Top      

                  Level: Hangaround             
                  Joined: Thu. Feb 11, 2016             
                  Posts: 122 View posts             
 
Posted by The High Septon:                Sat. Aug 5, 2017 - 03:14 PM                                            (Reply to #2)                          
  • 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?

Top      

                  Level: Hangaround             
                  Joined: Thu. Feb 11, 2016             
                  Posts: 122 View posts             
 
Posted by The High Septon:                Sat. Aug 5, 2017 - 03:15 PM                                       
  • 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的更多相关文章

  1. ARM与FPGA通过spi通信设计2.spi master的实现

    这里主要放两个代码第一个是正常的不使用状态机的SPI主机代码:第二个是状态机SPI代码 1.不使用状态机:特权同学<深入浅出玩转FPGA>中DIY数码相框部分代码: /////////// ...

  2. ARM与FPGA通过spi通信设计1.spi基础知识

    SPI(Serial Peripheral Interface--串行外设接口)总线系统是一种同步串行外设接口,它可以使MCU与各种外围设备以串行方式进行通信以交换信息.SPI总线可直接与各个厂家生产 ...

  3. dubbo源码分析2——SPI机制中的SPI实现类的读取和预处理

    SPI机制中的SPI实现类的读取和预处理是由ExtensionLoader类的loadFile方法来完成的 loadFile方法的作用是读取dubbo的某个SPI接口的spi描述文件,然后进行缓存,缓 ...

  4. Linux spi驱动分析(二)----SPI核心(bus、device_driver和device)

    一.spi总线注册 这里所说的SPI核心,就是指/drivers/spi/目录下spi.c文件中提供给其他文件的函数,首先看下spi核心的初始化函数spi_init(void).程序如下: 点击(此处 ...

  5. UART,USART,SPI,I2C等总线的介绍与区别20160526

    首先来说一下UART和USART的区别: 1.字面意义: UART:universal asynchronous receiver and transmitter通用异步收发器: USART:univ ...

  6. 使用STM32的USART的同步模式Synchronous调戏SPI[2] 【实现spi 9bit】

    [原创出品§转载请注明出处] 出处:http://www.cnblogs.com/libra13179/p/7064533.html 上回说道使用USART的来模拟SPI通讯.说说一下我什么写这个的原 ...

  7. SPI总线协议及SPI时序图详解

    SPI,是英语Serial Peripheral Interface的缩写,顾名思义就是串行外围设备接口.SPI,是一种高速的,全双工,同步的通信总线,并且在芯片的管脚上只占用四根线,节约了芯片的管脚 ...

  8. [SPI&I2C]I2C和SPI协议介绍

    IIC vs SPI 现今,在低端数字通信应用领域,我们随处可见IIC (Inter-Integrated Circuit) 和 SPI (Serial Peripheral Interface)的身 ...

  9. spi数据KL25用SPI操作nor flash

    最近研究spi数据,稍微总结一下,以后继续补充: KL25的SPI连接一个nor flash.该flash型号为FM25F04,支撑SPI的模式0和模式3,要求高位先发送,在上升沿采集数据. 通常,S ...

随机推荐

  1. 重要bug记录

    导唱功能:需求点分析:本地已下载歌曲播放,判断是否有音频原唱伴奏版权,无版权按钮显示“导唱”,有版权显示“播原唱”.程序实现逻辑: 1.下载歌曲时调用一个歌曲信息接口,返回歌曲的一些属性信息,其中包括 ...

  2. python学习第八天

    解析库之bs4的基本使用方法 ''' pip install beautifulsoup4#安装bs4 pip install lxml#安装lxml ''' html_doc = "&qu ...

  3. [MRCTF]Web WriteUp

    和武科大WUSTCTF同时打的一场比赛,最后因为精力放在武科大比赛上了,排名13  - -Web题目难度跨度过大,分不清层次,感觉Web题目分布不是很好,质量还是不错的 Ez_bypass 进入题目得 ...

  4. 做一名合格的DBA

    Oracle DBA的角色定义 开发型DBA 数据库安装 数据库架构设计(架构和建模) 代码开发(存储过程,SQL) 运维型DBA 数据库日常监控 故障处理 性能优化 数据备份,容灾 数据库安全规划 ...

  5. 基于django快速开发一个网站(一)

    基于django快速开发一个网站(一) *  创建虚拟环境.基于虚拟环境创建django==2.0.0和图片加载库和mysql数据库驱动 1. 创建目录并创建虚拟环境 ╰$ mkdir Cornuco ...

  6. Java并发编程:volatile关键字解析【转载】

    介绍 volatile这个关键字可能很多朋友都听说过,或许也都用过.在Java 5之前,它是一个备受争议的关键字,因为在程序中使用它往往会导致出人意料的结果.在Java 5之后,volatile关键字 ...

  7. AP、AC、无线路由器

    起因 AP.AC.无线路由器 一直都傻傻的分不清,今天就好好的研究一下他们之间到底有什么联系和区别~ AP 什么是AP? 无线AP(Access Point):即无线接入点,它用于无线网络的无线交换机 ...

  8. HTTP 协议类

    HTTP 协议的主要特点 简单快速:每个资源的URL是固定的 灵活:在每个 http 协议中都有一个头部分有一个数据类型,通过一个 http 协议就可以完成不同数据类型的传输 无连接:连接一次就好断掉 ...

  9. C#封装YOLOv4算法进行目标检测

    C#封装YOLOv4算法进行目标检测 概述 官网:https://pjreddie.com/darknet/ Darknet:[Github] C#封装代码:[Github] YOLO: 是实现实时物 ...

  10. xUnit测试的顺序执行总结

    cmliu 1,演示环境:windows 10企业版+Visual Studio 2019:.NET Core3.1:xUnit 2.4.1:.NET Standard 2.0.3 3,场景描述:前几 ...