BeRTOS
Data Structures | Enumerations | Functions
PWM API
PWM driver

With this driver you can control a device with multiple PWM channels. More...

Data Structures

struct  Pwm
 PWM context structure. More...

Enumerations

enum  PwmPolarity { PWM_POL_HIGH_PULSE, PWM_POL_POSITIVE = PWM_POL_HIGH_PULSE, PWM_POL_LOW_PULSE, PWM_POL_NEGATIVE = PWM_POL_LOW_PULSE }
 Enum describing PWM polarities. More...

Functions

void pwm_setDuty (Pwm *ctx, pwm_duty_t duty)
 Set the duty cycle of the PWM channel linked to ctx.
void pwm_setFrequency (Pwm *ctx, pwm_freq_t freq)
 Set PWM frequency of channel linked to ctx.
void pwm_setPolarity (Pwm *ctx, PwmPolarity pol)
 Set PWM polarity of pwm channel linked to ctx.
void pwm_enable (Pwm *ctx, bool state)
 Enable/Disable the pwm channel linked to ctx.
void pwm_init (Pwm *ctx, unsigned channel)
 Initialize PWM driver.

Detailed Description

With this driver you can control a device with multiple PWM channels.

You can enable/disable each channel indipendently and also set frequency and duty cycle.

API usage example:

 Pwm pwm; // declare a context structure
 pwm_init(&pwm, 0); // init pwm channel 0
 pwm_setFrequency(&pwm, 1000); // Set frequency of channel 0 to 1000Hz
 pwm_setDuty(&pwm, 0x7FFF); // Set duty to 50% (0xFFFF/2)
 pwm_enable(&pwm, true); // Activate the output

Enumeration Type Documentation

Enum describing PWM polarities.

Enumerator:
PWM_POL_HIGH_PULSE 

High pulse: increasing duty increases the part of the signal at high level.

PWM_POL_POSITIVE 

Positive pulse: same as High pulse.

PWM_POL_LOW_PULSE 

Low pulse: increasing duty increases the part of the signal at low level.

PWM_POL_NEGATIVE 

Negative pulse: same as Low pulse.

Definition at line 94 of file pwm.h.


Function Documentation

void pwm_enable ( Pwm ctx,
bool  enable 
)

Enable/Disable the pwm channel linked to ctx.

The modification will be applied to the channel immediatly.

Parameters:
ctxPWM channel context.
enableif true the channel will be enabled, if false will be disabled.
Note:
When a PWM channel is disabled, the output level will be the same as if the duty would be set to 0%. So, if current polarity is positive, a disabled channel will be low, if polarity is negative will be high.
See also:
pwm_setPolarity

Definition at line 220 of file pwm.c.

void pwm_init ( Pwm ctx,
unsigned  channel 
)

Initialize PWM driver.

Parameters:
ctxpointer to a PWM context structure, used for holding PWM driver related information.
channelthe channel you want to initialize.
Note:
The channel will be initialized disabled and with High polarity.

Definition at line 237 of file pwm.c.

void pwm_setDuty ( Pwm ctx,
pwm_duty_t  duty 
)

Set the duty cycle of the PWM channel linked to ctx.

The modification will be applied to the channel immediatly. The current frequency of the channel will be maintained.

Parameters:
ctxPWM channel context.
dutythe new duty cycle value.
See also:
pwm_duty_t

Definition at line 168 of file pwm.c.

void pwm_setFrequency ( Pwm ctx,
pwm_freq_t  freq 
)

Set PWM frequency of channel linked to ctx.

The modification will be applied to the channel immediatly. The duty cycle of the channel will be maintained.

Parameters:
ctxPWM channel context.
freqthe new frequency of the signal, in Hz.
Note:
Depending on the hardware implementation, this function may generate a glitch in the output signal upon frequency changing.

Definition at line 185 of file pwm.c.

void pwm_setPolarity ( Pwm ctx,
PwmPolarity  pol 
)

Set PWM polarity of pwm channel linked to ctx.

The modification will be applied to the channel immediatly.

Parameters:
ctxPWM channel context.
polthe new polarity of the signal.
Note:
if a channel is disabled, changing its polarity will change the current steady output level.
See also:
pwm_enable
PwmPolarity

Definition at line 202 of file pwm.c.