#c #stm32 #interrupt #gpio
Вопрос:
Я пишу проект, в котором данные поступают из UART, ввод Aamp;B от кодера и некоторые выходные контакты для выходных сигналов. Я проверил и убедился, что и UART, и входной/выходной сигнал кодера работают нормально отдельно. Однако я заметил, что прерывание «void USART1_IRQHandler(void)» сработает, когда поступит сигнал AB от кодировщика, который замораживает весь код…
Поэтому каждый раз, когда я включаю кодер или подключаю штырь сигнала AB, будет срабатывать » USART1_IRQHandler(void)». Если я выключу прерывание, все будет в порядке. Что не так с моим кодом?
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
uint8_t MSG[50] = {''};
uint8_t RX4_data[20];
uint8_t temp = 0;
uint8_t AB_in = 0;
uint8_t AB_in1 = 0;
uint8_t AB_in2 = 0;
uint8_t AB_in3 = 0;
uint16_t enn=0x2B;
uint16_t mo[7] ;
uint16_t en[7] ;
uint16_t add[7]={0,5000,10000,10000,0,0,0} ;
uint16_t bas=10000;
uint16_t B_out=0x00000000;
char m1=0x0;
char m2=0x0;
char m3=0x0;
char m4=0x0;
char m5=0x0;
char m6=0x0;
uint16_t mo8 =0x100;
uint16_t mo9 = 0x200;
uint16_t resolu= 1000;
uint8_t RX4_Buffer[28]; // uart received encrypt data
uint8_t RX4_Buffer_save[28];
uint8_t COM4Eq_RX4[21]; // 20 data decrpt
uint8_t Servo_Stat[7][4]; // status‘B
uint16_t Servo_Data[7][8]; //data
char hexcheck=0;
char current=0;
uint8_t start_com[9]= {0};
uint8_t first_receive_comfirm[9]= {0};
uint8_t end_receive_comfirm[9]= {0};
uint8_t p7send[11]= {0};
uint8_t test[1]={0x01};
uint8_t test1[1]={0x02};
uint8_t test2[1]={0x03};
uint8_t test3[1]={0x04};
char com_connect=0;
char com_finish=0;
char fault_relay=1;
uint8_t test_send[28];
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
TIM_HandleTypeDef htim1;
TIM_HandleTypeDef htim2;
UART_HandleTypeDef huart1;
UART_HandleTypeDef huart2;
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_TIM2_Init(void);
static void MX_USART1_UART_Init(void);
static void MX_TIM1_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
void check_com(void)
{
}
void delay_us (uint16_t us)
{
__HAL_TIM_SET_COUNTER(amp;htim1,0); // set the counter value a 0
while ((uint16_t)__HAL_TIM_GET_COUNTER(amp;htim1) < us); // wait for the counter to reach the us input in the parameter
//while (__HAL_TIM_GET_COUNTER(amp;htim1) < us);
}
uint8_t UART1_rxBuffer[1]={0};
uint8_t i;
uint8_t count;
void calc(void){
check_com();
if((COM4Eq_RX4[2]amp;0x07)==0x07 amp;amp; com_connect==0 amp;amp; COM4Eq_RX4[3] == 0x01 amp;amp; hexcheck ==1){
for(i=0; i<1; i ){
Encryption_Decryption(first_receive_comfirm 2 i*4,p7send 2 i*6,0);
}
p7send[8]=0xA5;
com_connect=1;
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_12);
}else if(COM4Eq_RX4[2]==0x57 amp;amp; COM4Eq_RX4[3]==0x01 amp;amp; hexcheck ==1){
for(i=0; i<1; i ){
Encryption_Decryption(end_receive_comfirm 2 i*4,p7send 2 i*6,0);
}
p7send[8]=0xF5;
// com_finish=1;
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_15);
HAL_UART_Transmit(amp;huart1,p7send,11,100);
// HAL_NVIC_DisableIRQ(USART1_IRQn);
}else if(hexcheck==1){
HAL_UART_Transmit(amp;huart1,RX4_Buffer_save,28, 100);
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_13);
}
HAL_UART_Receive_IT(amp;huart1, UART1_rxBuffer, 1); //reactivate Interupptt
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_11);
}
void HAL_UART_RxCpltCallback(UART_HandleTypeDef *huart)
{
if(UART1_rxBuffer[0] ==0xAE ){
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_15);
count=0;
RX4_Buffer[count]=UART1_rxBuffer[0] ;
count ;
//HAL_UART_Transmit(amp;huart1,RX4_Buffer,1, 100);
}
else if(count==27 amp;amp; UART1_rxBuffer[0]==0xDF){
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_15);
RX4_Buffer[count]=UART1_rxBuffer[0] ;
memcpy(RX4_Buffer_save,RX4_Buffer , 28);
calc();
count=0;
}
else if(count>=1){
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_14);
RX4_Buffer[count]=UART1_rxBuffer[0] ;
count ;
}
HAL_UART_Receive_IT(amp;huart1, UART1_rxBuffer, 1); //reactivate Interupptt
}
/* USER CODE END 0 */
/**
* @brief The application entry point.
* @retval int
*/
int main(void)
{
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_TIM2_Init();
MX_USART1_UART_Init();
MX_TIM1_Init();
/* USER CODE BEGIN 2 */
HAL_TIM_Base_Start(amp;htim1);
HAL_UART_Receive_IT (amp;huart1, UART1_rxBuffer, 1);
while(com_connect == 0){
uint8_t i;
for(i=0; i<1; i ){
Encryption_Decryption(start_com 2 i*4,p7send 2 i*6,0);
}
p7send[8]=0xB5;
HAL_UART_Transmit(amp;huart1,p7send,11,100);
HAL_Delay(300);
}
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
AB_in= (GPIOA->IDR) amp; 0x03;
{}
{}
{}
{}
AB_in1= (GPIOA->IDR) amp; 0x03;
{}
{}
{}
{}
AB_in2= (GPIOA->IDR) amp; 0x03;
{}
{}
{}
{}
AB_in3= (GPIOA->IDR) amp; 0x03;
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_13);
if( (AB_in == AB_in1) amp;amp; (AB_in1 == AB_in2) amp;amp; (AB_in2==AB_in3) ){
temp= ((temp << 2 ) | AB_in) amp; 0x0f;
/* x4 */
{
if(temp == 2 || temp == 4 || temp == 2 || temp == 11 || temp == 13){
// HAL_UART_Transmit(amp;huart1, test1,1, 100); //02
// delay_us(1);
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_12);
uint8_t outt= (m1 m2 m3 m4 m5 m6);
HAL_GPIO_TogglePin(GPIOA, GPIO_PIN_15);
GPIOB->ODR |= outt ;
delay_us(4);
GPIOB->ODR amp;= ~(outt);
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_8);
if(temp ==1 || temp == 7 || temp == 8 || temp == 14 ){
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_SET);
delay_us(4);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_9, GPIO_PIN_RESET);
}
}
}
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Initializes the CPU, AHB and APB busses clocks
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.HSIState = RCC_HSI_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(amp;RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB busses clocks
*/
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_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(amp;RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief TIM1 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM1_Init(void)
{
/* USER CODE BEGIN TIM1_Init 0 */
/* USER CODE END TIM1_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM1_Init 1 */
/* USER CODE END TIM1_Init 1 */
htim1.Instance = TIM1;
htim1.Init.Prescaler = 72-1;
htim1.Init.CounterMode = TIM_COUNTERMODE_UP;
htim1.Init.Period = 0xffff-1;
htim1.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim1.Init.RepetitionCounter = 0;
htim1.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(amp;htim1) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(amp;htim1, amp;sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(amp;htim1, amp;sMasterConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM1_Init 2 */
/* USER CODE END TIM1_Init 2 */
}
/**
* @brief TIM2 Initialization Function
* @param None
* @retval None
*/
static void MX_TIM2_Init(void)
{
/* USER CODE BEGIN TIM2_Init 0 */
/* USER CODE END TIM2_Init 0 */
TIM_ClockConfigTypeDef sClockSourceConfig = {0};
TIM_MasterConfigTypeDef sMasterConfig = {0};
/* USER CODE BEGIN TIM2_Init 1 */
/* USER CODE END TIM2_Init 1 */
htim2.Instance = TIM2;
htim2.Init.Prescaler = 0;
htim2.Init.CounterMode = TIM_COUNTERMODE_UP;
htim2.Init.Period = 0;
htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
htim2.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_DISABLE;
if (HAL_TIM_Base_Init(amp;htim2) != HAL_OK)
{
Error_Handler();
}
sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
if (HAL_TIM_ConfigClockSource(amp;htim2, amp;sClockSourceConfig) != HAL_OK)
{
Error_Handler();
}
sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
if (HAL_TIMEx_MasterConfigSynchronization(amp;htim2, amp;sMasterConfig) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN TIM2_Init 2 */
/* USER CODE END TIM2_Init 2 */
}
/**
* @brief USART1 Initialization Function
* @param None
* @retval None
*/
static void MX_USART1_UART_Init(void)
{
/* USER CODE BEGIN USART1_Init 0 */
/* USER CODE END USART1_Init 0 */
/* USER CODE BEGIN USART1_Init 1 */
/* USER CODE END USART1_Init 1 */
huart1.Instance = USART1;
huart1.Init.BaudRate = 38400;
huart1.Init.WordLength = UART_WORDLENGTH_9B;
huart1.Init.StopBits = UART_STOPBITS_1;
huart1.Init.Parity = UART_PARITY_EVEN;
huart1.Init.Mode = UART_MODE_TX_RX;
huart1.Init.HwFlowCtl = UART_HWCONTROL_NONE;
huart1.Init.OverSampling = UART_OVERSAMPLING_16;
if (HAL_UART_Init(amp;huart1) != HAL_OK)
{
Error_Handler();
}
/* USER CODE BEGIN USART1_Init 2 */
/* USER CODE END USART1_Init 2 */
}
/**
* @brief USART2 Initialization Function
* @param None
* @retval None
*/
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOD_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
__HAL_RCC_GPIOB_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_10
|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14
|GPIO_PIN_15|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5
|GPIO_PIN_8|GPIO_PIN_9, GPIO_PIN_RESET);
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14
|GPIO_PIN_15, GPIO_PIN_RESET);
/*Configure GPIO pins : In_A_Pin In_B_Pin */
GPIO_InitStruct.Pin = In_A_Pin|In_B_Pin;
GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOA, amp;GPIO_InitStruct);
/*Configure GPIO pins : PB0 PB1 PB2 PB3
PB4 PB5 PB8 */
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3
|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_8;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_MEDIUM;
HAL_GPIO_Init(GPIOB, amp;GPIO_InitStruct);
/*Configure GPIO pins : PB10 PB11 PB12 PB13
PB14 PB15 */
GPIO_InitStruct.Pin = GPIO_PIN_10|GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13
|GPIO_PIN_14|GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, amp;GPIO_InitStruct);
/*Configure GPIO pins : PA11 PA12 PA13 PA14
PA15 */
GPIO_InitStruct.Pin = GPIO_PIN_11|GPIO_PIN_12|GPIO_PIN_13|GPIO_PIN_14
|GPIO_PIN_15;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOA, amp;GPIO_InitStruct);
/*Configure GPIO pin : PB9 */
GPIO_InitStruct.Pin = GPIO_PIN_9;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_PULLDOWN;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOB, amp;GPIO_InitStruct);
}
/* USER CODE BEGIN 4 */
/* USER CODE END 4 */
/**
* @brief This function is executed in case of error occurrence.
* @retval None
*/
void Error_Handler(void)
{
/* USER CODE BEGIN Error_Handler_Debug */
/* User can add his own implementation to report the HAL error return state */
/* USER CODE END Error_Handler_Debug */
}
#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,
tex: printf("Wrong parameters value: file %s on line %drn", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
**
* @brief This function handles USART1 global interrupt.
*/
void USART1_IRQHandler(void)
{
/* USER CODE BEGIN USART1_IRQn 0 */
HAL_GPIO_TogglePin(GPIOB, GPIO_PIN_9);
/* USER CODE END USART1_IRQn 0 */
HAL_UART_IRQHandler(amp;huart1);
/* USER CODE BEGIN USART1_IRQn 1 */
/* USER CODE END USART1_IRQn 1 */
}
/* USER CODE BEGIN 1 */
/* USER CODE END 1 */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Комментарии:
1. Сначала отладьте через HAL_UART_IRQHandler и посмотрите, каково состояние регистров, установлены ли флаги ошибок на периферийном устройстве и т. Д. Проверьте, что на самом деле делает UART1.