NerdKits - electronics education for a digital generation

You are not logged in. [log in]

NEW: Learning electronics? Ask your questions on the new Electronics Questions & Answers site hosted by CircuitLab.

Support Forum » PWM Timer/Counter1 Problems

December 19, 2011
by Affarram
Affarram's Avatar

I am trying to use Timer/Counter0:2 to light a Red Green and Blue LED. I have the anode connected to the +5v and the Cathodes going to each timer. If anyone can tell me what i am doing wrong with Timer/Counter1 I would greatly appreciate it.

#define F_CPU 14745600
#include <stdio.h>
#include <avr/io.h>
#include "../libnerdkits/delay.h"

int main() {
    // Pin Def
    //PD6 = Blue LED
    //PB1 = Green LED
    //PB3 = Red LED

    // use Timer/Counter0 BLUE
    TCCR0A = (1<<COM0A1)            // non-inverting output
            | (1<<WGM01)            // fast PWM, top=0xFF
            | (1<<WGM00);
    TCCR0B = (1<<CS00);             // no clock prescaling
    DDRD |= (1<<PD6);               // configure OC0A output

    //Test Blue LED
    OCR0A = 255;
    delay_ms(500);
    OCR0A = 254;
    delay_ms(500);
    OCR0A = 253;
    delay_ms(500);
    OCR0A = 252;
    delay_ms(500);
    OCR0A = 251;
    delay_ms(500);
    OCR0A = 250;
    delay_ms(500);
    OCR0A = 249;
    delay_ms(500);
    OCR0A = 248;
    delay_ms(500);
    OCR0A = 247;
    delay_ms(500);
    OCR0A = 255;

    //Use Timer/Counter1 Green
    TCCR1A = (1<<COM1A1)|(1<<WGM11)|(1<<WGM10); //non-inverting output,fast PWM, top=0xFF
    TCCR1B = (1<<CS10);                         // no clock prescaling
    DDRB |= (1<<PB1);                           //configure OC1A output

    //Test Green LED
    OCR1A = 255;
    delay_ms(500);
    OCR1A = 254;
    delay_ms(500);
    OCR1A = 253;
    delay_ms(500);
    OCR1A = 252;
    delay_ms(500);
    OCR1A = 251;
    delay_ms(500);
    OCR1A = 250;
    delay_ms(500);
    OCR1A = 249;
    delay_ms(500);
    OCR1A = 248;
    delay_ms(500);
    OCR1A = 247;
    delay_ms(500);
    OCR1A = 255;

    //Use Timer/Counter2 Red
    TCCR2A = (1<<COM2A1)|(1<<WGM21)|(1<<WGM20); //non-inverting output,fast PWM, top=0xFF
    TCCR2B = (1<<CS20);                         // no clock prescaling
    DDRB |= (1<<PB3);                           //configure OC1A output

    //Test Green LED
    OCR2A = 255;
    delay_ms(500);
    OCR2A = 254;
    delay_ms(500);
    OCR2A = 253;
    delay_ms(500);
    OCR2A = 252;
    delay_ms(500);
    OCR2A = 251;
    delay_ms(500);
    OCR2A = 250;
    delay_ms(500);
    OCR2A = 249;
    delay_ms(500);
    OCR2A = 248;
    delay_ms(500);
    OCR2A = 247;
    delay_ms(500);
    OCR2A = 255;

    return 0;
}
December 20, 2011
by hevans
(NerdKits Staff)

hevans's Avatar

Hi Affarram,

Looking through your code I don't see anything that should be causing your LEDs to not light up. What are the LEDs doing? The only thing I see that is strange is that you are configuring your setup for Phase Correct PWM, not fast PWM like your comments say, but the LEDs should still light up.

Humberto

Post a Reply

Please log in to post a reply.

Did you know that a microcontroller can measure an RC time constant? Learn more...