Author Topic: struggling with ISR on digital pins  (Read 6151 times)

postuma

  • Jr. Member
  • **
  • Posts: 64
struggling with ISR on digital pins
« on: January 10, 2016, 05:28:59 pm »
Trying to get a bike computer working: two reed switches, one for pedal rotations, one for wheel rotations. Tried without ISRs - simple loop polling for digitalRead on pins 2 and 3 - works except it misses rotations when the wheel spins too fast.

Initial attempt w ISRs:

Code: [Select]
volatile unsigned int pedalCount = 0;
volatile unsigned long wheelCount = 0;

void setup() {
  pinMode(2, INPUT);
  pinMode(3, INPUT);
  attachInterrupt(2, incPedal, RISING);
  attachInterrupt(3, incWheel, RISING);
  ...

void incPedal() {
    pedalCount++;
}
void incWheel() {
  wheelCount++;
}

... which does not work, in part because pin interrupts use different pin definitions than Digispark's pinout, but if there's an easy fix?

tried Arduino's attachInterrupt(digitalPinToInterrupt(pin), blink, CHANGE); but the digitalPinToInterrupt function is not supported by Digispark.

tried to use TinyPinChange:

Code: [Select]
#define FIRST_INPUT         2
#define SECOND_INPUT        3

volatile uint16_t FirstInputChangeCount = 0;
volatile uint16_t SecondInputChangeCount = 0;

uint8_t VirtualPortNb;
uint8_t VirtualPortNb_;

void setup()
{
  TinyPinChange_Init();
  VirtualPortNb  = TinyPinChange_RegisterIsr(FIRST_INPUT,  InterruptFunctionToCall);
  VirtualPortNb_ = TinyPinChange_RegisterIsr(SECOND_INPUT, InterruptFunctionToCall);
  TinyPinChange_EnablePin(FIRST_INPUT);
  TinyPinChange_EnablePin(SECOND_INPUT);

etc ...

which compiles fine, but InterruptFunction never gets called. Anyone know what I'm doing wrong?

My apologies if this is too basic. I am new to this. I did read all the forum posts on this subject, read Arduino's documentation and Nick Gammon's treatise and more before posting, and tried the TinyPinChange and PinChangeInterrupt libraries. Still stuck.

Thanks very much

postuma

  • Jr. Member
  • **
  • Posts: 64
Re: struggling with ISR on digital pins
« Reply #1 on: January 11, 2016, 05:24:01 pm »
I think I got it ... don't have time to test but will post the code once I've had a chance.  :D

For those who are interested, found https://www.insidegadgets.com/2011/02/27/how-to-use-the-pin-change-interrupt-on-attiny85/ and http://www.atmel.com/Images/Atmel-8265-8-bit-AVR-Microcontroller-tinyAVR-ATtiny87-ATtiny167_datasheet.pdf quite handy (latter esp. page 12 and section 7).

postuma

  • Jr. Member
  • **
  • Posts: 64
Re: struggling with ISR on digital pins
« Reply #2 on: January 17, 2016, 03:54:06 pm »
it's working! Mucking about with the various Attiny registers directly proved a pain. Went back to the TinyPinChange library as posted above and it works very neatly - got two discrete ISRs running independently off pins 2 and 3, without problems.

My initial mistake was attempting to modify/tweak the source code for this library before getting it to work properly. Should have known better.

postuma

  • Jr. Member
  • **
  • Posts: 64
Re: struggling with ISR on digital pins
« Reply #3 on: January 17, 2016, 06:04:51 pm »