EmbDev.net

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


Author: Ajay R. (ajay_r)
Posted on:

Rate this post
0 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
Author: Jörg Wunsch (dl8dtl) (Moderator)
Posted on:

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

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

Author: Ajay R. (ajay_r)
Posted on:

Rate this post
0 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).

Author: karl heinz (Guest)
Posted on:

Rate this post
1 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.

Author: karl heinz (Guest)
Posted on:

Rate this post
0 useful
not useful
#define F_CPU 1000000UL
#include <avr/io.h>

int main(void) {

    DDRC = 0xFF;
    DDRB = ( 1 << PB0 );   // turn on pullup resistor
                           // to guarante, that the portpin will
                           // give a 1 Bit, even if there ist nothing
                           // connected to it


    PORTC = 0b00000000;

    while (1) {
      if( PINB & ( 1 << PB0 ) )
        PORTC |= ( 1 << PC0 );
      else
        PORTC &= ~( 1 << PC0 );

    }
}

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

Author: karl heinz (Guest)
Posted on:

Rate this post
0 useful
not useful
Sorry. My fault
#define F_CPU 1000000UL
#include <avr/io.h>

int main(void) {

    DDRC = 0xFF;
    PORTB = ( 1 << PB0 );   // turn on pullup resistor
                           // to guarante, that the portpin will
                           // give a 1 Bit, even if there ist nothing
                           // 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.

Author: Ajay R. (ajay_r)
Posted on:

Rate this post
0 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.

Author: karl heinz (Guest)
Posted on:

Rate this post
1 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
     register |= ( 1 << bitnumber );

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

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

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


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

Author: moez mallat (Guest)
Posted on:

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

Author: Vazgen Gabrielyan (viig)
Posted on:

Rate this post
0 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..

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.