简体   繁体   English

SD.h 和 U8x8.h (LCD) 之间的 SPI 冲突

[英]SPI conflict between SD.h and U8x8.h (LCD)

I want to reproduce a .wav file on my Arduino Uno via an SD card while at the same time using an LCD display (Nokia 5110).我想通过 SD 卡在我的 Arduino Uno 上重现 .wav 文件,同时使用 LCD 显示器(诺基亚 5110)。 I'm having trouble trying to make the double SPIs work and since I'm not really familiar with the libraries that I used I decided to ask for help我在尝试使双 SPI 工作时遇到了麻烦,因为我对我使用的库不是很熟悉,所以我决定寻求帮助

The problem is that once the program execute SD.begin() the display stops working and the SPI communication is working only on the SD.问题是一旦程序执行SD.begin()显示停止工作并且 SPI 通信仅在 SD 上工作。

In this setup that I'm showing I will initialize the SD in the setup() and in the loop using a button I'm supposed to write stuff on the display:在我展示的这个设置中,我将在 setup() 和循环中使用一个按钮初始化 SD,我应该在显示器上写东西:

//Libraries
  #include <U8x8lib.h>
  #include <SPI.h>
  #include "SD.h"
  #include "TMRpcm.h"

  //Display                         (pin 13,  pin 11,  pin A1, pin 12, pin A0 )
    U8X8_PCD8544_84X48_4W_SW_SPI lcd(LCD_CLK, LCD_DIN, LCD_CE, LCD_DC, LCD_RST); 

  //Speaker
    TMRpcm music;


void setup() {
  
  //Serial port -> Open
    Serial.begin(9600);
    while (!Serial) {}

  //LCD
    display_setup();
    display_somethingElse();

  //SD and Speaker
    sdAndSpeaker_setup();

}

void loop() {

  topButton.poll();
  if (topButton.buttonClicked) {
    display_something();  
  }
  
}

Ignore the function related to the button, I removed most of its code to avoid cluttering the page.忽略与按钮相关的功能,我删除了大部分代码以避免页面混乱。

Here's when the problem occurs:这是出现问题的时候:

void sdAndSpeaker_setup() {

    music.speakerPin = 10;
//               (pin 4)
    if (!SD.begin(SD_CS)) {
    Serial.println("SD fail");
    Serial.flush();
    abort();
    }
    
    music.setVolume(4);
    music.play("Daybreak.wav");
    music.quality(2);
    
}

I know that by changing the pins of display to other value (rather than 11, 12 and 13) the problems is solved but most of the pins in my board are already used and I don't have this luxury... I think this has something to do with the U8x8.h and SD.h library but I don't have the skills to tweak those.我知道通过将显示器的引脚更改为其他值(而不是 11、12 和 13),问题解决了,但我板上的大多数引脚都已经使用了,我没有这种奢侈......我认为这个与 U8x8.h 和 SD.h 库有关,但我没有调整这些的技能。

If anyone has a suggestion I'll appreciate!如果有人有建议,我将不胜感激! Thanks!谢谢!

The constructor构造函数

 U8X8_PCD8544_84X48_4W_SW_SPI(uint8_t clock, uint8_t data, uint8_t cs, uint8_t dc, uint8_t reset = U8X8_PIN_NONE)

creates a 'driver' which uses software SPI.创建一个使用软件 SPI 的“驱动程序”。 But you supply pins of hardware SPI as parameters.但是您提供硬件 SPI 的引脚作为参数。 Hardware SPI is used by the SD library over the SPI library to access the SD card, so hardware SPI conflicts with the software SPI of the display library . SD库通过SPI库使用硬件SPI访问SD卡,因此硬件SPI与显示库的软件SPI冲突

There is a有一个

U8X8_PCD8544_84X48_4W_HW_SPI(uint8_t cs, uint8_t dc, uint8_t reset = U8X8_PIN_NONE)

constructor which uses the hardware SPI over the SPI library.通过 SPI 库使用硬件 SPI 的构造函数。 The SPI library 'knows' the pin numbers of the SPI pins. SPI 库“知道”SPI 引脚的引脚编号。 (SPI library is part of the boards package. It can't be installed separately.) (SPI 库是板子包的一部分,不能单独安装。)

If you want to use the software SPI, use free pins.如果要使用软件 SPI,请使用空闲引脚。 The hardware SPI uses pins 11, 12, 13 on Uno.硬件 SPI 使用 Uno 上的引脚 11、12、13。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM