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.
EncoderCounter.cpp
00001 /* 00002 * EncoderCounter.cpp 00003 * Copyright (c) 2020, ZHAW 00004 * All rights reserved. 00005 */ 00006 00007 #include "EncoderCounter.h" 00008 00009 using namespace std; 00010 00011 /** 00012 * Creates and initialises 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 // check pins 00020 00021 if ((a == PA_15) && (b == PB_3)) { 00022 00023 // pinmap OK for TIM2 CH1 and CH2 00024 00025 TIM = TIM2; 00026 00027 // configure reset and clock control registers 00028 00029 RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN; // manually enable port B (port A enabled by mbed library) 00030 00031 // configure general purpose I/O registers 00032 00033 GPIOA->MODER &= ~GPIO_MODER_MODER15; // reset port A15 00034 GPIOA->MODER |= GPIO_MODER_MODER15_1; // set alternate mode of port A15 00035 GPIOA->PUPDR &= ~GPIO_PUPDR_PUPDR15; // reset pull-up/pull-down on port A15 00036 GPIOA->PUPDR |= GPIO_PUPDR_PUPDR15_1; // set input as pull-down 00037 GPIOA->AFR[1] &= ~0xF0000000; // reset alternate function of port A15 00038 GPIOA->AFR[1] |= 1 << 4*7; // set alternate funtion 1 of port A15 00039 00040 GPIOB->MODER &= ~GPIO_MODER_MODER3; // reset port B3 00041 GPIOB->MODER |= GPIO_MODER_MODER3_1; // set alternate mode of port B3 00042 GPIOB->PUPDR &= ~GPIO_PUPDR_PUPDR3; // reset pull-up/pull-down on port B3 00043 GPIOB->PUPDR |= GPIO_PUPDR_PUPDR3_1; // set input as pull-down 00044 GPIOB->AFR[0] &= ~(0xF << 4*3); // reset alternate function of port B3 00045 GPIOB->AFR[0] |= 1 << 4*3; // set alternate funtion 1 of port B3 00046 00047 // configure reset and clock control registers 00048 00049 RCC->APB1RSTR |= RCC_APB1RSTR_TIM2RST; //reset TIM2 controller 00050 RCC->APB1RSTR &= ~RCC_APB1RSTR_TIM2RST; 00051 00052 RCC->APB1ENR |= RCC_APB1ENR_TIM2EN; // TIM2 clock enable 00053 00054 } else if ((a == PB_4) && (b == PC_7)) { 00055 00056 // pinmap OK for TIM3 CH1 and CH2 00057 00058 TIM = TIM3; 00059 00060 // configure reset and clock control registers 00061 00062 RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN; // manually enable port B 00063 RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN; // manually enable port C 00064 00065 // configure general purpose I/O registers 00066 00067 GPIOB->MODER &= ~GPIO_MODER_MODER4; // reset port B4 00068 GPIOB->MODER |= GPIO_MODER_MODER4_1; // set alternate mode of port B4 00069 GPIOB->PUPDR &= ~GPIO_PUPDR_PUPDR4; // reset pull-up/pull-down on port B4 00070 GPIOB->PUPDR |= GPIO_PUPDR_PUPDR4_1; // set input as pull-down 00071 GPIOB->AFR[0] &= ~(0xF << 4*4); // reset alternate function of port B4 00072 GPIOB->AFR[0] |= 2 << 4*4; // set alternate funtion 2 of port B4 00073 00074 GPIOC->MODER &= ~GPIO_MODER_MODER7; // reset port C7 00075 GPIOC->MODER |= GPIO_MODER_MODER7_1; // set alternate mode of port C7 00076 GPIOC->PUPDR &= ~GPIO_PUPDR_PUPDR7; // reset pull-up/pull-down on port C7 00077 GPIOC->PUPDR |= GPIO_PUPDR_PUPDR7_1; // set input as pull-down 00078 GPIOC->AFR[0] &= ~0xF0000000; // reset alternate function of port C7 00079 GPIOC->AFR[0] |= 2 << 4*7; // set alternate funtion 2 of port C7 00080 00081 // configure reset and clock control registers 00082 00083 RCC->APB1RSTR |= RCC_APB1RSTR_TIM3RST; //reset TIM3 controller 00084 RCC->APB1RSTR &= ~RCC_APB1RSTR_TIM3RST; 00085 00086 RCC->APB1ENR |= RCC_APB1ENR_TIM3EN; // TIM3 clock enable 00087 00088 } else if ((a == PD_12) && (b == PD_13)) { 00089 00090 // pinmap OK for TIM4 CH1 and CH2 00091 00092 TIM = TIM4; 00093 00094 // configure reset and clock control registers 00095 00096 RCC->AHB1ENR |= RCC_AHB1ENR_GPIODEN; // manually enable port D 00097 00098 // configure general purpose I/O registers 00099 00100 GPIOD->MODER &= ~GPIO_MODER_MODER12; // reset port D12 00101 GPIOD->MODER |= GPIO_MODER_MODER12_1; // set alternate mode of port D12 00102 GPIOD->PUPDR &= ~GPIO_PUPDR_PUPDR12; // reset pull-up/pull-down on port D12 00103 GPIOD->PUPDR |= GPIO_PUPDR_PUPDR12_1; // set input as pull-down 00104 GPIOD->AFR[1] &= ~(0xF << 4*4); // reset alternate function of port D12 00105 GPIOD->AFR[1] |= 2 << 4*4; // set alternate funtion 2 of port D12 00106 00107 GPIOD->MODER &= ~GPIO_MODER_MODER13; // reset port D13 00108 GPIOD->MODER |= GPIO_MODER_MODER13_1; // set alternate mode of port D13 00109 GPIOD->PUPDR &= ~GPIO_PUPDR_PUPDR13; // reset pull-up/pull-down on port D13 00110 GPIOD->PUPDR |= GPIO_PUPDR_PUPDR13_1; // set input as pull-down 00111 GPIOD->AFR[1] &= ~(0xF << 4*5); // reset alternate function of port D13 00112 GPIOD->AFR[1] |= 2 << 4*5; // set alternate funtion 2 of port D13 00113 00114 // configure reset and clock control registers 00115 00116 RCC->APB1RSTR |= RCC_APB1RSTR_TIM4RST; //reset TIM4 controller 00117 RCC->APB1RSTR &= ~RCC_APB1RSTR_TIM4RST; 00118 00119 RCC->APB1ENR |= RCC_APB1ENR_TIM4EN; // TIM4 clock enable 00120 00121 } else { 00122 00123 printf("pinmap not found for peripheral\n"); 00124 00125 TIM = NULL; 00126 } 00127 00128 // configure general purpose timer 2, 3 or 4 00129 00130 if (TIM != NULL) { 00131 00132 TIM->CR1 = 0x0000; // counter disable 00133 TIM->CR2 = 0x0000; // reset master mode selection 00134 TIM->SMCR = TIM_SMCR_SMS_1 | TIM_SMCR_SMS_0; // counting on both TI1 & TI2 edges 00135 TIM->CCMR1 = TIM_CCMR1_CC2S_0 | TIM_CCMR1_CC1S_0; 00136 TIM->CCMR2 = 0x0000; // reset capture mode register 2 00137 TIM->CCER = TIM_CCER_CC2E | TIM_CCER_CC1E; 00138 TIM->CNT = 0x0000; // reset counter value 00139 TIM->ARR = 0xFFFF; // auto reload register 00140 TIM->CR1 = TIM_CR1_CEN; // counter enable 00141 } 00142 } 00143 00144 /** 00145 * Deletes this EncoderCounter object. 00146 */ 00147 EncoderCounter::~EncoderCounter() {} 00148 00149 /** 00150 * Resets the counter value to zero. 00151 */ 00152 void EncoderCounter::reset() { 00153 00154 TIM->CNT = 0x0000; 00155 } 00156 00157 /** 00158 * Resets the counter value to a given offset value. 00159 * @param offset the offset value to reset the counter to. 00160 */ 00161 void EncoderCounter::reset(short offset) { 00162 00163 TIM->CNT = -offset; 00164 } 00165 00166 /** 00167 * Reads the quadrature encoder counter value. 00168 * @return the quadrature encoder counter as a signed 16-bit integer value. 00169 */ 00170 short EncoderCounter::read() { 00171 00172 return (short)(-TIM->CNT); 00173 } 00174 00175 /** 00176 * The empty operator is a shorthand notation of the <code>read()</code> method. 00177 */ 00178 EncoderCounter::operator short() { 00179 00180 return read(); 00181 } 00182
Generated on Sat Jul 23 2022 17:01:00 by
