Lab 2: Quadrature Decoding using the FlexTimer2 Lab 2: Quadrature Decode •Use quadrature decode...

19
1 Lab 2: Quadrature Decoding using the FlexTimer Fall 2019

Transcript of Lab 2: Quadrature Decoding using the FlexTimer2 Lab 2: Quadrature Decode •Use quadrature decode...

Page 1: Lab 2: Quadrature Decoding using the FlexTimer2 Lab 2: Quadrature Decode •Use quadrature decode function of the S32K144 FlexTimer module •Read the optical encoder and update a

1

Lab 2: Quadrature Decoding using the FlexTimer

Fall 2019

Page 2: Lab 2: Quadrature Decoding using the FlexTimer2 Lab 2: Quadrature Decode •Use quadrature decode function of the S32K144 FlexTimer module •Read the optical encoder and update a

2

Lab 2: Quadrature Decode

• Use quadrature decode function of the S32K144 FlexTimer module

• Read the optical encoder and update a 16-bit position count register to track wheel position – in counts and

– as angular position

• Output position to 16 LEDs and demonstrate overflow and underflow

Page 3: Lab 2: Quadrature Decoding using the FlexTimer2 Lab 2: Quadrature Decode •Use quadrature decode function of the S32K144 FlexTimer module •Read the optical encoder and update a

• For a general explanationof encoders, see the websiteni.com/tutorial/7109/en

3

Lab 2: Quadrature Decode• The haptic wheel in the lab

uses a 1000 CPR (cycles per revolution) encoder from Avago.

• For an explanation of this terminology, see the website

usdigital.com/support/resources/glossary

Encoder Measurements: How-To GuidePublish Date: Aug 09, 2013

OverviewThis document is part of the “How-To Guide for Most Common Measurements” centralized resource portal.

Table of ContentsEncoder and Applications Overview1.

How to Make an Encoder Measurement2.

Connecting an Encoder to an Instrument3.

Recommended Hardware and Software4.

Encoder Webcasts, Tutorials, and Other How-To Resources5.

1. Encoder and Applications OverviewAn encoder is an electromechanical device that can measure motion or position. Most encoders use optical sensors to provide electricalsignals in the form of pulse trains, which can, in turn, be translated into motion, direction, or position.

Rotary encoders are used to measure the rotational motion of ashaft. Figure 1 shows the fundamental components of a rotaryencoder, which consists of a light-emitting diode (LED), a disk, anda light detector on the opposite side of the disk. The disk, which ismounted on the rotating shaft, has patterns of opaque andtransparent sectors coded into the disk. As the disk rotates, theopaque segments block the light and, where the glass is clear, lightis allowed to pass. This generates square-wave pulses, which canthen be interpreted into position or motion.

Encoders usually have from 100 to 6,000 segments per revolution. This means that these encoders can provide 3.6 deg of resolution forthe encoder with 100 segments and 0.06 deg of resolution for the encoder with 6,000 segments.

Linear encoders work under the same principle as rotary encoders except that instead of a rotating disk, there is a stationary opaque stripwith transparent slits along its surface, and the LED-detector assembly is attached to the moving body.

Figure 1. Optical Encoder Components

An encoder with one set of pulses would not be useful because it could not indicate the direction of rotation. Using two code tracks withsectors positioned 90 deg out of phase (Figure 2), the two output channels of the quadrature encoder indicate both position and direction ofrotation. If A leads B, for example, the disk is rotating in a clockwise direction. If B leads A, then the disk is rotating in a counter-clockwisedirection. Therefore, by monitoring both the number of pulses and the relative phase of signals A and B, you can track both the position anddirection of rotation.

Figure 2. Quadrature Encoder A and B Output Signals

In addition, some quadrature encoders include a third output channel – called a zero or reference signal – which supplies a single pulse perrevolution. You can use this single pulse for precise determination of a reference position. In the majority of encoders, this signal is calledthe Z-Terminal or the index.

