有一个简单的代码,其中按钮的外部中断通过 USART1 发送“1”,当同一 UART 到达零时,LED 熄灭。我使用KEIL 5编译。
#include <stdio.h>
#include <stdlib.h>
#include <stm32f10x.h>
#define BLUE_LED_ON GPIOC->BSRR=GPIO_BSRR_BS8
#define GREEN_LED_ON GPIOC->BSRR=GPIO_BSRR_BS9
#define BLUE_LED_OFF GPIOC->BSRR=GPIO_BSRR_BR8
#define GREEN_LED_OFF GPIOC->BSRR=GPIO_BSRR_BR9
void EXTI0_IRQHandler (void);
void USART1_IRQHandler (void);
void Button_Interrupt_Init (void);
void Led_Init (void);
void UART_Init (void);
int main (void)
{//USART1 TX-PA9 RX-PA10
SystemInit();
Button_Interrupt_Init();
UART_Init();
Led_Init();
while(1){}
}
void Button_Interrupt_Init (void)
{
RCC->APB2ENR |= RCC_APB2ENR_AFIOEN;
AFIO->EXTICR[0] |= AFIO_EXTICR1_EXTI0_PA;
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN;
__enable_irq();
EXTI->PR |= EXTI_PR_PR0;
EXTI->RTSR |= EXTI_RTSR_TR0;
EXTI->IMR |= EXTI_IMR_MR0;
NVIC_EnableIRQ(EXTI0_IRQn);
}
void Led_Init (void)
{
RCC->APB2ENR |= RCC_APB2ENR_IOPCEN;
GPIOC->CRH = GPIO_CRH_MODE8|GPIO_CRH_MODE9;
BLUE_LED_ON;
GREEN_LED_ON;
for (int i =0; i<300000; i++);
BLUE_LED_OFF;
GREEN_LED_OFF;
for (int i =0; i<300000; i++);
BLUE_LED_ON;
GREEN_LED_ON;
for (int i =0; i<300000; i++);
BLUE_LED_OFF;
GREEN_LED_OFF;
for (int i =0; i<300000; i++);
BLUE_LED_ON;
GREEN_LED_ON;
for (int i =0; i<300000; i++);
BLUE_LED_OFF;
GREEN_LED_OFF;
for (int i =0; i<300000; i++);
}
void UART_Init (void)
{
//CLEAN CNF AND MODE and SET 1011 - alternative push/pull
GPIOA->CRH &= ~(GPIO_CRH_CNF9|GPIO_CRH_CNF10|GPIO_CRH_MODE9|GPIO_CRH_MODE10);
GPIOA->CRH |= GPIO_CRH_MODE9|GPIO_CRH_CNF9_1|GPIO_CRH_MODE10|GPIO_CRH_CNF10_1;
RCC->APB2ENR |= RCC_APB2ENR_USART1EN;
USART1->BRR = 0x4E2;
USART1->CR1 |= USART_CR1_RXNEIE|USART_CR1_TE|USART_CR1_RE;
USART1->CR1 &= ~(USART_CR1_M|USART_CR1_PCE);
USART1->CR2 &= ~(USART_CR2_STOP);
NVIC_EnableIRQ(USART1_IRQn);
USART1->CR1 |= USART_CR1_UE;
}
void USART1_IRQHandler (void)
{
GREEN_LED_ON;
BLUE_LED_ON;
if(USART1->SR & USART_SR_RXNE) // Check is 'RX not empty' bit set -> data is in UART->DR
{
uint8_t RXc = (uint8_t)(USART1->DR&0xFF); // put symbol from UART->DR in RXc variable
switch(RXc) // Symbol-dependent cases
{
case '0':
GREEN_LED_OFF;
break;
default:
BLUE_LED_OFF;
break;
}
}
NVIC_ClearPendingIRQ(USART1_IRQn); // Clear pending bit from UART1 interrupt
}
void EXTI0_IRQHandler (void)
{
USART1->DR = 1;
EXTI->PR |= EXTI_PR_PR0;
}
问题是,当按下按钮时,“1”来到终端,一切正常,但是当我尝试从终端发送字符到微控制器时,MCU 没有进入中断。当 MCU 进入中断时,GREEN_LED_ON 和 BLUE_LED_ON 应为 ON,但没有任何反应。
我尝试制作一个带有USART1初始化的STM32CUBEIDE项目,只需在自动生成的代码中添加UART_Init(与上面相同,但这个没有引脚配置)和USART1_IRQHandler函数。 CUBEIDE 代码在这里(没有“用户代码开始/结束等内容”):
#include "main.h"
#define BLUE_LED_ON GPIOC->BSRR=GPIO_BSRR_BS8
#define GREEN_LED_ON GPIOC->BSRR=GPIO_BSRR_BS9
#define BLUE_LED_OFF GPIOC->BSRR=GPIO_BSRR_BR8
#define GREEN_LED_OFF GPIOC->BSRR=GPIO_BSRR_BR9
UART_HandleTypeDef huart1;
void UART_Init (void);
void USART1_IRQHandler (void);
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART1_UART_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
MX_USART1_UART_Init();
UART_Init();
GREEN_LED_ON;
BLUE_LED_ON;
while (1)
{
}
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL3;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
{
Error_Handler();
}
}
static void MX_USART1_UART_Init(void)
{
huart1.Instance = USART1;
huart1.Init.BaudRate = 115200;
huart1.Init.WordLength = UART_WORDLENGTH_8B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_NONE;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(&huart1) != HAL_OK)
{
Error_Handler();
}
}
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_8|GPIO_PIN_9, GPIO_PIN_RESET);
/*Configure GPIO pins : PC8 PC9 */
GPIO_InitStruct.Pin = GPIO_PIN_8|GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
}
void UART_Init (void)
{
USART1->CR1 |= USART_CR1_RXNEIE|USART_CR1_TE|USART_CR1_RE;
USART1->CR1 &= ~(USART_CR1_M|USART_CR1_PCE);
USART1->CR2 &= ~(USART_CR2_STOP);
NVIC_EnableIRQ(USART1_IRQn);
USART1->CR1 |= USART_CR1_UE;
}
void USART1_IRQHandler (void)
{
if(USART1->SR & USART_SR_RXNE) // Check is 'RX not empty' bit set -> data is in UART->DR
{
uint8_t RXc = (uint8_t)(USART1->DR&0xFF); // put symbol from UART->DR in RXc variable
switch(RXc) // Symbol-dependent cases
{
case '0':
GREEN_LED_OFF;
break;
case '1':
BLUE_LED_OFF;
break;
default:
BLUE_LED_ON;
GREEN_LED_ON;
break;
}
}
NVIC_ClearPendingIRQ(USART1_IRQn); // Clear pending bit from UART1 interrupt
}
void Error_Handler(void)
{
__disable_irq();
while (1)
{
}
}
#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 CODE BEGIN 6 */
/* 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) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
这段代码效果很好!我从终端接收所有消息,代码根据需要处理它们。
我注意到这些代码之间的唯一区别是 CUBE 代码中缺少触点 PA9 和 PA10(负责 USART)的初始化。
所以问题是:
谢谢!
一些便于理解的澄清:
使用STM32F100时,无法将引脚设置为复用功能INPUT(请参阅手册、CRH和CRL寄存器),因此将PA10(UART RX)配置为输入。就我而言,我只是在KEIL代码中的UART_Init函数中删除了PA10配置(PA10在复位状态下是浮动输入)