Variation Nigel Webb's. AB encoders, using pullups and diodes for connecting 5V (or higher voltage) encoders.
Fork of HardwareQuadratureEncoderABZ by
Revision 1:e63be50a9453, committed 2015-01-25
- Comitter:
- misan
- Date:
- Sun Jan 25 23:01:30 2015 +0000
- Parent:
- 0:25c34018702c
- Commit message:
- Adapting Nigel Webb code for my encoders, using internal pullups and a diode for the connection of the encoder inputs. No index signal though.
Changed in this revision
main.cpp | Show annotated file Show diff for this revision Revisions of this file |
mbed.bld | Show annotated file Show diff for this revision Revisions of this file |
diff -r 25c34018702c -r e63be50a9453 main.cpp --- a/main.cpp Mon Nov 17 19:17:21 2014 +0000 +++ b/main.cpp Sun Jan 25 23:01:30 2015 +0000 @@ -4,6 +4,7 @@ // 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 @@ -11,7 +12,11 @@ PA_4 = Encoder Z */ -InterruptIn ZPulse(PA_4) ; // Setup Interrupt for Z Pulse +// 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 @@ -38,16 +43,86 @@ 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 + //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 ;
diff -r 25c34018702c -r e63be50a9453 mbed.bld --- a/mbed.bld Mon Nov 17 19:17:21 2014 +0000 +++ b/mbed.bld Sun Jan 25 23:01:30 2015 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/mbed_official/code/mbed/builds/031413cf7a89 \ No newline at end of file +http://mbed.org/users/mbed_official/code/mbed/builds/4fc01daae5a5 \ No newline at end of file