小弟看资料写的USART1的程序,想与上位机通信,但是无法接受数据……求大神教育
/* Includes ------------------------------------------------------------------*/
#include "stm32f10x.h"
#include "platform_config.h"
GPIO_InitTypeDef GPIO_InitStructure;
USART_InitTypeDef USART_InitStructure;
/* Private function prototypes -----------------------------------------------*/
void RCC_Configuration(void);
void GPIO_Configuration(void);
void NVIC_Configuration(void);
int main(void)
{
RCC_Configuration();
NVIC_Configuration();
GPIO_Configuration();
GPIO_SetBits(GPIO_LED,DS1_PIN|DS2_PIN|DS3_PIN|DS4_PIN);/*关闭所有的LED指示灯*/
USART_InitStructure.USART_BaudRate = 115200; /*设置波特率为115200*/
USART_InitStructure.USART_WordLength = USART_WordLength_8b;/*设置数据位为8*/
USART_InitStructure.USART_StopBits = USART_StopBits_1; /*设置停止位为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; /*发送和接收*/
/*配置串口1 */
USART_Init(USART1, &USART_InitStructure);
/*使能串口1的发送和接收中断*/
USART_ITConfig(USART1, USART_IT_RXNE, ENABLE);
USART_ITConfig(USART1, USART_IT_TXE, ENABLE);
/* 使能串口1 */
USART_Cmd(USART1, ENABLE);
while (1)
{
USART_SendData(USART1, 'a');
}
}
void RCC_Configuration(void)
{
/*使能串口1使用的GPIO时钟*/
RCC_APB2PeriphClockCmd(USART1_GPIO_CLK , ENABLE);
/*使能串口1时钟*/
RCC_APB2PeriphClockCmd(USART1_CLK, ENABLE);
/*使能LED灯使用的GPIO时钟*/
RCC_APB2PeriphClockCmd(RCC_GPIO_LED, ENABLE);
}
/**
* @brief Configures the different GPIO ports.
* @param None
* @retval None
*/
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
/*串口1 RX管脚配置*/
/* Configure USART1 Rx as input floating */
GPIO_InitStructure.GPIO_Pin = USART1_RxPin;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(USART1_GPIO, &GPIO_InitStructure);
/*串口1 TX管脚配置*/
/* Configure USART1 Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = USART1_TxPin;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF_PP;
GPIO_Init(USART1_GPIO, &GPIO_InitStructure);
/* 配置神舟III号LED灯使用的GPIO管脚模式*/
GPIO_InitStructure.GPIO_Pin = DS1_PIN|DS2_PIN|DS3_PIN|DS4_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_Init(GPIO_LED, &GPIO_InitStructure);
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Configure the NVIC Preemption Priority Bits */
NVIC_PriorityGroupConfig(NVIC_PriorityGroup_0);
/* Enable the USART1 Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART1_IRQn;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
在USART_SendData(USART1, 'a');
前面加上这句
while (USART_GetFlagStatus(USART1, USART_FLAG_TC) == RESET)
试试看,这是查询法