stm32f030+LIS3DH:
LIS3DH手册上说CS接GND才是SPI模式,便SPI read/write时序图中CS需要下跳,上跳,这怎么理解?
我读WHO AM I返回的是FF或00,这不对呀?
示波器上能看到CS,MOSI输出的波形,就是MISO的波形不正确。
stm32f030配置代码:
GPIO_PinAFConfig(GPIOA, GPIO_PinSource5, GPIO_AF_0); //SCK
GPIO_PinAFConfig(GPIOA, GPIO_PinSource6, GPIO_AF_0); //MISO
GPIO_PinAFConfig(GPIOA, GPIO_PinSource7, GPIO_AF_0);
gpiodef.GPIO_Pin = GPIO_Pin_4; //SPI CS
gpiodef.GPIO_Mode = GPIO_Mode_OUT;
gpiodef.GPIO_OType = GPIO_OType_PP;
gpiodef.GPIO_PuPd = GPIO_PuPd_UP;
gpiodef.GPIO_Speed = GPIO_Speed_Level_3;
GPIO_Init(GPIOA, &gpiodef);
// GPIO_SetBits(GPIOA,GPIO_Pin_4);
GPIO_ResetBits(GPIOA,GPIO_Pin_4);
gpiodef.GPIO_Pin = GPIO_Pin_6;
gpiodef.GPIO_Mode = GPIO_Mode_AF;
gpiodef.GPIO_OType = GPIO_OType_PP;//GPIO_OType_OD;
gpiodef.GPIO_PuPd = GPIO_PuPd_NOPULL;//GPIO_PuPd_UP;
gpiodef.GPIO_Speed = GPIO_Speed_Level_3;
GPIO_Init(GPIOA, &gpiodef);
gpiodef.GPIO_Pin = GPIO_Pin_7 | GPIO_Pin_5;
gpiodef.GPIO_Mode = GPIO_Mode_AF;
gpiodef.GPIO_OType = GPIO_OType_PP;
gpiodef.GPIO_PuPd =GPIO_PuPd_UP; // GPIO_PuPd_NOPULL; //GPIO_PuPd_UP;
gpiodef.GPIO_Speed = GPIO_Speed_Level_3;
GPIO_Init(GPIOA, &gpiodef);
GPIO_SetBits(GPIOA,GPIO_Pin_7 | GPIO_Pin_6 | GPIO_Pin_5);
SPI_Cmd(SPI1, DISABLE);
spidef.SPI_Direction = SPI_Direction_2Lines_FullDuplex; //设置SPI单向或者双向的数据模式:SPI设置为双线双向全双工
spidef.SPI_Mode = SPI_Mode_Master; //设置SPI工作模式:设置为主SPI
spidef.SPI_DataSize = SPI_DataSize_8b; //设置SPI的数据大小:SPI发送接收8位帧结构
spidef.SPI_CPOL = SPI_CPOL_High; //选择了串行时钟的稳态:时钟悬空高
spidef.SPI_CPHA = SPI_CPHA_2Edge; //数据捕获于第二个时钟沿
spidef.SPI_NSS = SPI_NSS_Soft;//SPI_NSS_Hard;//SPI_NSS_Soft;
spidef.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256; //定义波特率预分频的值:波特率预分频值为256
spidef.SPI_FirstBit = SPI_FirstBit_MSB; //指定数据传输从MSB位还是LSB位开始:数据传输从MSB位开始
spidef.SPI_CRCPolynomial = 7; //CRC值计算的多项式
SPI_Init(SPI1, &spidef); //根据SPI_InitStruct中指定的参数初始化外设SPIx寄存器
SPI_RxFIFOThresholdConfig(SPI1,SPI_RxFIFOThreshold_QF);//SPI_RxFIFOThreshold_HF);//SPI_RxFIFOThreshold_QF);
SPI_Cmd(SPI1, ENABLE);
|