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

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
Nigel945426
Date:
Mon Nov 17 19:17:21 2014 +0000
Commit message:
Working version of Hardware Quadrature ABZ reader

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 25c34018702c main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Nov 17 19:17:21 2014 +0000
@@ -0,0 +1,62 @@
+#include "mbed.h"
+
+// Hardware Quadrature Encoder ABZ for Nucleo F401RE
+// Output on debug port to host PC @ 9600 baud
+// 
+// By Nigel Webb, November 2014
+
+/* Connections
+   PA_0 = Encoder A
+   PA_1 = Encoder B
+   PA_4 = Encoder Z 
+*/
+
+InterruptIn ZPulse(PA_4) ; // Setup Interrupt for Z Pulse
+
+void EncoderInitialise(void) {
+    // configure GPIO PA0 & PA1 as inputs for Encoder
+    RCC->AHB1ENR |= 0x00000001;  // Enable clock for GPIOA
+ 
+    GPIOA->MODER   |= GPIO_MODER_MODER0_1 | GPIO_MODER_MODER1_1 ;           //PA0 & PA1 as Alternate Function   /*!< GPIO port mode register,               Address offset: 0x00      */
+    GPIOA->OTYPER  |= GPIO_OTYPER_OT_0 | GPIO_OTYPER_OT_1 ;                 //PA0 & PA1 as Inputs               /*!< GPIO port output type register,        Address offset: 0x04      */
+    GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR0 | GPIO_OSPEEDER_OSPEEDR1 ;     // Low speed                        /*!< GPIO port output speed register,       Address offset: 0x08      */
+    GPIOA->PUPDR   |= GPIO_PUPDR_PUPDR0_1 | GPIO_PUPDR_PUPDR1_1 ;           // Pull Down                        /*!< GPIO port pull-up/pull-down register,  Address offset: 0x0C      */
+    GPIOA->AFR[0]  |= 0x00000011 ;                                          //  AF01 for PA0 & PA1              /*!< GPIO alternate function registers,     Address offset: 0x20-0x24 */
+    GPIOA->AFR[1]  |= 0x00000000 ;                                          //                                  /*!< GPIO alternate function registers,     Address offset: 0x20-0x24 */
+   
+    // configure TIM2 as Encoder input
+    RCC->APB1ENR |= 0x00000001;  // Enable clock for TIM2
+ 
+    TIM2->CR1   = 0x0001;     // CEN(Counter ENable)='1'     < TIM control register 1
+    TIM2->SMCR  = 0x0003;     // SMS='011' (Encoder mode 3)  < TIM slave mode control register
+    TIM2->CCMR1 = 0xF1F1;     // CC1S='01' CC2S='01'         < TIM capture/compare mode register 1
+    TIM2->CCMR2 = 0x0000;     //                             < TIM capture/compare mode register 2
+    TIM2->CCER  = 0x0011;     // CC1P CC2P                   < TIM capture/compare enable register
+    TIM2->PSC   = 0x0000;     // Prescaler = (0+1)           < TIM prescaler
+    TIM2->ARR   = 0xffffffff; // reload at 0xfffffff         < TIM auto-reload register
+  
+    TIM2->CNT = 0x0000;  //reset the counter before we use it  
+}
+
+// Z Pulse routine
+void ZeroEncoderCount() {
+    TIM2->CNT=0 ; //reset count to zero
+}
+
+int main() {
+    EncoderInitialise() ;
+    
+    ZPulse.rise(&ZeroEncoderCount) ; //Setup Interrupt for rising edge of Z pulse 
+    ZPulse.mode(PullDown) ; // Set input as pull down
+     
+    unsigned int EncoderPosition ;
+    
+    while (true) {
+        // Print Encoder Quadrature count to debug port every 0.5 seconds
+        EncoderPosition = TIM2->CNT ; // Get current position from Encoder
+        printf("Encoder Position %i\r\n", EncoderPosition); 
+        wait(0.5);
+    }
+   
+       
+}
\ No newline at end of file
diff -r 000000000000 -r 25c34018702c mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Mon Nov 17 19:17:21 2014 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/031413cf7a89
\ No newline at end of file