I am working on an ARM Cortex-M0+ 48-QFN mc that I have attached a single line from an encoder output to in order to measure the number of rotations the motor has made.
I am very confused as I appear to be able to measure changes using the digitalRead() function on the line I have connected, yet the interrupt function does not appear to be getting triggered at any point. Turning the motor increases the output of hpEncoderPosition but not hpEncoderInt.
Here is the code for reference:
volatile unsigned int hpEncoderPosition = 100;
volatile unsigned int hpEncoderInt = 100;
volatile unsigned short encoderState = 0;
volatile unsigned short encoderState_old = 0;
void interrupt_Test(){
//hpEncoderPosition++;
hpEncoderInt++;
}
void setup() {
// put your setup code here, to run once:
//Serial.println(hpEncoderPosition);
delay(300);
pinMode(9, INPUT);
attachInterrupt(digitalPinToInterrupt(9), interrupt_Test, CHANGE);
}
void loop() {
// put your main code here, to run repeatedly:
encoderState_old = encoderState;
encoderState = digitalRead(9);
if (encoderState != encoderState_old) {
hpEncoderPosition++;
}
Serial.print("No Int:");
Serial.println(hpEncoderPosition);
Serial.print("Int:");
Serial.println(hpEncoderInt);
delay(10);
}
I would appreciate any suggestions to help figure out why the interrupt is not triggering on activity in the connected line.
The exact behavior of the if statement is not important. My issue is that I am able to read changes in the line via the digitalRead() function but attaching an interrupt to the same pin does not trigger the interrupt even when the digital reading is changing.
With no Serial.begin(), I wonder how your code manages to print anything.
void setup() {
// put your setup code here, to run once:
//Serial.println(hpEncoderPosition);
delay(300);
pinMode(9, INPUT);
attachInterrupt(digitalPinToInterrupt(9), interrupt_Test, CHANGE);
}
On an ARM with native USB, there's no UART to be configured for Serial. The only thing Serial.begin() does is check to see if the USB is up yet. And, perhaps, a little delay.
I will try to find the encoder data sheet when I have the time, I don't remember the exact part name off the top of my head. For the schematic, the parts I have described are part of a much more complex system, at the moment I am merely trying to understand why attaching the interrupt to the signal from the encoder is not triggering the interrupt.
I have verified that the signal from the encoder is working via an oscilloscope, and I am able to increment a counter (hpEncoderPosition) by reading the change in states from the encoder signal using digitalRead(). When I turn the motor, hpEncoderPosition increments properly. What I am confused about, is that the interrupt I have attached to the signal on the same port does not fire when the signal changes.
Is it possible that I have to address the pin directly? Could it be possible that Arduino IDE can't interface with the mc properly?