////////////////////////////////////////////////////////////////////
// Author: RSP @KMUTNB
// Date: 2022-02-08
// File: nano_pwm_loopback_interrupt_demo.ino
////////////////////////////////////////////////////////////////////
// In order to the the code with the Arduino Nano board,
// connect the Arduino D2 and P5 pins together via a jumper wire.
#define PULSE_IN_PIN (2) // use Arduino D2 pin as digital input
#define PWM_OUT_PIN (5) // use Arduino D5 pin as PWM output
void setup() {
pinMode( PULSE_IN_PIN, INPUT );
pinMode( LED_BUILTIN, OUTPUT ); // use onboard LED pin as output
attachInterrupt( digitalPinToInterrupt(PULSE_IN_PIN), isr, CHANGE );
analogWrite( PWM_OUT_PIN, 2 ); // 2/256*1024 = 8 usec pulse width
}
void loop() {
}
#ifdef USE_SFR
void isr() {
PINB = _BV(PINB5);
}
#else
void isr() {
uint8_t new_value = digitalRead( PULSE_IN_PIN );
// use the new logic value for output
digitalWrite( LED_BUILTIN, new_value );
}
#endif
////////////////////////////////////////////////////////////////////