![](/media/cache/group/default_image.jpg.50x50_q85.jpg)
hallo
Fork of PES1 by
EncoderCounter/EncoderCounter.cpp@21:69ee872b8ee9, 2017-05-16 (annotated)
- Committer:
- itslinear
- Date:
- Tue May 16 16:09:08 2017 +0000
- Revision:
- 21:69ee872b8ee9
- Parent:
- 18:3ee1b02ed3aa
sieg
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
itslinear | 18:3ee1b02ed3aa | 1 | /* |
itslinear | 18:3ee1b02ed3aa | 2 | * EncoderCounter.cpp |
itslinear | 18:3ee1b02ed3aa | 3 | * Copyright (c) 2016, ZHAW |
itslinear | 18:3ee1b02ed3aa | 4 | * All rights reserved. |
itslinear | 18:3ee1b02ed3aa | 5 | */ |
itslinear | 18:3ee1b02ed3aa | 6 | |
itslinear | 18:3ee1b02ed3aa | 7 | #include "EncoderCounter.h" |
itslinear | 18:3ee1b02ed3aa | 8 | |
itslinear | 18:3ee1b02ed3aa | 9 | using namespace std; |
itslinear | 18:3ee1b02ed3aa | 10 | |
itslinear | 18:3ee1b02ed3aa | 11 | /** |
itslinear | 18:3ee1b02ed3aa | 12 | * Creates and initializes the driver to read the quadrature |
itslinear | 18:3ee1b02ed3aa | 13 | * encoder counter of the STM32 microcontroller. |
itslinear | 18:3ee1b02ed3aa | 14 | * @param a the input pin for the channel A. |
itslinear | 18:3ee1b02ed3aa | 15 | * @param b the input pin for the channel B. |
itslinear | 18:3ee1b02ed3aa | 16 | */ |
itslinear | 18:3ee1b02ed3aa | 17 | EncoderCounter::EncoderCounter(PinName a, PinName b) { |
itslinear | 18:3ee1b02ed3aa | 18 | |
itslinear | 18:3ee1b02ed3aa | 19 | // check pins |
itslinear | 18:3ee1b02ed3aa | 20 | |
itslinear | 18:3ee1b02ed3aa | 21 | if ((a == PA_6) && (b == PC_7)) { |
itslinear | 18:3ee1b02ed3aa | 22 | |
itslinear | 18:3ee1b02ed3aa | 23 | // pinmap OK for TIM3 CH1 and CH2 |
itslinear | 18:3ee1b02ed3aa | 24 | |
itslinear | 18:3ee1b02ed3aa | 25 | TIM = TIM3; |
itslinear | 18:3ee1b02ed3aa | 26 | |
itslinear | 18:3ee1b02ed3aa | 27 | // configure reset and clock control registers |
itslinear | 18:3ee1b02ed3aa | 28 | |
itslinear | 18:3ee1b02ed3aa | 29 | RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN; // manually enable port C (port A enabled by mbed library) |
itslinear | 18:3ee1b02ed3aa | 30 | |
itslinear | 18:3ee1b02ed3aa | 31 | // configure general purpose I/O registers |
itslinear | 18:3ee1b02ed3aa | 32 | |
itslinear | 18:3ee1b02ed3aa | 33 | GPIOA->MODER &= ~GPIO_MODER_MODER6; // reset port A6 |
itslinear | 18:3ee1b02ed3aa | 34 | GPIOA->MODER |= GPIO_MODER_MODER6_1; // set alternate mode of port A6 |
itslinear | 18:3ee1b02ed3aa | 35 | GPIOA->PUPDR &= ~GPIO_PUPDR_PUPDR6; // reset pull-up/pull-down on port A6 |
itslinear | 18:3ee1b02ed3aa | 36 | GPIOA->PUPDR |= GPIO_PUPDR_PUPDR6_1; // set input as pull-down |
itslinear | 18:3ee1b02ed3aa | 37 | GPIOA->AFR[0] &= ~(0xF << 4*6); // reset alternate function of port A6 |
itslinear | 18:3ee1b02ed3aa | 38 | GPIOA->AFR[0] |= 2 << 4*6; // set alternate funtion 2 of port A6 |
itslinear | 18:3ee1b02ed3aa | 39 | |
itslinear | 18:3ee1b02ed3aa | 40 | GPIOC->MODER &= ~GPIO_MODER_MODER7; // reset port C7 |
itslinear | 18:3ee1b02ed3aa | 41 | GPIOC->MODER |= GPIO_MODER_MODER7_1; // set alternate mode of port C7 |
itslinear | 18:3ee1b02ed3aa | 42 | GPIOC->PUPDR &= ~GPIO_PUPDR_PUPDR7; // reset pull-up/pull-down on port C7 |
itslinear | 18:3ee1b02ed3aa | 43 | GPIOC->PUPDR |= GPIO_PUPDR_PUPDR7_1; // set input as pull-down |
itslinear | 18:3ee1b02ed3aa | 44 | GPIOC->AFR[0] &= ~0xF0000000; // reset alternate function of port C7 |
itslinear | 18:3ee1b02ed3aa | 45 | GPIOC->AFR[0] |= 2 << 4*7; // set alternate funtion 2 of port C7 |
itslinear | 18:3ee1b02ed3aa | 46 | |
itslinear | 18:3ee1b02ed3aa | 47 | // configure reset and clock control registers |
itslinear | 18:3ee1b02ed3aa | 48 | |
itslinear | 18:3ee1b02ed3aa | 49 | RCC->APB1RSTR |= RCC_APB1RSTR_TIM3RST; //reset TIM3 controller |
itslinear | 18:3ee1b02ed3aa | 50 | RCC->APB1RSTR &= ~RCC_APB1RSTR_TIM3RST; |
itslinear | 18:3ee1b02ed3aa | 51 | |
itslinear | 18:3ee1b02ed3aa | 52 | RCC->APB1ENR |= RCC_APB1ENR_TIM3EN; // TIM3 clock enable |
itslinear | 18:3ee1b02ed3aa | 53 | |
itslinear | 18:3ee1b02ed3aa | 54 | } else if ((a == PB_6) && (b == PB_7)) { |
itslinear | 18:3ee1b02ed3aa | 55 | |
itslinear | 18:3ee1b02ed3aa | 56 | // pinmap OK for TIM4 CH1 and CH2 |
itslinear | 18:3ee1b02ed3aa | 57 | |
itslinear | 18:3ee1b02ed3aa | 58 | TIM = TIM4; |
itslinear | 18:3ee1b02ed3aa | 59 | |
itslinear | 18:3ee1b02ed3aa | 60 | // configure reset and clock control registers |
itslinear | 18:3ee1b02ed3aa | 61 | |
itslinear | 18:3ee1b02ed3aa | 62 | RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN; // manually enable port B (port A enabled by mbed library) |
itslinear | 18:3ee1b02ed3aa | 63 | |
itslinear | 18:3ee1b02ed3aa | 64 | // configure general purpose I/O registers |
itslinear | 18:3ee1b02ed3aa | 65 | |
itslinear | 18:3ee1b02ed3aa | 66 | GPIOB->MODER &= ~GPIO_MODER_MODER6; // reset port B6 |
itslinear | 18:3ee1b02ed3aa | 67 | GPIOB->MODER |= GPIO_MODER_MODER6_1; // set alternate mode of port B6 |
itslinear | 18:3ee1b02ed3aa | 68 | GPIOB->PUPDR &= ~GPIO_PUPDR_PUPDR6; // reset pull-up/pull-down on port B6 |
itslinear | 18:3ee1b02ed3aa | 69 | GPIOB->PUPDR |= GPIO_PUPDR_PUPDR6_1; // set input as pull-down |
itslinear | 18:3ee1b02ed3aa | 70 | GPIOB->AFR[0] &= ~(0xF << 4*6); // reset alternate function of port B6 |
itslinear | 18:3ee1b02ed3aa | 71 | GPIOB->AFR[0] |= 2 << 4*6; // set alternate funtion 2 of port B6 |
itslinear | 18:3ee1b02ed3aa | 72 | |
itslinear | 18:3ee1b02ed3aa | 73 | GPIOB->MODER &= ~GPIO_MODER_MODER7; // reset port B7 |
itslinear | 18:3ee1b02ed3aa | 74 | GPIOB->MODER |= GPIO_MODER_MODER7_1; // set alternate mode of port B7 |
itslinear | 18:3ee1b02ed3aa | 75 | GPIOB->PUPDR &= ~GPIO_PUPDR_PUPDR7; // reset pull-up/pull-down on port B7 |
itslinear | 18:3ee1b02ed3aa | 76 | GPIOB->PUPDR |= GPIO_PUPDR_PUPDR7_1; // set input as pull-down |
itslinear | 18:3ee1b02ed3aa | 77 | GPIOB->AFR[0] &= ~0xF0000000; // reset alternate function of port B7 |
itslinear | 18:3ee1b02ed3aa | 78 | GPIOB->AFR[0] |= 2 << 4*7; // set alternate funtion 2 of port B7 |
itslinear | 18:3ee1b02ed3aa | 79 | |
itslinear | 18:3ee1b02ed3aa | 80 | // configure reset and clock control registers |
itslinear | 18:3ee1b02ed3aa | 81 | |
itslinear | 18:3ee1b02ed3aa | 82 | RCC->APB1RSTR |= RCC_APB1RSTR_TIM4RST; //reset TIM4 controller |
itslinear | 18:3ee1b02ed3aa | 83 | RCC->APB1RSTR &= ~RCC_APB1RSTR_TIM4RST; |
itslinear | 18:3ee1b02ed3aa | 84 | |
itslinear | 18:3ee1b02ed3aa | 85 | RCC->APB1ENR |= RCC_APB1ENR_TIM4EN; // TIM4 clock enable |
itslinear | 18:3ee1b02ed3aa | 86 | |
itslinear | 18:3ee1b02ed3aa | 87 | } else { |
itslinear | 18:3ee1b02ed3aa | 88 | |
itslinear | 18:3ee1b02ed3aa | 89 | printf("pinmap not found for peripheral\n"); |
itslinear | 18:3ee1b02ed3aa | 90 | } |
itslinear | 18:3ee1b02ed3aa | 91 | |
itslinear | 18:3ee1b02ed3aa | 92 | // configure general purpose timer 3 or 4 |
itslinear | 18:3ee1b02ed3aa | 93 | |
itslinear | 18:3ee1b02ed3aa | 94 | TIM->CR1 = 0x0000; // counter disable |
itslinear | 18:3ee1b02ed3aa | 95 | TIM->CR2 = 0x0000; // reset master mode selection |
itslinear | 18:3ee1b02ed3aa | 96 | TIM->SMCR = TIM_SMCR_SMS_1 | TIM_SMCR_SMS_0; // counting on both TI1 & TI2 edges |
itslinear | 18:3ee1b02ed3aa | 97 | TIM->CCMR1 = TIM_CCMR1_CC2S_0 | TIM_CCMR1_CC1S_0; |
itslinear | 18:3ee1b02ed3aa | 98 | TIM->CCMR2 = 0x0000; // reset capture mode register 2 |
itslinear | 18:3ee1b02ed3aa | 99 | TIM->CCER = TIM_CCER_CC2E | TIM_CCER_CC1E; |
itslinear | 18:3ee1b02ed3aa | 100 | TIM->CNT = 0x0000; // reset counter value |
itslinear | 18:3ee1b02ed3aa | 101 | TIM->ARR = 0xFFFF; // auto reload register |
itslinear | 18:3ee1b02ed3aa | 102 | TIM->CR1 = TIM_CR1_CEN; // counter enable |
itslinear | 18:3ee1b02ed3aa | 103 | } |
itslinear | 18:3ee1b02ed3aa | 104 | |
itslinear | 18:3ee1b02ed3aa | 105 | EncoderCounter::~EncoderCounter() {} |
itslinear | 18:3ee1b02ed3aa | 106 | |
itslinear | 18:3ee1b02ed3aa | 107 | /** |
itslinear | 18:3ee1b02ed3aa | 108 | * Resets the counter value to zero. |
itslinear | 18:3ee1b02ed3aa | 109 | */ |
itslinear | 18:3ee1b02ed3aa | 110 | void EncoderCounter::reset() { |
itslinear | 18:3ee1b02ed3aa | 111 | |
itslinear | 18:3ee1b02ed3aa | 112 | TIM->CNT = 0x0000; |
itslinear | 18:3ee1b02ed3aa | 113 | } |
itslinear | 18:3ee1b02ed3aa | 114 | |
itslinear | 18:3ee1b02ed3aa | 115 | /** |
itslinear | 18:3ee1b02ed3aa | 116 | * Resets the counter value to a given offset value. |
itslinear | 18:3ee1b02ed3aa | 117 | * @param offset the offset value to reset the counter to. |
itslinear | 18:3ee1b02ed3aa | 118 | */ |
itslinear | 18:3ee1b02ed3aa | 119 | void EncoderCounter::reset(short offset) { |
itslinear | 18:3ee1b02ed3aa | 120 | |
itslinear | 18:3ee1b02ed3aa | 121 | TIM->CNT = -offset; |
itslinear | 18:3ee1b02ed3aa | 122 | } |
itslinear | 18:3ee1b02ed3aa | 123 | |
itslinear | 18:3ee1b02ed3aa | 124 | /** |
itslinear | 18:3ee1b02ed3aa | 125 | * Reads the quadrature encoder counter value. |
itslinear | 18:3ee1b02ed3aa | 126 | * @return the quadrature encoder counter as a signed 16-bit integer value. |
itslinear | 18:3ee1b02ed3aa | 127 | */ |
itslinear | 18:3ee1b02ed3aa | 128 | short EncoderCounter::read() { |
itslinear | 18:3ee1b02ed3aa | 129 | |
itslinear | 18:3ee1b02ed3aa | 130 | return (short)(-TIM->CNT); |
itslinear | 18:3ee1b02ed3aa | 131 | } |
itslinear | 18:3ee1b02ed3aa | 132 | |
itslinear | 18:3ee1b02ed3aa | 133 | /** |
itslinear | 18:3ee1b02ed3aa | 134 | * The empty operator is a shorthand notation of the <code>read()</code> method. |
itslinear | 18:3ee1b02ed3aa | 135 | */ |
itslinear | 18:3ee1b02ed3aa | 136 | EncoderCounter::operator short() { |
itslinear | 18:3ee1b02ed3aa | 137 | |
itslinear | 18:3ee1b02ed3aa | 138 | return read(); |
itslinear | 18:3ee1b02ed3aa | 139 | } |