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
- J-Link ARM
- PE Micro Multi-link universal
- Mini/micro USB cable
- USB A to micro AB cable
- Hardware (tower/base board, ...) for specific device
- Personal Computer
Toolchain requirements
- IAR embedded Workbench version 7.30.4
- ARM GCC 4.8.3 2014q3
- Keil MDK 5.13
- Kinetis Design Studio IDE v.2.5.0
- Atollic TrueSTUDIO for ARM win32 v5.2.1
Software requirements
- The project files are in: <SDK_Install>/examples/<board>/demo_apps/enc_demo/<toolchain>.
- Library dependencies: ksdk_platform_lib
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
- Connect a USB cable between the PC host and the OpenSDA USB port on the board.
- Open a serial terminal with these settings:
- 115200 baud rate
- 8 data bits
- No parity
- One stop bit
- No flow control
- Download the program to the target board.
- Either press the reset button on your board or launch the debugger in your IDE to begin running the demo.
Run the demo
- Terminal prints the message "Welcome to Quad ENC demo!"
- Move encoder to detect INDEX pulse which starts to show shaft position on 6 LEDs and terminal prints "INDEX pulse detected!"
- The LED on board switches on/off according to shaft position and direction of shaft move is shown, too.
- 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 |
#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
static void enable_squareWave(void)
{
CLOCK_SYS_EnablePortClock(PHASE_A_PORT_INS);
PORT_HAL_SetMuxMode(PHASE_A_PORT,PHASE_A_PORT_PIN,kPortMuxAsGpio);
GPIO_HAL_SetPinDir(PHASE_A_GPIO,PHASE_A_PORT_PIN,kGpioDigitalOutput);
PORT_HAL_SetMuxMode(PHASE_B_PORT,PHASE_B_PORT_PIN,kPortMuxAsGpio);
GPIO_HAL_SetPinDir(PHASE_B_GPIO,PHASE_B_PORT_PIN,kGpioDigitalOutput);
PORT_HAL_SetMuxMode(PHASE_INDEX_PORT,PHASE_INDEX_PORT_PIN,kPortMuxAsGpio);
GPIO_HAL_SetPinDir(PHASE_INDEX_GPIO,PHASE_INDEX_PORT_PIN,kGpioDigitalOutput);
}
{
hardware_init();
OSA_Init();
enable_squareWave();
while(1)
{
{
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);
}
}
}
}