Problem with reading fast signal and outputting signal at high speeds no delay

Hello there,

please would someone be able to explain if possible if there is a solution to what I am trying to achieve.

I am creating a 10us on/off pulse from D13 and am trying to read it on pin A0 and output identical signal on pins A2 and A3. I have the code below however the attach interrupt isnt being called is this due to my loop?

The pulse functions works as expected however nothing else does

#include <avr/io.h>
#include <avr/interrupt.h>
#include <avr/sleep.h>
#define SETBIT(ADDRESS, BIT)   (ADDRESS |= (1 << BIT))
#define CLEARBIT(ADDRESS, BIT) (ADDRESS &= ~(1 << BIT))
#define RX_BLANK_OUT PORTC2
#define TX_BLANK_OUT PORTC3
int sensorPin = A0;  
int sensorValue = 0;  

void setup() {
  DDRC |= _BV (2); // pinMode (A2, OUTPUT); 
  DDRC &= ~_BV (0); // pinMode (A0, INPUT);
  DDRB |= _BV (5); // pinMode (13, OUTPUT); 
  attachInterrupt(digitalPinToInterrupt(sensorPin), rise, RISING);
 // attachInterrupt(digitalPinToInterrupt(sensorPin), fall, FALLING);
}
void loop() {
PULSE();
}

void PULSE()
{
PORTB |= _BV (5); // digitalWrite (13, HIGH);  
delayMicroseconds(10);
PORTB &= ~_BV (5); // digitalWrite (13, LOW);
delayMicroseconds(10);
}
void rise ()
{
  sensorValue = (PINC & _BV (0)) == 0; // digitalRead (A0);
  if (sensorValue == HIGH){
  PORTC |= _BV (2); // digitalWrite (A2, HIGH);
  PORTC |= _BV (3);} // digitalWrite (A3, HIGH);
  else{
  PORTC &= ~_BV (3); // digitalWrite (A3, LOW);
  PORTC &= ~_BV (2);} // digitalWrite (A2, LOW);
  sensorValue = (PINC & _BV (0)) == 0; // digitalRead (A0);
  if (sensorValue == LOW){
  PORTC &= ~_BV (3); // digitalWrite (A3, LOW);
  PORTC &= ~_BV (2);} // digitalWrite (A2, LOW);
  else{
  PORTC |= _BV (2); // digitalWrite (A2, HIGH);
  PORTC |= _BV (3);} // digitalWrite (A3, HIGH);  
}

Any help would be massively appreciated.

Many Thanks, Jake

sketch_jan28b.ino (1.99 KB)

have you tried measuring how long the interrupt takes? needs to be far less than 10 usec if you need to do more than just service the ISR

the attach interrupt isn't being called

int sensorPin = A0; 
attachInterrupt(digitalPinToInterrupt(sensorPin), rise, RISING);

What Arduino are you using, and is A0 an external interrupt pin?

Hi, thanks for having a brief read

I'm using an Arduino Nano so ATMega328 A0 is PCINT8?

I dont know how to measure the interrupt or what you mean by it.

Apologies if I'm asking for a lot here.

Many Thanks,,

Jake

jakeshirley:
I dont know how to measure the interrupt or what you mean by it.

you expect an interrupt every 10us. what if it takes longer than that for an interrupt to be processed?

you could try pulsing at a much slower (1 msec) rate and seeing if that works as you expect and then try faster rates (500, 200, 100, 50, 20) to see at what point things stop working.

you can use micro() to capture timestamps and track the time between measurements. try capturing the max and min deltas and printing them later. do this in the interrupt to get some idea of frequently the interrupts are occurring.

if the interrupt is constantly being triggered the main loop processing may not get a chance to run, so the signal causing the interrupt may need to be disabled to do the prints

hope you understand all that i'm implying.

A nano has two pins (2 and 3) that you can use for interrupts. Your attempt to use A0 is not going to work.