Add software PWM support and test app

This commit is contained in:
Michael Hayes
2013-09-25 15:44:00 +12:00
parent 2662263794
commit a53f82d932
7 changed files with 305 additions and 0 deletions
+48
View File
@@ -0,0 +1,48 @@
/** @file spwm.c
@author M. P. Hayes, UCECE
@date 25 Sep 2013
@brief These routines are for software pulse width modulation. They are
useful for modulating the luiminance of an LED or beeping a piezo
tweeter.
*/
#include "system.h"
#include "spwm.h"
/** Set the period. This must be greater than the duty. */
void
spwm_period_set (spwm_t *spwm, spwm_period_t period)
{
spwm->period = period;
}
/** Set the duty. This must be less than the period. */
void
spwm_duty_set (spwm_t *spwm, spwm_period_t duty)
{
spwm->duty = duty;
}
/** Return whether spwm output should be high or low.
For example, to control the flashing of a LED use:
led_set (led, spwm_update (spwm)); */
bool
spwm_update (spwm_t *spwm)
{
spwm->count++;
if (spwm->count >= spwm->period)
spwm->count = 0;
return spwm->count < spwm->duty;
}
/* Reset spwm. */
void
spwm_reset (spwm_t *spwm)
{
spwm->count = 0;
}
+45
View File
@@ -0,0 +1,45 @@
/** @file spwm.h
@author M. P. Hayes, UCECE
@date 25 Sep 2013
@brief These routines are for software pulse width modulation. They are
useful for modulating the luiminance of an LED or beeping a piezo
tweeter.
*/
#ifndef SPWM_H
#define SPWM_H
#include "system.h"
typedef uint8_t spwm_period_t;
/* This structure is defined here so the compiler can allocate enough
memory for it. However, its fields should be treated as
private. */
typedef struct spwm_struct
{
spwm_period_t period;
spwm_period_t duty;
spwm_period_t count;
} spwm_t;
/** Set the period. This must be greater than the duty. */
void spwm_period_set (spwm_t *spwm, spwm_period_t period);
/** Set the duty. This must be less than the period. */
void spwm_duty_set (spwm_t *spwm, spwm_period_t duty);
/** Return whether spwm output should be high or low.
For example, to control the flashing of a LED use:
led_set (led, spwm_update (spwm)); */
bool spwm_update (spwm_t *spwm);
/* Reset spwm. */
void spwm_reset (spwm_t *spwm);
#endif