简体   繁体   English

发送2个SPI字节

[英]Sending 2 SPI Bytes ISSUE

When using the ATMega328 hardware SPI interface, the program gets suck on the second byte. 当使用ATMega328硬件SPI接口时,程序会在第二个字节上吸吮。

I have tried reading the SPDR to clear the SPIF bit in SPSR, I thought that the flag must be cleared before my code will continue 我尝试读取SPDR以清除SPSR中的SPIF位,我认为在继续执行代码之前必须清除该标志

PORTB &= !(1<<SSDAC); // Pulling slave select low
_delay_ms(10);

Serial.println("Sending MSB");
SPDR = MSByte;
while (!(SPSR & (1 << SPIF)));

Serial.println("Sending LSB");
SPDR = LSByte;
while (!(SPSR & (1 << SPIF)));

Serial.println("Sent!");
_delay_ms(10);
PORTB |= (1<<SSDAC); // Pulling slave select high

My serial monitor is getting up to "Sending LSB". 我的串行监视器即将达到“正在发送LSB”。 Removing the while loop after that allow the code to continue, but this implies the data isn't being sent. 在此之后删除while循环将允许代码继续进行,但这意味着未发送数据。

PS After removing the first while loop, it continues through and reads "Sent!". PS删除第一个while循环后,它将继续进行并显示“已发送!”。 However, when the code repeats for a second time; 但是,当代码第二次重复时; it again gets stuck at "Sending LSB"... I have now even tried using the Arduino SPI library... STILL STUCK! 它再次被卡在“发送LSB”上...我现在甚至尝试过使用Arduino SPI库...仍然卡住!

PORTB &= ~(1 << SSDAC);
_delay_us(1);
Serial.println(bits, BIN);
data = SPI.transfer16(MSByte);
Serial.println("Sent!");
_delay_us(1);
PORTB |= (1 << SSDAC);

It prints the sent (sending) bits, but the code get's stuck and doesn't reach the "Sent!"... 它打印已发送(发送)的位,但是代码被卡住并且没有到达“已发送!” ...

Did you configure the SS pin as an output? 您是否将SS引脚配置为输出? This is a common oversight when using ATMega SPI. 使用ATMega SPI时,这是常见的疏忽。

From the manual: 从手册中:

When the SPI is configured as a master (MSTR in SPCR is set), the user can determine the direction of the SS pin. 当将SPI配置为主机(设置SPCR中的MSTR)时,用户可以确定SS引脚的方向。 If SS is configured as an output, the pin is a general output pin which does not affect the SPI system. 如果将SS配置为输出,则该引脚为通用输出引脚,不会影响SPI系统。 Typically, the pin will be driving the SS pin of the SPI slave. 通常,该引脚将驱动SPI从设备的SS引脚。 If SS is configured as an input, it must be held high to ensure Master SPI operation. 如果将SS配置为输入,则必须将其保持高电平以确保主SPI操作。 If the SS pin is driven low by peripheral circuitry when the SPI is configured as a master with the SS pin defined as an input, the SPI system interprets this as another master selecting the SPI as a slave and starting to send data to it. 如果将SPI配置为主机且将SS引脚定义为输入时,外围电路将SS引脚驱动为低电平,则SPI系统会将其解释为另一个主机,选择SPI作为从机并开始向其发送数据。

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

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