-
Notifications
You must be signed in to change notification settings - Fork 0
PWM Example
This page describes the PWM example program distributed with the repository. All code examples can be found within the examples
folder of the repository.
/********************************************************
* File Name: PWM_ex.c
*
* Description:
* Main file
*
*
*********************************************************/
/*************************************************************************
System Includes
************************************************************************/
#include "sublibinal.h"
#include "sublibinal_config.h"
/*************************************************************************
Main Function
************************************************************************/
int main(void) {
Timer_Config timer_configuration = {0};
PWM_Config pwm_configuration = {0};
//configure our timer for use by the PWM module
timer_configuration.callback = NULL; //We do not need a timer callback function
timer_configuration.enabled = TRUE; //enable the timer when we initialize it
timer_configuration.frequency = 1000; //Run the timer at 1 KHz frequency
timer_configuration.pbclk = PB_CLK; //Tell the function our PB_CLK is 15MHz
timer_configuration.which_timer = Timer_2; //Use timer 1 for this timer
initialize_Timer(timer_configuration); //initialize and start the timer
//specify parameters of the PWM module
pwm_configuration.channel = PWM_CH_1; //Use PWM_CH_1 for this waveform
pwm_configuration.dutyCycle = .75; //Set the duty cycle to 75%
pwm_configuration.enable = TRUE; //enable the PWM
pwm_configuration.pin = Pin_RPB3; //Set our PWM to output on RB3
pwm_configuration.timer = Timer_2; //Assign the timer to the PWM configuration
initialize_PWM(pwm_configuration); //initialize the PWM module
//Global interrupt enable. Do this last!
enable_Interrupts();
//Begin the embedded system loop
while (1) {
}
return 0;
}
This program will implement a PWM waveform with a duty cycle of 75% and a frequency of 1KHz on pin B3. Please take note of the code section below.
//configure our timer for use by the PWM module
timer_configuration.callback = NULL; //We do not need a timer callback function
//...
timer_configuration.which_timer = Timer_2; //Use timer 2 for this timer
initialize_Timer(timer_configuration); //initialize and start the timer
//...
These two lines are important to understand, as the PWM waveform function does not require the use of any interrupts. The timer callback function for the associated PWM module is initialized to NULL so that interrupts are not generated. The next line is also important, as only timer_2 and timer_3 can be used for PWM waveform generation.
//specify parameters of the PWM module
pwm_configuration.channel = PWM_CH_1; //Use PWM_CH_1 for this waveform
pwm_configuration.dutyCycle = .75; //Set the duty cycle to 75%
pwm_configuration.enable = TRUE; //enable the PWM
pwm_configuration.pin = Pin_RPB3; //Set our PWM to output on RB3
pwm_configuration.timer = Timer_2; //Assign the timer to the PWM configuration
initialize_PWM(pwm_configuration); //initialize the PWM module
In this next section, we specify that we would like to use PWM channel 1 with a duty cycle of 75% on pin B3. We also specify that we will enable it when we configure the PWM and we will be sourcing the PWM using timer 2. We then initialize the PWM. After initialization, a PWM waveform is being generated on pin B3.
SUBLIBinal was created by the Palouse Robosub Club.