Stm32f7-Disco get rotary encoder data (Use GPIO interrupt)

0 nhận xét

Summary:

I try to use timer to get data from encoder with stm32f7-disco but no interface in there. Pins used for other purpose. I try to do with  GPIO input interrupt.
 Connection
Encoder                                 MCP
5V-----------------------------------------5V
Gnd---------------------------------------Gnd
Phase A---------------------------------PH_6
Phase B---------------------------------PI_3

1.Pin out setting
RCC->High speed clock ->Crystal ceramic resonator
Up system clock to 216 MHz
PH_6->EXTI6
PI_3->EXTI3

2. GPIO configuration
PH6 -> GPIO mode :  External interrupt mod rising/falling edge trigger detection && Pull up setting
PI3 -> GPIO mode :  External interrupt mod rising/falling edge trigger detection && Pull up setting

3. NVIC configuration
 EXTI line 3 interrupt check.
EXTI line [9:5] interrupts check.
4. Source generation and edition

In Src/main.c add

#include "stm32f7xx_hal.h"
#include "stm32f7xx_hal_uart.h"
#include "stm32f7xx_hal_sdram.h"
#include "stm32f7xx_hal_ltdc.h"
#include "stm32746g_discovery.h"
#include "stm32746g_discovery_lcd.h"
#include "stm32746g_discovery_sdram.h"
#include "stm32f7xx_ll_fmc.h"

extern uint8_t a,b,ab,c;
extern uint8_t pre_ab;
extern uint16_t count_;

-------------------------------------------------------------------------------------------

int main(void)
{
 
HAL_Init();
  SystemClock_Config();
  MX_GPIO_Init();
  BSP_LCD_Init();
  BSP_LCD_LayerDefaultInit(0, LCD_FB_START_ADDRESS);
 BSP_LCD_LayerDefaultInit(1, LCD_FB_START_ADDRESS+(BSP_LCD_GetXSize()*BSP_LCD_GetYSize()*4));
  BSP_LCD_DisplayOn();
  BSP_LCD_SelectLayer(0);
  BSP_LCD_Clear(LCD_COLOR_BLACK);
  BSP_LCD_SelectLayer(1);
   BSP_LCD_Clear(LCD_COLOR_BLACK);
  BSP_LCD_SetFont(&LCD_DEFAULT_FONT);
  BSP_LCD_SetBackColor(LCD_COLOR_WHITE);
  BSP_LCD_SetTextColor(LCD_COLOR_DARKBLUE); 


  char buffer[10]; 
   while (1)
  {
     BSP_LCD_Clear(LCD_COLOR_WHITE);


     sprintf(buffer,"count=%d",count_);
     BSP_LCD_DisplayStringAt(0, LINE(4), (uint8_t *)buffer, CENTER_MODE);
     HAL_Delay(100);
  }
 

}
 

In Src/stm32f7xx_it.c add

----------------------------------------------------------------------------------------------------------------------

uint8_t a,b,ab,c;
uint8_t pre_ab=0;
uint16_t count_=0;



---------------------------------------------------------------------------------------------- 


void EXTI3_IRQHandler(void)
{

    HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_3);
    a =HAL_GPIO_ReadPin(GPIOI,GPIO_PIN_3);
    ab=b<<1|a;
    c=ab<<2|pre_ab;
    pre_ab=ab;
    if((c%5)!=0){
        c=c|0x06;
         if(c%3==0){
            count_+=1;   
            }
                else if(c%3!=0){
                    count_-=1;
               }
        }

}

void EXTI9_5_IRQHandler(void)
{
    HAL_GPIO_EXTI_IRQHandler(GPIO_PIN_6);
    b = HAL_GPIO_ReadPin(GPIOH,GPIO_PIN_6);
}







Facebook Comments
Blogger Comments
Share knowledge

Copyright © 2016 - Share knowledge ®