本帖最后由 lhc960818 于 2019-6-3 20:10 编辑

本次测评是通过uFun硬件平台,触碰触摸按键切换RGB灯的颜色
准备工作:
1.JTAG转接板,JTAG,uFun开发板
微信图片_20190603195756.jpg
2.开发环境使用的是MDK5(注:需要更改配置,不然程序不能down下去)
微信图片_20190603200051.png
微信图片_20190603200053.png
使用STM32CubeMX生成底层,这里运用了PA0、PA1、PA2三个PWM RGB,同时定义了四个触摸按键的GPIO,KEY0、KEY1、KEY2、KEY3分别对应GPIOC5、GPIOC4、GPIOB3、GPIOB4
微信图片_20190603195653.png
通过读取GPIO的状态,显示灯的不同状态
  1. /**
  2.   * @brief  The application entry point.
  3.   * @retval int
  4.   */
  5. int main(void)
  6. {
  7.   /* MCU Configuration--------------------------------------------------------*/
  8.   /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  9.   HAL_Init();
  10.   /* Configure the system clock */
  11.   SystemClock_Config();
  12.   /* Initialize all configured peripherals */
  13.   MX_GPIO_Init();
  14.   MX_TIM5_Init();
  15.   HAL_TIM_PWM_Start(&htim5,TIM_CHANNEL_1);
  16.   HAL_TIM_PWM_Start(&htim5,TIM_CHANNEL_2);
  17.   HAL_TIM_PWM_Start(&htim5,TIM_CHANNEL_3);
  18.   
  19.   /* Infinite loop */
  20.   /* USER CODE BEGIN WHILE */
  21.   while (1)
  22.   {
  23.     if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_5) == GPIO_PIN_SET)
  24.     {
  25.         uFUN_RGB(255,0,0);        
  26.     }
  27.     else if(HAL_GPIO_ReadPin(GPIOC,GPIO_PIN_4) == GPIO_PIN_SET)
  28.     {
  29.         uFUN_RGB(0,255,0);        
  30.     }
  31.     else if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_3) == GPIO_PIN_SET)
  32.     {
  33.         uFUN_RGB(0,0,255);        
  34.     }
  35.     else if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_4) == GPIO_PIN_SET)
  36.     {
  37.         uFUN_RGB(10,10,10);        
  38.     }
  39.   }
  40. }
  41. /**
  42.   * @brief System Clock Configuration
  43.   * @retval None
  44.   */
  45. void SystemClock_Config(void)
  46. {
  47.   RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  48.   RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  49.   /** Initializes the CPU, AHB and APB busses clocks
  50.   */
  51.   RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
  52.   RCC_OscInitStruct.HSEState = RCC_HSE_ON;
  53.   RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
  54.   RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  55.   RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
  56.   RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
  57.   RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL6;
  58.   if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  59.   {
  60.     Error_Handler();
  61.   }
  62.   /** Initializes the CPU, AHB and APB busses clocks
  63.   */
  64.   RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  65.                               |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  66.   RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
  67.   RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  68.   RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
  69.   RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  70.   if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
  71.   {
  72.     Error_Handler();
  73.   }
  74. }
  75. /**
  76.   * @brief TIM5 Initialization Function
  77.   * @param None
  78.   * @retval None
  79.   */
  80. static void MX_TIM5_Init(void)
  81. {
  82.   /* USER CODE BEGIN TIM5_Init 0 */
  83.   /* USER CODE END TIM5_Init 0 */
  84.   TIM_MasterConfigTypeDef sMasterConfig = {0};
  85.   TIM_OC_InitTypeDef sConfigOC = {0};
  86.   /* USER CODE BEGIN TIM5_Init 1 */
  87.   /* USER CODE END TIM5_Init 1 */
  88.   htim5.Instance = TIM5;
  89.   htim5.Init.Prescaler = 0;
  90.   htim5.Init.CounterMode = TIM_COUNTERMODE_UP;
  91.   htim5.Init.Period = 255;
  92.   htim5.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  93.   htim5.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  94.   if (HAL_TIM_PWM_Init(&htim5) != HAL_OK)
  95.   {
  96.     Error_Handler();
  97.   }
  98.   sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  99.   sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  100.   if (HAL_TIMEx_MasterConfigSynchronization(&htim5, &sMasterConfig) != HAL_OK)
  101.   {
  102.     Error_Handler();
  103.   }
  104.   sConfigOC.OCMode = TIM_OCMODE_PWM1;
  105.   sConfigOC.Pulse = 127;
  106.   sConfigOC.OCPolarity = TIM_OCPOLARITY_LOW;
  107.   sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
  108.   if (HAL_TIM_PWM_ConfigChannel(&htim5, &sConfigOC, TIM_CHANNEL_1) != HAL_OK)
  109.   {
  110.     Error_Handler();
  111.   }
  112.   if (HAL_TIM_PWM_ConfigChannel(&htim5, &sConfigOC, TIM_CHANNEL_2) != HAL_OK)
  113.   {
  114.     Error_Handler();
  115.   }
  116.   if (HAL_TIM_PWM_ConfigChannel(&htim5, &sConfigOC, TIM_CHANNEL_3) != HAL_OK)
  117.   {
  118.     Error_Handler();
  119.   }
  120.   /* USER CODE BEGIN TIM5_Init 2 */
  121.   /* USER CODE END TIM5_Init 2 */
  122.   HAL_TIM_MspPostInit(&htim5);
  123. }
  124. /**
  125.   * @brief GPIO Initialization Function
  126.   * @param None
  127.   * @retval None
  128.   */
  129. static void MX_GPIO_Init(void)
  130. {
  131.   GPIO_InitTypeDef GPIO_InitStruct = {0};
  132.   /* GPIO Ports Clock Enable */
  133.   __HAL_RCC_GPIOC_CLK_ENABLE();
  134.   __HAL_RCC_GPIOD_CLK_ENABLE();
  135.   __HAL_RCC_GPIOA_CLK_ENABLE();
  136.   __HAL_RCC_GPIOB_CLK_ENABLE();
  137.   /*Configure GPIO pins : KEY1_Pin KEY0_Pin */
  138.   GPIO_InitStruct.Pin = KEY1_Pin|KEY0_Pin;
  139.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  140.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  141.   HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
  142.   /*Configure GPIO pins : KEY2_Pin KEY3_Pin */
  143.   GPIO_InitStruct.Pin = KEY2_Pin|KEY3_Pin;
  144.   GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  145.   GPIO_InitStruct.Pull = GPIO_NOPULL;
  146.   HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  147. }

触摸按键控制RGB灯.rar (1.24 MB, 下载次数: 0)
举报
内容系网友发布,其中涉及到安全隐患的内容系网友个人行为,不代表面包板社区观点
请点击举报理由
关闭 站长推荐上一条 /3 下一条
  • 返回顶部