Demo program to read ABZ Quadrature Encoder in Hardware on the Nucleo F401RE

Dependencies:   mbed

Committer:
Nigel945426
Date:
Mon Nov 17 19:17:21 2014 +0000
Revision:
0:25c34018702c
Working version of Hardware Quadrature ABZ reader

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Nigel945426 0:25c34018702c 1 #include "mbed.h"
Nigel945426 0:25c34018702c 2
Nigel945426 0:25c34018702c 3 // Hardware Quadrature Encoder ABZ for Nucleo F401RE
Nigel945426 0:25c34018702c 4 // Output on debug port to host PC @ 9600 baud
Nigel945426 0:25c34018702c 5 //
Nigel945426 0:25c34018702c 6 // By Nigel Webb, November 2014
Nigel945426 0:25c34018702c 7
Nigel945426 0:25c34018702c 8 /* Connections
Nigel945426 0:25c34018702c 9 PA_0 = Encoder A
Nigel945426 0:25c34018702c 10 PA_1 = Encoder B
Nigel945426 0:25c34018702c 11 PA_4 = Encoder Z
Nigel945426 0:25c34018702c 12 */
Nigel945426 0:25c34018702c 13
Nigel945426 0:25c34018702c 14 InterruptIn ZPulse(PA_4) ; // Setup Interrupt for Z Pulse
Nigel945426 0:25c34018702c 15
Nigel945426 0:25c34018702c 16 void EncoderInitialise(void) {
Nigel945426 0:25c34018702c 17 // configure GPIO PA0 & PA1 as inputs for Encoder
Nigel945426 0:25c34018702c 18 RCC->AHB1ENR |= 0x00000001; // Enable clock for GPIOA
Nigel945426 0:25c34018702c 19
Nigel945426 0:25c34018702c 20 GPIOA->MODER |= GPIO_MODER_MODER0_1 | GPIO_MODER_MODER1_1 ; //PA0 & PA1 as Alternate Function /*!< GPIO port mode register, Address offset: 0x00 */
Nigel945426 0:25c34018702c 21 GPIOA->OTYPER |= GPIO_OTYPER_OT_0 | GPIO_OTYPER_OT_1 ; //PA0 & PA1 as Inputs /*!< GPIO port output type register, Address offset: 0x04 */
Nigel945426 0:25c34018702c 22 GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR0 | GPIO_OSPEEDER_OSPEEDR1 ; // Low speed /*!< GPIO port output speed register, Address offset: 0x08 */
Nigel945426 0:25c34018702c 23 GPIOA->PUPDR |= GPIO_PUPDR_PUPDR0_1 | GPIO_PUPDR_PUPDR1_1 ; // Pull Down /*!< GPIO port pull-up/pull-down register, Address offset: 0x0C */
Nigel945426 0:25c34018702c 24 GPIOA->AFR[0] |= 0x00000011 ; // AF01 for PA0 & PA1 /*!< GPIO alternate function registers, Address offset: 0x20-0x24 */
Nigel945426 0:25c34018702c 25 GPIOA->AFR[1] |= 0x00000000 ; // /*!< GPIO alternate function registers, Address offset: 0x20-0x24 */
Nigel945426 0:25c34018702c 26
Nigel945426 0:25c34018702c 27 // configure TIM2 as Encoder input
Nigel945426 0:25c34018702c 28 RCC->APB1ENR |= 0x00000001; // Enable clock for TIM2
Nigel945426 0:25c34018702c 29
Nigel945426 0:25c34018702c 30 TIM2->CR1 = 0x0001; // CEN(Counter ENable)='1' < TIM control register 1
Nigel945426 0:25c34018702c 31 TIM2->SMCR = 0x0003; // SMS='011' (Encoder mode 3) < TIM slave mode control register
Nigel945426 0:25c34018702c 32 TIM2->CCMR1 = 0xF1F1; // CC1S='01' CC2S='01' < TIM capture/compare mode register 1
Nigel945426 0:25c34018702c 33 TIM2->CCMR2 = 0x0000; // < TIM capture/compare mode register 2
Nigel945426 0:25c34018702c 34 TIM2->CCER = 0x0011; // CC1P CC2P < TIM capture/compare enable register
Nigel945426 0:25c34018702c 35 TIM2->PSC = 0x0000; // Prescaler = (0+1) < TIM prescaler
Nigel945426 0:25c34018702c 36 TIM2->ARR = 0xffffffff; // reload at 0xfffffff < TIM auto-reload register
Nigel945426 0:25c34018702c 37
Nigel945426 0:25c34018702c 38 TIM2->CNT = 0x0000; //reset the counter before we use it
Nigel945426 0:25c34018702c 39 }
Nigel945426 0:25c34018702c 40
Nigel945426 0:25c34018702c 41 // Z Pulse routine
Nigel945426 0:25c34018702c 42 void ZeroEncoderCount() {
Nigel945426 0:25c34018702c 43 TIM2->CNT=0 ; //reset count to zero
Nigel945426 0:25c34018702c 44 }
Nigel945426 0:25c34018702c 45
Nigel945426 0:25c34018702c 46 int main() {
Nigel945426 0:25c34018702c 47 EncoderInitialise() ;
Nigel945426 0:25c34018702c 48
Nigel945426 0:25c34018702c 49 ZPulse.rise(&ZeroEncoderCount) ; //Setup Interrupt for rising edge of Z pulse
Nigel945426 0:25c34018702c 50 ZPulse.mode(PullDown) ; // Set input as pull down
Nigel945426 0:25c34018702c 51
Nigel945426 0:25c34018702c 52 unsigned int EncoderPosition ;
Nigel945426 0:25c34018702c 53
Nigel945426 0:25c34018702c 54 while (true) {
Nigel945426 0:25c34018702c 55 // Print Encoder Quadrature count to debug port every 0.5 seconds
Nigel945426 0:25c34018702c 56 EncoderPosition = TIM2->CNT ; // Get current position from Encoder
Nigel945426 0:25c34018702c 57 printf("Encoder Position %i\r\n", EncoderPosition);
Nigel945426 0:25c34018702c 58 wait(0.5);
Nigel945426 0:25c34018702c 59 }
Nigel945426 0:25c34018702c 60
Nigel945426 0:25c34018702c 61
Nigel945426 0:25c34018702c 62 }