Encoder

Dependencies:   mbed

Fork of HardwareQuadratureEncoderABZ by Nigel Webb

Revision:
1:7a96e635f7a7
Parent:
0:25c34018702c
--- a/main.cpp	Mon Nov 17 19:17:21 2014 +0000
+++ b/main.cpp	Sat Apr 29 09:54:34 2017 +0000
@@ -5,58 +5,71 @@
 // 
 // By Nigel Webb, November 2014
 
-/* Connections
-   PA_0 = Encoder A
-   PA_1 = Encoder B
-   PA_4 = Encoder Z 
+/* Encoder Connections
+   PA8 = CH1
+   PA9 = CH2
+   
+   PA6 = CH1
+   PA7 = CH2
 */
 
-InterruptIn ZPulse(PA_4) ; // Setup Interrupt for Z Pulse
 
 void EncoderInitialise(void) {
-    // configure GPIO PA0 & PA1 as inputs for Encoder
+    // configure GPIO PA8 & PA9 as CH1 & CH2 inputs for Encoder1
     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 */
+    GPIOA->MODER   |= GPIO_MODER_MODER8_1 | GPIO_MODER_MODER9_1 ;           //PA8 & PA9 as Alternate Function   /*!< GPIO port mode register,               Address offset: 0x00      */
+    GPIOA->OTYPER  |= GPIO_OTYPER_OT_8 | GPIO_OTYPER_OT_9 ;                 //PA8 & PA9 as Inputs               /*!< GPIO port output type register,        Address offset: 0x04      */
+    GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR8_1 | GPIO_OSPEEDER_OSPEEDR9_1 ; // High speed                        /*!< GPIO port output speed register,       Address offset: 0x08      */
+    GPIOA->PUPDR   |= GPIO_PUPDR_PUPDR8_1 | GPIO_PUPDR_PUPDR9_1 ;           // Pull Down                        /*!< GPIO port pull-up/pull-down register,  Address offset: 0x0C      */
+    //GPIOA->AFR[0]  |= 0x11000000 ;                                          //  AF02 for PA8 & PA9              /*!< GPIO alternate function registers,     Address offset: 0x20-0x24 */
+    //GPIOA->AFR[1]  |= 0x00000011 ;                                          //                                  /*!< 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  
-}
+    // configure GPIO PA6 & PA7 as CH1 & CH2 inputs for Encoder2
+    RCC->AHB1ENR |= 0x00000001;  // Enable clock for GPIOA
+    GPIOA->MODER   |= GPIO_MODER_MODER6_1 | GPIO_MODER_MODER7_1 ;           //PA6 & PA7 as Alternate Function   /*!< GPIO port mode register,               Address offset: 0x00      */
+    GPIOA->OTYPER  |= GPIO_OTYPER_OT_6 | GPIO_OTYPER_OT_7 ;                 //PA6 & PA7 as Inputs               /*!< GPIO port output type register,        Address offset: 0x04      */
+    GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR6_1 | GPIO_OSPEEDER_OSPEEDR7_1 ; // High speed                        /*!< GPIO port output speed register,       Address offset: 0x08      */
+    GPIOA->PUPDR   |= GPIO_PUPDR_PUPDR6_1 | GPIO_PUPDR_PUPDR7_1 ;           // Pull Down                        /*!< GPIO port pull-up/pull-down register,  Address offset: 0x0C      */
+    GPIOA->AFR[0]  |= 0x22000000 ;                                          //  AF02 for PA6 & PA7              /*!< GPIO alternate function registers,     Address offset: 0x20-0x24 */
+    GPIOA->AFR[1]  |= 0x00000011 ;                                          //                                  /*!< GPIO alternate function registers,     Address offset: 0x20-0x24 */
 
-// Z Pulse routine
-void ZeroEncoderCount() {
-    TIM2->CNT=0 ; //reset count to zero
+    // configure TIM1 & TIM3 as Encoder input
+    RCC->APB2ENR |= 0x00000001;  // Enable clock for TIM1
+    RCC->APB1ENR |= 0x00000002;  // Enable clock for TIM3
+    
+    TIM1->CR1   = 0x0001;     // CEN(Counter ENable)='1'     < TIM control register 1
+    TIM1->SMCR  = 0x0001;     // SMS='001' (Encoder mode 1)  < TIM CH2 Edge
+    TIM1->CCMR1 = 0xF1F1;     // CC1S='01' CC2S='01'         < TIM capture/compare mode register 1
+    TIM1->CCMR2 = 0x0000;     //                             < TIM capture/compare mode register 2
+    TIM1->CCER  = 0x0011;     // CC1P CC2P                   < TIM capture/compare enable register
+    TIM1->PSC   = 0x0000;     // Prescaler = (0+1)           < TIM prescaler
+    TIM1->ARR   = 0x0000000a; // reload at 10                < TIM auto-reload register
+    TIM1->CNT   = 0x0000;     //reset the counter before we use it
+    
+    TIM3->CR1   = 0x0001;     // CEN(Counter ENable)='1'     < TIM control register 1
+    TIM3->SMCR  = 0x0001;     // SMS='001' (Encoder mode 1)  < TIM CH2 Edge
+    TIM3->CCMR1 = 0xF1F1;     // CC1S='01' CC2S='01'         < TIM capture/compare mode register 1
+    TIM3->CCMR2 = 0x0000;     //                             < TIM capture/compare mode register 2
+    TIM3->CCER  = 0x0011;     // CC1P CC2P                   < TIM capture/compare enable register
+    TIM3->PSC   = 0x0000;     // Prescaler = (0+1)           < TIM prescaler
+    TIM3->ARR   = 0x0000000a; // reload at 10                < TIM auto-reload register 
+    TIM3->CNT   = 0x0000;     //reset the counter before we use it  
+       
 }
 
 int main() {
+    
+    printf("\e[1;1H\e[2J");
     EncoderInitialise() ;
     
-    ZPulse.rise(&ZeroEncoderCount) ; //Setup Interrupt for rising edge of Z pulse 
-    ZPulse.mode(PullDown) ; // Set input as pull down
-     
-    unsigned int EncoderPosition ;
+    uint16_t count1=0, count2=0;
     
-    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); 
+    while (1) {
+        // Print Encoder count to debug port every 0.5 seconds
+        count1 = TIM1->CNT ; // Get current position from Encoder1
+        count2 = TIM3->CNT ; // Get current position from Encoder2
+        printf("COUNT1 %d COUNT2 %d\r\n", count1, count2);
         wait(0.5);
     }
-   
-       
+      
 }
\ No newline at end of file