EmbDev.net

Forum: µC & Digital Electronics ATMEGA8 unable to take input


von Ajay R. (ajay_r)


Rate this post
useful
not useful
I am working on atmega 8 .I am new to it ,and making a program that will 
glow a led on one pin depending on the input in another pin ie if input 
is 5v led will glow otherwise it will not.But i guess something is wrong 
with my program.

#define F_CPU 1000000UL

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

int main (void)
{DDRB=0x03;
PORTB=0x00;
while(1)
{
if(PINB && 0b00000001)
PORTB=0x02;
else
PORTB=0x00;
}}

: Moved by Moderator
von Jörg W. (dl8dtl) (Moderator)


Rate this post
useful
not useful
“&&” is a logical AND.

What you (probably) want is a bitwise AND though, which is “&”.

von Ajay R. (ajay_r)


Rate this post
useful
not useful
After few days i was able to overcome the problem in the code,my new 
code:
#define F_CPU 1000000UL

 #include <avr/io.h>


int main(void) {


    DDRC = 0xFF;


    DDRB = 0;


    PORTB = 0;

  PORTC=0b00000000;

    while (1) {


if(PINB==0b00000001)
        PORTC = 0b00000001;
    else
    PORTC=0;

    }
}
But still there is some problem,the led is turning off when i connect 
the input to ground but when the input is not grounded the led glows and 
when input is connected to 5v led glows but slight brightly.I want to 
know why is led not turning off completely when there is no 
input(neither ground nor 5v).

von karl heinz (Guest)


Rate this post
useful
not useful
because it is a common misconception, that an input pin, which is not 
connected to anything, gives a value of 0.

In fact, that pin is nothing more then an antenna, which reacts on any 
electromagnetic field nearby. And there are lots of them. Eg all the 
power cords in the wall, your mobile phone, ther µc itself, etc. etc.

Exactly this is, why we use pullup or pulldown resistors, which 
guarantee a specific value on an input portpin, even if the usual switch 
is not closed.

von karl heinz (Guest)


Rate this post
useful
not useful
1
#define F_CPU 1000000UL
2
#include <avr/io.h>
3
4
int main(void) {
5
6
    DDRC = 0xFF;
7
    DDRB = ( 1 << PB0 );   // turn on pullup resistor
8
                           // to guarante, that the portpin will
9
                           // give a 1 Bit, even if there ist nothing
10
                           // connected to it
11
12
13
    PORTC = 0b00000000;
14
15
    while (1) {
16
      if( PINB & ( 1 << PB0 ) )
17
        PORTC |= ( 1 << PC0 );
18
      else
19
        PORTC &= ~( 1 << PC0 );
20
21
    }
22
}

and familiarize yourself with C-bitoperations. They are your 'working 
horse' in µC-programing!

von karl heinz (Guest)


Rate this post
useful
not useful
Sorry. My fault
1
#define F_CPU 1000000UL
2
#include <avr/io.h>
3
4
int main(void) {
5
6
    DDRC = 0xFF;
7
    PORTB = ( 1 << PB0 );   // turn on pullup resistor
8
                           // to guarante, that the portpin will
9
                           // give a 1 Bit, even if there ist nothing
10
                           // connected to it

you need to set a 1 Bit in the PORTx Register, to turn on the pullup 
resistor for that pin, if the pin is an input pin.

von Ajay R. (ajay_r)


Rate this post
useful
not useful
Thanks...can you suggest me a source from where i can learn basic c-bit 
operations,i know how to use them but find it little confusing when 
using it with avr.

von karl heinz (Guest)


Rate this post
useful
not useful
Well. Any C Progamming book should do.

Unfortunately I don't know any english written Tutorial for the topic of 
bit-operations.

In a Nutshell:
to turn a bit to 1
1
     register |= ( 1 << bitnumber );

to turn a bit to 0
1
     register &= ~( 1 << bitnumber );

to check if a bit is 1
1
   if( register & ( 1 << bitnumber ) )

to check if a bit is 0
1
   if( !(register & ( 1 << bitnumber ) )


look up the operators & | << >> and ~ in the C textbook of your choice.

von moez mallat (Guest)


Rate this post
useful
not useful
S'il vous plaît entrer un texte plus long.

von Vazgen G. (viig)


Rate this post
useful
not useful
If you know russian i can send you a very detailed tutorial in russian 
about the avr and really simple examples and how to use them..

Please log in before posting. Registration is free and takes only a minute.
Existing account
Do you have a Google/GoogleMail account? No registration required!
Log in with Google account
No account? Register here.