NRF51822之SPI

 

 1 /**@brief Function for initializing a SPI master driver.
 2  *
 3  * @param[in] p_instance    Pointer to SPI master driver instance.
 4  */
 5 static void spi_master_init(nrf_drv_spi_t const * p_instance)
 6 {
 7     uint32_t err_code = NRF_SUCCESS;
 8      
 9     nrf_drv_spi_config_t config =
10     {
11  
12         .irq_priority = APP_IRQ_PRIORITY_LOW,
13         .orc          = 0x80,
14         .frequency    = NRF_DRV_SPI_FREQ_1M,
15         .mode         = NRF_DRV_SPI_MODE_0,
16         .bit_order    = NRF_DRV_SPI_BIT_ORDER_MSB_FIRST,
17     };
18 
19     #if (SPI0_ENABLED == 1)
20     if (p_instance == &m_spi_master_0)
21     {
22         config.ss_pin   = SPIM0_SS_PIN,
23         config.sck_pin  = SPIM0_SCK_PIN;
24         config.mosi_pin = SPIM0_MOSI_PIN;
25         config.miso_pin = SPIM0_MISO_PIN;
26         err_code = nrf_drv_spi_init(p_instance, 
27                                     &config,
28                                     spi_master_0_event_handler);
29     }
30     else
31     #endif // (SPI0_ENABLED == 1)
32 
33     APP_ERROR_CHECK(err_code);
34 }

 

 1 uint8_t  spi_write_reg(uint8_t ucRegAddr, uint8_t ucRegVal)
 2 {
 3     uint8_t buf[2];
 4     uint32_t err_code;
 5     
 6     spi_xfer_done = false;
 7     
 8     buf[0] = ucRegAddr<<1;
 9     buf[1] = ucRegVal;
10 
11     err_code = nrf_drv_spi_transfer(&m_spi_master_0, buf, 2, NULL, 0);
12     APP_ERROR_CHECK(err_code);
13     
14     while (!spi_xfer_done) 
15     {
16         __WFE();
17     }
18     return true;
19 }
20 
21 
22 uint8_t spi_read_reg(uint8_t ucRegAddr)
23 {
24     uint8_t buf[2];
25     uint8_t rx[2];
26     uint32_t err_code;
27     
28     spi_xfer_done = false;
29     
30     buf[0] =( ucRegAddr<<1 ) | 0x80;
31     buf[1] = 0x00;
32     
33     err_code = nrf_drv_spi_transfer(&m_spi_master_0,buf,2,rx,2);
34     APP_ERROR_CHECK(err_code);
35     
36     while(!spi_xfer_done)
37     {
38         __WFE();
39     }
40     
41     return rx[1];
42 }

 

现在我们对spi_write_reg(uint8_t ucRegAddr, uint8_t ucRegVal)进行测试

更据我们的代码在Saleae Logic 1.1.16C中进行设置如下图所示

什么看不懂上面的配置?请右转补课 http://www.cnblogs.com/libra13179/p/5740716.html

 

 

spi_write_reg(0x00,0x00)

 

 

spi_write_reg(0x01,0x02)  故意留下思考?为何下图显示是0X02而不是0x01,有什么作用呢?

 

转载于:https://www.cnblogs.com/libra13179/p/5740724.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值