- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * [url=home.php?mod=space&uid=288409]@file[/url] : main.c
- * [url=home.php?mod=space&uid=247401]@brief[/url] : Main program body
- ******************************************************************************
- * @attention
- *
- * Copyright (c) 2022 STMicroelectronics.
- * All rights reserved.
- *
- * This software is licensed under terms that can be found in the LICENSE file
- * in the root directory of this software component.
- * If no LICENSE file comes with this software, it is provided AS-IS.
- *
- ******************************************************************************
- */
- /* USER CODE END Header */
- /* Includes ------------------------------------------------------------------*/
- #include "main.h"
- #include "tim.h"
- #include "usart.h"
- #include "gpio.h"
- /* Private includes ----------------------------------------------------------*/
- /* USER CODE BEGIN Includes */
- #include "OLED.h"
- #include "hw.h"
- #include "HCSR04.h"
- #include "HW_remote.h"
- /* 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 ---------------------------------------------------------*/
- /* USER CODE BEGIN PV */
- /* USER CODE END PV */
- /* Private function prototypes -----------------------------------------------*/
- void SystemClock_Config(void);
- /* USER CODE BEGIN PFP */
- /* USER CODE END PFP */
- /* Private user code ---------------------------------------------------------*/
- /* USER CODE BEGIN 0 */
- extern float distant1;//超声波1测量距离
- extern float distant2;//超声波2测量距离
- extern uint8_t measure_Cnt1;//超声波1状态标志位
- extern uint32_t measure_Buf1[];//存放定时器计数值的数组
- extern uint8_t measure_Cnt2;//超声波2状态标志位
- extern uint32_t measure_Buf2[];//存放定时器计数值的数组
- extern uint16_t Dval; //下降沿时计数器的值
- extern uint8_t RmtSta;
- extern uint32_t RmtRec; //红外接收到的数据
- extern uint8_t RmtCnt; //按键按下的次数
- uint8_t key_num;
- //定时器输入捕获中断回调函数
- void HAL_TIM_IC_CaptureCallback(TIM_HandleTypeDef *htim)//捕获中断发生时执行
- {
-
- if(TIM2 == htim->Instance)// 判断触发的中断的定时器为TIM2
- {
- switch(measure_Cnt1){
- case 1:
- measure_Buf1[0] = HAL_TIM_ReadCapturedValue(&htim2,TIM_CHANNEL_1);//获取当前的捕获值.
- __HAL_TIM_SET_CAPTUREPOLARITY(&htim2,TIM_CHANNEL_1,TIM_ICPOLARITY_FALLING); //设置为下降沿捕获
- measure_Cnt1++;
- break;
- case 2:
- measure_Buf1[1] = HAL_TIM_ReadCapturedValue(&htim2,TIM_CHANNEL_1);//获取当前的捕获值.
- HAL_TIM_IC_Stop_IT(&htim2,TIM_CHANNEL_1); //停止捕获 或者: __HAL_TIM_DISABLE(&htim5);
- measure_Cnt1++;
-
- }
-
- }
- if(TIM3 == htim->Instance)// 判断触发的中断的定时器为TIM3
- {
- switch(measure_Cnt2){
- case 1:
- measure_Buf2[0] = HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_1);//获取当前的捕获值.
- __HAL_TIM_SET_CAPTUREPOLARITY(&htim3,TIM_CHANNEL_1,TIM_ICPOLARITY_FALLING); //设置为下降沿捕获
- measure_Cnt2++;
- break;
- case 2:
- measure_Buf2[1] = HAL_TIM_ReadCapturedValue(&htim3,TIM_CHANNEL_1);//获取当前的捕获值.
- HAL_TIM_IC_Stop_IT(&htim3,TIM_CHANNEL_1); //停止捕获 或者: __HAL_TIM_DISABLE(&htim5);
- measure_Cnt2++;
-
- }
-
- }
-
- //红外遥控
- if(TIM1 == htim->Instance)
- {
- // printf("ic\r\n");
- if(HAL_GPIO_ReadPin(IRpa8_GPIO_Port,IRpa8_Pin))//上升沿捕获
- {
- TIM_RESET_CAPTUREPOLARITY(&htim1,TIM_CHANNEL_1); //一定要先清除原来的设置!!
- TIM_SET_CAPTUREPOLARITY(&htim1,TIM_CHANNEL_1,TIM_ICPOLARITY_FALLING);//CC1P=1 设置为下降沿捕获
- __HAL_TIM_SET_COUNTER(&htim1,0); //清空定时器值
- RmtSta|=0X10; //标记上升沿已经被捕获
- }else //下降沿捕获
- {
- Dval=HAL_TIM_ReadCapturedValue(&htim1,TIM_CHANNEL_1);//读取CCR1也可以清CC1IF标志位
- TIM_RESET_CAPTUREPOLARITY(&htim1,TIM_CHANNEL_1); //一定要先清除原来的设置!!
- TIM_SET_CAPTUREPOLARITY(&htim1,TIM_CHANNEL_1,TIM_ICPOLARITY_RISING);//配置TIM5通道1上升沿捕获
- if(RmtSta&0X10) //完成一次高电平捕获
- {
- if(RmtSta&0X80)//接收到了引导码
- {
-
- if(Dval>300&&Dval<800) //560为标准值,560us
- {
- RmtRec<<=1; //左移一位.
- RmtRec|=0; //接收到0
- }else if(Dval>1400&&Dval<1800) //1680为标准值,1680us
- {
- RmtRec<<=1; //左移一位.
- RmtRec|=1; //接收到1
- }else if(Dval>2200&&Dval<2600) //得到按键键值增加的信息 2500为标准值2.5ms
- {
- RmtCnt++; //按键次数增加1次
- RmtSta&=0XF0; //清空计时器
- }
- }else if(Dval>4200&&Dval<4700) //4500为标准值4.5ms
- {
- RmtSta|=1<<7; //标记成功接收到了引导码
- RmtCnt=0; //清除按键次数计数器
- }
- }
- RmtSta&=~(1<<4);
- }
- }
-
-
- }
- //PWM控制舵机
- // angle:角度值,500-1000-1500-2000-2500
- //角度
- uint16_t angle;
- void Servo_Control(angle)
- {
- //设置默认的占空比值
- // __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_1, (uint16_t )angle / 180 * 2000 + 500);
- __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_1, angle);
- }
- /* USER CODE END 0 */
- /**
- * @brief The application entry point.
- * @retval int
- */
- int main(void)
- {
- /* USER CODE BEGIN 1 */
- //超声波设置距离
- float jl;
- /* 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_TIM3_Init();
- MX_TIM1_Init();
- MX_TIM4_Init();
- /* USER CODE BEGIN 2 */
- HAL_TIM_Base_Start_IT(&htim1);
- HAL_TIM_IC_Start_IT(&htim1,TIM_CHANNEL_1);
- HAL_TIM_PWM_Start(&htim4,TIM_CHANNEL_1);
- __HAL_TIM_SET_COMPARE(&htim4, TIM_CHANNEL_1, 500);
- printf("test\r\n");
-
-
- OLED_Init();
-
-
- OLED_ShowCHinese(0, 0, 3); //人
- OLED_ShowCHinese(0, 16, 4); //数
- OLED_ShowCHinese(0, 32, 8); //:
-
- OLED_ShowCHinese(2, 0, 0); //超
- OLED_ShowCHinese(2, 16, 1); //声
- OLED_ShowCHinese(2, 32, 2); //波
- OLED_ShowCHinese(2, 48, 9); //1
- OLED_ShowCHinese(2, 64, 8); //:
- OLED_ShowCHinese(4, 0, 0); //超
- OLED_ShowCHinese(4, 16, 1); //声
- OLED_ShowCHinese(4, 32, 2); //波
- OLED_ShowCHinese(4, 48, 10); //2
- OLED_ShowCHinese(4, 64, 8); //:
-
- OLED_ShowCHinese(6, 0, 15); //键
- OLED_ShowCHinese(6, 16, 16); //值
- OLED_ShowCHinese(6, 32, 8); //:
- OLED_ShowCHinese(6, 90, 13); //关
- OLED_ShowCHinese(6, 106, 14); //闭
-
- // Servo_Control(0);
- /* USER CODE END 2 */
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- //显示人数
- hw_git();//获取电平
- OLED_ShowNum(1,7,number,3);//显示无符号数
- //超声波1检测距离
- SR04_GetData1();
- OLED_ShowNum(2,10,distant1,2);
- OLED_ShowChar(2,12,'.');
- OLED_ShowNum(2,13,(uint16_t)(distant1*100)%100,2);
- if(distant1<=15)
- {
- angle= 2200;
- Servo_Control(angle);
- OLED_ShowCHinese(6, 90, 11); //打
- OLED_ShowCHinese(6, 106, 12); //开
- angle=500;
- }
- //超声波2检测距离
- SR04_GetData2();
- OLED_ShowNum(3,10,distant2,2);
- OLED_ShowChar(3,12,'.');
- OLED_ShowNum(3,13,(uint16_t)(distant2*100)%100,2);
- if(distant2<=10)
- {
- angle= 500;
- Servo_Control(angle);
- OLED_ShowCHinese(6, 90, 13); //关
- OLED_ShowCHinese(6, 106, 14); //闭
- }
- //提示
- key_num=Remote_Scan();
- if(key_num)
- {
- printf("键值:%d\r\n",key_num);
- OLED_ShowNum(4,6,key_num,3);
- switch (key_num)
- {
- case 104:
- angle= 2200;
- OLED_ShowCHinese(6, 90, 11); //打
- OLED_ShowCHinese(6, 106, 12); //开
- break;
- case 176:
- angle= 500;
- OLED_ShowCHinese(6, 90, 13); //关
- OLED_ShowCHinese(6, 106, 14); //闭
- break;
- default:
- break;
- }
- key_num=0;
- Servo_Control(angle);
- }
-
- /* USER CODE END WHILE */
- /* USER CODE BEGIN 3 */
- }
-
-
- /* 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 RCC Oscillators according to the specified parameters
- * in the RCC_OscInitTypeDef structure.
- */
- 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(&RCC_OscInitStruct) != HAL_OK)
- {
- Error_Handler();
- }
- /** Initializes the CPU, AHB and APB buses 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(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /* 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 */
- __disable_irq();
- while (1)
- {
- }
- /* 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,
- ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
- /* USER CODE END 6 */
- }
- #endif /* USE_FULL_ASSERT */