Maker Pro
Maker Pro

avr ledbar programming

Areeha Durrani

Jul 8, 2015
45
Joined
Jul 8, 2015
Messages
45
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;
}
 

Harald Kapp

Moderator
Moderator
Nov 17, 2011
13,700
Joined
Nov 17, 2011
Messages
13,700
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

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