Notifications
Clear all

Hardware interrupts

13 Posts
3 Users
0 Reactions
148 Views
 DanB
(@danb)
Member
Joined: 3 weeks ago
Posts: 11
Topic starter  

Hi all

I have been messing around with an Attiny85 for some time.
I have setup the watchdog timer interrupt and it is working super.

Now I am messing around with hardware interrupts.
I have coded my attiny85 and set up the interrupts, pins and the initialization is good, so will not share the code for it here.

It works like:
When I touch the PB4 with GND it changes the state of PB3
When I touch PB2 with GND it changes state on PB0 and PB1 (RED and GREEN led respectively)

BUT
And here comes my problem!
I start out in the setup, setting my PB2 HIGH
but then when I touch the PB4 with the GND wire, 
It also changes the state of PB2, even I DONT TOUCH PB2.

Any idea why that is?

Here is the code that I use to detect which pin caused the interrupt inside the ISR(PCINT0_vect):

    if (!(pinState & (1 << ESPdone))){// This is PB4
      TurnOffESP();  
    }

    // Check if BoatPresent_PIN triggered the interrupt
    if (pinState & (1 <<Present_PIN)){ // This is PB2
      Present = true;
    }else{
      Present = false;
    }

 

Any help appriciated 🙂

 

-Dan


   
Quote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7410
 

@danb That sounds like the underlying hardware has something like a port that has 2 interrupts as in 2 and 4. Check out Nick Gammon's page, he is the authority. Also just google it, if you word the query right you may find the answer. My 'hunch' is this needs low level C involving MASKS and possibly SHIFTS. Good luck.

Here is the link https://www.gammon.com.au/interrupts

There is a discussion of the ATtiny85 there. Just do a FIND on the page, usually Ctl-F for Win, Cmd-F for Macs if it's a chrome browser. May be universal.

This post was modified 1 week ago 2 times by Ron

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7410
 

@danb Did you mean to use the bitwise AND (&) or the logical AND (&&). I am too tired and out of practice to make a quick decision but that is a common error. Since we don't have all the code copied here, it is a bit of a guess on our part.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
noweare
(@noweare)
Member
Joined: 4 years ago
Posts: 128
 

Do your buttons have pullups on them? Could your interrupt be changing states of other pins.

Please post all your code, it will make it easier to help us help you.


   
ReplyQuote
 DanB
(@danb)
Member
Joined: 3 weeks ago
Posts: 11
Topic starter  

@zander

 

Bitwise. 

I am al new to this... So... Bear with me. 

This

if (pinState & (1 <<Present_PIN))

I try to Check if the present_pin is high in the pinState that contains all the pin states of all pins etc. like 00011000

Present_PIN is PB2 so that would Be the third from The right. So in this case. It would Be low and the if evaluate to false. 

Right? 

 

 

-Dan


   
ReplyQuote
 DanB
(@danb)
Member
Joined: 3 weeks ago
Posts: 11
Topic starter  

Yes I use pull_up on the pins.

But I read many many online posts, and found a few that mention that physical pull_down resistors might be the way to go. Any thoughts on that?

 

I realize I should have also added this part of the code,

uint8_t pinState = PINB;
 
This post was modified 1 week ago by DanB

-Dan


   
ReplyQuote
 DanB
(@danb)
Member
Joined: 3 weeks ago
Posts: 11
Topic starter  

@zander I wonder why the need for the "tone"?
Of course I know how to copy/paste and use some common shortkeys.

AND

Of course i did EXTENSIVE searching onine before posting.
And I also did setup the interupt for the pins (same way mentioned in the post you reference).

  //Setup external interrupt
  PCMSK |= (1 << ESPdone);
  PCMSK |= (1 << Present_PIN);
  GIMSK |= (1 << PCIE);

The problem is not that the interrupts dont trigger, the DO
The problem is that I cant seem to differentiate them.
Triggering one pin, also affects the other.

Might be a coding issue, might be hardware, I cant figure it out, and that is why I ask here.
To get advice.

-Dan


   
ReplyQuote
noweare
(@noweare)
Member
Joined: 4 years ago
Posts: 128
 

@danb 

I have had to scroll around to look at different parts of your code to try to figure out what is going on.  

It would make it much easier to help you if you posted all of your code in one place. 

 

Try using PINB in your code and not pinState. Using PINB will always get the current state of that register. pinState only has the state of PINB when you made the assignment. 

 

 


   
ReplyQuote
 DanB
(@danb)
Member
Joined: 3 weeks ago
Posts: 11
Topic starter  

ok I solved it....

 

What I did.

1. I put a 10Kohm pull down resistor on Present_BIN (and of course removed the software pull_up)

2. I changed the code so I use

if ((PINB & (1 <<ESPdone)) == 0){}
and

if (!(PINB & (1 <<Present_PIN)) == 0){}

So I guess what made the change is that now one pin need to go high to activate, and the other need to go low.
or might just be the pull down resistor that did it all. But I dont want to put a pull down on present_pin just to test that.

Thank you for the help here.

Its really not easy when you first get in to C and bit shifting and what not .-)

 

-Dan


   
ReplyQuote
noweare
(@noweare)
Member
Joined: 4 years ago
Posts: 128
 

To work with pullups both statements in your interrupt should of been "Notted" (!).  Then the if statements in the interrupt would be true on a  high to low change.  You would still of needed the PINB change as well. 

 


   
ReplyQuote
Ron
 Ron
(@zander)
Father of a miniature Wookie
Joined: 4 years ago
Posts: 7410
 

@danb It might be interesting to see what the compiler was thinking of the if statement before adding the explicit == 0 test. My thinking is it was NOT boolean, but some sort of bit variable. If it was one bit, then the code may work the same but in this case of multiple (8?) bits, I suspect there is a failure path.

As someone who has been writing code for over half a century, that kind of code still gives me headaches. I usually end up with something that works, but may not be the most elegant.

First computer 1959. Retired from my own computer company 2004.
Hardware - Expert in 1401, and 360, fairly knowledge in PC plus numerous MPU's and MCU's
Major Languages - Machine language, 360 Macro Assembler, Intel Assembler, PL/I and PL1, Pascal, Basic, C plus numerous job control and scripting languages.
My personal scorecard is now 1 PC hardware fix (circa 1982), 1 open source fix (at age 82), and 2 zero day bugs in a major OS.


   
ReplyQuote
 DanB
(@danb)
Member
Joined: 3 weeks ago
Posts: 11
Topic starter  

@noweare i just added pull down to one of the pins.

-Dan


   
ReplyQuote
noweare
(@noweare)
Member
Joined: 4 years ago
Posts: 128
 

@danb 

I know. I was saying that the code will work with both switches pulled up if you use  !   operator in the interrupt.


   
ReplyQuote