Read Input with ATMEGA16: can you send me sample code?

Go To Last Post
9 posts / 0 new
Author
Message
#1
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Dear Friends

can you post here "very simply sample code" already tested for read input on ATmega16?

I would read active High signal and active Low signal

Thanks

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0
x=PINB;
if (x & (1<<some_bit)) {
  // do active high stuff
}
if (!(x & (1<<some_bit))) {
  // do active low stuff
}

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

#include
//What I would do: with external wire connect on D.7 I would turn on
//all led of Port C when I touch A.1 pin with wire

int main(void)
{

char x;

DDRA = 0b00000000; //Input where I read

DDRC = 0b11111111; //Output High if read was succesfull

DDRD = 0b10000000; //one output = signal source
PORTD = 0b10000000; //D.7 High = signal source

x=PINA;

if (x & (0b0000001))
{
PORTC = 0b11111111;
}

return(0);
}

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

your code:

if (x & (0b0000001))
{
PORTC = 0b11111111;
} 

Generally it is easier to connect your LEDs via current limiting resistors to 5V and the cathodes to the Port output pins. So you light the LED when the pins is driven low. e.g. PORTC = 0b00000000;

You need to test (x & 0b00000010) for PINA.1 because port bits start at 0. I would suggest a resistor between PORTD.7 and PINA.1 in case you ever inadvertently have both ports as outputs. You will detect the switch between PORTD.7 and PINA.1 when PINA.1 == 1 as you have written.

It is even simpler to just have your switch between PINA.1 and 0V and use the internal pullup on PINA.1
e.g.

DDRA.1 = 0; // make PINA.1 input
PORTA.1 = 1; // enable pullup
if (PINA.1 == 0) { ... }; // read the pin and act accordingly.

You also need to place your code within a while() loop because main() never returns.

I have used the notation PORTD.7 = 1 etc. Standard C syntax would be for example: PORTD |= (1<<7);

If your compiler supports this syntax I would advise using it for clarity. When you have more experience you can use macros instead.

HTH David

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Dear Friends

thanks for help.
Here the working sample code:

#include
//What I would do: with external wire connect on D.7 I would turn on
//all led of Port C when I touch with wire A.1 pin

int main(void)
{

char x;

DDRA = 0b00000000; //Input where I read

DDRC = 0b11111111; //Output High if read was succesfull

DDRD = 0b10000000; //one output = signal source
PORTD = 0b10000000; //D.7 High = signal source
while(1)
{
x=PINA;

if (x & (0b00000001))
{
PORTC = 0b11111111;
}
}

return(0);

}

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Use a loop in main. Dont return. There is nowhere to return to.

Imagecraft compiler user

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Jean Paul Sartre: No Exit
Bob Gardner (C. Lawson, collab): No Return

Chuck Baird

"I wish I were dumber so I could be more certain about my opinions. It looks fun." -- Scott Adams

http://www.cbaird.org

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:

Bob Gardner (C. Lawson, collab): No Return

No Deposit, either, but please recycle and don't litter.

You can put lipstick on a pig, but it is still a pig.

I've never met a pig I didn't like, as long as you have some salt and pepper.

  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Just to say that he DOES have a loop in main(). Just have a look of while(1) in there. But the lack of code tags (and hence any kind of indentation) makes it almost impossible to see this!

Cliff

PS It was the Eagles who said "you can check-out any time, but you can never leave" by the way.