So far, this document has addressed only what are called single-ended incremental quadrature encoders. These are called single-endedbecause the A and B signals are both referenced to ground, so there is one wire (or end) per signal. Another commonly used type ofencoder is a differential encoder, where there are two lines per each A and B signal. The two lines for the A signal are A’ and A, and the twolines for the B signal are B’ and B. This type of configuration is also called push-pull because all four lines are always supplying a knownvoltage (either 0 V of Vcc). When A is Vcc, A’ is 0 V , and when A is 0 V, A’ is Vcc. In the case of a single-ended encoder, A is either Vcc or itfloats. Differential encoders are often used in electrically noisy environments because taking differential measurements protects theintegrity of the signal.

With incremental encoders, you can measure only changes in position (from which you can determine velocity and acceleration), but it isnot possible to determine the absolute position of an object. A third type of encoder, called an absolute encoder, is capable of determiningthe absolute position of an object. This type of encoder has alternating opaque and transparent segments like the incremental encoder, butthe absolute encoder uses multiple groups of segments that form concentric circles on the encoder wheel like a bull’s-eye on a target or

Encoder Measurements: How-To Guide - National Instruments http://www.ni.com/white-paper/7109/en/

1 of 5 8/26/14 2:51 PM

Page 4: Lab 2: Quadrature Decoding using the FlexTimer2 Lab 2: Quadrature Decode •Use quadrature decode function of the S32K144 FlexTimer module •Read the optical encoder and update a

4

FlexTimer Module (FTM)

• S32K144 has four FlexTimer modules, FTM0-3.• Each FTM has 8 channels that may be used for input

capture, output compare, or PWM generation (Lab 4).• Two FlexTimer modules, FTM1-2, may be used for

quadrature decoding – we will use FTM2 for QD.• Each FTM has a 16-bit counter that, when used for

quadrature decoding, is incremented or decremented depending on encoder input.

• See Chapter 45 in the S32K Reference Manual.

Page 5: Lab 2: Quadrature Decoding using the FlexTimer2 Lab 2: Quadrature Decode •Use quadrature decode function of the S32K144 FlexTimer module •Read the optical encoder and update a

FlexTimer Block Diagram

• FlexTimer accepts several sources of inputs, including channels 0-7, and primaryand secondary quadraturesignals, referred to asphase A and phase B.

• We will use pin A13 for the primary quadrature signal (phase A) and pin A12 for the secondary quadrature signal (phase B).

5

Page 6: Lab 2: Quadrature Decoding using the FlexTimer2 Lab 2: Quadrature Decode •Use quadrature decode function of the S32K144 FlexTimer module •Read the optical encoder and update a

Configuring the PCRs for QD

• In Lab 1, you configured pins for general purpose I/O by writing appropriate values to pin configuration registers (PCRs)

• To configure the FlexTimer for quadrature decoding, you must program the PCRs associated with pins A12 and A13 for inputs from quadrature decoding signals.

• To find the addresses of these PCRs, see Section 12.1.2.• From the table in the spreadsheet

S32K144_IO_Signal_Description_Input_Multiplexing/IO Signal Table

we see that the MUX bitfield should be set to 0b110.

6

Page 7: Lab 2: Quadrature Decoding using the FlexTimer2 Lab 2: Quadrature Decode •Use quadrature decode function of the S32K144 FlexTimer module •Read the optical encoder and update a

FTM Registers and Memory Map7

• Several registers are used to configure each FTM – we will only need a few.

Page 8: Lab 2: Quadrature Decoding using the FlexTimer2 Lab 2: Quadrature Decode •Use quadrature decode function of the S32K144 FlexTimer module •Read the optical encoder and update a

Counter Register: CNT8

• The CNT register contains the current value of the 16-bit quadrature decode counter. We will need to read the value of this counter and compare it tothe previous value to determine the change in wheel position.

Page 9: Lab 2: Quadrature Decoding using the FlexTimer2 Lab 2: Quadrature Decode •Use quadrature decode function of the S32K144 FlexTimer module •Read the optical encoder and update a

