inverse logic why is it needed

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

Hi folks I'm new to AVR, and microproccors, and I was wondering why inverse logic is used?

For me On is 1 HIGH or 5v or what ever VCC is and 0 or Off is 0 zero volts, but I came across this beginners example.

The program flashes a led on and off.

 /* led on, pin=0 */

        PORTD &= ~(1<<PD5);

         delay_ms(500);
		  

	
	/* set output to 5V, LED off */

         PORTD|= (1<<PD5); 

        delay_ms(500);

My setup is Pin PD5 to 1k current limiting resistor to led then to GND.

The program runs fine, but the inverse logic bugs me. Seems to be a layer of added strangeness.

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

I have not checked but expect that the demo code is for a specific board that have it that way!

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

It goes back to the time when sourcing enough current out of a semiconductor was hard or impossible, but sinking enough current was easy.

There is no reason to do that with AVRs anymore.

However pushbuttons are simpler to have 5V when not pushed and 0V when pushed, because AVRs have internal pull-up resistors for that. Some other microcontrollers have also pull-down resistors so again then there is no point doing the inverse logic.

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

Most electronic chips are happier sinking current rather than sourcing current.

The STK500 has active-low LEDs.
The STK500 has active-low push-button switches.

Look at most integrated circuits. They tend to have 'active-low' /CS (chip-select) pins.

I quite agree. It is not intuitive to human beings. You just have to live with it.

There are two approaches.
1. Write macros for LED_ON() and LED_OFF(). Then your program always reads as common sense to humans.

2. Write all your logic as positive. Then at the very last point where you write to the port --- flip the bits. e.g.

PORTB = result ^ 0xFF;  // flip every bit

You do the same with input pins. For very good electrical reasons, you normally connect switches to 0V. So when you push a button that 'bit' reads as 0V.

    switch (PIND ^ 0xFF) {  // flip all the bits
        case 0b00000100: ...
        case 0b00000010: ...
        case 0b00010000: ...

You will find lots of code where people go to great lengths to preserve the 'inverse-logic'. It does my head in. But if that is what they want to do, you cannot argue with them.

David.

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

If you use discrete transistors you get NPN and PNP
resp N-Channel or P-channel. The NPN (N channel) need
a pos. voltage on the base to turn on (conduct), so
"active High", the PNP (P-channel) need a negative
voltage, so sort of "active LOW".

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

Thanks everyone for making sense of all the strangeness. I will need to experiment further and see how all of this applies.

Cheers
Rich

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

As the others say , it depends on how the "LED" is wired.
If the led is wired to VCC , and an avr pin , then the avr must supply GND for the led to lit up.
If the led is wired to GND , and an avr pin , then the avr must supply VCC for the led to lit up.

Afaik the old TTL chips could sink (to GND) , approx 10 TTL loads , but it couldn't source (to VCC) much. And there was pull-up's everywhere. The same goes for the old MCU's , and thats why you see many old schematics switching things to GND.

/Bingo

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

I think in railway brake system its
pressure-off = active
so that it gets fail safe. So sometimes the application
makes you think that way.

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

Jepael wrote:
However pushbuttons are simpler to have 5V when not pushed and 0V when pushed, because AVRs have internal pull-up resistors for that. Some other microcontrollers have also pull-down resistors so again then there is no point doing the inverse logic.
There's a second very good reason for this with "normally open" pushbuttons: an input on an AVR (or any CMOS chip) has an incredibly high impedance (resistance), so if it is not actively pulled up or down, it will "float". And "float" is bad -- the pin picks up all sorts of stray voltages: the microwave cooking your popcorn, the AM radio station over the hill, the computer you're using to debug code, the electric clock on the shelf, and so on. The AVR will see the "switch" mysteriously turning on and off randomly. Not good at all.

You can either use a pull-up resistor or a pull-down resistor, but you should make sure the pin does not float. Since the AVR has a built-in pull-up that can be enabled by the program, most designers use the pull-up and have the switch pull the signal to ground.

Stu

Engineering seems to boil down to: Cheap. Fast. Good. Choose two. Sometimes choose only one.

Newbie? Be sure to read the thread Newbie? Start here!

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

I'm sure the question has already been answered so I'm going to facetiously suggest "it's needed when talking to women".

:P

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

Another reason would be to make dual use of the lines. You can interface a keypad and an LCD interface to the same 8 bit port. Normally the LCD would be updated after reading a keypress so you could write your code to make dual use of the port. In this case a pull down operation of the switches will not interfere with the LCD (if resistors are put in series with the switches).

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

kscharf wrote:
Another reason would be to make dual use of the lines. You can interface a keypad and an LCD interface to the same 8 bit port. Normally the LCD would be updated after reading a keypress so you could write your code to make dual use of the port. In this case a pull down operation of the switches will not interfere with the LCD (if resistors are put in series with the switches).

I don't understand why dual use of pins is only possible with inverse logic. It is just as possible to share the pins with positive logic.

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

My argument was based on the high current sink that the AVR processor can provide, which most devices (such as LCD's) cannot do.