gpt4 book ai didi

c - 发送 2 个 SPI 字节问题

转载 作者:行者123 更新时间:2023-11-30 16:14:22 24 4
gpt4 key购买 nike

当使用 ATMega328 硬件 SPI 接口(interface)时,程序在第二个字节上出错。

我尝试读取 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

我的串行监视器正在“发送 LSB”。之后删除 while 循环允许代码继续,但这意味着数据没有被发送。

P.S 删除第一个 while 循环后,它继续执行并显示“已发送!”。然而,当代码第二次重复时;它再次陷入“发送 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);

它打印已发送(发送)位,但代码被卡住并且无法到达“已发送!”...

最佳答案

您是否将 SS 引脚配置为输出?这是使用 ATMega SPI 时常见的疏忽。

来自手册:

When the SPI is configured as a master (MSTR in SPCR is set), the user can determine the direction of the SS pin. If SS is configured as an output, the pin is a general output pin which does not affect the SPI system. Typically, the pin will be driving the SS pin of the SPI slave. If SS is configured as an input, it must be held high to ensure Master SPI operation. 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.

关于c - 发送 2 个 SPI 字节问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57736944/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com