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