I’m using this code here for a rotary encoder knob. On it’s own it seems to work pretty well and it gives me the results I expect without fail: https://www.ghielectronics.com/community/forum/topic?id=15231 (code towards bottom)
I started to add features to my device (Cerb 40 II) and the rotary encoder started to act up. I was able to narrow it down to PWM. By commenting out the 5 PWM lines, the problem goes away. In particular, the values are all wrong and it fires 16ish times the amount of interrupts . The encoder is hardware debounced and I get a clean output.
Is there something I’m doing wrong?
Here’s the simplified code I’m using to reproduce the case:
using GHI.Hardware;
using GHI.Hardware.FEZCerb;
namespace PWMInterruptBugTest
{
public class Program
{
static InterruptPort PinA;
static InterruptPort PinB;
static InterruptPort RotaryButton;
static int InterruptCountA;
static int InterruptCountB;
public static void Main()
{
InterruptCountA = 0;
InterruptCountB = 0;
PinA = new InterruptPort(Pin.PC10, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth);//Pins are pulled high.
PinB = new InterruptPort(Pin.PA14, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeBoth);//Interrupt Needed here.
PinA.OnInterrupt +=PinA_OnInterrupt;
PinB.OnInterrupt +=PinB_OnInterrupt;
RotaryButton = new InterruptPort(Pin.PC11, false, Port.ResistorMode.Disabled, Port.InterruptMode.InterruptEdgeHigh);
RotaryButton.OnInterrupt += RotaryButton_OnInterrupt;
PWM PWMPin;
PWMPin = new PWM(Cpu.PWMChannel.PWM_7, 100000, .5, false);
PWMPin.Start();
PWMPin.Frequency = 100000;
PWMPin.DutyCycle = .5;
while (true)
{
//do nothing wait for interrupts
}
}
static void RotaryButton_OnInterrupt(uint data1, uint data2, DateTime time)
{
Debug.Print("Button Interrupt");
InterruptCountB = 0;
InterruptCountA = 0;
}
private static void PinB_OnInterrupt(uint data1, uint data2, DateTime time)
{
InterruptCountB++;
Debug.Print(InterruptCountB.ToString() + " PinB Interrupt");
}
static void PinA_OnInterrupt(uint data1, uint data2, DateTime time)
{
InterruptCountA++;
Debug.Print(InterruptCountA.ToString() + " PinA Interrupt");
}
}
}