PWM

Icône de l'outil pédagogique Simplified use of the PWM outputs

( IUT de Cachan - Version 12/2011 )

The available functions :

void pwm_init(unsigned char period, char nb_canaux);

///////////////////////////////////////////////////////////////////////////////
// Function name : pwm_init
// Return value : none
// Parameters : unsigned char period
// functioning frequency f = (3e6 / (period+1))
// for the Fosc. = 48 MHz
// example : if period=150, f~20kHz
// char nb_channel
// choice of the number of channels PWM (1 ou 2)
// 1 -> only the pin C2 is a PWM output
// 2 -> pins C2 and C1 are PWM outputs
// Description : configuration of the frequency and the number of channels
///////////////////////////////////////////////////////////////////////////////

Initialization of the PWM outputs.
This function allows to use 1 or 2 PWM outputs, to choose the number of channels and to set the functioning frequency f = (3e6 / (period+1)) pour Fosc = 48 MHz

void pwm_setdc1(unsigned int cycles_H_state);

///////////////////////////////////////////////////////////////////////////////
// Function name : pwm_setdc1
// Return value : none
// Parameters : unsigned int cycles_H_state
// number of cycles of the high state, coded on 10 bits
// 1 period cycle <=> 4 cycles of cycles_H_state
// duty cycle ~ cycles_H_state/ (4 * period)
// example : if period=150, f~20kHz
// cycles_H_state?- Rapport cyclique
// 0 - 0
// 150 - 0.25
// 300 - 0.5
// 600 - 1
// Description : set the duty cycle of the channel PWM1 (broche C2)
///////////////////////////////////////////////////////////////////////////////

Setting of the duty cycle of PWM1 (pin C2)
Examples of values of the duty cycle, as a function of the value chosen of the period with the function pwm_init :
cycles_H_state Duty cycle
0 0
period 0.25
2 * period
0.5
4 * period 1

void pwm_setdc2(unsigned int cycles_etat_haut);

///////////////////////////////////////////////////////////////////////////////
// Function name : pwm_setdc2
// Return value : none
// Parameters : unsigned int cycles_H_state
// number of cycles of the high state, coded on 10 bits
// 1 period cycle <=> 4 cycles of cycles_H_state
// duty cycle ~ cycles_H_state/ (4 * period)
// Description : set the duty cycle of the channel PWM2 (pin C1)
///////////////////////////////////////////////////////////////////////////////

Duty cycle setting for PWM2 (pin C1)

Pin
PWM Channel
C2 PWM1
C1 PWM2

 

For more information, see p18f4550_39632e.pdf §15