nrf52 iic使用
生活随笔
收集整理的這篇文章主要介紹了
nrf52 iic使用
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
一、使用環(huán)境
SDK17.1 ble_app_template例程
二、工程配置
2.1 添加官方驅(qū)動(dòng)文件
2.2 使能TWI
三、代碼移植
驅(qū)動(dòng)代碼 peripheral_iic.h
#ifndef _PERIPHERAL_I2C_H_ #define _PERIPHERAL_I2C_H_#include "boards.h" #include "nrf_delay.h" #include "nrf_drv_twi.h" #include "nrf_gpio.h"#include <stdio.h> #include <string.h>bool I2C_register_read(uint8_t slave_address, uint8_t register_address, uint8_t *destination, uint8_t number_of_bytes); bool I2C_register_write(uint8_t slave_address, unsigned char *data_buf, uint8_t len);int16_t I2C_Write_Reg(unsigned char i2c_add, unsigned char reg_add, unsigned char cmd); int16_t I2C_Read_Reg(unsigned char i2c_add, unsigned char reg_add, unsigned char *ucData);int16_t I2C_MultiWrite_Reg(unsigned char i2c_add, unsigned char reg_add, int16_t num, unsigned char *ucData); int16_t I2C_MultiRead_Reg(unsigned char i2c_add, unsigned char reg_add, int16_t num, unsigned char *ucData);void I2C_init(void); void I2C_uninit(void);#endif // END _PERIPHERAL_I2C_H_驅(qū)動(dòng)代碼 peripheral_iic.c
#include "peripheral_iic.h"// TWI驅(qū)動(dòng)程序?qū)嵗齀D,ID和外設(shè)編號(hào)對(duì)應(yīng),0:TWI0 1:TWI1 #define TWI_INSTANCE_ID_S 0 // TWI傳輸完成標(biāo)志 static volatile bool I2C_xfer_done = false;//定義TWI驅(qū)動(dòng)程序?qū)嵗?#xff0c;名稱為m_twi static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID_S);// TWI事件處理函數(shù) static void twi_s_handler(nrf_drv_twi_evt_t const *p_event, void *p_context) {//判斷TWI事件類型switch (p_event->type){//傳輸完成事件case NRF_DRV_TWI_EVT_DONE:I2C_xfer_done = true; //置位傳輸完成標(biāo)志break;default:break;} }/************************************************************************** 功 能 : 寫(xiě)I2C_Slave寄存器* 參 數(shù) : register_address[in]:寄存器地址* : value[in]:寫(xiě)入的數(shù)據(jù)* 返回值 : true:寫(xiě)數(shù)據(jù)成功,false:寫(xiě)入失敗*************************************************************************/ bool I2C_register_write(uint8_t slave_address, unsigned char *data_buf, uint8_t len) {uint8_t timeOut = 0;ret_code_t err_code;// TWI傳輸完成標(biāo)志設(shè)置為falseI2C_xfer_done = false;//寫(xiě)入數(shù)據(jù)err_code = nrf_drv_twi_tx(&m_twi, slave_address, data_buf, len, false);//等待TWI總線傳輸完成timeOut = 0;while (I2C_xfer_done == false){if (timeOut > 50){return false;}else{timeOut++;}nrf_delay_us(100);}if (NRF_SUCCESS != err_code){return false;}return true; }/************************************************************************** 功 能 : 讀I2C_Slave寄存器* 參 數(shù) : register_address[in]:寄存器地址* : * destination[out] :指向保存讀取數(shù)據(jù)的緩存* : number_of_bytes[in] :讀取的數(shù)據(jù)長(zhǎng)度* 返回值 : true:操作成功,false:操作失敗*************************************************************************/ bool I2C_register_read(uint8_t slave_address, uint8_t register_address, uint8_t *destination, uint8_t number_of_bytes) {uint8_t timeOut = 0;ret_code_t err_code;// TWI傳輸完成標(biāo)志設(shè)置為falseI2C_xfer_done = false;err_code = nrf_drv_twi_tx(&m_twi, slave_address, ®ister_address, 1, true);//等待TWI總線傳輸完成timeOut = 0;while (I2C_xfer_done == false){if (timeOut > 50){return false;}else{timeOut++;}nrf_delay_us(100);}if (NRF_SUCCESS != err_code){return false;}// TWI傳輸完成標(biāo)志設(shè)置為falseI2C_xfer_done = false;err_code = nrf_drv_twi_rx(&m_twi, slave_address, destination, number_of_bytes);//等待TWI總線傳輸完成timeOut = 0;while (I2C_xfer_done == false){if (timeOut > 50){return false;}else{timeOut++;}nrf_delay_us(100);}if (NRF_SUCCESS != err_code){return false;}return true; }int16_t I2C_Write_Reg(unsigned char i2c_add, unsigned char reg_add, unsigned char cmd) {uint8_t BufTmp[2];BufTmp[0] = reg_add;BufTmp[1] = cmd;return I2C_register_write(i2c_add, BufTmp, 2); }int16_t I2C_Read_Reg(unsigned char i2c_add, unsigned char reg_add, unsigned char *ucData) {return I2C_register_read(i2c_add, reg_add, ucData, 1); }int16_t I2C_MultiWrite_Reg(unsigned char i2c_add, unsigned char reg_add, int16_t num, unsigned char *ucData) {uint8_t BufTmp[num + 1];BufTmp[0] = reg_add;memcpy(&BufTmp[1], ucData, num);return I2C_register_write(i2c_add, BufTmp, num + 1); }int16_t I2C_MultiRead_Reg(unsigned char i2c_add, unsigned char reg_add, int16_t num, unsigned char *ucData) {return I2C_register_read(i2c_add, reg_add, ucData, num); }void I2C_init(void) { ret_code_t err_code;//定義并初始化TWI配置結(jié)構(gòu)體const nrf_drv_twi_config_t twi_config_s = {.scl = ARDUINO_SCL_PIN, //定義TWI SCL引腳.sda = ARDUINO_SDA_PIN, //定義TWI SDA引腳.frequency = NRF_DRV_TWI_FREQ_400K, // TWI速率.interrupt_priority = APP_IRQ_PRIORITY_HIGHEST, // TWI優(yōu)先級(jí).clear_bus_init = false //初始化期間不發(fā)送9個(gè)SCL時(shí)鐘};//初始化TWIerr_code = nrf_drv_twi_init(&m_twi, &twi_config_s, twi_s_handler, NULL);//檢查返回的錯(cuò)誤代碼APP_ERROR_CHECK(err_code);//使能TWInrf_drv_twi_enable(&m_twi); }void I2C_uninit(void) {//失能TWInrf_drv_twi_disable(&m_twi);nrf_drv_twi_uninit(&m_twi);nrf_gpio_cfg_default(ARDUINO_SDA_PIN);nrf_gpio_cfg_default(ARDUINO_SCL_PIN); }添加我們的驅(qū)動(dòng)文件和頭文件路徑,然后使用例子。
四、驅(qū)動(dòng)測(cè)試
4.1 添加頭文件
#include "peripheral_iic.h"4.2 簡(jiǎn)單讀寫(xiě)測(cè)試
#define LIS2MDL_ADDR_WRITE 0x3C//0x3C 0x1EU #define LIS2MDL_ADDR_READ 0x3D//0x3D 0x1EU/* 設(shè)備寄存器地址 */ #define LIS2MDL_ADDR_CFGA 0x60 #define LIS2MDL_ADDR_CFGB 0x61 #define LIS2MDL_ADDR_CFGC 0x62 #define LIS2MDL_ADDR_INTCRTL 0x63 #define LIS2MDL_ADDR_INTSOURCE 0x64 #define LIS2MDL_ADDR_INTTHSL 0x65 #define LIS2MDL_ADDR_INTTHSH 0x66 #define LIS2MDL_ADDR_STATUS 0x67#define LIS2MDL_ADDR_XOUTL 0x68 #define LIS2MDL_ADDR_XOUTH 0x69 #define LIS2MDL_ADDR_YOUTHL 0x6A #define LIS2MDL_ADDR_YOUTH 0x6B #define LIS2MDL_ADDR_ZOUTL 0x6C #define LIS2MDL_ADDR_ZOUTH 0x6Dvoid i2c_test(void) {unsigned char writeData = 0x47;unsigned char readData = 0;I2C_init();I2C_MultiWrite_Reg(LIS2MDL_ADDR_WRITE, LIS2MDL_ADDR_CFGA, 1, &writeData);I2C_MultiRead_Reg(LIS2MDL_ADDR_READ, LIS2MDL_ADDR_XOUTL, 1, &readData); }總結(jié)
以上是生活随笔為你收集整理的nrf52 iic使用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 走向卓越,从远离这5种职场谎言开始
- 下一篇: 今天公司来了个拿 30K 出来的测试,算