Counter Initial Value (CNTIN) and Modulo (MOD) Registers

9

• The CNTIN register contains the initial value INIT of the 16-bit quadrature decode counter. This is the value the counter will return to after it “rolls over” by counting up to the value set in the MOD register.• Set INIT = 0x0000 and MOD = 0XFFFF to use all 16 bits of the counter.

Page 10: Lab 2: Quadrature Decoding using the FlexTimer2 Lab 2: Quadrature Decode •Use quadrature decode function of the S32K144 FlexTimer module •Read the optical encoder and update a

Features Mode Selection (MODE) Register 10

• The MODE register contains several bitfields used to configure the FTM. • We shall only need to ensure that the Write Protection Disable is set so that

we can write to write-protected registers (WPDIS=1). (Note that 1 is the reset value of this bitfield.)

Page 11: Lab 2: Quadrature Decoding using the FlexTimer2 Lab 2: Quadrature Decode •Use quadrature decode function of the S32K144 FlexTimer module •Read the optical encoder and update a

Input Capture Filter Control (FILTER) Register 11

• The FlexTimer has optional filters for the inputs from the quadrature signals. • These are useful to suppress noise bursts that might be interpreted as valid

rising or falling edges of the signals.• We will disable these filters by setting CH2FVAL = 0x0, its reset value.• The effect of the noise filters on noise immunity and latency will be explored

in the homework.

Page 12: Lab 2: Quadrature Decoding using the FlexTimer2 Lab 2: Quadrature Decode •Use quadrature decode function of the S32K144 FlexTimer module •Read the optical encoder and update a

Status and Control (SC) Register 12

• The SC register is used to configure the FTM. • The FLTPS bitfield is used to set the value of an additional prescaler that

affects the operation of the FlexTimer filter. We do not use the filter and thusleave FLTPS = 0x0, its reset value (prescaler = 1).

• In Lab 4, we will set several bitfields in the SC register to use the FTM for PWM signal generation.

Page 13: Lab 2: Quadrature Decoding using the FlexTimer2 Lab 2: Quadrature Decode •Use quadrature decode function of the S32K144 FlexTimer module •Read the optical encoder and update a

Quadrature Decoder Control And Status(QDCTRL) Register

13

• The QDCTRL register has several bitfields that configure the FTM for quadrature decoding.

• All these should be left at their reset values of 0 except the Quadrature Decoder Mode Enable bit, which should be set to enable quadrature decoding (QUADEN=1).

• If set, the bits PHAFLTREN and PHBFLTREN enable input filters on the two quadrature signal. We leave these bits at their reset value of 0.

• Note: QUADMODE=0 selects 4X decoding as described in lecture.

Page 14: Lab 2: Quadrature Decoding using the FlexTimer2 Lab 2: Quadrature Decode •Use quadrature decode function of the S32K144 FlexTimer module •Read the optical encoder and update a

S32K144 Register Definitions: FlexTimer14

/** FTM - Register Layout Typedef */typedef struct {__IO uint32_t SC; /**< Status And Control, offset: 0x0 */__IO uint32_t CNT; /**< Counter, offset: 0x4 */__IO uint32_t MOD; /**< Modulo, offset: 0x8 */

...__IO uint32_t MODE; /**< Features Mode Selection, offset: 0x54 */__IO uint32_t SYNC; /**< Synchronization, offset: 0x58 */

...__IO uint32_t FILTER; /**< Input Capture Filter Control, offset: 0x78 */__IO uint32_t FLTCTRL; /**< Fault Control, offset: 0x7C */__IO uint32_t QDCTRL; /**< Quadrature Decoder Control And Status, offset: 0x80*/

...} FTM_Type, *FTM_MemMapPtr;

.../** Peripheral FTM2 base address */#define FTM2_BASE (0x4003A000u)/** Peripheral FTM2 base pointer */#define FTM2 ((FTM_Type *)FTM2_BASE)

