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