廢舊鼠標先別丟,用來學習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 \n", v_accread);

        

        NRF_LOG_FLUSH();

      }

    }

}

 

 

//---------------------代碼結束---------------------------------------------

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM