本次测评是通过uFun硬件平台,触碰触摸按键切换RGB灯的颜色
准备工作:
1.JTAG转接板,JTAG,uFun开发板
2.开发环境使用的是MDK5(注:需要更改配置,不然程序不能down下去)
使用STM32CubeMX生成底层,这里运用了PA0、PA1、PA2三个PWM RGB,同时定义了四个触摸按键的GPIO,KEY0、KEY1、KEY2、KEY3分别对应GPIOC5、GPIOC4、GPIOB3、GPIOB4
通过读取GPIO的状态,显示灯的不同状态
- /**
- * @brief The application entry point.
- * @retval int
- */
- int main(void)
- {
- /* MCU Configuration--------------------------------------------------------*/
- /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
- HAL_Init();
- /* Configure the system clock */
- SystemClock_Config();
- /* Initialize all configured peripherals */
- MX_GPIO_Init();
- MX_TIM5_Init();
- HAL_TIM_PWM_Start(&htim5,TIM_CHANNEL_1);
- HAL_TIM_PWM_Start(&htim5,TIM_CHANNEL_2);
- HAL_TIM_PWM_Start(&htim5,TIM_CHANNEL_3);
- /* Infinite loop */
- /* USER CODE BEGIN WHILE */
- while (1)
- {
- if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_5) == GPIO_PIN_SET)
- {
- uFUN_RGB(255,0,0);
- }
- else if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_4) == GPIO_PIN_SET)
- {
- uFUN_RGB(0,255,0);
- }
- else if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_3) == GPIO_PIN_SET)
- {
- uFUN_RGB(0,0,255);
- }
- else if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_4) == GPIO_PIN_SET)
- {
- uFUN_RGB(10,10,10);
- }
- }
- }
- /**
- * @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_MUL6;
- 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_DIV2;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /**
- * @brief TIM5 Initialization Function
- * @param None
- * @retval None
- */
- static void MX_TIM5_Init(void)
- {
- /* USER CODE BEGIN TIM5_Init 0 */
- /* USER CODE END TIM5_Init 0 */
- TIM_MasterConfigTypeDef sMasterConfig = {0};
- TIM_OC_InitTypeDef sConfigOC = {0};
- /* USER CODE BEGIN TIM5_Init 1 */
- /* USER CODE END TIM5_Init 1 */
- htim5.Instance = TIM5;
- htim5.Init.Prescaler = 0;
- htim5.Init.CounterMode = TIM_COUNTERMODE_UP;
- htim5.Init.Period = 255;
- htim5.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
- htim5.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
- if (HAL_TIM_PWM_Init(&htim5) != HAL_OK)
- {
- Error_Handler();
- }
- sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
- sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
- if (HAL_TIMEx_MasterConfigSynchronization(&htim5, &sMasterConfig) != HAL_OK)
- {
- Error_Handler();
- }
- sConfigOC.OCMode = TIM_OCMODE_PWM1;
- sConfigOC.Pulse = 127;
- sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;
- sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
- if (HAL_TIM_PWM_ConfigChannel(&htim5, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_TIM_PWM_ConfigChannel(&htim5, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
- {
- Error_Handler();
- }
- if (HAL_TIM_PWM_ConfigChannel(&htim5, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
- {
- Error_Handler();
- }
- /* USER CODE BEGIN TIM5_Init 2 */
- /* USER CODE END TIM5_Init 2 */
- HAL_TIM_MspPostInit(&htim5);
- }
- /**
- * @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_GPIOC_CLK_ENABLE();
- __HAL_RCC_GPIOD_CLK_ENABLE();
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- /*Configure GPIO pins : KEY1_Pin KEY0_Pin */
- GPIO_InitStruct.Pin = KEY1_Pin|KEY0_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
- /*Configure GPIO pins : KEY2_Pin KEY3_Pin */
- GPIO_InitStruct.Pin = KEY2_Pin|KEY3_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- }
举报
内容系网友发布,其中涉及到安全隐患的内容系网友个人行为,不代表面包板社区观点
关闭
站长推荐 /3
- 返回顶部