江濤帶你玩STM32-CubeMX紅外NEC解碼實戰(上)--外部中斷方式
目錄
1.STM32-CubeMx配置步驟及注意事項
stm32-cube的配置如上圖所示。
2. 代碼部分講解
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2020 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
#include "tim.h"
#include "usart.h"
#include "gpio.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "stdio.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
#define IR_IN HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
uint8_t KeyCount = 0 ;
uint32_t OrderData = 0;
uint8_t ReadyFlag = 0;
uint8_t TimeOut = 0 ;
uint8_t OK = 0;
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
/* USER CODE BEGIN PFP */
uint8_t Code_Process(void);
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
/* 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();
/* USER CODE BEGIN 2 */
HAL_TIM_Base_Start(&htim2);
HAL_TIM_Base_Start_IT(&htim2);
HAL_TIM_IC_Start(&htim2,TIM_CHANNEL_1);
HAL_TIM_IC_Start_IT(&htim2,TIM_CHANNEL_1);
printf("IR Capture !! \r\n");
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
if(ReadyFlag)
{
uint8_t key = Code_Process();
printf("order=%08X , code=%02X ,KeyCount=%d\r\n",OrderData,key,KeyCount);
}
}
/* 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_MUL4;
if (HAL_RCC_OscConfig(&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_DIV1;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_1) != HAL_OK)
{
Error_Handler();
}
}
/* USER CODE BEGIN 4 */
int fputc(int ch , FILE *f)
{
HAL_UART_Transmit(&huart1, (uint8_t *)&ch , 1 , 10);
return ch;
}
// 紅外解碼處理
uint8_t Code_Process(void)
{
uint8_t t1 ,t2 ;
t1 = OrderData >> 24 ;
t2 = (OrderData >> 16) & 0xFF ;
ReadyFlag = 0;
OK = 0 ;
if(t1 ==(uint8_t) ~t2)
{
t1 = OrderData >> 8;
t2 = OrderData ;
if(t1 ==(uint8_t) ~ t2) return t1;
}
return 0;
}
// 捕獲中斷回調函數,每次捕獲到信號就會進入這個回調函數
void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)
{
uint32_t fallingCount = 0 ; // 下降沿計數
uint8_t temp = 0 ;
// 判斷是否是定時器2的外部捕獲口1
if(htim->Instance == TIM2)
{
// 捕獲到了上升沿
if(IR_IN)
{
__HAL_TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_1, TIM_INPUTCHANNELPOLARITY_FALLING); // 改變捕獲極性爲下降沿捕獲
__HAL_TIM_SET_COUNTER(htim, 0); // 計數清零,從頭開始計
}
else
{
fallingCount = HAL_TIM_ReadCapturedValue(htim,TIM_CHANNEL_1); // 讀取捕獲計數,這個時間即爲上升沿持續的時間
__HAL_TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_1, TIM_INPUTCHANNELPOLARITY_RISING); // 改變捕獲極性爲上升沿捕獲
if((fallingCount > 4400) && (fallingCount < 4600)) OK = 1;// 4.5ms引導電平
else if ((fallingCount > 500) && (fallingCount < 600)) temp = 0;//560 us
else if ((fallingCount > 1600) && (fallingCount < 1750)) temp = 1;//1680 us
else if ((fallingCount > 2000) && (fallingCount < 4000)) //2.5 ms
{
ReadyFlag = 1 ;
KeyCount ++ ;
}
if(OK)
{
OrderData <<= 1 ;
OrderData += temp ;
KeyCount = 0; // 按鍵次數
}
}
}
}
// 定時器更新中斷回調函數
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
if(htim->Instance == TIM2)
{
// 10ms進入一次中斷,計時一共200ms
if(OK)
{
TimeOut = (TimeOut<20?0:TimeOut++);
if(TimeOut==0)
{
ReadyFlag = 1;
}
}
}
}
/* 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 %d\r\n", file, line) */
/* USER CODE END 6 */
}
#endif /* USE_FULL_ASSERT */
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
2.1 注意修改庫文件的錯誤
在編譯的過程中可能會遇到報錯,主要是翻轉捕獲極性的兩句代碼報錯。
__HAL_TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_1, TIM_INPUTCHANNELPOLARITY_FALLING); // 改變捕獲極性爲下降沿捕獲
__HAL_TIM_SET_CAPTUREPOLARITY(htim, TIM_CHANNEL_1, TIM_INPUTCHANNELPOLARITY_RISING); // 改變捕獲極性爲上升沿捕獲
其實這個保存主要是庫文件中有錯誤導致的,下面給出解決方案。
2.2 延時200ms的作用
main.c中有個定時器更新的中斷,我這裏設置的200ms,主要是用來處理連按的,因爲連按的時候響應處理時間可能會超過200ms。
2.3 關於中斷標誌的清除
有寫人在兩個回調函數中都手動加了清除中斷標誌的操作,其實是不需要的,因爲庫函數在我們使用的回調函數之前就已經做了處理了,所以不需要再去處理一次了。
3. 演示結果
本次實驗使用的也是串口打印,所以放上串口解碼的結果
4. 源碼索取
如果有感興趣的可以加羣交流,以上的代碼我會在羣裏分享。QQ羣:1087285029
或者掃碼