Skip to content

Community.Hardware.QuadratureEncoder

NicolasG3 edited this page Jun 7, 2013 · 2 revisions

Community.Hardware.QuadratureEncoder class provide access to hardware quadrature encoder channels.

  • the Count value can be read and write at any time
  • counting can be enabled/disabled
  • the encoder channel IO pins can be configured to input-capture or output-compare
  • the encoder channel IO pins are left available for another use if not configured for capture or compare
  • OutputCompare can generate a pulse on a specific IO pin when the encoder count equals the compare value.
  • InputCapture can capture the counter value on a specific IO pin rising edgde.
  • IOEvent is raised for each output-compare and input-capture.

STM32F4 implementation
STM32F4 hardware provide one quadrature encoder channel.
Two IO pins are available for this encoder channel to perform hardware output-compare and/or input-capture.
Known issues: count mode X1, X2 and X4 are interpreted as X1.

Sample code

using System;
using System.Threading;
using Community.Hardware;
using Microsoft.SPOT;
using Microsoft.SPOT.Hardware;

namespace QuadratureEncoderTest
    {
    public class Program
        {
        static QuadratureEncoder encoder;

        public static void Main() {
            Debug.Print("Quadrature encoder test started");
            //Initialize encoder
            encoder = new QuadratureEncoder(QuadratureEncoderChannel.Ch0, QuadratureEncoder.CountMode.X1);
            encoder.IOChanged += encoder_IOChanged;
            //set initial counter value
            encoder.Count = 0;
            //check the pin
            Cpu.Pin pin = encoder.GetIOPin(0);
            encoder.InitOutputCompare(0, 10);
            Debug.Print("Output compare init to IO 0: pin " + pin.ToString());
            pin = encoder.GetIOPin(1);
            encoder.InitInputCapture(1);
            Debug.Print("Input capture init to IO 1: pin " + pin.ToString());
            //display the actual count in the debug output window
            for (int i = 0; i < 10000; i++) {
                Debug.Print("Count = " + encoder.Count.ToString());
                Thread.Sleep(200);
                }
            Debug.Print("End of test");
            }
        /// <summary>
        /// Handle the quadrature encoder events
        /// </summary>
        /// <param name="IOIndex"></param>
        /// <param name="count"></param>
        /// <param name="time"></param>
        /// <param name="overlap"></param>
        static void encoder_IOChanged(int IOIndex, int count, DateTime time, bool overlap) {
            if (IOIndex == 0) {
                Debug.Print(time.TimeOfDay.ToString() + ": Output compare at " + count.ToString());
                //set next output compare a few count after
                encoder.InitOutputCompare(IOIndex, count + 10);
                }
            else if (IOIndex == 1) {
                Debug.Print(time.TimeOfDay.ToString() + ": Input capture at " + count.ToString());
                }

            }
        }
    }