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 Micromouse_alpha_copy_copy by
EncoderCounter.cpp
00001 /* 00002 * EncoderCounter.cpp 00003 * Copyright (c) 2018, ZHAW 00004 * All rights reserved. 00005 */ 00006 00007 #include "EncoderCounter.h" 00008 00009 using namespace std; 00010 00011 /** 00012 * Creates and initializes the driver to read the quadrature 00013 * encoder counter of the STM32 microcontroller. 00014 * @param a the input pin for the channel A. 00015 * @param b the input pin for the channel B. 00016 */ 00017 EncoderCounter::EncoderCounter(PinName a, PinName b) 00018 { 00019 00020 // check pins 00021 00022 if ((a == PA_0) && (b == PA_1)) { 00023 00024 // pinmap OK for TIM2 CH1 and CH2 00025 00026 TIM = TIM2; 00027 00028 // configure general purpose I/O registers 00029 00030 GPIOA->MODER &= ~GPIO_MODER_MODER0; // reset port A0 00031 GPIOA->MODER |= GPIO_MODER_MODER0_1; // set alternate mode of port A0 00032 GPIOA->PUPDR &= ~GPIO_PUPDR_PUPDR0; // reset pull-up/pull-down on port A0 00033 GPIOA->PUPDR |= GPIO_PUPDR_PUPDR0_1; // set input as pull-down 00034 GPIOA->AFR[0] &= ~(0xF << 4*0); // reset alternate function of port A0 00035 GPIOA->AFR[0] |= 1 << 4*0; // set alternate funtion 1 of port A0 00036 00037 GPIOA->MODER &= ~GPIO_MODER_MODER1; // reset port A1 00038 GPIOA->MODER |= GPIO_MODER_MODER1_1; // set alternate mode of port A1 00039 GPIOA->PUPDR &= ~GPIO_PUPDR_PUPDR1; // reset pull-up/pull-down on port A1 00040 GPIOA->PUPDR |= GPIO_PUPDR_PUPDR1_1; // set input as pull-down 00041 GPIOA->AFR[0] &= ~(0xF << 4*1); // reset alternate function of port A1 00042 GPIOA->AFR[0] |= 1 << 4*1; // set alternate funtion 1 of port A1 00043 00044 // configure reset and clock control registers 00045 00046 RCC->APB1RSTR |= RCC_APB1RSTR_TIM2RST; //reset TIM2 controller 00047 RCC->APB1RSTR &= ~RCC_APB1RSTR_TIM2RST; 00048 00049 RCC->APB1ENR |= RCC_APB1ENR_TIM2EN; // TIM2 clock enable 00050 00051 } else if ((a == PA_6) && (b == PC_7)) { 00052 00053 // pinmap OK for TIM3 CH1 and CH2 00054 00055 TIM = TIM3; 00056 00057 // configure reset and clock control registers 00058 00059 RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN; // manually enable port C (port A enabled by mbed library) 00060 00061 // configure general purpose I/O registers 00062 00063 GPIOA->MODER &= ~GPIO_MODER_MODER6; // reset port A6 00064 GPIOA->MODER |= GPIO_MODER_MODER6_1; // set alternate mode of port A6 00065 GPIOA->PUPDR &= ~GPIO_PUPDR_PUPDR6; // reset pull-up/pull-down on port A6 00066 GPIOA->PUPDR |= GPIO_PUPDR_PUPDR6_1; // set input as pull-down 00067 GPIOA->AFR[0] &= ~(0xF << 4*6); // reset alternate function of port A6 00068 GPIOA->AFR[0] |= 2 << 4*6; // set alternate funtion 2 of port A6 00069 00070 GPIOC->MODER &= ~GPIO_MODER_MODER7; // reset port C7 00071 GPIOC->MODER |= GPIO_MODER_MODER7_1; // set alternate mode of port C7 00072 GPIOC->PUPDR &= ~GPIO_PUPDR_PUPDR7; // reset pull-up/pull-down on port C7 00073 GPIOC->PUPDR |= GPIO_PUPDR_PUPDR7_1; // set input as pull-down 00074 GPIOC->AFR[0] &= ~0xF0000000; // reset alternate function of port C7 00075 GPIOC->AFR[0] |= 2 << 4*7; // set alternate funtion 2 of port C7 00076 00077 // configure reset and clock control registers 00078 00079 RCC->APB1RSTR |= RCC_APB1RSTR_TIM3RST; //reset TIM3 controller 00080 RCC->APB1RSTR &= ~RCC_APB1RSTR_TIM3RST; 00081 00082 RCC->APB1ENR |= RCC_APB1ENR_TIM3EN; // TIM3 clock enable 00083 00084 } else if ((a == PB_6) && (b == PB_7)) { 00085 00086 // pinmap OK for TIM4 CH1 and CH2 00087 00088 TIM = TIM4; 00089 00090 // configure reset and clock control registers 00091 00092 RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN; // manually enable port B (port A enabled by mbed library) 00093 00094 // configure general purpose I/O registers 00095 00096 GPIOB->MODER &= ~GPIO_MODER_MODER6; // reset port B6 00097 GPIOB->MODER |= GPIO_MODER_MODER6_1; // set alternate mode of port B6 00098 GPIOB->PUPDR &= ~GPIO_PUPDR_PUPDR6; // reset pull-up/pull-down on port B6 00099 GPIOB->PUPDR |= GPIO_PUPDR_PUPDR6_1; // set input as pull-down 00100 GPIOB->AFR[0] &= ~(0xF << 4*6); // reset alternate function of port B6 00101 GPIOB->AFR[0] |= 2 << 4*6; // set alternate funtion 2 of port B6 00102 00103 GPIOB->MODER &= ~GPIO_MODER_MODER7; // reset port B7 00104 GPIOB->MODER |= GPIO_MODER_MODER7_1; // set alternate mode of port B7 00105 GPIOB->PUPDR &= ~GPIO_PUPDR_PUPDR7; // reset pull-up/pull-down on port B7 00106 GPIOB->PUPDR |= GPIO_PUPDR_PUPDR7_1; // set input as pull-down 00107 GPIOB->AFR[0] &= ~0xF0000000; // reset alternate function of port B7 00108 GPIOB->AFR[0] |= 2 << 4*7; // set alternate funtion 2 of port B7 00109 00110 // configure reset and clock control registers 00111 00112 RCC->APB1RSTR |= RCC_APB1RSTR_TIM4RST; //reset TIM4 controller 00113 RCC->APB1RSTR &= ~RCC_APB1RSTR_TIM4RST; 00114 00115 RCC->APB1ENR |= RCC_APB1ENR_TIM4EN; // TIM4 clock enable 00116 00117 } else { 00118 00119 printf("pinmap not found for peripheral\n"); 00120 } 00121 00122 // configure general purpose timer 3 or 4 00123 00124 TIM->CR1 = 0x0000; // counter disable 00125 TIM->CR2 = 0x0000; // reset master mode selection 00126 TIM->SMCR = TIM_SMCR_SMS_1 | TIM_SMCR_SMS_0; // counting on both TI1 & TI2 edges 00127 TIM->CCMR1 = TIM_CCMR1_CC2S_0 | TIM_CCMR1_CC1S_0; 00128 TIM->CCMR2 = 0x0000; // reset capture mode register 2 00129 TIM->CCER = TIM_CCER_CC2E | TIM_CCER_CC1E; 00130 TIM->CNT = 0x0000; // reset counter value 00131 TIM->ARR = 0xFFFF; // auto reload register 00132 TIM->CR1 = TIM_CR1_CEN; // counter enable 00133 } 00134 00135 EncoderCounter::~EncoderCounter() {} 00136 00137 /** 00138 * Resets the counter value to zero. 00139 */ 00140 void EncoderCounter::reset() 00141 { 00142 00143 TIM->CNT = 0x0000; 00144 } 00145 00146 /** 00147 * Resets the counter value to a given offset value. 00148 * @param offset the offset value to reset the counter to. 00149 */ 00150 void EncoderCounter::reset(short offset) 00151 { 00152 00153 TIM->CNT = -offset; 00154 } 00155 00156 /** 00157 * Reads the quadrature encoder counter value. 00158 * @return the quadrature encoder counter as a signed 16-bit integer value. 00159 */ 00160 short EncoderCounter::read() 00161 { 00162 00163 return (short)(-TIM->CNT); 00164 } 00165 00166 /** 00167 * The empty operator is a shorthand notation of the <code>read()</code> method. 00168 */ 00169 EncoderCounter::operator short() 00170 { 00171 00172 return read(); 00173 }
Generated on Fri Jul 15 2022 03:19:30 by
1.7.2
