car/my_src/pwm.c
Wolfgang Hottgenroth 36ce69de93 led control
2018-01-18 15:06:37 +01:00

49 lines
917 B
C

/*
* pwm.c
*
* Created on: Jan 16, 2018
* Author: wn
*/
#include "pwm.h"
#include "stm32f1xx_hal.h"
#include <PontCoopScheduler.h>
extern TIM_HandleTypeDef htim3;
const int32_t PWM_MAX = 10000;
const int32_t PWM_STEP = 200;
typedef struct {
int32_t value;
int32_t direction;
} tPwmHandle;
tPwmHandle pwmHandle = { .value = 0, .direction = 1 };
void pwmExec(void *handle) {
tPwmHandle *lPwmHandle = (tPwmHandle*) handle;
lPwmHandle->value += (PWM_STEP * lPwmHandle->direction);
if (lPwmHandle->value >= PWM_MAX) {
lPwmHandle->direction = -1;
}
if (lPwmHandle->value <= 0) {
lPwmHandle->direction = 1;
}
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, lPwmHandle->value);
}
void pwmInit() {
schAdd(pwmExec, &pwmHandle, 0, 50);
__HAL_TIM_SET_AUTORELOAD(&htim3, PWM_MAX);
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, 0);
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
}