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.

Microcontroller Programming » I'm trying to dim some LEDs...

June 10, 2012
by Pew446
Pew446's Avatar

Well I wanted to hook up my charlieplexed array to the PWM so I could change the brightness of the LEDs, but I'm under the impression I can only use one pin per timer (Right?), and I have 5 wires I need hooked up to individual pins. So I tried to copy the Valentines Heart method of lowering brightness, but I'm probably making a ton of mistakes. Either way, here's my code, and the issue is that the LED is always on, no matter what I have set as the duty.

    #include <stdio.h>
    #include <string.h>

    #include <avr/io.h>
    #include <avr/interrupt.h>
    #include <avr/pgmspace.h>
    #include <inttypes.h>

    #include "../libnerdkits/delay.h"
    #include "../libnerdkits/uart.h"

    #define F_CPU 14745600

    #define R1 (1<<PC1)
    #define R2 (1<<PC2)
    #define R3 (1<<PC3)
    #define R4 (1<<PC4)
    #define R5 (1<<PC5)

    volatile uint8_t getd[1];
    volatile uint8_t incrementor;

    inline void setled(char i, char j, char o){
        if(o==1){
            if(i==1){
                if(j==2){
                    DDRC |= R1 | R2;
                    PORTC |= R1;
                    PORTC &= ~R2;
                }
                else if(j==3){
                    DDRC |= R1 | R3;
                    PORTC |= R1;
                    PORTC &= ~R3;
                }
                else if(j==4){
                    DDRC |= R1 | R4;
                    PORTC |= R1;
                    PORTC &= ~R4;
                }
                else if(j==5){
                    DDRC |= R1 | R5;
                    PORTC |= R1;
                    PORTC &= ~R5;
                }
            }
            else if(i==2){
                if(j==1){
                    DDRC |= R2 | R1;
                    PORTC |= R2;
                    PORTC &= ~R1;
                }else if(j==3){
                    DDRC |= R2 | R3;
                    PORTC |= R2;
                    PORTC &= ~R3;
                }else if(j==4){
                    DDRC |= R2 | R4;
                    PORTC |= R2;
                    PORTC &= ~R4;
                }else if(j==5){
                    DDRC |= R2 | R5;
                    PORTC |= R2;
                    PORTC &= ~R5;
                }
            }
            else if(i==3){
                if(j==1){
                    DDRC |= R3 | R1;
                    PORTC |= R3;
                    PORTC &= ~R1;
                }else if(j==2){
                    DDRC |= R3 | R2;
                    PORTC |= R3;
                    PORTC &= ~R2;
                }else if(j==4){
                    DDRC |= R3 | R4;
                    PORTC |= R3;
                    PORTC &= ~R4;
                }else if(j==5){
                    DDRC |= R3 | R5;
                    PORTC |= R3;
                    PORTC &= ~R5;
                }
            }
            else if(i==4){
                if(j==1){
                    DDRC |= R4 | R1;
                    PORTC |= R4;
                    PORTC &= ~R1;
                }else if(j==2){
                    DDRC |= R4 | R2;
                    PORTC |= R4;
                    PORTC &= ~R2;
                }else if(j==3){
                    DDRC |= R4 | R3;
                    PORTC |= R4;
                    PORTC &= ~R3;
                }else if(j==5){
                    DDRC |= R4 | R5;
                    PORTC |= R4;
                    PORTC &= ~R5;
                }
            }
            else if(i==5){
                if(j==1){
                    DDRC |= R5 | R1;
                    PORTC |= R5;
                    PORTC &= ~R2;
                }else if(j==2){
                    DDRC |= R5 | R2;
                    PORTC |= R5;
                    PORTC &= ~R2;
                }else if(j==3){
                    DDRC |= R5 | R3;
                    PORTC |= R5;
                    PORTC &= ~R3;
                }else if(j==4){
                    DDRC |= R5 | R4;
                    PORTC |= R5;
                    PORTC &= ~R4;
                }
            }
        }   
    }
    inline void alloff() {  
      DDRC &= ~( (1<<PC1) | (1<<PC2) | (1<<PC3) | (1<<PC4) | (1<<PC5) );
      PORTC &= ~( (1<<PC1) | (1<<PC2) | (1<<PC3) | (1<<PC4) | (1<<PC5) );
    }
    inline void init_pwm() {
        incrementor = 0;
        // Timer0 CK/8 (7000Hz)
        TCCR0B = (1<<CS01) | (0<<CS00);
        TIMSK0 = (1<<TOIE0);
    }
    SIGNAL(SIG_OVERFLOW0) {  
        //keep a counter to provide duty cycle
        incrementor++;
        if(incrementor == 64) 
            incrementor = 0;
        alloff();
        if(getd[0] > incrementor){
            setled(1,2,1);
        }else{
            setled(1,2,0);
        }
    }
    int main() {
    init_pwm();
    sei();
    int i;
    while(1){
    for(i=0;i<64;i++){
    getd[0]=i;
    delay_ms(10);
    }
    i=0;
    }
    return 1;
    }

Excuse my novice coding, I'm not used to C.

June 10, 2012
by Pew446
Pew446's Avatar

Well I guess I should say the LED is always at full brightness no matter the duty.

June 10, 2012
by Pew446
Pew446's Avatar

Here is my schematic if it helps:

June 10, 2012
by Pew446
Pew446's Avatar

Wow what silly mistakes. I forgot to specify what happens when setled()'s third argument is a 0 (turn off the led.)

I hope someone finds this thread partly useful.

Post a Reply

Please log in to post a reply.

Did you know that you can use a transistor to interface between different voltage levels of digital logic? Learn more...