Prescaler = 6 -> PWM frequency is PWM frequency is 62000 Hz Prescaler = 3 -> PWM frequency is 490 Hz (default value) Prescaler = 2 -> PWM frequency is 4000 Hz Prescaler = 1 -> PWM frequency is 31000 Hz I registered those values on all PWM pins, changing the value of prescaler (the only exception are pins 13 and 14, see later): now we have a new PWM frequency on pin 9 and 10!.TCCR2B |= myPrescaler //this operation (OR), replaces the last three bits in TCCR2B with our new value 011 In this case, 3 corresponds in binary to 011. Int myPrescaler = 3 // this could be a number in. now that CS02, CS01, CS00 are clear, we write on them a new value:.TCCR2B &= ~myEraser // this operation (AND plus NOT), set the three bits in TCCR2B to 0 Int myEraser = 7 // this is 111 in binary and is used as an eraser Those bits in fact represent an integer number (from 0 to 7) called 'prescaler', that Arduino uses to generate the frequency for PWM.įirst of all, we have to clear these three bits, i.e they must be all set to 0: The first three bits (from right to left!) are called CS02, CS01, CS00, and they are the bits we have to change. So, if we want to change the PWM frequency of pins 10 and 9, we will have to act on TCCR2B. You can access a timer simply changing in your code (tipically in the setup()), the value of variable TCCRnB, where 'n' is the number of register. This is the list of timers in Arduino Mega 2560:Īs you can see, a given timer controls more than one pin (every change about a timer will affect all pins depending on it!). In order to change frequency on pin 'A', we have to change some value in the timer Whose frequency is 980 Hz (I checked with an oscilloscope). The PWM default frequency is 490 Hz for all pins, with the exception of pin 13 and 4, Here follows my brief instructions and descriptions!Īrduino 2560 has 12 pins supporting PWM. Here I simply modified a little bit the author'code to make it match with Arduino 2560. In this brief article the author explains very clearly how to change some values in Arduino Diecimila registers, in order to enhance the PWM frequency.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |