PORT C as digital inputs

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

Hi all,

I have been usig timers, the UART, ADC ports of my ATMEGA16 successfully for quite a while now. I now want to use digital inputs for the first time and am failing. I studied the datasheet but I can't figure out what I am doing wrong.

I am doing

        cbi(DDRC,PC1);                  /* digital input */
        sbi(DDRB,PB5);                  /* digital output */

        while (1)                       /* loop forever */
        { 
         if ((PORTC & _BV(PC1)) > 0) sbi(PORTB, PB5);
         else cbi(PORTB, PB5);
        }

and would like to see the PB5 LED of my Arduino board to glow when I touch PC1 with a 5V wire. But this does not happen. THE LED is OK (I can switch it on programmatically). I tried other pins (ports) as well as input with the same result. I am putting a TTL signal on the input pins, so I don't need no internal pull-up. If I do

sbi(PORTC,PC1); /* pull up*/

to activate the internal pull-up, reading the pin gives me a high although I am dragging down the pin externally to ground!??

What am I missing?

Thanks a lot,

Andreas

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

Use internal pull-up on PC1 & connect a switch between PC1 and ground.

DDRB=(1<<PB5);   //output
PORTC=(1<<PC1);  //input with internal pull-up enabled
while(1)
{
   if (PINC&(1<<PC1)==0)
      PORTB|=(1<<PB5);
   else
      PORTB&=~(1<<PB5);
)

By and large it is safer to ground things than to connect them to Vcc. This is probably why ATMEL have internal pull-ups and not pull-downs.

Thanks Stefan...PORTC changed to PINC :oops:

Charles Darwin, Lord Kelvin & Murphy are always lurking about!
Lee -.-
Riddle me this...How did the serpent move around before the fall?

Last Edited: Sat. Jul 24, 2010 - 11:56 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Quote:
This is probably why ATMEL have internal pull-ups and not pull-downs.
..apart from the Xmegas. :wink:

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

And for reading the input use PINC (not PORTC).

Stefan Ernst

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

^ You found Waldo!

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

Where X is a has been or an unknown quantity! :)

Charles Darwin, Lord Kelvin & Murphy are always lurking about!
Lee -.-
Riddle me this...How did the serpent move around before the fall?

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

The one with the X makes an excellent conroller for a new and improved mouse trap. :)

John Samperi

Ampertronics Pty. Ltd.

www.ampertronics.com.au

* Electronic Design * Custom Products * Contract Assembly

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

Hi Stefan and all,

Thanks for your helpful comments!

sternst wrote:
And for reading the input use PINC (not PORTC).

That was the problem! It's working now!

Thanks a lot,

Andreas