Hi
I am trying to read values from 2 separate DRO scales ( Digital Read Out ), most commonly used on lathe and milling equipment, CNC, or any X,Y,Z table
These scales need to use 2 interrupt pins per device, due to using pulses for the measurement data. These scales are incremental only.
To get around only having 2 interupt pins ( D2, D3) I want to use PinChange interrupts on 2 other pins
The code I have so far compiles and serial mon displays the measurements from pin D2,D3, but I get nothing from pin D4,D5
I am wanting to know how to invoke PinChange interupts on D4,D5 to read the data from the scale
I dont want to buy a different board with more interrupt pins as I have a supply on NANO's that I want to use, and will fit in the space available.
/*
Pins require interrups to read scale value. Scale is incremental only
pinA, pinB are for dro scale 1
pinC, pinD1 are for dro scale 2
D2 = pinA - Hardware Interrupt - Scale 1 clk
D3 = pinB - Hardware Interrupt - scale 1 data
D4 = pinC - Pinchange Interrupt - scale 2 clk
D5 = pinD1 -Pinchange Interrupt - scale 2 data
*/
const int pinA = 2, pinB = 3, phaseLookup1[] = {0,-1,1,-1,1,0,-1,-1,-1,1,0,1,1,1,-1,0};
const int pinC = 4, pinD1 = 5, phaseLookup2[] = {0,-1,1,-1,1,0,-1,-1,-1,1,0,1,1,1,-1,0}; // seperat lookup tables for each scale
volatile bool A = false, B = false,C = false, D1 = false, updated = false;
volatile int scale1 = 0, phase1 = 0, scale2 = 0, phase2 = 0;
void setup()
{
Serial.begin(9600);
Serial.println("T.I.S.M");// Goolge it
pinMode(pinA, INPUT);
pinMode(pinB, INPUT);
pinMode(pinC, INPUT);
pinMode(pinD1, INPUT);
PCICR |= B00000100;// Enable PCIE2 Bit3 = 1 (Port D)
PCMSK2 |= B00110000;// Enable PCINT20 & PCINT21 (Pins D4 & D5)
//ISR (PCINT2_vect);
attachInterrupt(digitalPinToInterrupt(pinA), trig, CHANGE);
attachInterrupt(digitalPinToInterrupt(pinB), trig, CHANGE);
attachInterrupt(digitalPinToInterrupt(pinC), trig, CHANGE);
attachInterrupt(digitalPinToInterrupt(pinD1), trig, CHANGE);
}
ISR (PCINT2_vect);
void loop()
//
{
if (updated)
{
Serial.println(String(scale1));
Serial.println(String(scale2));
updated = false;
}
}
void trig()
{
A = digitalRead(pinA);//Scale 1
B = digitalRead(pinB);// "
C = digitalRead(pinC);// Scale 2
D1 = digitalRead(pinD1);// "
phase1 <<= 1;//Scale 1
phase1 |= A;
phase1 <<= 1;
phase1 |= B;
phase1 &= 0xF;
phase2 <<= 1;//Scale 2
phase2 |= C;
phase2 <<= 1;
phase2 |= D1;
phase2 &= 0xF;
scale1 += phaseLookup1[phase1];
scale2 += phaseLookup2[phase2];
updated = true;
}