blink LED on 4809

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

I have a 4809 based curiosity nano.   I want to blink the user LED.   The hardware data sheet says.

So the LED has an external pull-up.  In order to blink the LED is it better to (explicitly) toggle the DIR bit or the OUT bit?  

 

Here is code toggling the DIR bit.

#ifdef F_CPU
#undef F_CPU
#endif
#define F_CPU 3333333UL

#include <avr/io.h>
#include <util/delay.h>

int main(void) {
  _delay_ms(100);
  
  PORTF.OUTCLR = PIN5_bm;
  while (1) {
    /* LED on 0.5 sec */
    PORTF.DIRSET = PIN5_bm;
    _delay_ms(500);

    /* LED off 0.1 sec */
    PORTF.DIRCLR = PIN5_bm;
    _delay_ms(100);
  }
}

The other choice is to DIRSET once, and then OUTCLR, OUTSET in the loop.

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

I use OUTCLR and OUTSET, leaving the DIRection alone. 

Not sure it really makes a difference though. 

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

The situation I was thinking of if is when the LED and the MCU are source at different voltage levels, say 5 v and 3.3 v, respectively.

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

The circuit does not allow the LED supply voltage to exceed the MCU supply voltage.

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

MattRW wrote:
So the LED has an external pull-up

To be pedantic, it's not a pull-up - it's a series resistor.

 

https://en.wikipedia.org/wiki/Pull-up_resistor

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
Last Edited: Fri. Sep 13, 2019 - 08:12 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

You can simulate an "open collector output" with an AVR by setting the output bit low, and controlling the DIR function of the pin.

When the DIR = input, the pin is at high impedance, without pullup (output bit is low).

When the DIR = output, the pin is to ground, as a NPN transistor conducting to ground.

 

This is specially interesting when you really want an "open collector", not supplying any positive voltage to the load, from the AVR pin.

If the DIR = output and the output port pin is at high level, the pin will supply +Vcc to the load via the pin, killing the "open collector" function.

 

Of course, you can avoid the +Vcc from the pin to reach load, including a series diode (anode to AVR) and using the pin as output no matter the level, but it will require an extra diode.

 

So, playing with the DIR register you can create a three state on the I/O pin;  Ground, VCC and High Impedance.

Sometimes it can help when dealing with keyboard or LED matrix.

 

Wagner Lipnharski
Orlando Florida USA

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

wagnerlip wrote:
You can simulate an "open collector output" with an AVR

Indeed.

 

And not just an AVR - the trick can often be used on other devices to get a "hi-Z" state when the chip doesn't directly offer it ...

Top Tips:

  1. How to properly post source code - see: https://www.avrfreaks.net/comment... - also how to properly include images/pictures
  2. "Garbage" characters on a serial terminal are (almost?) invariably due to wrong baud rate - see: https://learn.sparkfun.com/tutorials/serial-communication
  3. Wrong baud rate is usually due to not running at the speed you thought; check by blinking a LED to see if you get the speed you expected
  4. Difference between a crystal, and a crystal oscillatorhttps://www.avrfreaks.net/comment...
  5. When your question is resolved, mark the solution: https://www.avrfreaks.net/comment...
  6. Beginner's "Getting Started" tips: https://www.avrfreaks.net/comment...
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 1

You can simulate an "open collector output" with [any chip having a "DIRECTION" register]

Yes, but you have to remember that you don't get the "high voltage" OC behavior that you used to get with old TTL chips (30V on a 7407?)
You can't use the trick to (for example) drive a mosfet gate pulled up externally to 10V from an AVR output...