废旧鼠标先别丢,用来学习nRF52832 的QDEC

刚发现nRF52832有一个 QDEC,SDK13.0中还有驱动,但是不太友好。  如果大家有废旧鼠标,建议拆一个编码器下来“学习”。鼠标的一般原理如下:

                                                                                               图一

图中那个SW4 ALPS EC10E / Scroll wheel encoder 便是编码器了, 拆下来之后,还是按照这个电路连接到NRF52832,这里我选择依照原版程序,使用P03 P04,方向不重要。我找了一个接上之后,还先用示波器看了一下,QA/QB输出正常。

                                                                                   图二

nrf52832 QDEC

                                                            图三

代码有一点变化,仍然使用PCA10040来运行。

 //---------------------代码开始---------------------------------------------

static volatile uint32_t m_accdblread;

static volatile int32_t m_accread;

static volatile bool m_report_ready_flag = false;

#if (QDEC_CONFIG_LEDPRE >= 128)

    #warning "This example assumes that the QDEC LED changes state. Make sure that 'Sample Period' in QDEC config is less than 'LED pre-time'."

#endif

#define NRF_DRV_QDEC_COCO_CONFIG                                         

    {                                                                       

        .reportper          = (nrf_qdec_reportper_t)QDEC_CONFIG_REPORTPER,  

        .sampleper          = (nrf_qdec_sampleper_t)0,  

        .psela              = QDEC_CONFIG_PIO_A,                            

        .pselb              = QDEC_CONFIG_PIO_B,                            

        .pselled            = 0xFFFFFFFF,                          

        .ledpre             = QDEC_CONFIG_LEDPRE,                           

        .ledpol             = (nrf_qdec_ledpol_t)QDEC_CONFIG_LEDPOL,        

        .interrupt_priority = QDEC_CONFIG_IRQ_PRIORITY,                     

        .dbfen              = 0,                            

        .sample_inten       = 0                      

    }

static const nrf_drv_qdec_config_t m_qdec_coco_config = NRF_DRV_QDEC_COCO_CONFIG;

//static const nrf_drv_qdec_config_t m_default_config = NRF_DRV_QDEC_DEFAULT_CONFIG;

static void qdec_event_handler(nrf_drv_qdec_event_t event)

{

    if (event.type == NRF_QDEC_EVENT_REPORTRDY)

    {

        m_accdblread        = event.data.report.accdbl;

        m_accread           = event.data.report.acc;

    }

    else if ( event.type == NRF_QDEC_EVENT_SAMPLERDY )

    {

      

    }

}

int main(void)

{  

    uint32_t err_code;

    

    uint32_t v_accread = 0;

    err_code = NRF_LOG_INIT(NULL);

    APP_ERROR_CHECK(err_code);

    NRF_LOG_DEFAULT_BACKENDS_INIT();

    // Initialize hardware

    err_code = nrf_drv_qdec_init(&m_qdec_coco_config, qdec_event_handler);

    APP_ERROR_CHECK(err_code);

    nrf_qdec_reportper_to_value(QDEC_CONFIG_REPORTPER);

    // Initialize quadrature encoder simulator

    qenc_init((nrf_qdec_ledpol_t)nrf_qdec_ledpol_get());

    NRF_LOG_INFO("QDEC testing started");

  

    nrf_drv_qdec_enable();    

  

    while (true)

    {

      

      if ( m_report_ready_flag )

      {

        m_report_ready_flag = 0;

        

        v_accread += m_accread;

        

        NRF_LOG_INFO("ACC IS %d ", v_accread);

        

        NRF_LOG_FLUSH();

      }

    }

}

//---------------------代码结束---------------------------------------------

原文地址:https://www.cnblogs.com/newbit/p/8337550.html