Variation Nigel Webb's. AB encoders, using pullups and diodes for connecting 5V (or higher voltage) encoders.
Fork of HardwareQuadratureEncoderABZ by
main.cpp
- Committer:
- misan
- Date:
- 2015-01-25
- Revision:
- 1:e63be50a9453
- Parent:
- 0:25c34018702c
File content as of revision 1:e63be50a9453:
#include "mbed.h" // Hardware Quadrature Encoder ABZ for Nucleo F401RE // Output on debug port to host PC @ 9600 baud // // By Nigel Webb, November 2014 // Modified by Miguel Sánchez, January 2015 /* Connections PA_0 = Encoder A PA_1 = Encoder B PA_4 = Encoder Z */ // ZPulse(PA_4) ; // Setup Interrupt for Z Pulse --> no index needed here DigitalIn A(PA_0); DigitalIn B(PA_1); void EncoderInitialise(void) { // configure GPIO PA0 & PA1 as inputs for Encoder RCC->AHB1ENR |= 0x00000001; // Enable clock for GPIOA GPIOA->MODER |= GPIO_MODER_MODER0_1 | GPIO_MODER_MODER1_1 ; //PA0 & PA1 as Alternate Function /*!< GPIO port mode register, Address offset: 0x00 */ GPIOA->OTYPER |= GPIO_OTYPER_OT_0 | GPIO_OTYPER_OT_1 ; //PA0 & PA1 as Inputs /*!< GPIO port output type register, Address offset: 0x04 */ GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR0 | GPIO_OSPEEDER_OSPEEDR1 ; // Low speed /*!< GPIO port output speed register, Address offset: 0x08 */ GPIOA->PUPDR |= GPIO_PUPDR_PUPDR0_1 | GPIO_PUPDR_PUPDR1_1 ; // Pull Down /*!< GPIO port pull-up/pull-down register, Address offset: 0x0C */ GPIOA->AFR[0] |= 0x00000011 ; // AF01 for PA0 & PA1 /*!< GPIO alternate function registers, Address offset: 0x20-0x24 */ GPIOA->AFR[1] |= 0x00000000 ; // /*!< GPIO alternate function registers, Address offset: 0x20-0x24 */ // configure TIM2 as Encoder input RCC->APB1ENR |= 0x00000001; // Enable clock for TIM2 TIM2->CR1 = 0x0001; // CEN(Counter ENable)='1' < TIM control register 1 TIM2->SMCR = 0x0003; // SMS='011' (Encoder mode 3) < TIM slave mode control register TIM2->CCMR1 = 0xF1F1; // CC1S='01' CC2S='01' < TIM capture/compare mode register 1 TIM2->CCMR2 = 0x0000; // < TIM capture/compare mode register 2 TIM2->CCER = 0x0011; // CC1P CC2P < TIM capture/compare enable register TIM2->PSC = 0x0000; // Prescaler = (0+1) < TIM prescaler TIM2->ARR = 0xffffffff; // reload at 0xfffffff < TIM auto-reload register TIM2->CNT = 0x0000; //reset the counter before we use it } void EncoderInitialiseTIM3(void) { // configure GPIO PA0 & PA1 aka A0 & A1 as inputs for Encoder // Enable clock for GPIOA __GPIOA_CLK_ENABLE(); //equivalent from hal_rcc.h //stm32f4xx.h GPIOA->MODER |= GPIO_MODER_MODER6_1 | GPIO_MODER_MODER7_1 ; //PA6 & PA7 as Alternate Function /*!< GPIO port mode register, Address offset: 0x00 */ GPIOA->OTYPER |= GPIO_OTYPER_OT_6 | GPIO_OTYPER_OT_7 ; //PA6 & PA7 as Inputs /*!< GPIO port output type register, Address offset: 0x04 */ GPIOA->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR6 | GPIO_OSPEEDER_OSPEEDR7 ; //Low speed /*!< GPIO port output speed register, Address offset: 0x08 */ GPIOA->PUPDR |= GPIO_PUPDR_PUPDR6_1 | GPIO_PUPDR_PUPDR7_1 ; //Pull Down /*!< GPIO port pull-up/pull-down register, Address offset: 0x0C */ GPIOA->AFR[0] |= 0x22000000 ; //AF02 for PA6 & PA7 /*!< GPIO alternate function registers, Address offset: 0x20-0x24 */ GPIOA->AFR[1] |= 0x00000000 ; //nibbles here refer to gpio8..15 /*!< GPIO alternate function registers, Address offset: 0x20-0x24 */ // configure TIM3 as Encoder input // Enable clock for TIM3 __TIM3_CLK_ENABLE(); TIM3->CR1 = 0x0001; // CEN(Counter ENable)='1' < TIM control register 1 TIM3->SMCR = TIM_ENCODERMODE_TI12; // SMS='011' (Encoder mode 3) < TIM slave mode control register TIM3->CCMR1 = 0xF1F1; // CC1S='01' CC2S='01' < TIM capture/compare mode register 1 TIM3->CCMR2 = 0x0000; // < TIM capture/compare mode register 2 TIM3->CCER = 0x0011; // CC1P CC2P < TIM capture/compare enable register TIM3->PSC = 0x0000; // Prescaler = (0+1) < TIM prescaler TIM3->ARR = 0xffffffff; // reload at 0xfffffff < TIM auto-reload register TIM3->CNT = 0x0000; //reset the counter before we use it } void EncoderInitialiseTIM4(void) { //PB6 PB7 aka D10 MORPHO_PB7 // Enable clock for GPIOA __GPIOB_CLK_ENABLE(); //equivalent from hal_rcc.h //stm32f4xx.h GPIOB->MODER |= GPIO_MODER_MODER6_1 | GPIO_MODER_MODER7_1 ; //PB6 & PB7 as Alternate Function /*!< GPIO port mode register, Address offset: 0x00 */ GPIOB->OTYPER |= GPIO_OTYPER_OT_6 | GPIO_OTYPER_OT_7 ; //PB6 & PB7 as Inputs /*!< GPIO port output type register, Address offset: 0x04 */ GPIOB->OSPEEDR |= GPIO_OSPEEDER_OSPEEDR6 | GPIO_OSPEEDER_OSPEEDR7 ; //Low speed /*!< GPIO port output speed register, Address offset: 0x08 */ GPIOB->PUPDR |= GPIO_PUPDR_PUPDR6_1 | GPIO_PUPDR_PUPDR7_1 ; //Pull Down /*!< GPIO port pull-up/pull-down register, Address offset: 0x0C */ GPIOB->AFR[0] |= 0x22000000 ; //AF02 for PB6 & PB7 /*!< GPIO alternate function registers, Address offset: 0x20-0x24 */ GPIOB->AFR[1] |= 0x00000000 ; //nibbles here refer to gpio8..15 /*!< GPIO alternate function registers, Address offset: 0x20-0x24 */ // configure TIM4 as Encoder input // Enable clock for TIM4 __TIM4_CLK_ENABLE(); TIM4->CR1 = 0x0001; // CEN(Counter ENable)='1' < TIM control register 1 TIM4->SMCR = TIM_ENCODERMODE_TI12; // < TIM slave mode control register //TIM_ENCODERMODE_TI1 input 1 edges trigger count //TIM_ENCODERMODE_TI2 input 2 edges trigger count //TIM_ENCODERMODE_TI12 all edges trigger count TIM4->CCMR1 = 0xF1F1; // CC1S='01' CC2S='01' < TIM capture/compare mode register 1 //0xF nibble sets up filter TIM4->CCMR2 = 0x0000; // < TIM capture/compare mode register 2 TIM4->CCER = TIM_CCER_CC1E | TIM_CCER_CC2E; // < TIM capture/compare enable register TIM4->PSC = 0x0000; // Prescaler = (0+1) < TIM prescaler TIM4->ARR = 0xffff; // reload at 0xfffffff < TIM auto-reload register TIM4->CNT = 0x0000; //reset the counter before we use it } /* // Z Pulse routine void ZeroEncoderCount() { TIM2->CNT=0 ; //reset count to zero } */ int main() { EncoderInitialise() ; //ZPulse.rise(&ZeroEncoderCount) ; //Setup Interrupt for rising edge of Z pulse //ZPulse.mode(PullDown) ; // Set input as pull down A.mode(PullUp); B.mode(PullUp); unsigned int EncoderPosition ; while (true) { // Print Encoder Quadrature count to debug port every 0.5 seconds EncoderPosition = TIM2->CNT ; // Get current position from Encoder printf("Encoder Position %i\r\n", EncoderPosition); wait(0.5); } }