DUE CANTx pin weird behavior

Hello, I'm experiencing a weird behavior in the CANTx pin.

I have two buttons attached to two pins, in this case, CANTx and CANRx, I am using these pins as regular digital input pins, but something weird happens, one pin works great, the other one has a weird behavior, let me explain.

Circuit:
from the DUE 3.3V goes to one terminal of the button, the other terminal has a 1uF capacitor to ground and a 47K pulldown resistor, both buttons are wired the same. One goes to CANTx the other one to CANRx

My goal was to create a counter that you can modify with two buttons, one increments the counter, the other decrements it, simple.

My first thought was to do it using interrupts. So I made two ISR, one for each button and configurated it as RISING

attachInterrupt(button_A, button_A_ISR, RISING);
attachInterrupt(button_B, button_B_ISR, RISING);
button_A_ISR(){
  button_A_flag = true;
  show_number = true;
  show_number_pressed = millis();
  number++;
  if (number > 32){
    number = 1;
  }
}

button_B_ISR(){
  button_B_flag = true;
  show_number = true;
  show_number_pressed = millis();
  number--;
  if (number < 1){
    number = 32;
  }
}

(I know the correct thing is to use digitalPinToInterrupt() but did not work for me, also this constructor works for DUE)

All variables used inside the ISR are declared as volatile

The weird thing is that the Button A increments one by one as it should, but Button B decrements repeated times as if the button had some sort of bouncing, wich is weird because the button has hardware debounce. It looks like the ISR for button B is firing multiple times

At first I thought It was some sort of hardware bounce, but after hooking the oscilloscope up I found out that wasn't the case, the signals from both buttons are very clean.


Then I tried to not use interrupts and use regular code inside the loop and add software debounce (a delay)

button_A_current = digitalRead(button_A);
  button_B_current = digitalRead(button_B);
  
  //digitalWrite(led_button_A, button_A_current);
  //digitalWrite(led_button_B, button_B_current);

  if(button_A_current != last_button_A){
    last_button_A = button_A_current;
    if(button_A_current){
      button_A_flag = true;
      show_number = true;
      show_number_pressed = millis();
      number++;
      if (number > 32){
        number = 1;
      }
      delay(10);
    }
  }

  if(button_B_current != last_button_B){
    last_button_B = button_B_current;
    if(button_B_current){
      button_B_flag = true;
      show_number = true;
      show_number_pressed = millis();
      number--;
      if (number < 1){
        number = 32;
      }
      delay(100);
    }
  }

again, button A works great and button B doesn't, this time the button B fires two times, one when I press the button and another one when I release it. It looks like In that Pin specifically, when a rising or falling edge occurs the DUE detects several edges, as if It had some sort of capacitor in series and the voltage started to oscillate a few time when an edge happens.

Then I went to the datasheet of the SAM3X8E.

I found out that CANTx and CANRx are PA0 and PA1 respectively, and have a hardware difference between both. CANTx (PA0) does not have a Schmitt Trigger input

Captura de pantalla 2024-02-08 162742

But again, the signal is pretty clean, I don't think the button should work THAT BAD, but I don't know, what could I try or test next?

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