fdsaf
hallsensor_hardware_decoder.h@0:0532a6e10a23, 2019-03-13 (annotated)
- Committer:
- bobolee1239
- Date:
- Wed Mar 13 06:22:03 2019 +0000
- Revision:
- 0:0532a6e10a23
revised original code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bobolee1239 | 0:0532a6e10a23 | 1 | #ifndef _HALLSENSOR_HARDWARE_DECODER_H |
bobolee1239 | 0:0532a6e10a23 | 2 | #define _HALLSENSOR_HARDWARE_DECODER_H |
bobolee1239 | 0:0532a6e10a23 | 3 | |
bobolee1239 | 0:0532a6e10a23 | 4 | |
bobolee1239 | 0:0532a6e10a23 | 5 | |
bobolee1239 | 0:0532a6e10a23 | 6 | #include "mbed.h" |
bobolee1239 | 0:0532a6e10a23 | 7 | |
bobolee1239 | 0:0532a6e10a23 | 8 | // Hardware Quadrature Encoder ABZ for Nucleo F401RE |
bobolee1239 | 0:0532a6e10a23 | 9 | // Output on debug port to host PC @ 9600 baud |
bobolee1239 | 0:0532a6e10a23 | 10 | |
bobolee1239 | 0:0532a6e10a23 | 11 | |
bobolee1239 | 0:0532a6e10a23 | 12 | /* Connections |
bobolee1239 | 0:0532a6e10a23 | 13 | PA_0 = Encoder A |
bobolee1239 | 0:0532a6e10a23 | 14 | PA_1 = Encoder B |
bobolee1239 | 0:0532a6e10a23 | 15 | PA_4 = Encoder Z |
bobolee1239 | 0:0532a6e10a23 | 16 | */ |
bobolee1239 | 0:0532a6e10a23 | 17 | |
bobolee1239 | 0:0532a6e10a23 | 18 | InterruptIn ZPulse(PA_4) ; // Setup Interrupt for Z Pulse |
bobolee1239 | 0:0532a6e10a23 | 19 | |
bobolee1239 | 0:0532a6e10a23 | 20 | |
bobolee1239 | 0:0532a6e10a23 | 21 | void EncoderInitialise(void) |
bobolee1239 | 0:0532a6e10a23 | 22 | { |
bobolee1239 | 0:0532a6e10a23 | 23 | // configure GPIO PA0 & PA1 as inputs for Encoder |
bobolee1239 | 0:0532a6e10a23 | 24 | RCC->AHB1ENR |= 0x00000001; // Enable clock for GPIOA |
bobolee1239 | 0:0532a6e10a23 | 25 | |
bobolee1239 | 0:0532a6e10a23 | 26 | GPIOA->MODER |= 0x0000000A;//GPIO_MODER_MODER0_1 | GPIO_MODER_MODER1_1 ; //PA0 & PA1 as Alternate Function /*!< GPIO port mode register, Address offset: 0x00 */ |
bobolee1239 | 0:0532a6e10a23 | 27 | GPIOA->OTYPER |= 0x00000003;//GPIO_OTYPER_OT_0 | GPIO_OTYPER_OT_1 ; //PA0 & PA1 as Inputs /*!< GPIO port output type register, Address offset: 0x04 */ |
bobolee1239 | 0:0532a6e10a23 | 28 | GPIOA->OSPEEDR |= 0x0000000F;//high speed //GPIO_OSPEEDER_OSPEEDR0 | GPIO_OSPEEDER_OSPEEDR1 ; // Low speed /*!< GPIO port output speed register, Address offset: 0x08 */ |
bobolee1239 | 0:0532a6e10a23 | 29 | GPIOA->PUPDR |= 0x00000005;//pull high //GPIO_PUPDR_PUPDR0_1 | GPIO_PUPDR_PUPDR1_1 ; // Pull Down /*!< GPIO port pull-up/pull-down register, Address offset: 0x0C */ |
bobolee1239 | 0:0532a6e10a23 | 30 | GPIOA->AFR[0] |= 0x00000011 ; // AF01 for PA0 & PA1 /*!< GPIO alternate function registers, Address offset: 0x20-0x24 */ |
bobolee1239 | 0:0532a6e10a23 | 31 | GPIOA->AFR[1] |= 0x00000000 ; // /*!< GPIO alternate function registers, Address offset: 0x20-0x24 */ |
bobolee1239 | 0:0532a6e10a23 | 32 | |
bobolee1239 | 0:0532a6e10a23 | 33 | // configure TIM2 as Encoder input |
bobolee1239 | 0:0532a6e10a23 | 34 | RCC->APB1ENR |= 0x00000001; // Enable clock for TIM2 |
bobolee1239 | 0:0532a6e10a23 | 35 | |
bobolee1239 | 0:0532a6e10a23 | 36 | TIM2->CR1 = 0x0001; // CEN(Counter ENable)='1' < TIM control register 1 |
bobolee1239 | 0:0532a6e10a23 | 37 | TIM2->SMCR = 0x0003; // SMS='011' (Encoder mode 3) < TIM slave mode control register |
bobolee1239 | 0:0532a6e10a23 | 38 | TIM2->CCMR1 = 0xF1F1; // CC1S='01' CC2S='01' < TIM capture/compare mode register 1 |
bobolee1239 | 0:0532a6e10a23 | 39 | TIM2->CCMR2 = 0x0000; // < TIM capture/compare mode register 2 |
bobolee1239 | 0:0532a6e10a23 | 40 | TIM2->CCER = 0x0011; // CC1P CC2P < TIM capture/compare enable register |
bobolee1239 | 0:0532a6e10a23 | 41 | TIM2->PSC = 0x0000; // Prescaler = (0+1) < TIM prescaler |
bobolee1239 | 0:0532a6e10a23 | 42 | TIM2->ARR = 0xffffffff; // reload at 0xfffffff < TIM auto-reload register |
bobolee1239 | 0:0532a6e10a23 | 43 | |
bobolee1239 | 0:0532a6e10a23 | 44 | TIM2->CNT = 0x0000; //reset the counter before we use it |
bobolee1239 | 0:0532a6e10a23 | 45 | } |
bobolee1239 | 0:0532a6e10a23 | 46 | |
bobolee1239 | 0:0532a6e10a23 | 47 | void EncoderInitialise_HAL(void) |
bobolee1239 | 0:0532a6e10a23 | 48 | { |
bobolee1239 | 0:0532a6e10a23 | 49 | |
bobolee1239 | 0:0532a6e10a23 | 50 | |
bobolee1239 | 0:0532a6e10a23 | 51 | // init structure |
bobolee1239 | 0:0532a6e10a23 | 52 | TIM_HandleTypeDef htim2; |
bobolee1239 | 0:0532a6e10a23 | 53 | TIM_Encoder_InitTypeDef sConfig; |
bobolee1239 | 0:0532a6e10a23 | 54 | TIM_MasterConfigTypeDef sMasterConfig; |
bobolee1239 | 0:0532a6e10a23 | 55 | GPIO_InitTypeDef GPIO_InitStruct; |
bobolee1239 | 0:0532a6e10a23 | 56 | // timer GPIO setup |
bobolee1239 | 0:0532a6e10a23 | 57 | __HAL_RCC_GPIOA_CLK_ENABLE(); |
bobolee1239 | 0:0532a6e10a23 | 58 | GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;//pin num PA "0" |
bobolee1239 | 0:0532a6e10a23 | 59 | GPIO_InitStruct.Mode = GPIO_MODE_AF_PP; |
bobolee1239 | 0:0532a6e10a23 | 60 | GPIO_InitStruct.Pull = GPIO_PULLUP; |
bobolee1239 | 0:0532a6e10a23 | 61 | GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH; |
bobolee1239 | 0:0532a6e10a23 | 62 | GPIO_InitStruct.Alternate = GPIO_AF1_TIM2; //timer |
bobolee1239 | 0:0532a6e10a23 | 63 | HAL_GPIO_Init(GPIOA, &GPIO_InitStruct); //port P"A"0 |
bobolee1239 | 0:0532a6e10a23 | 64 | // basic timer setup |
bobolee1239 | 0:0532a6e10a23 | 65 | __HAL_RCC_TIM2_CLK_ENABLE(); |
bobolee1239 | 0:0532a6e10a23 | 66 | htim2.Instance = TIM2; //timer |
bobolee1239 | 0:0532a6e10a23 | 67 | htim2.Init.Prescaler = 0; |
bobolee1239 | 0:0532a6e10a23 | 68 | htim2.Init.CounterMode = TIM_COUNTERMODE_UP; |
bobolee1239 | 0:0532a6e10a23 | 69 | htim2.Init.Period = 0x000fffff; |
bobolee1239 | 0:0532a6e10a23 | 70 | htim2.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; |
bobolee1239 | 0:0532a6e10a23 | 71 | //encoder mode setup |
bobolee1239 | 0:0532a6e10a23 | 72 | sConfig.EncoderMode = TIM_ENCODERMODE_TI12; |
bobolee1239 | 0:0532a6e10a23 | 73 | sConfig.IC1Polarity = TIM_ICPOLARITY_FALLING; |
bobolee1239 | 0:0532a6e10a23 | 74 | sConfig.IC1Selection = TIM_ICSELECTION_DIRECTTI; |
bobolee1239 | 0:0532a6e10a23 | 75 | sConfig.IC1Prescaler = TIM_ICPSC_DIV1; |
bobolee1239 | 0:0532a6e10a23 | 76 | sConfig.IC1Filter = 2; |
bobolee1239 | 0:0532a6e10a23 | 77 | sConfig.IC2Polarity = TIM_ICPOLARITY_RISING; |
bobolee1239 | 0:0532a6e10a23 | 78 | sConfig.IC2Selection = TIM_ICSELECTION_DIRECTTI; |
bobolee1239 | 0:0532a6e10a23 | 79 | sConfig.IC2Prescaler = TIM_ICPSC_DIV1; |
bobolee1239 | 0:0532a6e10a23 | 80 | sConfig.IC2Filter = 2; |
bobolee1239 | 0:0532a6e10a23 | 81 | HAL_TIM_Encoder_Init(&htim2, &sConfig); |
bobolee1239 | 0:0532a6e10a23 | 82 | // timer mode setup |
bobolee1239 | 0:0532a6e10a23 | 83 | sMasterConfig.MasterOutputTrigger = TIM_TRGO_RESET; |
bobolee1239 | 0:0532a6e10a23 | 84 | sMasterConfig.MasterSlaveMode = TIM_MASTERSLAVEMODE_DISABLE; |
bobolee1239 | 0:0532a6e10a23 | 85 | HAL_TIMEx_MasterConfigSynchronization(&htim2, &sMasterConfig); |
bobolee1239 | 0:0532a6e10a23 | 86 | // timer start |
bobolee1239 | 0:0532a6e10a23 | 87 | HAL_TIM_Encoder_Start(&htim2, TIM_CHANNEL_ALL); |
bobolee1239 | 0:0532a6e10a23 | 88 | |
bobolee1239 | 0:0532a6e10a23 | 89 | } |
bobolee1239 | 0:0532a6e10a23 | 90 | |
bobolee1239 | 0:0532a6e10a23 | 91 | // Z Pulse routine |
bobolee1239 | 0:0532a6e10a23 | 92 | void ZeroEncoderCount() |
bobolee1239 | 0:0532a6e10a23 | 93 | { |
bobolee1239 | 0:0532a6e10a23 | 94 | TIM2->CNT=0 ; //reset count to zero |
bobolee1239 | 0:0532a6e10a23 | 95 | } |
bobolee1239 | 0:0532a6e10a23 | 96 | |
bobolee1239 | 0:0532a6e10a23 | 97 | |
bobolee1239 | 0:0532a6e10a23 | 98 | #endif |