Encoder Hardware Configuration Problem

Status
Not open for further replies.

Gunner

New member
I am trying to configure the Teensy 4.0 encoder hardware to count pulses from a rotary encoder. This is the one I am using: https://www.amazon.com/gp/product/B085ZLCYS1/ref=ppx_yo_dt_b_search_asin_image?ie=UTF8&psc=1

I think I have configured everything correctly, but when I print the value of "encoderCount" to the serial monitor, it just flips between either 0 and 1 or 0 and -1. Any help is appreciated. Thanks.

Code:
#include <Arduino.h>

int32_t encoderCount = 0;

void setup() {
  //// Configure Encoder Hardware ////
  CCM_CCGR2 |= CCM_CCGR2_XBAR1(3); // Enable XBAR1 clock
  CCM_CCGR4 |= CCM_CCGR4_ENC1(3); // Enable Encoder 1 clock
  IOMUXC_SW_MUX_CTL_PAD_GPIO_AD_B0_02 = 1; // Map XBAR_INOUT16 to pad AD_B0_02 (Pin 1 on the Teensy)
  IOMUXC_SW_MUX_CTL_PAD_GPIO_AD_B0_03 = 1; // Map XBAR_INOUT17 to pad AD_B0_03 (Pin 0 on the Teensy)
  XBARA1_SEL33 = XBARA1_IN_IOMUX_XBAR_INOUT17 | (XBARA1_IN_IOMUX_XBAR_INOUT16 << 8); // Connect XBAR_INOUT17 to ENC1_PHASEA_INPUT and XBAR_INOUT16 to ENC1_PHASEB_INPUT
  IOMUXC_SW_PAD_CTL_PAD_GPIO_AD_B0_02 |= IOMUXC_PAD_PUS(3) | IOMUXC_PAD_PKE | IOMUXC_PAD_PUE | IOMUXC_PAD_HYS | IOMUXC_PAD_DSE(7);
  IOMUXC_SW_PAD_CTL_PAD_GPIO_AD_B0_03 |= IOMUXC_PAD_PUS(3) | IOMUXC_PAD_PKE | IOMUXC_PAD_PUE | IOMUXC_PAD_HYS | IOMUXC_PAD_DSE(7);
  ENC1_UINIT = 0;
  ENC1_LINIT = 0;
  ENC1_CTRL |= (1 << 11);

  Serial.begin(9600);
}

void loop() {
  encoderCount = ENC1_UPOS << 16;
  encoderCount |= ENC1_LPOSH;
  Serial.println(encoderCount);
  delay(100);
}
 
Status
Not open for further replies.
Back
Top