issues with pullup resistors and if statements 2313 [solved]

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

ive been attempting to teach myself about the pull-up resistors on the attiny2313.

ive successfully activated the resistors on all of portB, i have LEDs on all of those ports(to see if the resistors are actually on) and they glow slightly.

after removing the LED on PINB0.

my next attempt was to trigger an led on portD (using an if statement) by looking at PINB0 and activating PORTD when PINB0 went low.(by connecting the PINB0 directly to GND)
i can not get this to work and i have tried:

if (PINB0 == 0)
if (PINB0 == 1)
if (PINB0 <= 1)
if (PINB0 >= 0)

i even tried setting PINB0 to a variable X and compared using that. nothing seems to work... (as seen in my code below)

am i doing something wrong?
is there a better way to do this?

here is my code ive been toying with.

#include 

int main(void)
{
MCUCR = 0x00;	
DDRB = 0x00;
DDRD = 0xFF;
PORTB = 0xFF;	
unsigned char x;	
    while(1)
    {x = PINB1; 
		if (x >= 0x00 )
		{
			PORTD = 0xFF;	
		} else {
			PORTD = 0x00; 
		}

    }
}
Last Edited: Sun. Dec 8, 2013 - 09:14 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

PINB0 is a simple number.

The input port register is called PINB.
If you want to see a single port bit, you do:

     button = PINB & (1<<PB3);     // read pin PB3

This will contain 0x08 (1<<3) when the button is up. And 0 when the button is down.

David.

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

thanks so much, ive been trying to figure this out for days, embarrassingly.

so when would you call " PINBx " what do you mean by

david.prentice wrote:
PINB0 is a simple number.

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

theycallmeshua wrote:

so when would you call " PINBx " what do you mean by
david.prentice wrote:
PINB0 is a simple number.

If you open the 2313 header you will see that what you are using is defined as

/* Input Pins, Port B */
#define    PINB7   7
#define    PINB6   6
#define    PINB5   5
#define    PINB4   4
#define    PINB3   3
#define    PINB2   2
#define    PINB1   1
#define    PINB0   0

PINNB0/1/2... are defined as plain numbers

When you use

button = PINB & (1<<PB3);

it's the same as

button = PINB & (1<<3);

You can use either coding style

Alex

"For every effect there is a root cause. Find and address the root cause rather than try to fix the effect, as there is no end to the latter."
Author Unknown