【iCore4 雙核心板_ARM】例程十二:通用定時器實驗——定時點亮LED

XiaomaGee發表於2017-08-24

實驗原理:

  通過STM32的三個GPIO口來驅動LED燈的三個通道,設定GPIO為推輓輸出模式,採

用灌電流的方式與LED連線,輸出高電平LED滅,輸出低電平LED亮,通過通用定時器TIM3

實現500ms定時,每500ms變換一次LED顏色。

核心程式碼:

void MX_TIM3_Init(void)
{
  TIM_ClockConfigTypeDef sClockSourceConfig;
  TIM_MasterConfigTypeDef sMasterConfig;

  htim3.Instance = TIM3;
  htim3.Init.Prescaler = 21599;
  htim3.Init.CounterMode = TIM_COUNTERMODE_UP;
  htim3.Init.Period = 4999;
  htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1;
  htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE;
  if (HAL_TIM_Base_Init(&htim3) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL;
  if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

  sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET;
  sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE;
  if (HAL_TIMEx_MasterConfigSynchronization(&htim3, &sMasterConfig) != HAL_OK)
  {
    _Error_Handler(__FILE__, __LINE__);
  }

}
void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
    static int counter = 0;
    
    if(counter % 3 == 0){
        LED_RED_ON;
        LED_BLUE_OFF;
        LED_GREEN_OFF;
    }else if(counter % 3 == 1){
        LED_RED_OFF;
        LED_BLUE_ON;
        LED_GREEN_OFF;
    }else if(counter % 3 == 2){
        LED_RED_OFF;
        LED_BLUE_OFF;
        LED_GREEN_ON;
    }
    counter ++;
}

原始碼下載連結:

連結:http://pan.baidu.com/s/1kVeflcj 密碼:z812

iCore4連結:

相關文章