Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of HardwareQuadratureEncoderABZ by
main.cpp
00001 #include "mbed.h" 00002 00003 // Hardware Quadrature Encoder ABZ for Nucleo F401RE 00004 // Output on debug port to host PC @ 9600 baud 00005 // 00006 // By Nigel Webb, November 2014 00007 00008 /* Encoder Connections 00009 PA8 = CH1 00010 PA9 = CH2 00011 00012 PA6 = CH1 00013 PA7 = CH2 00014 */ 00015 00016 00017 void EncoderInitialise(void) { 00018 // configure GPIO PA8 & PA9 as CH1 & CH2 inputs for Encoder1 00019 RCC->AHB1ENR |= 0x00000001; // Enable clock for GPIOA 00020 GPIOA->MODER |= GPIO_MODER_MODER8_1 | GPIO_MODER_MODER9_1 ; //PA8 & PA9 as Alternate Function /*!< GPIO port mode register, Address offset: 0x00 */ 00021 GPIOA->OTYPER |= GPIO_OTYPER_OT_8 | GPIO_OTYPER_OT_9 ; //PA8 & PA9 as Inputs /*!< GPIO port output type register, Address offset: 0x04 */ 00022 GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR8_1 | GPIO_OSPEEDER_OSPEEDR9_1 ; // High speed /*!< GPIO port output speed register, Address offset: 0x08 */ 00023 GPIOA->PUPDR |= GPIO_PUPDR_PUPDR8_1 | GPIO_PUPDR_PUPDR9_1 ; // Pull Down /*!< GPIO port pull-up/pull-down register, Address offset: 0x0C */ 00024 //GPIOA->AFR[0] |= 0x11000000 ; // AF02 for PA8 & PA9 /*!< GPIO alternate function registers, Address offset: 0x20-0x24 */ 00025 //GPIOA->AFR[1] |= 0x00000011 ; // /*!< GPIO alternate function registers, Address offset: 0x20-0x24 */ 00026 00027 // configure GPIO PA6 & PA7 as CH1 & CH2 inputs for Encoder2 00028 RCC->AHB1ENR |= 0x00000001; // Enable clock for GPIOA 00029 GPIOA->MODER |= GPIO_MODER_MODER6_1 | GPIO_MODER_MODER7_1 ; //PA6 & PA7 as Alternate Function /*!< GPIO port mode register, Address offset: 0x00 */ 00030 GPIOA->OTYPER |= GPIO_OTYPER_OT_6 | GPIO_OTYPER_OT_7 ; //PA6 & PA7 as Inputs /*!< GPIO port output type register, Address offset: 0x04 */ 00031 GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR6_1 | GPIO_OSPEEDER_OSPEEDR7_1 ; // High speed /*!< GPIO port output speed register, Address offset: 0x08 */ 00032 GPIOA->PUPDR |= GPIO_PUPDR_PUPDR6_1 | GPIO_PUPDR_PUPDR7_1 ; // Pull Down /*!< GPIO port pull-up/pull-down register, Address offset: 0x0C */ 00033 GPIOA->AFR[0] |= 0x22000000 ; // AF02 for PA6 & PA7 /*!< GPIO alternate function registers, Address offset: 0x20-0x24 */ 00034 GPIOA->AFR[1] |= 0x00000011 ; // /*!< GPIO alternate function registers, Address offset: 0x20-0x24 */ 00035 00036 // configure TIM1 & TIM3 as Encoder input 00037 RCC->APB2ENR |= 0x00000001; // Enable clock for TIM1 00038 RCC->APB1ENR |= 0x00000002; // Enable clock for TIM3 00039 00040 TIM1->CR1 = 0x0001; // CEN(Counter ENable)='1' < TIM control register 1 00041 TIM1->SMCR = 0x0001; // SMS='001' (Encoder mode 1) < TIM CH2 Edge 00042 TIM1->CCMR1 = 0xF1F1; // CC1S='01' CC2S='01' < TIM capture/compare mode register 1 00043 TIM1->CCMR2 = 0x0000; // < TIM capture/compare mode register 2 00044 TIM1->CCER = 0x0011; // CC1P CC2P < TIM capture/compare enable register 00045 TIM1->PSC = 0x0000; // Prescaler = (0+1) < TIM prescaler 00046 TIM1->ARR = 0x0000000a; // reload at 10 < TIM auto-reload register 00047 TIM1->CNT = 0x0000; //reset the counter before we use it 00048 00049 TIM3->CR1 = 0x0001; // CEN(Counter ENable)='1' < TIM control register 1 00050 TIM3->SMCR = 0x0001; // SMS='001' (Encoder mode 1) < TIM CH2 Edge 00051 TIM3->CCMR1 = 0xF1F1; // CC1S='01' CC2S='01' < TIM capture/compare mode register 1 00052 TIM3->CCMR2 = 0x0000; // < TIM capture/compare mode register 2 00053 TIM3->CCER = 0x0011; // CC1P CC2P < TIM capture/compare enable register 00054 TIM3->PSC = 0x0000; // Prescaler = (0+1) < TIM prescaler 00055 TIM3->ARR = 0x0000000a; // reload at 10 < TIM auto-reload register 00056 TIM3->CNT = 0x0000; //reset the counter before we use it 00057 00058 } 00059 00060 int main() { 00061 00062 printf("\e[1;1H\e[2J"); 00063 EncoderInitialise() ; 00064 00065 uint16_t count1=0, count2=0; 00066 00067 while (1) { 00068 // Print Encoder count to debug port every 0.5 seconds 00069 count1 = TIM1->CNT ; // Get current position from Encoder1 00070 count2 = TIM3->CNT ; // Get current position from Encoder2 00071 printf("COUNT1 %d COUNT2 %d\r\n", count1, count2); 00072 wait(0.5); 00073 } 00074 00075 }
Generated on Tue Jul 12 2022 15:58:20 by
1.7.2
