各位,我用STM32F103RE的芯片,测试SPI1发现异常。
我将MOSI和MISO两个管脚短接,然后写数据,写完之后读数据,发现写入的数据最后一位与读出来的数据相反。
代码大概如下
SPI_InitTypeDef spi;
GPIO_InitTypeDef gpio;
EXTI_InitTypeDef diox;
NVIC_InitTypeDef nvic;
/* configure spi gpio */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_AFIO | RCC_APB2Periph_SPI1,ENABLE);
gpio.GPIO_Pin = GPIO_Pin_4; //NSS -> PA4
gpio.GPIO_Mode = GPIO_Mode_Out_PP;
gpio.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&gpio);
GPIO_SetBits(GPIOA,GPIO_Pin_4);
gpio.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_7; //SCK -> PA5,MOSI -> PA7
gpio.GPIO_Mode = GPIO_Mode_AF_PP;
gpio.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&gpio);
gpio.GPIO_Pin = GPIO_Pin_6; //MISO -> PA6
gpio.GPIO_Mode = GPIO_Mode_IN_FLOATING;
gpio.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA,&gpio);
/* configure spi */
spi.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;
spi.SPI_CPHA = SPI_CPHA_1Edge;
spi.SPI_CPOL = SPI_CPOL_Low;
spi.SPI_CRCPolynomial = 7;
spi.SPI_DataSize = SPI_DataSize_8b;
spi.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
spi.SPI_FirstBit = SPI_FirstBit_MSB;
spi.SPI_Mode = SPI_Mode_Master;
spi.SPI_NSS = SPI_NSS_Hard;
SPI_I2S_DeInit(SPI1);
SPI_Init(SPI1,&spi);
SPI_Cmd(SPI1,ENABLE);
我降低时钟频率,依然如此,想问下各位如何调试? |