Go Down

Topic: set desired PWM Channel Frequency (Read 334 times) previous topic - next topic

shazmirza

i am Using all 15 Channels of PWM on Arduino mega2560 and i need to set their frequency to 40hz constant... please anyone tell me how can i do that i will be very Thankful.

DrAzzy

#1
Feb 24, 2017, 08:24 pm Last Edit: Feb 24, 2017, 08:27 pm by DrAzzy
Use a timer library, or take over the timers by directly manipulating the timer control registers (see the sections of the datasheet on the timers). You can change the clock prescaler, as well as the number that the timer counts up to before going back to 0 (you have to change the WGM to one of the ones that lets you adjust that - and you may not be able to get that on all the mega's timers - most of the Mega's timers are the 16-bit ones, but timer 0 and 2 are 8-bit ones with different featuresets). Between manipulating TOP and the prescalar, you can get PWM at 40hz (or almost any other frequency you want).

You'd need to write a replacement for analog write, naturally, since you're counting to a number other than 255.
ATTinyCore for x4/x5/x61/x7/x8/x41/1634/828/x313 megaTinyCore for the megaavr ATtinies - Board Manager:
http://drazzy.com/package_drazzy.com_index.json
ATtiny breakouts, mosfets, awesome prototyping board in my store http://tindie.com/stores/DrAzzy

Go Up