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.
main.cpp
00001 #include "mbed.h" 00002 #include "FastPWM.h" 00003 00004 00005 //FastPWM duty(PA_3); 00006 Ticker loop; 00007 unsigned int old_velocity = 0; 00008 unsigned int velocity; 00009 AnalogOut output(A2); 00010 00011 void EncoderInitialise(void) { 00012 // configure GPIO PA0 & PA1 as inputs for Encoder 00013 RCC->AHB1ENR |= 0x00000001; // Enable clock for GPIOA 00014 00015 GPIOA->MODER |= GPIO_MODER_MODER0_1 | GPIO_MODER_MODER1_1 ; //PA0 & PA1 as Alternate Function /*!< GPIO port mode register, Address offset: 0x00 */ 00016 GPIOA->OTYPER |= GPIO_OTYPER_OT_0 | GPIO_OTYPER_OT_1 ; //PA0 & PA1 as Inputs /*!< GPIO port output type register, Address offset: 0x04 */ 00017 GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR0 | GPIO_OSPEEDER_OSPEEDR1 ; // Low speed /*!< GPIO port output speed register, Address offset: 0x08 */ 00018 GPIOA->PUPDR |= GPIO_PUPDR_PUPDR0_1 | GPIO_PUPDR_PUPDR1_1 ; // Pull Down /*!< GPIO port pull-up/pull-down register, Address offset: 0x0C */ 00019 GPIOA->AFR[0] |= 0x00000011 ; // AF01 for PA0 & PA1 /*!< GPIO alternate function registers, Address offset: 0x20-0x24 */ 00020 GPIOA->AFR[1] |= 0x00000000 ; // /*!< GPIO alternate function registers, Address offset: 0x20-0x24 */ 00021 00022 // configure TIM2 as Encoder input 00023 RCC->APB1ENR |= 0x00000001; // Enable clock for TIM2 00024 00025 TIM2->CR1 = 0x0001; // CEN(Counter ENable)='1' < TIM control register 1 00026 TIM2->SMCR = 0x0003; // SMS='011' (Encoder mode 3) < TIM slave mode control register 00027 TIM2->CCMR1 = 0xF1F1; // CC1S='01' CC2S='01' < TIM capture/compare mode register 1 00028 TIM2->CCMR2 = 0x0000; // < TIM capture/compare mode register 2 00029 TIM2->CCER = 0x0011; // CC1P CC2P < TIM capture/compare enable register 00030 TIM2->PSC = 0x0000; // Prescaler = (0+1) < TIM prescaler 00031 TIM2->ARR = 0xffffffff; // reload at 0xfffffff < TIM auto-reload register 00032 00033 TIM2->CNT = 0x0000; //reset the counter before we use it 00034 } 00035 00036 // Z Pulse routine 00037 void ZeroEncoderCount() { 00038 TIM2->CNT=0 ; //reset count to zero 00039 } 00040 00041 void setp(void){ 00042 output.write_u16((TIM2->CNT)<<4); 00043 } 00044 00045 void print_serial(void){ 00046 velocity = (TIM2->CNT); 00047 printf("%i\n\r",velocity-old_velocity); 00048 old_velocity = velocity; 00049 } 00050 00051 int main() { 00052 EncoderInitialise() ; 00053 00054 00055 unsigned int EncoderPosition ; 00056 loop.attach(&setp, .000001); 00057 00058 while (true) { 00059 // Print Encoder Quadrature count to debug port every 0.5 seconds 00060 //EncoderPosition = TIM2->CNT ; // Get current position from Encoder 00061 //printf("Encoder Position %i\r\n", EncoderPosition); 00062 //wait(0.1); 00063 } 00064 00065 00066 }
Generated on Sat Aug 20 2022 18:53:12 by
1.7.2