I've been trying to set up my own PWM code for better timings and to learn about the hardware under Arduino but i can't seem to get it to work. I've read the section of the manual about timers and PWM so many times i could probably recite it from memory and have all the timers are working fine but PWM output just refuses to work. I'm trying to set up fast PWM and (for now) dim the LED on pin 13 (timer0) though I would rather it have it on timer2. I've been staring at the code for three days now and tried every combination of bits i could think of but nothing i try works, so i believe I'm missing something vital somewhere.
If someone could please look at my code and point out what I'm doing wrong i would be grateful. You would probably prevent me from going prematurely bald as well.
//////////
// includes
///////////////////
#include <avr/io.h>
#include <avr/interrupt.h>
//////////
// constants
///////////////////
#define PWM_OUT OCR0A
#define pin_out 13
//////////
// global variables
///////////////////
// hold current value of PWM_OUT
byte DUTY = 0;
//////////
// arduino setup
///////////////////
void setup() {
// setup interupt/pwm code
// enable pwm pin
pinMode(pin_out, OUTPUT);
// set fast pwm mode, clear on match
//TCCR0A = 0b10000011;
//TCCR0B = 0b00001100;
TCCR0A = _BV(COM0A1) | _BV(WGM01) | _BV(WGM00);
TCCR0B = _BV(WGM02);
// set timer0 prescaler to 64
// 1/ (16000000 / 64) = 1/250000
TCCR0B |= _BV(CS02);
// Timer0 match A Interrupt Enable
TIMSK0 = _BV(OCIE0A);
// turn on global interupts
sei();
// set initial value of OCR0A(TOP)
DUTY = 50;
PWM_OUT = DUTY;
// setup testing
Serial.begin(9600);
}
//////////
// arduino main loop
///////////////////
void loop(){
//PWM_OUT = DUTY;
//delay(100);
//DUTY++;
}
//////////
// interupt code
///////////////////
ISR(TIMER0_COMPA_vect) {
Serial.println("hi");
}