kings / Mbed 2 deprecated biszueimchlotz

Dependencies:   Servo mbed

Fork of DrehungMitStopp by kings

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers MotorEncoder.cpp Source File

MotorEncoder.cpp

00001 #include "MotorEncoder.h"
00002 
00003 //E. Hess
00004 //MotorEncoder.h
00005 
00006 using namespace std;
00007 
00008 // Erstellt einen Treiber um den Encoder zu lesen und initialisiert ihn
00009 // PinName a ist der Input für Kanal A
00010 // PinName b ist der Input für Kanal B
00011 
00012 MotorEncoder::MotorEncoder(PinName a, PinName b) {
00013     // Überprüft die Inputs
00014     if ((a == PA_6) && (b == PC_7)) {   // Rechter Motor
00015         // pinmap OK for TIM3 CH1 and CH2
00016         TIM = TIM3;
00017         
00018         // Kunfiguration vom Reset und der Taktsteuerung
00019         RCC->AHB1ENR |= RCC_AHB1ENR_GPIOCEN;    // manually enable port C (port A enabled by mbed library)
00020         
00021         // configure general purpose I/O registers
00022         
00023         GPIOA->MODER &= ~GPIO_MODER_MODER6;     // reset port A6
00024         GPIOA->MODER |= GPIO_MODER_MODER6_1;    // set alternate mode of port A6
00025         GPIOA->PUPDR &= ~GPIO_PUPDR_PUPDR6;     // reset pull-up/pull-down on port A6
00026         GPIOA->PUPDR |= GPIO_PUPDR_PUPDR6_1;    // set input as pull-down
00027         GPIOA->AFR[0] &= ~(0xF << 4*6);         // reset alternate function of port A6
00028         GPIOA->AFR[0] |= 2 << 4*6;              // set alternate funtion 2 of port A6
00029         
00030         GPIOC->MODER &= ~GPIO_MODER_MODER7;     // reset port C7
00031         GPIOC->MODER |= GPIO_MODER_MODER7_1;    // set alternate mode of port C7
00032         GPIOC->PUPDR &= ~GPIO_PUPDR_PUPDR7;     // reset pull-up/pull-down on port C7
00033         GPIOC->PUPDR |= GPIO_PUPDR_PUPDR7_1;    // set input as pull-down
00034         GPIOC->AFR[0] &= ~0xF0000000;           // reset alternate function of port C7
00035         GPIOC->AFR[0] |= 2 << 4*7;              // set alternate funtion 2 of port C7
00036         
00037         // configure reset and clock control registers
00038         
00039         RCC->APB1RSTR |= RCC_APB1RSTR_TIM3RST;  //reset TIM3 controller
00040         RCC->APB1RSTR &= ~RCC_APB1RSTR_TIM3RST;
00041         
00042         RCC->APB1ENR |= RCC_APB1ENR_TIM3EN;     // TIM3 clock enable
00043         
00044     } else if ((a == PB_6) && (b == PB_7)) {    // Links Motor
00045         
00046         // pinmap OK for TIM4 CH1 and CH2
00047         
00048         TIM = TIM4;
00049         
00050         // configure reset and clock control registers
00051         
00052         RCC->AHB1ENR |= RCC_AHB1ENR_GPIOBEN;    // manually enable port B (port A enabled by mbed library)
00053         
00054         // configure general purpose I/O registers
00055         
00056         GPIOB->MODER &= ~GPIO_MODER_MODER6;     // reset port B6
00057         GPIOB->MODER |= GPIO_MODER_MODER6_1;    // set alternate mode of port B6
00058         GPIOB->PUPDR &= ~GPIO_PUPDR_PUPDR6;     // reset pull-up/pull-down on port B6
00059         GPIOB->PUPDR |= GPIO_PUPDR_PUPDR6_1;    // set input as pull-down
00060         GPIOB->AFR[0] &= ~(0xF << 4*6);         // reset alternate function of port B6
00061         GPIOB->AFR[0] |= 2 << 4*6;              // set alternate funtion 2 of port B6
00062         
00063         GPIOB->MODER &= ~GPIO_MODER_MODER7;     // reset port B7
00064         GPIOB->MODER |= GPIO_MODER_MODER7_1;    // set alternate mode of port B7
00065         GPIOB->PUPDR &= ~GPIO_PUPDR_PUPDR7;     // reset pull-up/pull-down on port B7
00066         GPIOB->PUPDR |= GPIO_PUPDR_PUPDR7_1;    // set input as pull-down
00067         GPIOB->AFR[0] &= ~0xF0000000;           // reset alternate function of port B7
00068         GPIOB->AFR[0] |= 2 << 4*7;              // set alternate funtion 2 of port B7
00069         
00070         // configure reset and clock control registers
00071         
00072         RCC->APB1RSTR |= RCC_APB1RSTR_TIM4RST;  //reset TIM4 controller
00073         RCC->APB1RSTR &= ~RCC_APB1RSTR_TIM4RST;
00074         
00075         RCC->APB1ENR |= RCC_APB1ENR_TIM4EN;     // TIM4 clock enable
00076         
00077     } else {
00078         printf("Pinmap konnte nicht gefunden werden!\n");
00079     }
00080     
00081     // configure general purpose timer 3 or 4
00082     
00083     TIM->CR1 = 0x0000;          // counter disable
00084     TIM->CR2 = 0x0000;          // reset master mode selection
00085     TIM->SMCR = TIM_SMCR_SMS_1 | TIM_SMCR_SMS_0; // counting on both TI1 & TI2 edges
00086     TIM->CCMR1 = TIM_CCMR1_CC2S_0 | TIM_CCMR1_CC1S_0;
00087     TIM->CCMR2 = 0x0000;        // reset capture mode register 2
00088     TIM->CCER = TIM_CCER_CC2E | TIM_CCER_CC1E;
00089     TIM->CNT = 0x0000;          // reset counter value
00090     TIM->ARR = 0xFFFF;          // auto reload register
00091     TIM->CR1 = TIM_CR1_CEN;     // counter enable
00092 }
00093 
00094 MotorEncoder::~MotorEncoder() {}
00095 
00096 // Setzt den Zähler auf NULL
00097 void MotorEncoder::reset() {
00098     
00099     TIM->CNT = 0x0000;
00100 }
00101 
00102 // Setzt den Zähler auf NULL zurück
00103 void MotorEncoder::reset(short offset) {
00104     TIM->CNT = -offset;
00105 }
00106 
00107 // Liest den Zähler
00108 short MotorEncoder::read() {
00109     return (short)(-TIM->CNT);
00110 }
00111 
00112 // Kurznotation für read()
00113 MotorEncoder::operator short() {
00114     return read();
00115 }