CARME-M4 BSP  V1.5
pwm.c

Explains how to read from ADC and control the PWM.

/*
* This example shows how to read from ADC and control the PWM.
*/
#include <stm32f4xx.h>
#include <carme.h>
#include <carme_io2.h>
#define PWM_HZ 2000 /* PWM counter speed */
#define PWM_PERIOD 100 /* PWM period length */
#define ADC_MAX 1023 /* maximal ADC value */
int main(void) {
TIM_TimeBaseInitTypeDef TIM_TimeBaseStruct;
uint16_t value;
uint16_t prescaler;
prescaler = (uint16_t) ((SystemCoreClock / 2) / PWM_HZ) - 1;
TIM_TimeBaseStruct.TIM_Prescaler = prescaler;
TIM_TimeBaseStruct.TIM_CounterMode = TIM_CounterMode_Up;
TIM_TimeBaseStruct.TIM_Period = PWM_PERIOD - 1;
TIM_TimeBaseStruct.TIM_ClockDivision = TIM_CKD_DIV1;
TIM_TimeBaseStruct.TIM_RepetitionCounter = 0;
CARME_IO2_PWM_Settings(&TIM_TimeBaseStruct);
for (;;) {
CARME_IO2_PWM_Set(CARME_IO2_PWM0, value * PWM_PERIOD / ADC_MAX);
}
return 0;
}