RT,最近用STM8L151K4的SPI1外挂无线模块,感觉SPI不听使唤呢,下面是我的代码和抓到的波形图,请大家指点一下。
- #define SPI_PIN_SCK_PORT GPIOB
- #define SPI_PIN_SCK GPIO_Pin_5
- #define SPI_PIN_MOSI_PORT GPIOB
- #define SPI_PIN_MOSI GPIO_Pin_6
- #define SPI_PIN_MISO_PORT GPIOB
- #define SPI_PIN_MISO GPIO_Pin_7
- #if 1
- void SpiInit( void )
- {
- /* Enable SPI clock */
- GPIO_Init(SPI_PIN_SCK_PORT, SPI_PIN_SCK , GPIO_Mode_Out_PP_Low_Fast);
- GPIO_Init(SPI_PIN_MOSI_PORT , SPI_PIN_MOSI , GPIO_Mode_Out_PP_Low_Fast);
- GPIO_Init(SPI_PIN_MISO_PORT , SPI_PIN_MISO , GPIO_Mode_In_PU_No_IT);
- SPI_DeInit(SPI1);
- CLK_PeripheralClockConfig(CLK_Peripheral_SPI1, ENABLE);
- //SPI_DeInit(SPI1);
- //GPIO_Init(GPIOB, GPIO_Pin_5, GPIO_Mode_Out_PP_High_Fast);
- //GPIO_Init(GPIOB, GPIO_Pin_6, GPIO_Mode_Out_PP_High_Fast);
- //主机模式,配置为输入 该设置很关键
- //GPIO_Init(GPIOB, GPIO_Pin_7, GPIO_Mode_In_PU_No_IT);
- /* Initialize SPI */
- SPI_Init(
- SPI1,
- SPI_FirstBit_MSB,
- SPI_BaudRatePrescaler_16,
- SPI_Mode_Master,
- SPI_CPOL_Low,
- SPI_CPHA_1Edge,
- SPI_Direction_2Lines_FullDuplex,
- SPI_NSS_Soft,
- 0x07
- );
- SPI_Cmd(SPI1,ENABLE);
- }
- uint8_t SpiInOut( uint8_t outData )
- {
- /* Loop while DR register in not emplty */
- while (SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE) == RESET);
- /* Send byte through the SPI1 peripheral */
- SPI_SendData(SPI1, outData);
- /* Wait to receive a byte */
- while (SPI_GetFlagStatus(SPI1, SPI_FLAG_RXNE) == RESET);
- /* Return the byte read from the SPI bus */
- return SPI_ReceiveData(SPI1);
- }
- void SX1276WriteBuffer( uint8_t addr, uint8_t *buffer, uint8_t size )
- {
- uint8_t i;
- GPIO_WriteBit( NSS_IOPORT, NSS_PIN, RESET );
- //temp = SpiInOut( addr | 0x80 );
- SpiInOut( addr );
- //SpiInOut(0);
- for( i = 0; i < size; i++ )
- {
- SpiInOut(buffer);
- //SpiInOut(0);
- }
- GPIO_WriteBit( NSS_IOPORT, NSS_PIN, SET );
- //SPI_Cmd(SPI1,DISABLE);
- }
- 最后调用SX1276Write(0xff,0x02);发现裸机分析仪抓到的和写入的值不一致,而且相差很多。用的是内部16M时钟