Skip to content

Instantly share code, notes, and snippets.

@gamazeps
Created June 16, 2014 14:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gamazeps/20ededf78bc2e2ceb442 to your computer and use it in GitHub Desktop.
Save gamazeps/20ededf78bc2e2ceb442 to your computer and use it in GitHub Desktop.
/* Simple PWM example */
/*
* PWMD3 is a HAL defined variable of type PWMDriver - it is associated with
* TIM3.
*/
#include "ch.h"
#include "hal.h"
static PWMConfig pwmcfg = {
200000, /* 200Khz PWM clock frequency */
1024, /* PWM period 0.005 second */
NULL, /* No callback */
/* Only channel 1 enabled */
{
{PWM_OUTPUT_ACTIVE_HIGH, NULL},
{PWM_OUTPUT_ACTIVE_HIGH, NULL},
{PWM_OUTPUT_ACTIVE_HIGH, NULL},
{PWM_OUTPUT_ACTIVE_HIGH, NULL},
},
0
};
int main(void) {
halInit();
chSysInit();
uint16_t i = 1;
/* Enables PWM output (of TIM3, channel 1) on LED connected to PC6 */
palSetPadMode(GPIOC, 6, PAL_MODE_ALTERNATE(2));
pwmStart(&PWMD3, &pwmcfg);
while (TRUE) {
if (i >= 1024) {
i = 1;
} else {
i *= 2;
}
pwmEnableChannel(&PWMD3, 0, i);
chThdSleepMilliseconds(50);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment