int main(void)
{
RCC_Configuration();
GPIO_Configuration();
/* NVIC configuration */
NVIC_Configuration();
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_Init(USART2, &USART_InitStructure);
USART_ITConfig(USART2, USART_IT_TXE, ENABLE);
USART_ITConfig(USART2, USART_IT_RXNE, ENABLE);
USART_Cmd(USART2, ENABLE);
while (1)
{
}
}
void NVIC_Configuration(void)
{
NVIC_InitTypeDef NVIC_InitStructure;
/* Enable the USARTx Interrupt */
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQChannel;
NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
}
void USART2_IRQHandler(void)
{
if(USART_GetITStatus(USART2, USART_IT_RXNE) != RESET)
{
/* Read one byte from the receive data register */
RxBuffer[RxCounter++] = (USART_ReceiveData(USART2) & 0x7F);
if(RxCounter == NbrOfDataToRead)
{
/* Disable the EVAL_COM1 Receive interrupt */
USART_ITConfig(USART2, USART_IT_RXNE, DISABLE);
}
}
if(USART_GetITStatus(USART2, USART_IT_TXE) != RESET)
{
/* Write one byte to the transmit data register */
USART_SendData(USART2, TxBuffer[TxCounter++]);
if(TxCounter == NbrOfDataToTransfer)
{
/* Disable the EVAL_COM1 Transmit interrupt */
USART_ITConfig(USART2, USART_IT_TXE, DISABLE);
}
}
}
void RCC_Configuration(void)
{
ErrorStatus HSEStartUpStatus;
RCC_HSEConfig(RCC_HSE_ON);//开启HSE
HSEStartUpStatus=RCC_WaitForHSEStartUp(); //等待HSE高速晶振稳定
if(HSEStartUpStatus==SUCCESS)
{ RCC_HCLKConfig(RCC_SYSCLK_Div1);//设置高速总线时钟
RCC_PCLK2Config(RCC_HCLK_Div1);//设置高速时钟
RCC_PCLK1Config(RCC_HCLK_Div2); //设置低速时钟
FLASH_SetLatency(FLASH_Latency_2);//延时
FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
RCC_PLLConfig(RCC_PLLSource_HSE_Div1,RCC_PLLMul_9);//设置PLL时钟源及倍频数
RCC_ADCCLKConfig(RCC_PCLK2_Div6);
RCC_PLLCmd(ENABLE);//使能PLL锁相环
while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY)==RESET);
RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);//设置系统时钟
while(RCC_GetSYSCLKSource()!=0x08);
}
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
/* GPIOA and GPIOB clock enable */
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
}
void GPIO_Configuration(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
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_3;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
GPIO_Init(GPIOA, &GPIO_InitStructure);
}
请大家帮忙看看,这个是官方的例程,USART没有数据输出,不知道是哪里有问题? |