Pin change interrupt on Attiny85

Hello,
I am trying to implement the pin change interrupt for reading the PWM signal. I started from the example on this webpage. Here's the code:

#include <PinChangeInt.h>
 
#define MY_PIN 5 // we could choose any pin
 
volatile int pwm_value = 0;
volatile int prev_time = 0;
uint8_t latest_interrupted_pin;
 
void rising()
{
  latest_interrupted_pin=PCintPort::arduinoPin;
  PCintPort::attachInterrupt(latest_interrupted_pin, &falling, FALLING);
  prev_time = micros();
}
 
void falling() {
  latest_interrupted_pin=PCintPort::arduinoPin;
  PCintPort::attachInterrupt(latest_interrupted_pin, &rising, RISING);
  pwm_value = micros()-prev_time;
//  Serial.println(state);
  Serial.println("pwm_value");
}
 
void setup() {
  pinMode(MY_PIN, INPUT); digitalWrite(MY_PIN, HIGH);
  Serial.begin(115200);
  PCintPort::attachInterrupt(MY_PIN, &rising, RISING);
}
 
void loop() { }

When compiled on Arduino Uno, the code works. However, the code does not compile on Attiny85 - it complains about the PCintPort command. Is there a simple way to solve this problem? Maybe another library that works better? I need the pin change interrupt since the only external interrupt pin of Attiny85 is used for another purpose.

1 Like

That library is so old, the example sketches still have the .pde extension! I would try the PinChangeInterrupt library which you can install from the IDE Library Manager (Tools->Manage libraries...).

Here is your sketch modified for the newer library, I don't know if it will work on an ATtiny85 but it does compile without error using the ATtinyCore.

#include <PinChangeInterrupt.h>

#define MY_PIN 5 // we could choose any pin

volatile int pwm_value = 0;
volatile int rising_time = 0;
uint8_t latest_interrupted_pin;

void rising()
{
  attachInterrupt(MY_PIN, &falling, FALLING);
  rising_time = micros();
}

void falling()
{
  attachPinChangeInterrupt(MY_PIN, &rising, RISING);
  pwm_value = micros() - rising_time;
  Serial.println("pwm_value");
}

void setup()
{
  pinMode(MY_PIN, INPUT); digitalWrite(MY_PIN, HIGH);
  Serial.begin(115200);
  attachPinChangeInterrupt(MY_PIN, &rising, RISING);
}

void loop() { }

Thank you!
I will test this code tomorrow. I modified it slightly, in order to calculate the off-time and the duty cycle:

#include <PinChangeInterrupt.h>

#define MY_PIN 5 // we could choose any pin

volatile float pwm_on_value = 0;
volatile float pwm_off_value = 0;
volatile float duty_cycle = 0;
volatile int rising_time = 0;
volatile int falling_time = 0;
uint8_t latest_interrupted_pin;

void rising()
{
  attachInterrupt(MY_PIN, &falling, FALLING);
  rising_time = micros();
  pwm_off_value = prev_time - falling_time;
  Serial.print("pwm_off_value = ");
  Serial.println(pwm_off_value);
}

void falling()
{
  attachPinChangeInterrupt(MY_PIN, &rising, RISING);
  pwm_on_value = micros() - rising_time;
  Serial.print("pwm_on_value = ");
  Serial.println(pwm_on_value);
  falling_time = micros();
  duty_cycle = pwm_on_value * (1.0/(pwm_on_value + pwm_off_value));
  Serial.print("duty cycle = ");
  Serial.println(duty_cycle);
}

void setup()
{
  pinMode(MY_PIN, INPUT); digitalWrite(MY_PIN, HIGH);
  Serial.begin(115200);
  attachPinChangeInterrupt(MY_PIN, &rising, RISING);
}

void loop() { }

Not sure whether I calculate the off-time and the duty cycle correctly.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.