我打算用STM32的SPI模式与ch376通信,可是不正确,大家帮我看看哪错了?
程序实现的功能是:设置好SPI后,先发送一个0x05命令对ch376进行复位,然后再发送一个0x06命令,用以测试ch376的工作状态。将读回的结果赋给PD口的高八位(此8位连有LED)
下面是源码:
/*******************************************************************************
* Function Name : GPIO_Configuration
* Description : Configures the different GPIO ports.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/* Configure PA1 as Out */
GPIO_InitStructure.GPIO_Pin = CH376_CS; //控制CH376的使能端
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* Configure SPI1 pins: SCK, MISO and MOSI */
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5 | GPIO_Pin_6 | GPIO_Pin_7;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; //设置为复用功能
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8 | GPIO_Pin_9 | GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12 | GPIO_Pin_13 | GPIO_Pin_14 | GPIO_Pin_15;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_Init(GPIOD, &GPIO_InitStructure);
}
/*******************************************************************************
* Function Name : RCC_Configuration
* Description : Configures the different system clocks.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void RCC_Configuration(void)
{
~~~~~~~~
~~~~~
RCC_APB2PeriphClockCmd( RCC_APB2Periph_SPI1 | RCC_APB2Periph_GPIOC| RCC_APB2Periph_AFIO|RCC_APB2Periph_USART1|RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOA, ENABLE);
}
/*******************************************************************************
* Function Name : SPI_Configuration
* Description : Configure the SPI.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
void SPI_Configuration(void)
{
SPI_InitTypeDef SPI_InitStructure;
SPI_InitStructure.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
SPI_InitStructure.SPI_Mode = SPI_Mode_Master;
SPI_InitStructure.SPI_DataSize = SPI_DataSize_8b;
SPI_InitStructure.SPI_CPOL = SPI_CPOL_Low;
SPI_InitStructure.SPI_CPHA = SPI_CPHA_1Edge;
SPI_InitStructure.SPI_NSS = SPI_NSS_Soft;
SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;
SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB;
SPI_InitStructure.SPI_CRCPolynomial = 7;
SPI_Init(SPI1,&SPI_InitStructure);
// SPI_DataSizeConfig(SPI1,SPI_DataSize_8b);
SPI_Cmd(SPI1,ENABLE);
}
/* Private functions ---------------------------------------------------------*/
/*******************************************************************************
* Function Name : main
* Description : Main program.
* Input : None
* Output : None
* Return : None
*******************************************************************************/
int main(void)
{
#ifdef DEBUG
debug();
#endif
/* System Clocks Configuration */
RCC_Configuration();
/* NVIC configuration */
NVIC_Configuration();
/* SPI configuration */
SPI_Configuration();
/* Configure the GPIO ports */
GPIO_Configuration();
GPIO_ResetBits(GPIOA,CH376_CS); //使能CH376
SPI_I2S_SendData(SPI1, 0x05); //对ch376进行复位
GPIO_SetBits(GPIOA,CH376_CS); //禁止CH376
delay(200); //等待40ms (此处等待时间实际为60ms)
GPIO_ResetBits(GPIOA,CH376_CS); //使能CH376
SPI_I2S_SendData(SPI1, 0x06); //测试ch376的工作状态
SPI_I2S_SendData(SPI1, 0x55); //检测ch376的工作状态
result = SPI_I2S_ReceiveData(SPI1); //若操作正确,此时result读回的结果应该是对0x55取反后的值,即0xaa!
GPIO_SetBits(GPIOA,CH376_CS); //禁止CH376
while (1)
{
GPIO_SetBits(GPIOD, ((u16)result)<<8);
}
} |