ATMEGA8 unable to take input

OP #3510364
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;
}}
OP #3519138
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).
Guest #3519146
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.
Guest #3519151
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!
Guest #3519161
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.
Guest #3519170
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.

Reply

Please log in before posting.

or

Log in with Google account

Registration is free and takes only a minute.

Register now