car/my_src/pwm.c

49 lines
917 B
C
Raw Normal View History

2018-01-16 17:56:07 +01:00
/*
* pwm.c
*
* Created on: Jan 16, 2018
* Author: wn
*/
#include "pwm.h"
#include "stm32f1xx_hal.h"
#include <PontCoopScheduler.h>
extern TIM_HandleTypeDef htim3;
2018-01-18 15:06:37 +01:00
const int32_t PWM_MAX = 10000;
const int32_t PWM_STEP = 200;
typedef struct {
int32_t value;
int32_t direction;
} tPwmHandle;
2018-01-16 17:56:07 +01:00
2018-01-18 15:06:37 +01:00
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);
2018-01-16 17:56:07 +01:00
}
void pwmInit() {
2018-01-18 15:06:37 +01:00
schAdd(pwmExec, &pwmHandle, 0, 50);
2018-01-16 17:56:07 +01:00
2018-01-18 15:06:37 +01:00
__HAL_TIM_SET_AUTORELOAD(&htim3, PWM_MAX);
2018-01-16 17:56:07 +01:00
2018-01-18 15:06:37 +01:00
__HAL_TIM_SET_COMPARE(&htim3, TIM_CHANNEL_1, 0);
2018-01-16 17:56:07 +01:00
HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_1);
}