EmbDev.net

Forum: µC & Digital Electronics ATMEGA8 beginner problem


Author: Ajay R. (ajay_r)
Posted on:

Rate this post
0 useful
not useful
I'm using ATmega 8 (8L - 8PI) , I am trying to blink two LED's in 
alternate order with legs PB0 and PB6 for each LED's but its not 
working..
#define F_CPU 1000000UL

#include <avr/io.h>
#include <util/delay.h>

int
main (void)
{
DDRB |= _BV(DDB0);
DDRB |= _BV(DDB6);
while(1)
{
PORTB = 00000001;
_delay_ms(500);
PORTB = 01000000;

_delay_ms(500);
}
}

: Edited by Admin
Author: Lothar Miller (lkmiller) (Moderator)
Posted on:

Rate this post
0 useful
not useful
Ajay R. wrote:
> but its not working
WHAT is not working?

Author: Ajay R. (ajay_r)
Posted on:

Rate this post
0 useful
not useful
two leds are not blinking...only one is blinking

Author: MWS (Guest)
Posted on:

Rate this post
0 useful
not useful
Ajay R. wrote:
> PORTB = 01000000;

You need to tell the compiler it's a binary expression.

Author: Ajay R. (ajay_r)
Posted on:

Rate this post
0 useful
not useful
I am new to this(noob),can you please modify my code.

Author: Ajay R. (ajay_r)
Posted on:

Rate this post
0 useful
not useful
Ajay R. wrote:
> I am new to this(noob),can you please modify my code.

converting it to hexadecimal form would help??

Author: Hartl192 (Guest)
Posted on:

Rate this post
0 useful
not useful
Ajay R. wrote:
> PORTB = 00000001;
> _delay_ms(500);
> PORTB = 01000000;

Try it like this:
 PORTB = 0x01;
> _delay_ms(500);
> PORTB = 0x40;

Author: A.S.M. (Guest)
Posted on:

Rate this post
0 useful
not useful
Ajay R. wrote:

 while(1)
 {
 PORTB = 0b00000001;
 _delay_ms(500);
 PORTB = 0b01000000;

 _delay_ms(500);
 }

Author: MWS (Guest)
Posted on:

Rate this post
0 useful
not useful
Ajay R. wrote:
> can you please modify my code.

You already got the hint ;D

Btw., I think binary representation is the better translation.

For the compiler 01000000 is a number, one million, which doesn't fit a 
port of 8 bit very well. 0b01000000 will do a better job.

Author: Karol Babioch (johnpatcher)
Posted on:

Rate this post
0 useful
not useful
Ajay R. wrote:
> I am new to this(noob),can you please modify my code.

Adopt your main loop to look something like this:
PORTB = 0x01;
_delay_ms(500);
PORTB = 0x40;
_delay_ms(500);

Alternatively, if you are using avr-gcc, you can also make use of the 
"0b" notation:
PORTB = 0b00000001;
_delay_ms(500);
PORTB = 0b01000000;
_delay_ms(500);

Best regards,
Karol Babioch

Author: Ajay R. (ajay_r)
Posted on:

Rate this post
0 useful
not useful
Thanks to all of you,its working now :D

Author: Karol Babioch (johnpatcher)
Posted on:

Rate this post
0 useful
not useful
Ajay R. wrote:
> Thanks to all of you,its working now :D

Well, you should try to understand what was wrong with your notation in 
order to learn something from your mistake ;).

Best regards,
Karol Babioch

Author: Ajay R. (ajay_r)
Posted on:

Rate this post
0 useful
not useful
Karol Babioch wrote:
> Ajay R. wrote:
>> Thanks to all of you,its working now :D
>
> Well, you should try to understand what was wrong with your notation in
> order to learn something from your mistake ;).
>
> Best regards,
> Karol Babioch

Karol can you name a book or give a link from where i can learn basic 
AVR,I understood what i did wrong but want to further expand my 
knowledge.
Thanks in advance

Author: Lothar Miller (lkmiller) (Moderator)
Posted on:

Rate this post
0 useful
not useful
Karol Babioch wrote:
> you should try to understand
And also you should have learned, that its not enough to say:
Its not working!

Author: Ajay R. (ajay_r)
Posted on:

Rate this post
0 useful
not useful
Lothar Miller wrote:
> Karol Babioch wrote:
>> you should try to understand
> And also you should have learned, that its not enough to say:
> Its not working

Ok,from now on i will be specific about the problem i faced.

Reply

Entering an e-mail address is optional. If you want to receive reply notifications by e-mail, please log in.

Rules — please read before posting

  • Post long source code as attachment, not in the text
  • Posting advertisements is forbidden.

Formatting options

  • [c]C code[/c]
  • [avrasm]AVR assembler code[/avrasm]
  • [code]code in other languages, ASCII drawings[/code]
  • [math]formula (LaTeX syntax)[/math]




Bild automatisch verkleinern, falls nötig
Note: the original post is older than 6 months. Please don't ask any new questions in this thread, but start a new one.