问题描述
我正在 Proteus 上模拟我的代码,并尝试通过 SPI 作为主设备和从设备在两个 ATmega32 设备之间发送数据。但是,对于我尝试发送的字符串的每个字符,我一直收到从设备的警告“SPDR 写入冲突。写入的数据被忽略”。
#define F_CPU 8000000UL
#include <avr/io.h>
void PORT_INIT(void);
void SPI_SLAVE_INIT(void);
void SPI_SEND_STRING(void);
char *transmit = "Hello there!"; ///< Data to be sent.
int main(void){
DDRA = 0xFF;
PORT_INIT();
SPI_SLAVE_INIT();
SPI_SEND_STRING();
}
/*!
* @brief Initialize ports.
*/
void PORT_INIT(void){
DDRB &= ~((1<<PB4)|(1<<PB5)|(1<<PB7)); ///< Set MOSI,SCK and SS pins to input.
DDRB |= (1<<PB6); ///< Set MISO pin to output.
}
/*!
* @brief Initialize SPI in slave mode.
*/
void SPI_SLAVE_INIT(void){
SPCR = (1<<SPE); ///< Enable SPI in Slave SPI mode.
}
/*!
* @brief Transmit a message over SPI.
*/
void SPI_SEND_STRING(void){
for(uint8_t i=0;transmit[i]!=0;i++){
SPDR = transmit[i]; ///< Load the data byte to the SPDR shift register to transmit.
while(!(SPSR & (1<<SPIF))); ///< Wait until the full data byte is received.
PORTA = SPDR;
}
}
我在这里遗漏了什么吗?主设备时钟频率为 Fosc/16。
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)