easy pwm on any pin or pins with multiple freq's
Posted: Sat Dec 01, 2012 4:42 am
i made a post in v5 with a simular topic, this one is slightly different in the way it operates and is easy to manipulate... the way it works with 1% accuracy mode
- the tmr2 macro increases a count everytime its accessed, the count starts at zero and counts up to 100, when count = 100 the count is returned to zero, what i did is turned the output on at count=0 and turned output off when the pwm variable = count,
this gives you a 0-100% dutycycle, all examples are running at 1000hz with 32Mhz osc but have different tmr2 frequencies
- in 1% resolution the frequency of the pwm is determined by tmr2 frequency / 100
- by modifying the tmr2 frequency you can hit many frequencys
- this makes it possible to use pwms on pins that do not have ccp so a board that doesn't give you access to the ccps pins thats not a problem when using something like this project
- you can use as many pwms as you want, and use any i/o pins you want
- in 1% fcf martins code ((adc*100)/255) rescales adc to 0-100, this goes directly to the pwm variable and you can print to the display those numbers as pwm% as its the same
- in other variations 2%,5%,10%,25% to print the number in the lcd macro i use code pwm* XX to put corrent number on display for pwm%
*** the pwm mentioned above and as in first fcf has a resolution of 1% per pwm increase, but you dont have to run 1% you can drop the resolution to anything you want by modifying this line of code in the tmr2 isr to other values, common are listed below, but to maintain the same frequency you will have to change the tmr2 frequency as in martins post http://www.matrixmultimedia.com/mmforum ... 26&t=10827
1% --> 100
2% --> 50
5% --> 20
10% --> 10
25% --> 4
- all simplified flowcharts below have 1000hz as output frequency notice tmr2 settings are different
- for example if you want to set pwm frequency to 620hz with 5% resolution is 620*20= 12,400
use tmr2 calculator in seperate post (revised fcf) http://www.matrixmultimedia.com/mmforum ... 580#p42633 to find the settings for 12,400 hz, the closest frequency is 12,403 which has the prescaler,postscaler,rollover values of ( 1 - 15 - 43 ) @32Mhz osc , then since 5/100 is 20counts set your tmr2 macro decision to
print to lcd will have to be modified as well sorry for my sudo arduino like code
but hopefully get the idea
-
- the tmr2 macro increases a count everytime its accessed, the count starts at zero and counts up to 100, when count = 100 the count is returned to zero, what i did is turned the output on at count=0 and turned output off when the pwm variable = count,
this gives you a 0-100% dutycycle, all examples are running at 1000hz with 32Mhz osc but have different tmr2 frequencies
- in 1% resolution the frequency of the pwm is determined by tmr2 frequency / 100
- by modifying the tmr2 frequency you can hit many frequencys
- this makes it possible to use pwms on pins that do not have ccp so a board that doesn't give you access to the ccps pins thats not a problem when using something like this project
- you can use as many pwms as you want, and use any i/o pins you want
- in 1% fcf martins code ((adc*100)/255) rescales adc to 0-100, this goes directly to the pwm variable and you can print to the display those numbers as pwm% as its the same
- in other variations 2%,5%,10%,25% to print the number in the lcd macro i use code pwm* XX to put corrent number on display for pwm%
*** the pwm mentioned above and as in first fcf has a resolution of 1% per pwm increase, but you dont have to run 1% you can drop the resolution to anything you want by modifying this line of code in the tmr2 isr
Code: Select all
if(count==100){count=0}
1% --> 100
2% --> 50
5% --> 20
10% --> 10
25% --> 4
- all simplified flowcharts below have 1000hz as output frequency notice tmr2 settings are different
- for example if you want to set pwm frequency to 620hz with 5% resolution is 620*20= 12,400
use tmr2 calculator in seperate post (revised fcf) http://www.matrixmultimedia.com/mmforum ... 580#p42633 to find the settings for 12,400 hz, the closest frequency is 12,403 which has the prescaler,postscaler,rollover values of ( 1 - 15 - 43 ) @32Mhz osc , then since 5/100 is 20counts set your tmr2 macro decision to
Code: Select all
if(count==20({count=0}
Code: Select all
printNumber(pwm*5)

-