Moved PWM handler to SysTick and created LED demo

This commit is contained in:
dooku
2021-06-06 09:44:32 +02:00
parent 9d08b7df59
commit eedfcba0a5
7 changed files with 27 additions and 32 deletions

View File

@@ -39,14 +39,9 @@ extern int led_blue_intensity;
* Function prototypes
*/
void led_test();
void led_set_color(float red, float green, float blue);
// led_off(): turn off all LEDs
void led_off();
// led_pwm_handler(): handles switching LEDs on/off according to desired intensity;
// should be regularly called in timer routine, preferably in SysTick_Handler()
void led_pwm_handler();
void led_init(led_context_t *context, int pwm_freq, int pwm_handler_freq);
void led_test(int r, int g, int b);
#endif /* INC_LED_H_ */

View File

@@ -19,7 +19,7 @@ int led_blue_intensity;
int led_red_state;
int led_green_state;
int led_blue_state;
led_context_t *led_context;
led_context_t *led_context = NULL;
/*
* Functions
@@ -60,6 +60,12 @@ void led_pwm_handler()
{
int new_red_state, new_green_state, new_blue_state;
if (led_context == NULL) {
// led_pwm_handler() may be called before led_init() was called;
// this would result in a crash
return;
}
new_red_state = led_pwm_counter >= led_red_intensity ? 1 : 0;
new_green_state = led_pwm_counter >= led_green_intensity ? 1 : 0;
new_blue_state = led_pwm_counter >= led_blue_intensity ? 1 : 0;
@@ -76,7 +82,7 @@ void led_pwm_handler()
HAL_GPIO_WritePin(led_context->blue_led_port, led_context->blue_led_pin, new_blue_state);
led_blue_state = new_blue_state;
}
if (++led_pwm_counter > led_pwm_max) {
if (++led_pwm_counter >= led_pwm_max) {
led_pwm_counter = 0;
}
}
@@ -112,10 +118,3 @@ void led_init(led_context_t *context, int pwm_freq, int pwm_handler_freq)
// resolution of 40 steps for pwm
led_pwm_max = pwm_handler_freq / pwm_freq;
}
void led_test(int r, int g, int b)
{
HAL_GPIO_WritePin(led_context->red_led_port, led_context->red_led_pin, r);
HAL_GPIO_WritePin(led_context->green_led_port, led_context->green_led_pin, g);
HAL_GPIO_WritePin(led_context->blue_led_port, led_context->blue_led_pin, b);
}

View File

@@ -101,24 +101,24 @@ int main(void)
led_context.green_led_pin = LED_G_Pin;
led_context.blue_led_port = LED_B_GPIO_Port;
led_context.blue_led_pin = LED_B_Pin;
led_init(&led_context, 25, 1000);
// TODO tady je neco spatne, jako by SysTick nemel 1 kHz?
// TODO premerit osciloskopem frekvenci spinani led
led_init(&led_context, 50, 1000);
/* Turn off all LEDs */
led_off();
led_set_color(0.1, 0.0, 0.0);
// led_test(0,0,0);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
// LED PWM test
int counter = 0;
float R = 0.0, G = 0.0, B = 0.0;
while (1)
{
led_pwm_handler();
if (counter % 1000 == 0) {
R += 0.05;
G += 0.01;
B += 0.02;
R += 0.035;
G += 0.015;
B += 0.005;
if (R > 1.0) R = 0;
if (G > 1.0) G = 0;
if (B > 1.0) B = 0;

View File

@@ -131,7 +131,7 @@ void SysTick_Handler(void)
/* USER CODE END SysTick_IRQn 0 */
HAL_IncTick();
/* USER CODE BEGIN SysTick_IRQn 1 */
// led_pwm_handler();
led_pwm_handler();
/* USER CODE END SysTick_IRQn 1 */
}