废旧鼠标先别丢,用来学习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();

      }

    }

}

 

 

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

 

转载于:https://www.cnblogs.com/newbit/p/8337550.html

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值