硬體小分隊第二次任務報告

OutsiderMing發表於2020-12-16

第二次任務不完全報告

  • 任務內容
    LED燈初始設定每過兩秒閃一下,按一次按鍵,閃爍間隔減少0.5s,等間隔到0秒之後,led由閃爍變為呼吸燈,並且蜂鳴器開始播放音樂小星星。
  • 任務完成情況
    剛開始的時候沒有找到合適的學習的方法,很多次的死腦經,很多次的繞彎路都使得我的效率不高。其實仍有部分功能我尚未實現,讓蜂鳴器唱出小星星,以及外部中斷按鍵的防抖。我想過很多辦法都沒有完成其功能。
  • 程式碼及解釋

主函式

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 */
	#define WAKEUP HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0)
  /* USER CODE END Init */

  /* Configure the system clock */
  SystemClock_Config();

  /* USER CODE BEGIN SysInit */

	uint16_t pwm_value = 2000;
	uint16_t step = 100;
	uint16_t pwm_value2 = 2000;
	uint16_t step1;
  uint16_t it =0;
  /* USER CODE END SysInit */

  /* Initialize all configured peripherals */
  MX_GPIO_Init();
  MX_TIM3_Init();
  MX_TIM9_Init();
  MX_TIM12_Init();
  /* USER CODE BEGIN 2 */
  HAL_TIM_Base_Start_IT(&htim3);     //啟動TIM3定時器
  /* USER CODE END 2 */

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
		if(pwmt == 1)          //當LED常亮時開啟PWM呼吸燈模式
		{
	HAL_Delay(10);  
  if(pwm_value == 0) step = 50;
  if(pwm_value == 2000) step = -50;
  pwm_value += step;
  user_pwm_setvalue(pwm_value);          //呼吸燈的PWM
  if(pwm_value2 == 0) step1 = 100;
  if(pwm_value2 == 2000) step1 = -100;      
  pwm_value2 += step1;
  user_pwm_setvalue2(pwm_value2);      //蜂鳴器的PWM
		}
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
  }
  /* USER CODE END 3 */
}

定時器

是利用將亮的時間與LED燈暗的時間由每次進入定時器的次數來決定的

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)
{
    if (htim->Instance == htim3.Instance)
    {
			t += 1;
			if(t%tiim == 0)
			{
				HAL_GPIO_WritePin(GPIOF,GPIO_PIN_9,GPIO_PIN_RESET);
				t = 0;
			}
			else
				HAL_GPIO_WritePin(GPIOF,GPIO_PIN_9,GPIO_PIN_SET);
    }
}

PWM定時器

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{

	  if(GPIO_Pin == GPIO_PIN_2)
  {
		HAL_GPIO_TogglePin(GPIOF,GPIO_PIN_9);

		if(b == 0)
		{
			tiim = 53;
			b ++;
		}
		if(b == 1)
		{
			tiim = 36;
			b ++;
		}
		if(b == 2)
		{
			tiim = 19;
			b ++;
		}
		if(b == 3)
		{
			tiim = 1;
			b = 3;
			HAL_GPIO_WritePin(GPIOF,GPIO_PIN_9,GPIO_PIN_RESET);
			HAL_TIM_Base_Stop_IT(&htim3);      //關閉TIM3定時器
			pwmt = 1;
			HAL_TIM_PWM_Start(&htim9, TIM_CHANNEL_1);
			HAL_TIM_PWM_Start(&htim12, TIM_CHANNEL_1);      
			//開啟兩個PWM定時器
		}
  }
}

初始化PWM

void user_pwm_setvalue(uint16_t value)
{
    TIM_OC_InitTypeDef sConfigOC;
 
    sConfigOC.OCMode = TIM_OCMODE_PWM1;
    sConfigOC.Pulse = value;
    sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
    sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
    HAL_TIM_PWM_ConfigChannel(&htim9, &sConfigOC, TIM_CHANNEL_1);
    HAL_TIM_PWM_Start(&htim9, TIM_CHANNEL_1);   
}
void user_pwm_setvalue2(uint16_t value2)
{
    TIM_OC_InitTypeDef sConfigOC;
 
    sConfigOC.OCMode = TIM_OCMODE_PWM1;
    sConfigOC.Pulse = value2;
    sConfigOC.OCPolarity = TIM_OCPOLARITY_HIGH;
    sConfigOC.OCFastMode = TIM_OCFAST_DISABLE;
    HAL_TIM_PWM_ConfigChannel(&htim12, &sConfigOC, TIM_CHANNEL_1);
    HAL_TIM_PWM_Start(&htim12, TIM_CHANNEL_1);   
}

(學習使用函式庫主要來自於微雪)
視訊展示

相關文章