Author | Stef Mientki, Copyright © 2002-2006, all rights reserved. |
Adapted-by | Sebastien Lelong, Rob Hamerling |
Compiler | 2.4p |
This lib handles common operations on PWM, regardless the channel number. It is used in combination with one or more pwm_ccplibraries (eg. pwm_ccp1.jal, pwm_ccp2.jal, etc.). These libraries are included automatically dependent of the configuration of the target PIC. . Oscillator refequency, PWM frequency and PWM resolution are a tighly coupled set of parameters. Refer to the PWM section in the datasheets how these are related.
This is a heavy refactoring of the original pwm_hardware.jal (Stef's lib). . Changes dd October 2012: - Added function pwm_get_resolution() returning the actual resolution in PWM steps. --------------------------------------------------------------------------
No dependency found
var volatile word _pr2_shadow_plus1 = 256
Shadow of PR2 (value incremented by 1, i.e. value in range 1..256) Set to maximum value by default for maximum resolution.
pwm_max_resolution(byte in prescaler)
Purpose: Set all(!) CCP modules for max (10-bits) PWM resolution. Arguments: byte - prescaler value for Timer2 (allowed value 1, 4 or 16) (other values will result in disabled PWM) Notes: - Allowed values of input parameter: 1, 4 and 16 User may check bit TIMER2_TMR2ON to see if call was successful TRUE: Timer2 ON, PWM resolution set FALSE: Timer2 OFF, no PWM signal . While setting maximum resolution, the Timer2 prescaler can be used to adjust frequency to some extent with the Timer2 parameter value which can be either: * 1 : highest frequency * 4 : medium frequency * 16: lowest frequency Other values result in PWM being disabled. . Below a table showing the PWM frequencies for common clock speeds and different Timer2 prescaler values. ||*Timer2 prescaler* || *4MHz* || *8MHz* || *10MHz* || *20MHz* || || 1 || 3.90 kHz || 7.81 kHz || 9.77 kHz || 19.43 kHz || || 4 || 976 Hz || 1.95 kHz || 2.44 kHz || 4.88 kHz || || 16 || 244 Hz || 488 Hz || 610 Hz || 1.22 kHz || For PICs with more than one CCP module you should realize that all CCP modules use Timer2 and PR2 for PWM operations. This means that the PWM period (frequency) of all PWM output pins is the same. Only the duty cycle can vary between one and another PWM pin. . This implies 1 out of 3 specific PWM frequencies, depending on the Timer2 prescaler value passed with the call. See examples in the table above, or use the formula in the datasheet to calculate the resulting frequency.
pwm_set_frequency(dword in freq)
Purpose: Set all(!) CCP modules for a specific PWM frequency. Arguments: Input: dword - PWM frequency Output: none Notes: - User may check bit TIMER2_TMR2ON to see if call successful TRUE: Timer2 ON, PWM frequency set FALSE: Timer2 OFF, no PWM signal (frequency too high or too low with current Fosc) The minimum and maximum allowed PWM frequencies depend on the clock speed. When specifying a value beyond the limits PWM will not be enabled. . The chosen frequency may enforce a limit on the PWM resolution (number of steps). The application program should not specify a higher value for the duty cycle than this limit. When exceeding this limit the dutycycle will probably result in a 'weird' value. Use the procedures pwm?_set_dutycycle_percent() and pwm?_set_dutycycle_ratio() to set the desired duty cycle.
pwm_get_resolution() return word
Purpose: Obtain actuial number of PWM steps Arguments: (none) Returns: word = actual number of PWM steps