.../* QDCTRL Bit Fields */#define FTM_QDCTRL_QUADEN_MASK 0x1u#define FTM_QDCTRL_QUADEN_SHIFT 0u#define FTM_QDCTRL_QUADEN_WIDTH 1u#define FTM_QDCTRL_QUADEN(x) /* see S32K144.h */

Page 15: Lab 2: Quadrature Decoding using the FlexTimer2 Lab 2: Quadrature Decode •Use quadrature decode function of the S32K144 FlexTimer module •Read the optical encoder and update a

15

Files and Documents• You are given qd.h, function prototype header file• You need to write three functions in qd.c• You are given a template file qd_template.c

– Init_QD(); /* initialize the FlexTimer */– updateCounter(); /* update wheel position */– updateAngle(); /* convert counts to angle */

• Also need to write– Lab2.c /* read the encoder position, update position count and output the result to the LED */

Page 16: Lab 2: Quadrature Decoding using the FlexTimer2 Lab 2: Quadrature Decode •Use quadrature decode function of the S32K144 FlexTimer module •Read the optical encoder and update a

16

Notes on Casting• We need to read the position count register and

accumulate a running count of wheel position:

curr_count = FTM2->CNT & FTM_CNT_COUNT_MASK;total_count = total_count + (curr_count-last_count);

• total_count is a signed 32-bit integer• curr_count and last_count are unsigned 16-bit

integers• Will this code work?

Page 17: Lab 2: Quadrature Decoding using the FlexTimer2 Lab 2: Quadrature Decode •Use quadrature decode function of the S32K144 FlexTimer module •Read the optical encoder and update a

17

Notes on Casting• Recall integral promotion:

– Before basic operation ( + - * / ), both operands converted to same type

– The smaller type is �promoted� to the larger type– Value of promoted type is preserved

• Suppose– total_count = 0x00007FFF – curr_count = 0xFFFF – last_count = 0x0000

• �-� operand promotes curr_count and last_count to 32 bit signed integers:

• curr_count - last_count = 0x0000FFFF -0x00000000= 0x0000FFFF

• Wrong! Large positive value, not one step negative

1 step backwards

Page 18: Lab 2: Quadrature Decoding using the FlexTimer2 Lab 2: Quadrature Decode •Use quadrature decode function of the S32K144 FlexTimer module •Read the optical encoder and update a

18

Notes on Casting• Do this:

total_count = (total_count + (int16_t)(curr_count-last_count));

• Cast (curr_count-last_count) to a 16-bit signed integer

• The result will be sign-extended and summed with the 32-bit signed value, TOTAL

0x0007FFF + 0xFFFFFFFF = 0x0007FFE

-1 (base 10) The correct answer: 1 step backwards

Page 19: Lab 2: Quadrature Decoding using the FlexTimer2 Lab 2: Quadrature Decode •Use quadrature decode function of the S32K144 FlexTimer module •Read the optical encoder and update a

S32K144 Clocks• The S32K144 system clock can be set to

– 112 MHz (HSRUN mode)– 80 MHz (RUN mode)

• Various other clock frequencies set in eecs461.h:/* Clock Frequencies */

#define SPLL_CLK_FREQ 160000000u /* 160 MHz */#define SYS_CLK_FREQ 80000000u /* 80 MHz */

#define BUS_CLK_FREQ 40000000u /* 40 MHz */#define SPLLDIV2_CLK_FREQ 40000000u /* 40 MHz */

#define SPLLDIV1_CLK_FREQ 10000000u /* 10 MHz */#define SOSC_CLK_FREQ 8000000u /* 8 MHz */

#define SOSCDIV1_CLK_FREQ 8000000u /* 8 MHz */#define SOSCDIV2_CLK_FREQ 8000000u /* 8 MHz */

• In Lab 2 we need to know that the FlexTimer clock is set to SPLLDIV1 = 10 MHz

• See Chapters 27-29

19