0% found this document useful (0 votes)
153 views

PWM Psedocode

The document provides pseudo code for a PWM service that initializes PWM modules and ports, sets the system clock, configures PWM outputs, and provides functions to set duty cycles, frequencies, and restore duty cycles. It initializes the PWM service by assigning a priority, enabling clocks, waiting for modules and ports to be ready, setting the system clock, disabling PWM during initialization, and configuring compare values and periods. It also posts the service, runs the service by returning no events, and includes functions to set duty channels, restore duty cycles, and set frequencies.

Uploaded by

api-438120791
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
153 views

PWM Psedocode

The document provides pseudo code for a PWM service that initializes PWM modules and ports, sets the system clock, configures PWM outputs, and provides functions to set duty cycles, frequencies, and restore duty cycles. It initializes the PWM service by assigning a priority, enabling clocks, waiting for modules and ports to be ready, setting the system clock, disabling PWM during initialization, and configuring compare values and periods. It also posts the service, runs the service by returning no events, and includes functions to set duty channels, restore duty cycles, and set frequencies.

Uploaded by

api-438120791
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Pseudo Code for PWM Service

bool InitPWMService(uint8_t Priority)

// initializing PWM

Assign priority number to PWM Service

Enable clock to PWM Module 0 Module 1

Enable clock to Port B,C,D and E

Wait till the Ports B,C,D and E are ready i.e the clock is active

Wait till the PWM modules are ready i.e the clock is active

Set the system clock to 32 BITS

Disable the PWM while initializing

Set PWM Generator to go to 1 on rising edge compare value and to 0 on falling edge
compare value

Set the PWM period as per frequency

Set the initial duty for every channel

Enable PWM outputs

Configure Port B,C,D and E pins to alternate functions

Choose to map PWM to those Pins

Digital enable the Port Pins

Enable the Direction on Port pins as Output

Set the up/DoWn count mode and enable the PWM generator. Both generator updates
synchronised to zero count

Post ES_init to PWMService

bool PostPWMService(ES_Event_t ThisEvent)

return ES_PostToService(MyPriority, ThisEvent)

ES_Event_t RunPWMService(ES_Event_t ThisEvent)

define ES_Event_t ReturnEvent as = ES_NO_EVENT;

return ReturnEvent;

void SetDutyChannel(uint8_t duty)

// write separate function for each PWM channel to avoid precedence of action taken on load
value (no action) over compare values at 0 and 100

if duty is 100

set the PWM duty to be 100

else if duty is 0

set the PWM duty to be 0

else set the compare register value for desired duty

void RestoreDutyCycle(void)

// define this function for each channel

restore duty cycle back to normal

void SetFrequency(uint16_t period_in_us)

Pseudo Code for PWM Service


change the frequency for channels wherever needed by changing the load value in the respective
PWM load register

You might also like