我做STM32和STC12LE5412的SPI通讯,STM32在接受到USART1发出的0x55后就连续发送20个字节,但是在发送第一个字节的时候要将NSS拉低(NSS脚被软件配置为用户自用),接着将剩余的19个字节发送出去。STM32为主,STC12为从。 我的程序如下,结果是STC12可以正确的收到STM32发出的数据,但是STM32接受不到STC12的数据。请高手帮帮看看我的程序是不是有问题。STC12的软件硬件设置都可以保证没有问题。谢谢了!!
/* Includes ------------------------------------------------------------------*/ #include "stm32f10x_lib.h" #include "stdio.h"
/* Local includes ------------------------------------------------------------*/ /* Private typedef -----------------------------------------------------------*/
/* Private define --------------------------------------------------*/ #define BufferSize 20
/* Private macro -------------------------------------------------------------*/ /* Private variables ---------------------------------------------------------*/ u8 SPI1_Buffer_Tx[BufferSize] = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20}; u8 SPI1_Buffer_Rx[BufferSize]={}; ErrorStatus HSEStartUpStatus;
/* Private functions ---------------------------------------------------------*/ void RCC_Configuration(void); void GPIO_Configuration(void); void NVIC_Configuration(void); void USART_Configuration(void) ; void SPI_Configuration(void) ;
void Delay(vu32 nCount) { for(; nCount != 0; nCount--); }
void SPI1_SUB(void) { u8 i; USART_ClearFlag(USART1,USART_FLAG_RXNE); GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_RESET); //设置联系线为低电平 NSS SPI_SendData(SPI1,SPI1_Buffer_Tx[0]); //SPI1发送第一个数据; while(SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE)==RESET) //等待,直到SPI1数据发送完毕 while(SPI_GetFlagStatus(SPI1,SPI_FLAG_RXNE)==RESET) //等待,直到SPI1接受到数据 SPI1_Buffer_Rx[0]=SPI_ReceiveData(SPI1); //将数据放入缓冲区 USART_SendData(USART1,SPI1_Buffer_Rx[0]); Delay(200);//保证联系信号持续时间足够 GPIO_WriteBit(GPIOA, GPIO_Pin_4, Bit_SET); // 恢复联系线 for(i=19;i>0;i--) //发送剩余的19个数据,接受和发送的过程如上 { SPI_SendData(SPI1,SPI1_Buffer_Tx[20-i]); while(SPI_GetFlagStatus(SPI1, SPI_FLAG_TXE)==RESET) while(SPI_GetFlagStatus(SPI1,SPI_FLAG_RXNE)==RESET) SPI1_Buffer_Rx[20-i]=*(vu32 *)SPI1_DR_Address; USART_SendData(USART1,SPI1_Buffer_Rx[20-i]); }
} /******************************************************************************* * Function Name : main * Description : Main program * Input : None * Output : None * Return : None *******************************************************************************/ int main(void) { /* System clocks configuration ---------------------------------------------*/ RCC_Configuration();
/* NVIC configuration ------------------------------------------------------*/ NVIC_Configuration();
/* GPIO configuration ------------------------------------------------------*/ GPIO_Configuration();
/* 通用串口设置*/ USART_Configuration();
SPI_Configuration(); USART_Cmd(USART1, ENABLE);
/* Enable SPI1 */
SPI_CalculateCRC(SPI1,DISABLE); //不需要校验
SPI_Cmd(SPI1, ENABLE);
while(1) { if(USART_GetFlagStatus(USART1,USART_FLAG_RXNE)) //当从USART接受到0x55后传输数据 { SPI1_SUB(); } } }
/******************************************************************************* * Function Name : RCC_Configuration * Description : Configures the different system clocks. * Input : None * Output : None * Return : None *******************************************************************************/ void RCC_Configuration(void) { ErrorStatus HSEStartUpStatus; /* RCC system reset(for debug purpose) */ RCC_DeInit();
/* Enable HSE */ RCC_HSEConfig(RCC_HSE_ON);
/* Wait till HSE is ready */ HSEStartUpStatus = RCC_WaitForHSEStartUp();
if(HSEStartUpStatus == SUCCESS) { /* HCLK = SYSCLK =36MHZ*/ RCC_HCLKConfig(RCC_SYSCLK_Div1); /* PCLK2 = HCLK=36MHZ */ RCC_PCLK2Config(RCC_HCLK_Div1);
/* PCLK1 = HCLK/2 =18MHZ*/ RCC_PCLK1Config(RCC_HCLK_Div2);
/* Flash 2 wait state */ FLASH_SetLatency(FLASH_Latency_2); /* Enable Prefetch Buffer */ FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
/*系统时钟36MK PLLCLK = 4MHz * 9 = 36MHz */ RCC_PLLConfig(RCC_PLLSource_HSE_Div2, RCC_PLLMul_9);
/* Enable PLL */ RCC_PLLCmd(ENABLE);
/* Wait till PLL is ready */ while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET) { }
/* Select PLL as system clock source */ RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
/* Wait till PLL is used as system clock source */ while(RCC_GetSYSCLKSource() != 0x08) { } }
/* GPIOA, GPIOB and SPI1 clock enable */ RCC_APB2PeriphClockCmd(RCC_APB2Periph_USART1 |RCC_APB2Periph_GPIOA | RCC_APB2Periph_SPI1, ENABLE);
}
/******************************************************************************* * 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 SPI1 pins: SCK, MISO and MOSI ---------------------------------*/ GPIO_InitStructure.GPIO_Pin =GPIO_Pin_5 |GPIO_Pin_6| GPIO_Pin_7; GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure);
GPIO_InitStructure.GPIO_Pin =GPIO_Pin_4;//配置NSS脚为联系线 GPIO_InitStructure.GPIO_Mode =GPIO_Mode_Out_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); /* 配置PA10位USART1的接收引脚 ,50MHZ*/ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_10; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING; GPIO_Init(GPIOA, &GPIO_InitStructure);
/* 配置PA9为USART1的发送引脚 */ GPIO_InitStructure.GPIO_Pin = GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP; GPIO_Init(GPIOA, &GPIO_InitStructure); }
/******************************************************************************* * Function Name : NVIC_Configuration * Description : Configure the nested vectored interrupt controller. * Input : None * Output : None * Return : None *******************************************************************************/ void NVIC_Configuration(void) {
#ifdef VECT_TAB_RAM /* Set the Vector Table base location at 0x20000000 */ NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0); #else /* VECT_TAB_FLASH */ /* Set the Vector Table base location at 0x08000000 */ NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0); #endif /* 2 bit for pre-emption priority, 3 bits for subpriority */
}
/******************************************************************************* * Function Name : SPI_Configuration * Description : Configures the different SPI. * Input : None * Output : None * Return : None *******************************************************************************/ void SPI_Configuration(void) { /* SPI1 configuration ------------------------------------------------------*/ 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_High; SPI_InitStructure.SPI_CPHA = SPI_CPHA_2Edge; SPI_InitStructure.SPI_NSS = SPI_NSS_Soft; SPI_InitStructure.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_64; SPI_InitStructure.SPI_FirstBit = SPI_FirstBit_MSB; SPI_InitStructure.SPI_CRCPolynomial = 7; SPI_Init(SPI1, &SPI_InitStructure); }
/******************************************************************************* * Function Name : GPIO_Configuration * Description : 设置串口. * Input : None * Output : None * Return : None *******************************************************************************/
void USART_Configuration(void) {
/*9600波特,8数据位,1停止位,无校验位,全双工通讯。*/ USART_InitTypeDef USART_InitStructure; USART_InitStructure.USART_BaudRate = 9600; USART_InitStructure.USART_WordLength = USART_WordLength_8b; USART_InitStructure.USART_StopBits = USART_StopBits_1; USART_InitStructure.USART_Parity = USART_Parity_No; USART_InitStructure.USART_HardwareFlowControl = USART_HardwareFlowControl_None; USART_InitStructure.USART_Mode =USART_Mode_Rx | USART_Mode_Tx; USART_InitStructure.USART_Clock = USART_Clock_Disable; USART_InitStructure.USART_CPOL = USART_CPOL_Low; USART_InitStructure.USART_CPHA = USART_CPHA_2Edge; USART_InitStructure.USART_LastBit = USART_LastBit_Disable; /* Configure USART1 */ USART_Init(USART1, &USART_InitStructure); /* Configure USART1 */
}
#ifdef DEBUG /******************************************************************************* * Function Name : assert_failed * Description : Reports the name of the source file and the source line number * where the assert error has occurred. * Input : - file: pointer to the source file name * - line: assert error line source number * Output : None * Return : None *******************************************************************************/ void assert_failed(u8* file, u32 line) { /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d
", file, line) */
/* Infinite loop */ while (1) { } } #endif |
|