change led blinking behaviour

This commit is contained in:
Johnny Hsu 2024-11-13 01:10:52 +01:00
parent 33dbe89a2f
commit aaa053fa8b
2 changed files with 18 additions and 12 deletions

View File

@ -5,6 +5,10 @@
#include "stm32f0xx_hal.h"
#include "stm32f0xx_hal_tim.h"
#define DELAY 50000
int Counter = 0;
void led_init(TIM_HandleTypeDef* htim) {
HAL_TIM_PWM_Start(htim, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(htim, TIM_CHANNEL_2);
@ -23,6 +27,11 @@ void led_init(TIM_HandleTypeDef* htim) {
}
void blink(TIM_HandleTypeDef* htim, int32_t color) {
if(Counter < DELAY) {
Counter ++;
} else {
Counter = 0;
}
int32_t Blue = color & 0x000000FF;
int32_t Green = (color & 0x0000FF00) >> 8;
int32_t Red = (color & 0x00FF0000) >> 16;
@ -31,15 +40,13 @@ void blink(TIM_HandleTypeDef* htim, int32_t color) {
TIM2->CCR2 = Red;
TIM2->CCR3 = Blue;
HAL_TIM_PWM_Stop(htim, TIM_CHANNEL_1);
HAL_TIM_PWM_Stop(htim, TIM_CHANNEL_2);
HAL_TIM_PWM_Stop(htim, TIM_CHANNEL_3);
HAL_Delay(500);
HAL_TIM_PWM_Start(htim, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(htim, TIM_CHANNEL_2);
HAL_TIM_PWM_Start(htim, TIM_CHANNEL_3);
HAL_Delay(500);
if (Counter < DELAY/2) {
HAL_TIM_PWM_Stop(htim, TIM_CHANNEL_1);
HAL_TIM_PWM_Stop(htim, TIM_CHANNEL_2);
HAL_TIM_PWM_Stop(htim, TIM_CHANNEL_3);
} else {
HAL_TIM_PWM_Start(htim, TIM_CHANNEL_1);
HAL_TIM_PWM_Start(htim, TIM_CHANNEL_2);
HAL_TIM_PWM_Start(htim, TIM_CHANNEL_3);
}
}

View File

@ -24,7 +24,6 @@
#include "adc.h"
#include "led.h"
#include "can-halal.h"
#include "farbe.h"
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/