Attiny85: Unable to get working input of *any* kind

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

Hi, I'm a fairly competent developer for POSIX systems, especially with C and C++, but lately I've tried getting into programming some attiny85 chips.

I've tried multiple chips thinking maybe it was a bad one, even reset fuses to default, but no matter what I do, I can't get any input of any kind working, not even pin change interrupt!

I've tried reading the state of PINB, INT0's interrupt, and pin change interrupts, and I can't get ANY of them to do ANYTHING.

I can blink an LED just fine, even a ton of them, but I can't get any button on any pin to do anything of any kind.

I've tried the pull-up resistors method, I've tried tying a button to one pin and the other end of the button to ground, nothing works. I can't even get any kind of response with pin change interrupt!

 

Any ideas? I'm writing for AVR-gcc, no arduino, though I get the feeling I don't need to say that in this forum.

 

Got any code that should absolutely work no matter what? I've tried multiple tutorials for all 3 input methods, nothing.

Last Edited: Tue. Sep 12, 2017 - 08:42 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

Show you code and schematics.
.
MG

I don't know why I'm still doing this hobby

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

So show what you've tried - minimal, complete, compilable code, plus schematic.

 

A photograph of your setup may also help.

 

How to post code & images (eg, your schematic): http://www.avrfreaks.net/comment...

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

For one, I got frustrated and tried the code in this tutorial verbatim along with an exact duplicate of the circuit:

http://www.learningaboutelectron...

 

I applied indentation, but that's all I changed.

Last Edited: Tue. Sep 12, 2017 - 08:52 AM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

How did you build the code - makefile? Atmel/AVR Studio? Other? Versions?

 

How did you load the code into the chip?

 

How did you test that a signal was actually arriving at pin 1

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

Subsentient wrote:
tried the code in this tutorial verbatim
That code is nonsense. It makes the pin with the button an output, and the pin with the LED an input. And the line reading the input is also wrong.

Stefan Ernst

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

Subsentient wrote:
 this tutorial verbatim along with an exact duplicate of the circuit:

http://www.learningaboutelectron...

 

So the circuit is:

ATtiny85 pushbutton circuit

 

But look at the chip pinout:

ATtiny85 pinout

 

PB5 is RESET

 

 

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

So when the button is pressed the led turn off, if button released the led blink?
.
MG

I don't know why I'm still doing this hobby

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

 

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

But DON'T, whatever you do, use your ISP programmer to activate the RSTDISBL fuse because if you do that will almost certainly be the last time you communicate with the tiny85.

 

To be honest this is why these small 8 pin chips are pretty hopeless for learning AVR. You need to keep Reset active and you obviously need Vcc and Gnd so there are only 5 usable pins left.

 

You are far better off getting an Arduino board to start learning AVR - you have 10's of pins to play with on one of those!

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

learningaboutelectronics.com wrote:
When the pushbutton is unpressed, not pressed, the pin is HIGH.

This is because this side is connected to +5V of power. Being that the pushbutton forms an open circuit when it is unpressed,

This is true, but the following explanation is nonsense:

 

all the voltage falls across the resistor. Therefore, the whole voltage of the power supply falls across the resistor. 

 

Because the switch is an open circuit, there is no current flowing. This means that there is no voltage drop across the resistor - hence the input "sees" the full 5V

 

Therefore, when unpressed, the voltage at pin PB5 is +5V.

 

True.

 

Now when the pushbutton is pressed down, the electrical pathway is closed all the way from +5V to ground.

When the pushbutton is closed, it is a short circuit;  so the "bottom" and of the resistor and PB5 are directly connected to ground - and the input "sees" 0V

 

 

Now all of the voltage is not concentrated across the resistor

No!

 

All of the voltage is across the resistor - +5V at the top, and 0V at the bottom!

 

 

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

clawson wrote:
You are far better off getting an Arduino board to start learning AVR

Or get an XPlained-Pro or XPlained-Mini board;  they come complete with debugger - so you can actually see what's going on inside the chip, single-step, etc.

 

http://www.atmel.com/products/mi...

 

eg

 

ATmega324PB Xplained Pro

 

ATmega168 Xplained Mini

ATmega168PB Xplained Mini

ATmega328P Xplained Mini

ATmega328PB Xplained Mini

 

 

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

Also in that tutorial

learningaboutelectronics.com wrote:

ATtiny85 pushbutton breadboard circuit

 

Note that the switch shown has four pins.

 

Often, with this type of switch, pairs of pins are connected together - so it matters which way around you plug it in to the breadboard!

 

 

The tutorial fails to mention anything about switch bounce - which happens not to matter here, but must generally be considered.

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

Subsentient wrote:
Got any code that should absolutely work no matter what?

For that, get an XPlained-Pro or XPlained-Mini board (see #12).

 

These are automatically recognised by Atmel Studio, which will then present you with a list of the examples specifically written for the particular board ...

 

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

awneil wrote:

Note that the switch shown has four pins.

 

Often, with this type of switch, pairs of pins are connected together - so it matters which way around you plug it in to the breadboard!

Yes, a definite possible source of heartache.  OP can use the LED, temporarily connected between pin 1 (anode) and GND (cathode), to see if the switch is wired correctly.  LED should be on until the switch is pressed, when it should go off.  Once confirmed that the switch is doing its job, return the LED to its output function.

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

sternst wrote:

Subsentient wrote:
tried the code in this tutorial verbatim
That code is nonsense. It makes the pin with the button an output, and the pin with the LED an input. And the line reading the input is also wrong.

 

Right on all counts.  Another innocent gets hosed by a garbage tutorial.  To clarify for the OP, a '1' bit in DDRx sets that port bit to output, a '0' (the reset state) sets the port bit to input.  And when reading a single input bit/pin, always mask out that bit with (typically) an and-immediate operation - don't test the entire byte for zero/non-zero.  And also as mentioned, move the input bit off of the RESET pin (and change the code accordingly).

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

Here's one version of the code for main done properly.

 

int main(void)
{

    DDRB= 0b00000001;     // PB0 set to output, all others input

    while(1)
    {
        unsigned char pushbutton = PINB & 0b00010000;    // using PB4 for input

        if (pushbutton)
        {
            PORTB |= 0b00000001;
        }
        else
        {
            PORTB &= ~0b00000001;
        }
    }
}

Notes:

1) DDRB only needs to be set once, not every time through the loop

2) The variable holding PINB should fit the size of PINB, which is 8 bits.  Unsigned char or uint8_t are suitable.

3) Explicitly comparing 'pushbutton' to '0' is superfluous

4) In general you never write over an entire 8 bits to change one output bit.  Get in the habit of reading the current output bits and only changing the bit you want to change, using an OR or AND operation.

 

It is also very common to skip the 'pushbutton' variable and just work on the PINB value directly:

if (PINB & 0b00010000)
{
    something
}
else
{
    something else
}

 

And of course, general programming practice would call for substituting hard-coded numeric values with human-friendly labels such as SWITCH_MASK and LED_MASK.

Last Edited: Tue. Sep 12, 2017 - 04:30 PM
  • 1
  • 2
  • 3
  • 4
  • 5
Total votes: 0

kk6gm wrote:
 Another innocent gets hosed by a garbage tutorial. 

Referring to this: http://www.avrfreaks.net/comment...