Kinetis SDK v.1.2 Demo Applications User's Guide  Rev. 0
Freescale Semiconductor, Inc.
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Macros Groups Pages
Quadrature Encoder Demo

This demo application demonstrates the Quadrature Encoder demo. More...

Overview

This application demonstrates the Quadrature Encoder decoder function. It decodes quadrature signals and outputs position on LEDs.

Supported Platforms

This Freescale Tower System development platform is supported by the Kinetis software development kit enc demo.

System Requirement

Hardware requirements

Toolchain requirements

Software requirements

Getting Started

Hardware configuration

For the Freescale TWR-KV46F150M board, connect encoder quadrature outputs to XBARA input pins.

Platform Inputs
PHASE A PHASE B INDEX HOME
TWR-KV46F150M J1-2 J1-4 J501-26 N/A

Prepare the Demo

  1. Connect a USB cable between the PC host and the OpenSDA USB port on the board.
  2. Open a serial terminal with these settings:
    • 115200 baud rate
    • 8 data bits
    • No parity
    • One stop bit
    • No flow control
  3. Download the program to the target board.
  4. Either press the reset button on your board or launch the debugger in your IDE to begin running the demo.

Run the demo

  1. Terminal prints the message "Welcome to Quad ENC demo!"
  2. Move encoder to detect INDEX pulse which starts to show shaft position on 6 LEDs and terminal prints "INDEX pulse detected!"
  3. The LED on board switches on/off according to shaft position and direction of shaft move is shown, too.
  4. One turn of the shaft is presented with 6 LED switching.

Simulate encoder

If there is not any real encoder, the source code below can be used to simulate an encoder. It was tested on TWR-K21D50M.

Platform Outputs
PHASE A PHASE B INDEX HOME
TWR-K21D50M J15-1 J15-3 J15-5 N/A
// Includes
// SDK Included Files
#include "board.h"
#include "fsl_lptmr_driver.h"
#include "fsl_debug_console.h"
#define ENCODER_PULSES 1024
#define PHASE_A_PORT PORTD
#define PHASE_A_GPIO GPIOD
#define PHASE_A_PORT_PIN 4u
#define PHASE_A_PORT_INS PORTD_IDX
#define PHASE_B_PORT PORTD
#define PHASE_B_GPIO GPIOD
#define PHASE_B_PORT_PIN 5u
#define PHASE_B_PORT_INS PORTD_IDX
#define PHASE_INDEX_PORT PORTD
#define PHASE_INDEX_GPIO GPIOD
#define PHASE_INDEX_PORT_PIN 6u
#define PHASE_INDEX_PORT_INS PORTD_IDX
// Code
static void enable_squareWave(void)
{
/* Enable clock for ports of Phase A, B and INDEX if necessary */
CLOCK_SYS_EnablePortClock(PHASE_A_PORT_INS);
// CLOCK_SYS_EnablePortClock(PHASE_B_PORT_INS);
// CLOCK_SYS_EnablePortClock(PHASE_INDEX_PORT_INS);
/* Create square wave for phase A*/
PORT_HAL_SetMuxMode(PHASE_A_PORT,PHASE_A_PORT_PIN,kPortMuxAsGpio);
GPIO_HAL_SetPinDir(PHASE_A_GPIO,PHASE_A_PORT_PIN,kGpioDigitalOutput);
/* Create square wave for phase A*/
PORT_HAL_SetMuxMode(PHASE_B_PORT,PHASE_B_PORT_PIN,kPortMuxAsGpio);
GPIO_HAL_SetPinDir(PHASE_B_GPIO,PHASE_B_PORT_PIN,kGpioDigitalOutput);
/* Create signal index for INDEX_pin*/
PORT_HAL_SetMuxMode(PHASE_INDEX_PORT,PHASE_INDEX_PORT_PIN,kPortMuxAsGpio);
GPIO_HAL_SetPinDir(PHASE_INDEX_GPIO,PHASE_INDEX_PORT_PIN,kGpioDigitalOutput);
}
int main (void)
{
uint32_t i;
hardware_init();
OSA_Init();
enable_squareWave();
while(1)
{
for(i = 0; i < ENCODER_PULSES; i++)
{
GPIO_HAL_TogglePinOutput(PHASE_A_GPIO, 4u);
OSA_TimeDelay(1);
GPIO_HAL_TogglePinOutput(PHASE_B_GPIO, 5u);
if(i == 0)
{
GPIO_HAL_SetPinOutput(PHASE_INDEX_GPIO, 6u);
OSA_TimeDelay(1);
GPIO_HAL_ClearPinOutput(PHASE_INDEX_GPIO, 6u);
}
else
{
OSA_TimeDelay(1);
}
}
}
}