ATTINY416-XNANO Pullup

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

Dear Valued Members, 

 

for some tasks I need small MCUs, and the tinyAVR® 1-Series seems to be quite suitable.

 

But unexpected a curious occurrence: after going to sleep the pullup is disconnected (detected with a multimeter).

 

Narrowed code example 
(after button pressed und released 5x blink and go to sleep again):

/* ------------------------------------------
ATTINY416-XNANO
LED = PB5, active LOW
Button = PB4, pressed = GND
-------------------------------------------- */

#include <avr/io.h>
#define F_CPU 3333333				// 20MHz/6
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>

uint8_t count = 0;				// cycle count

ISR(PORTB_PORT_vect) {
  PORTB.INTFLAGS = PORT_INT4_bm;		// clear the interrupt flag
}

int main( void )
{
  PORTB.PIN4CTRL = PORT_PULLUPEN_bm;		// PB4 pullup
  PORTB.OUTSET = PIN5_bm;			// PB5 HIGH, LED off
  PORTB.DIRSET = PIN5_bm;			// PB5 as output
  SLPCTRL.CTRLA = SLPCTRL_SMODE_PDOWN_gc;	// select Power-Down sleep mode
  sei();					// enable global interrupts
  while(1){
    while(!(PORTB.IN & PIN4_bm)) {}		// wait while button pressed (LOW)
    PORTB.OUTCLR = PIN5_bm;			// PB5 LOW, LED on
    _delay_ms(100);
    PORTB.OUTSET = PIN5_bm;			// PB5 HIGH, LED off
    _delay_ms(400);
    count++;
    if(count == 5) {
      count = 0;
      PORTB.DIRCLR = PIN5_bm;			// PB5 turned input
      PORTB.PIN4CTRL = PORT_ISC_LEVEL_gc;	// LOW level interrupt on PB4
      sleep_mode();
      PORTB.PIN4CTRL = PORT_ISC_INTDISABLE_gc;	// disable interrupt on PB4
      PORTB.DIRSET = PIN5_bm;			// PB5 turned output
    }
  }
}

 

Does anybody have an ATTINY416-XNANO? Please find out my code error.

 

 

The truth is more important than the facts.

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

Doesn't this line turn off your pull-up?

 

PORTB.PIN4CTRL = PORT_ISC_LEVEL_gc;	// LOW level interrupt on PB4

 

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

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

Brian Fairchild wrote:

Doesn't this line turn off your pull-up?

PORTB.PIN4CTRL = PORT_ISC_LEVEL_gc;	// LOW level interrupt on PB4

 

Obviously. And what would be the correct code?

 

The truth is more important than the facts.

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

poftamunk wrote:

Obviously. And what would be the correct code?

 

Well, since you want to 'add' in the bits needed for pull-up and interrupt you want something like...

 

PORTB.PIN4CTRL = PORT_PULLUPEN_bm | PORT_ISC_LEVEL_gc;		// PB4 pullup and LOW level interrupt on PB4

 

So you are 'or-ing' the bits together.

#1 Hardware Problem? https://www.avrfreaks.net/forum/...

#2 Hardware Problem? Read AVR042.

#3 All grounds are not created equal

#4 Have you proved your chip is running at xxMHz?

#5 "If you think you need floating point to solve the problem then you don't understand the problem. If you really do need floating point then you have a problem you do not understand."

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

poftamunk wrote:
what would be the correct code?

Something that would enable the low-level interrupt and the pullup

 

ie, set bits 3, 2, and 0

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: 0

Brian Fairchild wrote:

 

Well, since you want to 'add' in the bits needed for pull-up and interrupt you want something like...

PORTB.PIN4CTRL = PORT_PULLUPEN_bm | PORT_ISC_LEVEL_gc;		// PB4 pullup and LOW level interrupt on PB4

So you are 'or-ing' the bits together.

 

awneil wrote:

Something that would enable the low-level interrupt and the pullup

 

ie, set bits 3, 2, and 0

 

 

Thank you so much! The problem is solved. The PULLUPEN Bit must be confirmed again and again.

. . . . . .
PORTB.PIN4CTRL = PORT_PULLUPEN_bm | PORT_ISC_LEVEL_gc;		// PB4 pullup and LOW level interrupt on PB4
sleep_mode();
PORTB.PIN4CTRL = PORT_PULLUPEN_bm | PORT_ISC_INTDISABLE_gc;	// PB4 pullup and disable interrupt on PB4
. . . . . .

 

But it is not very convenient. If I may mention it.

 

 

The truth is more important than the facts.

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

Time to look into the |= operator,  then ... ?

 

https://www.avrfreaks.net/forum/tut-c-bit-manipulation-aka-programming-101

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: Thu. May 21, 2020 - 03:13 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

poftamunk wrote:

 | PORT_ISC_INTDISABLE_gc

Since PORT_ISC_INTDISABLE_gc = (0x00<<0), ORing anything with 0 has no effect.

Of course the compiler will just treat it as

PORTB.PIN4CTRL = PORT_PULLUPEN_bm;

 

David