Arduino SPI不在atmega328p上工作
我正在尝试编程atmega328p,但SPI总线不工作。它没有通过总线发送任何数据 我的代码如下:Arduino SPI不在atmega328p上工作,arduino,spi,atmega,atmel,ice,Arduino,Spi,Atmega,Atmel,Ice,我正在尝试编程atmega328p,但SPI总线不工作。它没有通过总线发送任何数据 我的代码如下: #include <asf.h> #include <stdio.h> #include <main.h> int main (void) { board_init(); SPI_MasterInit(); DDRD = (1<<DDD5); while(1) { PORTB &=
#include <asf.h>
#include <stdio.h>
#include <main.h>
int main (void)
{
board_init();
SPI_MasterInit();
DDRD = (1<<DDD5);
while(1)
{
PORTB &= ~(1<<DDB2);
SPI_MasterTransmit(0xAB);
PORTB |= (1<<DDB2);
PORTD ^= (1<<DDD5);
}
}
void SPI_MasterInit(void)
{
/* Set MOSI and SCK output, all others input */
DDRB = (1<<5) || (1<<3) || (1<<2);
/* Enable SPI, Master, set clock rate fck/16 */
SPCR = (1<<SPE)|(1<<MSTR)|(1<<SPR0);
}
void SPI_MasterTransmit(char cData)
{
/* Start transmission */
SPDR = cData;
/* Wait for transmission complete */
while(!(SPSR & (1<<SPIF)));
}
#包括
#包括
#包括
内部主(空)
{
board_init();
SPI_MasterInit();
DDRD=(1我的第一个猜测是没有将SS引脚(PB2)设置为输出。这可能会导致几乎随机切换到从属模式。但它似乎被设置为输出(注释中没有说明)
但仔细观察这个表达式后,很明显它根本没有设置:
DDRB = (1<<5) || (1<<3) || (1<<2);
DDRB=(1解决了这个问题。有点愚蠢的错误。谢谢。