PWM Library
CMO module is available with a number of AVR MCUs. mikroBasic PRO for AVR provides library which simplifies using PWM HW Module.

- AVR MCUs require you to specify the module you want to use. To select the desired PWM, simply change the letter x in the prototype for a number from 1 to 2. Number of UART modules per MCU differs from chip to chip. Please, read the appropriate datasheet before utilizing this library.
For the XMEGA family of MCUs change the xn in the routine prototype with C0, C1, D0, D1, E0, E1, F0 or F1 (MCU dependent). - For better understanding of PWM module it would be best to start with the example provided in Examples folder of our mikroBasic PRO for AVR compiler.
- When you select a MCU, mikroBasic PRO for AVR automatically loads the correct PWM library (or libraries), which can be verified by looking at the Library Manager.
- PWM library handles and initializes the PWM module on the given AVR MCU, but it is up to user to set the correct pins as PWM output, this topic will be covered later in this section.
- mikroBasic PRO for AVR does not support enhanced PWM modules.
Library Routines
PWMx_Init
Prototype |
sub procedure PWMx_Init(dim wave_mode as byte, dim prescaler as byte, dim inverted as byte, dim duty as byte) |
||||||||||||||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
Nothing. |
||||||||||||||||||||||||||||||||||||||||||||||||||
Description |
Initializes the PWM module. Parameters :
PWMx_Init must be called before using other functions from PWM Library. |
||||||||||||||||||||||||||||||||||||||||||||||||||
Requires |
You need a CMO on the given MCU (that supports PWM). Before calling this routine you must set the output pin for the PWM (according to the datasheet). |
||||||||||||||||||||||||||||||||||||||||||||||||||
Example |
Initialize PWM module: PWM1_Init(_PWM1_FAST_MODE, _PWM1_PRESCALER_8, _PWM1_NON_INVERTED, 127) |
PWM_xn_Init
Prototype |
sub function PWM_xn_Init(dim freq_hz as longword, dim wave_mode as byte) as word |
||||||
---|---|---|---|---|---|---|---|
Returns |
Calculated timer period (value written in PER register) for XMEGA family of MCUs. |
||||||
Description |
Initializes the PWM module. Change the xn in the routine prototype with C0, C1, D0, D1, E0, E1, F0 or F1 (MCU dependent). Parameters :
|
||||||
Requires |
You need a CMO on the given MCU (that supports PWM). Before calling this routine you must set the output pin for the PWM (according to the datasheet). |
||||||
Example |
Initialize PWM module: |
PWMx_Set_Duty
Prototype |
sub procedure PWMx_Set_Duty(dim duty as byte) |
---|---|
Returns |
Nothing. |
Description |
Changes PWM duty ratio. Parameter Parameters :
|
Requires |
PWM module must to be initialised (PWMx_Init) before using this function. |
Example |
For example lets set duty ratio to 75%: PWM1_Set_Duty(192) |
PWM_xn_Set_Duty
Prototype |
sub procedure PWM_xn_Set_Duty(dim duty_ratio as word, dim inverted as byte, dim channel as byte) |
||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
Nothing. |
||||||||||||||||
Description |
Changes PWM duty ratio. Parameter Parameters :
|
||||||||||||||||
Requires |
PWM module must to be initialised (PWM_xn_Init) before using this function. |
||||||||||||||||
Example |
For example lets set duty ratio to 75%: |
PWMx_Start
Prototype |
sub procedure PWMx_Start() |
---|---|
Returns |
Nothing. |
Description |
Starts PWM. |
Requires |
MCU must have CMO module to use this library. PWMx_Init must be called before using this routine. |
Example |
PWM1_Start() |
PWM_xn_Start
Prototype |
sub procedure PWM_xn_Start(dim channel as byte) |
||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
Nothing. |
||||||||||
Description |
Starts PWM. Parameters :
|
||||||||||
Requires |
MCU must have CMO module to use this library. PWM module must to be initialised (PWM_xn_Init) before using this function. |
||||||||||
Example |
|
PWMx_Stop
Prototype |
sub procedure PWMx_Stop() |
---|---|
Returns |
Nothing. |
Description |
Stops the PWM. |
Requires |
MCU must have CMO module to use this library. PWMx_Init and PWMx_Start must be called before |
Example |
PWM1_Stop() |
PWM_xn_Stop
Prototype |
sub procedure PWM_xn_Stop(dim channel as byte) |
||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|
Returns |
Nothing. |
||||||||||
Description |
Stops the PWM. Parameters :
|
||||||||||
Requires |
MCU must have CMO module to use this library. PWM_xn_Init and PWM_xn_Start must be called before |
||||||||||
Example |
|
Library Example
The example changes PWM duty ratio on PB3 and PB7 pins continually. If LED is connected to PB3 and PB7, you can observe the gradual change of emitted light.
program PWM_Test dim current_duty as byte current_duty1 as byte main: DDB0_bit = 0 ' Set PORTB pin 0 as input DDB1_bit = 0 ' Set PORTB pin 1 as input DDC0_bit = 0 ' Set PORTC pin 0 as input DDC1_bit = 0 ' Set PORTC pin 1 as input current_duty = 16 ' initial value for current_duty current_duty1 = 16 ' initial value for current_duty DDB3_bit = 1 ' Set PORTB pin 3 as output pin for the PWM (according to datasheet) DDD7_bit = 1 ' Set PORTD pin 7 as output pin for the PWM1 (according to datasheet) PWM_Init(_PWM_FAST_MODE, _PWM_PRESCALER_8, _PWM_NON_INVERTED, 16) PWM1_Init(_PWM_FAST_MODE, _PWM1_PRESCALER_8, _PWM1_NON_INVERTED, 16) while TRUE ' Endless loop if (PINB0_bit <> 0) then ' Detect if PORTB pin 0 is pressed Delay_ms(40) ' Small delay to avoid deboucing effect Inc(current_duty) ' Increment duty ratio PWM_Set_Duty(current_duty) ' Set incremented duty end if if (PINB1_bit <> 0) then ' Detect if PORTB pin 1 is pressed Delay_ms(40) ' Small delay to avoid deboucing effect Dec(current_duty) ' Decrement duty ratio PWM_Set_Duty(current_duty) ' Set decremented duty ratio end if if (PINC0_bit <> 0) then ' Detect if PORTC pin 0 is pressed Delay_ms(40) ' Small delay to avoid deboucing effect Inc(current_duty1) ' Increment duty ratio PWM1_Set_Duty(current_duty1) ' Set incremented duty end if if (PINC1_bit <> 0) then ' Detect if PORTC pin 1 is pressed Delay_ms(40) ' Small delay to avoid deboucing effect Dec(current_duty1) ' Decrement duty ratio PWM1_Set_Duty(current_duty1) ' Set decremented duty ratio end if wend end.
HW Connection
PWM demonstration
What do you think about this topic ? Send us feedback!