Connect with us

avr ledbar programming

Discussion in 'Microcontrollers, Programming and IoT' started by Areeha Durrani, Feb 13, 2016.

Scroll to continue with content
  1. Areeha Durrani

    Areeha Durrani

    45
    1
    Jul 8, 2015
    hello,i am new to avr and stuck at a point.My task is to do is turn on LEDs(ON LED BAR) one by one in forward direction (example 10000000,11000000,11100000).
    But the code that i am writing turns the leds on in a pattern (1000000,01000000,00100000..).kindly help me with this.
    my code is:
    #include <avr/io.h>
    #include <util/delay.h>


    int main(void)
    {
    DDRB=0XFF;
    PORTB=0XFF;
    int i;
    for(i=0;i<=8;i++)
    {
    PORTB=(1<<i);
    _delay_ms(500);
    }

    return 0;
    }
     
  2. Harald Kapp

    Harald Kapp Moderator Moderator

    10,060
    2,147
    Nov 17, 2011
    The issue is with this instruction:
    Code:
    PORTB=(1<<i);
    It sets one bit of port B to 1 at position i, all other bits are cleared.
    Proper code could look e.g. like this:
    Code:
    PORTB = PORTB | (1<<i) // or use PORTB |= (1<<i)
    Note also that
    Code:
    PORTB=0XFF;
    presumably turns on all LEDs. You probably want to turn them all off, therefore
    Code:
    PORTB=0X00;
    is advisable.
     
    Areeha Durrani likes this.
  3. Areeha Durrani

    Areeha Durrani

    45
    1
    Jul 8, 2015
    Yes I understood that after posting the thread.thankyou sir.but what if i want to reverse the same pattern??as in;11111110,11111100,...?
     
  4. Areeha Durrani

    Areeha Durrani

    45
    1
    Jul 8, 2015
    understood the logic again!thanks..
     
Ask a Question
Want to reply to this thread or ask your own question?
You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.
Electronics Point Logo
Continue to site
Quote of the day

-