void USART2_IRQHandler (void)//
{
if (USART_GetFlagStatus(USART2,USART_FLAG_RXNE) ==SET)
{
USART_SendData(USART2,USART_ReceiveData(USART2));
while (!USART_GetFlagStatus(USART2,USART_FLAG_TC));
}
USART_ClearITPendingBit(USART2,USART_IT_RXNE);//Öжϱê־λ
}
int main(void)
{
/*!< At this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_stm32f030.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32f0xx.c file
*/
/* SysTick end of count event each 1ms */
RCC_GetClocksFreq(&RCC_Clocks);
SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000);
/* Initialize LED2 */
STM_EVAL_LEDInit(LED2);
/* Initialize User_Button on STM32NUCLEO */
STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_EXTI);
//USART_SendData(USART2, 0x5555);
}
}
/**
* @brief Inserts a delay time.
* @param nTime: specifies the delay time length, in 1 ms.
* @retval None
*/
void Delay(__IO uint32_t nTime)
{
TimingDelay = nTime;
while(TimingDelay != 0);
}
/**
* @brief Decrements the TimingDelay variable.
* @param None
* @retval None
*/
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00)
{
TimingDelay--;
}
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t 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\r\n", file, line) */
/* Configure USART Tx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = COM_TX_PIN;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(COM_TX_PORT, &GPIO_InitStructure);
/* Configure USART Rx as alternate function push-pull */
GPIO_InitStructure.GPIO_Pin = COM_RX_PIN;
GPIO_Init(COM_RX_PORT, &GPIO_InitStructure);
IO配置为串口功能。
/* Connect PXx to USARTx_Tx */
GPIO_PinAFConfig(GPIOx,GPIO_PinSource,GPIO_AF_1);
/* Connect PXx to USARTx_Rx */
GPIO_PinAFConfig(GPIOx,GPIO_PinSource,GPIO_AF_1);
这怎么改啊,和我上面写的不是一样的吗
谢谢了,帮解决一下呗
我知道问题在哪了,因为我在配置时钟的时候应该选择APB1,如果我想要通过pc端的串口工具往板子里写东西,大体的做法可否指点一下,谢谢
我写的差不多,可是接收不到,在NVIC那里有点问题,我从网上找的,但是会报错,我对报错的几个区自己定义也不行,唉,大哥帮我看一下把,我的usart是有的,直接用的usart.c的,就没再main这写
#include "main.h"
static __IO uint32_t TimingDelay;
uint8_t __IO BlinkSpeed = 0;
/* Private function prototypes -----------------------------------------------*/
RCC_ClocksTypeDef RCC_Clocks;
USART_InitTypeDef USART2_InitStruct;
GPIO_InitTypeDef GPIO_InitStructure;
NVIC_InitTypeDef NVIC_InitStructure;
void USART2_IRQHandler (void)//
{
if (USART_GetFlagStatus(USART2,USART_FLAG_RXNE) ==SET)
{
USART_SendData(USART2,USART_ReceiveData(USART2));
while (!USART_GetFlagStatus(USART2,USART_FLAG_TC));
}
USART_ClearITPendingBit(USART2,USART_IT_RXNE);//Öжϱê־λ
}
int main(void)
{
/*!< At this stage the microcontroller clock setting is already configured,
this is done through SystemInit() function which is called from startup
file (startup_stm32f030.s) before to branch to application main.
To reconfigure the default setting of SystemInit() function, refer to
system_stm32f0xx.c file
*/
/* SysTick end of count event each 1ms */
RCC_GetClocksFreq(&RCC_Clocks);
SysTick_Config(RCC_Clocks.HCLK_Frequency / 1000);
/* Initialize LED2 */
STM_EVAL_LEDInit(LED2);
/* Initialize User_Button on STM32NUCLEO */
STM_EVAL_PBInit(BUTTON_USER, BUTTON_MODE_EXTI);
/* Initiate Blink Speed variable */
BlinkSpeed = 0;
USART_StructInit(&USART2_InitStruct);
RCC_AHBPeriphClockCmd(RCC_AHBPeriph_GPIOA, ENABLE);
RCC_APB1PeriphClockCmd(RCC_APB1Periph_USART2, ENABLE);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource2, GPIO_AF_1);
GPIO_PinAFConfig(GPIOA, GPIO_PinSource3, GPIO_AF_1);
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_2;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AF;
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
GPIO_InitStructure.GPIO_OType = GPIO_OType_PP;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_UP;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
GPIO_Init(GPIOA, &GPIO_InitStructure);
//
GPIO_PinAFConfig(GPIOA,GPIO_PinSource2,GPIO_AF_1);
GPIO_PinAFConfig(GPIOA,GPIO_PinSource3,GPIO_AF_1);
//nvic
//NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
NVIC_InitStructure.NVIC_IRQChannel = USART2_IRQn;
//NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 1;
//NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;
NVIC_InitStructure.NVIC_IRQChannelPriority= 0x03;
NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;
NVIC_Init(&NVIC_InitStructure);
USART_DeInit(USART2);
USART_Init(USART2, &USART2_InitStruct);
USART_ITConfig(USART2,USART_IT_RXNE,ENABLE);
USART_ClearFlag (USART2,USART_FLAG_TC);
USART_Cmd(USART2, ENABLE);
/* Infinite loop */
while (1)
{
/* Test on blink speed */
if(BlinkSpeed == 0)
{
/*LED2 Toggle each 50ms*/
STM_EVAL_LEDToggle(LED2);
Delay(50);
}
else if(BlinkSpeed == 1)
{
STM_EVAL_LEDToggle(LED2);
/*LED2 Toggle each 200ms */
Delay(200);
}
//USART_SendData(USART2, 0x5555);
}
}
/**
* @brief Inserts a delay time.
* @param nTime: specifies the delay time length, in 1 ms.
* @retval None
*/
void Delay(__IO uint32_t nTime)
{
TimingDelay = nTime;
while(TimingDelay != 0);
}
/**
* @brief Decrements the TimingDelay variable.
* @param None
* @retval None
*/
void TimingDelay_Decrement(void)
{
if (TimingDelay != 0x00)
{
TimingDelay--;
}
}
#ifdef USE_FULL_ASSERT
/**
* @brief Reports the name of the source file and the source line number
* where the assert_param error has occurred.
* @param file: pointer to the source file name
* @param line: assert_param error line source number
* @retval None
*/
void assert_failed(uint8_t* file, uint32_t 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\r\n", file, line) */
/* Infinite loop */
while (1)
{
}
}
#endif
/**
* @}
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
https://www.stmcu.org.cn/document/list/index/category-550
我现在通过串口工具发送可以在接收到,可是我突然发现我定义的USART2_IRQHandler压根就没地方调用,那为什么可以通呢