So you might be thinking, Bullshit, you cannot get an 8mhz pwm signal out of an arduino. But you can. Ignoring the sine like shape of the waveform below for a minute, that is the actual PWM output from an arduino uno I was using to do this. So, why is it so.
Well, all you have to do is use some code wizardry using interrupts, timers and registers in such a way that the maximum pwm frequency you can get from most microcontrollers is 50% of the clock speed. Given that the arduino usually has a clock speed of 16mhz, this gives an actual maximum pwm out of 8mhz. And while this is nice, in practice it is not all that useful as the discrete numbers of frequencies that can be derived this way is not all that useful.
The code is below, I did not write this code and sorry to whomever did, I do not have a link back to it and your name was not included in the snippet, so i cannot credit you. Enjoy.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
#include <avr/interrupt.h> #include <avr/io.h> #define TMR1 0 //for 8MHz: TMR1 = 0 , for 10kHz: TMR1 = 799 void setup() { // put your setup code here, to run once: pinMode(9, OUTPUT); //********************************************************************* //TIMER1 (16 bits) in mode TOP TCCR1B |= (1 << CS10); //selecting prescaler 0b001 (Tclk/1) TCCR1B &= ~((1<<CS12) | (1<<CS11)); // turn off CS12 and CS11 bits TCCR1A |= ((1<<WGM11) | (1<<WGM10)); //Configure timer 1 for TOP mode (with TOP = OCR1A) TCCR1B |= ((1<<WGM13) | (1<<WGM12)); TCCR1A |= (1 << COM1A0); // Enable timer 1 Compare Output channel A in toggle mode TCCR1A &= ~(1 << COM1A1); TCNT1 = 0; OCR1A = TMR1; //********************************************************************* } void loop() { } |




