0% found this document useful (0 votes)
46 views2 pages

PWM Library for PIC Microcontrollers

The PWM library simplifies using the PWM hardware module available on many PICmicro microcontrollers. It includes routines to initialize the PWM module, change the duty ratio, start and stop PWM output. The library example shows how to continually change the duty ratio to gradually adjust the light from an LED connected to the PWM output pin.
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)
46 views2 pages

PWM Library for PIC Microcontrollers

The PWM library simplifies using the PWM hardware module available on many PICmicro microcontrollers. It includes routines to initialize the PWM module, change the duty ratio, start and stop PWM output. The library example shows how to continually change the duty ratio to gradually adjust the light from an LED connected to the PWM output pin.
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

PWM Library

CCP module is available with a number of PICmicros. mikroC provides library which simplifies
using PWM HW Module.

Note: Certain PICmicros with two or more CCP modules, such as P18F8520, require you to
specify the module you want to use. Simply append the number 1 or 2 to a Pwm. For example,
Pwm2_Start(); Also, for the sake of backward compabitility with previous compiler versions
and easier code management, MCU's with multiple PWM modules have PWM library which is
identical to PWM1 (i.e. you can use PWM_Init() instead of PWM1_Init() to initialize CCP1).

Library Routines

• Pwm_Init
• Pwm_Change_Duty
• Pwm_Start
• Pwm_Stop

Pwm_Init

Prototype void Pwm_Init(unsigned long freq);


Returns Nothing.
Description Initializes the PWM module with duty ratio 0. Parameter freq is a desired PWM
frequency in Hz (refer to device data sheet for correct values in respect with Fosc).

This routine needs to be called before using other functions from PWM Library.
Requires MCU must have CCP module.
Example Initialize PWM module at 5KHz:

Pwm_Init(5000);

Pwm_Change_Duty

Prototype void Pwm_Change_Duty(unsigned short duty_ratio);


Returns Nothing.
Description Changes PWM duty ratio. Parameter duty takes values from 0 to 255, where 0 is
0%, 127 is 50%, and 255 is 100% duty ratio. Other specific values for duty ratio can
be calculated as (Percent*255)/100.
Requires MCU must have CCP module. Pwm_Init must be called before using this routine.
Example Set duty ratio to 75%:

Pwm_Change_Duty(192);
Pwm_Start

Prototype void Pwm_Start(void);


Returns Nothing.
Description Starts PWM.
Requires MCU must have CCP module. Pwm_Init must be called before using this routine.
Example Pwm_Start();

Pwm_Stop

Prototype void Pwm_Stop(void);


Returns Nothing.
Description Stops PWM.
Requires MCU must have CCP module. Pwm_Init must be called before using this routine.
Pwm_Start should be called before using this routine, otherwise it will have no
effect as the PWM module is not running.
Example Pwm_Stop();

Library Example

The example changes PWM duty ratio on pin RC2 continually. If LED is connected to RC2, you
can observe the gradual change of emitted light.

void main() {

Pwm_Init(5000); // Initialize PWM module


Pwm_Start(); // Start PWM

while (1)
{
Pwm_Change_Duty(50); // set new duty ratio,

}
Delay_ms(200); // Slow down a bit
}
}

Common questions

Powered by AI

If Pwm_Stop is called before Pwm_Start in a mikroC-based PWM routine, the call to Pwm_Stop would have no effect as the PWM signal generation hadn't been initiated in the first place. This improper sequence in function calls could lead to confusion during debugging and inconsistent control flow in applications needing precise PWM control, as the PWM module needs to be started first to be stopped or adjusted effectively .

To control an LED’s brightness using the mikroC PWM library, first, initialize the PWM with a frequency suitable for human-perceived flicker-free operation, such as 5 kHz, using Pwm_Init(5000). Next, start the PWM module with Pwm_Start(). To control brightness, adjust the duty ratio with Pwm_Change_Duty(), where lower values dim the LED, and higher values brighten it. For example, setting the duty to 127 gives a 50% brightness level. Finally, regularly update the duty as needed, observing changes visually on the LED .

The frequency parameter in the Pwm_Init function is crucial as it defines the desired PWM frequency in Hertz, influencing how often the PWM signal cycles between high and low states. This parameter must be chosen in accordance with the oscillator frequency (Fosc) of the MCU, as specified in the device datasheet, to maintain the desired timing and duty cycle accuracy. Properly setting this frequency ensures the PWM signal operates within the expected parameters for the application .

Not calling Pwm_Init before using Pwm_Change_Duty in a mikroC application would mean the PWM module is not properly initialized, which would prevent the duty ratio change from being applied as the module setup is not complete. This might result in undefined behavior or no PWM signal being generated. It’s crucial to follow the required sequence to ensure the PWM signal parameters can be adjusted successfully .

To start a PWM signal using the mikroC PWM library, the process begins by initializing the PWM module via the Pwm_Init() function, which sets up the module at the desired frequency. Once the PWM module is initialized, the Pwm_Start() function can be called to commence PWM signal generation. It is required that the MCU has a CCP module and that Pwm_Init() is invoked before Pwm_Start() to ensure the module is correctly set up to handle PWM operations .

In mikroC, PWM initialization for PICmicro MCUs with multiple CCP modules is handled by using either PWM_Init() for CCP1 or PWM1_Init() but also providing the option to specify the module by appending a number. This design allows backward compatibility with older codes and simplifies code management by maintaining a consistent approach across different MCU versions. For example, Pwm2_Start() would be used to specify using the second CCP module on a PICmicro with such capability .

The mikroC library ensures backward compatibility with PWM functionalities by maintaining standard functions like PWM_Init(), which historically correspond to initializing CCP1. For MCUs with multiple CCP modules, it includes numbered functions like PWM1_Init() and PWM2_Init(), matching CCP1 and CCP2 respectively. This approach ensures that older codebases remain functional while providing flexibility for new applications to leverage multiple modules without modification to legacy code .

Referring to a device datasheet is necessary when initializing the PWM module using the mikroC library to ensure that the selected frequency is compatible with the MCU's oscillator frequency (Fosc). This helps maintain timing accuracy and avoids configuration conflicts that could arise from unsupported frequency settings. The datasheet provides critical details about the device's PWM module capabilities and limitations, which are essential for proper setup and operation .

The duty ratio parameter in the Pwm_Change_Duty function is significant as it determines the proportion of the PWM cycle that the signal remains high, thus controlling the power delivered to the load. The parameter takes a value from 0 to 255, where 0 signifies a 0% duty cycle and 255 signifies 100%. Other values are calculated using the formula (Percent * 255) / 100, allowing fine control over the PWM signal's active period, which is essential for modulating output power in applications such as motor speed control .

To demonstrate gradual change in light emission of an LED using PWM with the mikroC library, first initialize the PWM module with Pwm_Init() at a suitable frequency. Then start the PWM with Pwm_Start(). In a loop, periodically adjust the duty cycle using Pwm_Change_Duty(), incrementing or decrementing the duty value gradually from 0 to 255 and back, to cycle the LED brightness smoothly. Adding a delay like Delay_ms(200) in each cycle step can make the transition visible to the human eye .

You might also like