完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
使用 CC2652RB 的 SDK 开发包提供的例程 ( I2CTemp ) 加以修改读取加速度传感器 fxos8700cq .
SDK里已经把它所有外设的驱动都封装成库,只要按照 /*!**************************************************************************** * @file I2C.h * @brief Inter-Integrated Circuit (I2C) Driver * * @anchor ti_drivers_I2C_Overview * # Overview * * The I2C driver is designed to operate as an I2C master and will not * function as an I2C slave. Multi-master arbitration is not supported; * therefore, this driver assumes it is the only I2C master on the bus. * This I2C driver's API set provides the ability to transmit and receive * data over an I2C bus between the I2C master and I2C slave(s). The * application is responsible for manipulating and interpreting the data. * * * * @anchor ti_drivers_I2C_Usage * # Usage * * This section provides a basic @ref ti_drivers_I2C_Synopsis * "usage summary" and a set of @ref ti_drivers_I2C_Examples "examples" * in the form of commented code fragments. Detailed descriptions of the * I2C APIs and their effect are provided in subsequent sections. * * @anchor ti_drivers_I2C_Synopsis * ## Synopsis # * @anchor ti_drivers_I2C_Synopsis_Code * @code * // Import I2C Driver definitions * #include * * // Define name for an index of an I2C bus * #define SENSORS 0 * * // Define the slave address of device on the SENSORS bus * #define OPT_ADDR 0x47 * * // One-time init of I2C driver * I2C_init(); * * // initialize optional I2C bus parameters * I2C_Params params; * I2C_Params_init(¶ms); * params.bitRate = I2C_400kHz; * * // Open I2C bus for usage * I2C_Handle i2cHandle = I2C_open(SENSORS, ¶ms); * * // Initialize slave address of transaction * I2C_Transaction transaction = {0}; * transaction.slaveAddress = OPT_ADDR; * * // Read from I2C slave device * transaction.readBuf = data; * transaction.readCount = sizeof(data); * transaction.writeCount = 0; * I2C_transfer(i2cHandle, &transaction); * * // Write to I2C slave device * transaction.writeBuf = command; * transaction.writeCount = sizeof(command); * transaction.readCount = 0; * I2C_transferTimeout(i2cHandle, &transaction, 5000); * * // Close I2C * I2C_close(i2cHandle); 这里仅贴出了一下部分,具体请到对应的 .h 文件中查阅. 初始化硬件I2C I2C_Handle i2cHandle; //硬件 I2C 的句柄 I2C_Params i2cParams; //硬件 I2C 参数配置的变量 /* * ======== i2cErrorHandler ======== */ static void i2cErrorHandler(I2C_Transaction *transaction, Display_Handle display) { switch (transaction->status) { case I2C_STATUS_TIMEOUT: Display_printf(display, 0, 0, "I2C transaction timed out!"); break; case I2C_STATUS_CLOCK_TIMEOUT: Display_printf(display, 0, 0, "I2C serial clock line timed out!"); break; case I2C_STATUS_ADDR_NACK: Display_printf(display, 0, 0, "I2C slave address 0x%x not" " acknowledged!", transaction->slaveAddress); break; case I2C_STATUS_DATA_NACK: Display_printf(display, 0, 0, "I2C data byte not acknowledged!"); break; case I2C_STATUS_ARB_LOST: Display_printf(display, 0, 0, "I2C arbitration to another master!"); break; case I2C_STATUS_INCOMPLETE: Display_printf(display, 0, 0, "I2C transaction returned before completion!"); break; case I2C_STATUS_BUS_BUSY: Display_printf(display, 0, 0, "I2C bus is already in use!"); break; case I2C_STATUS_CANCEL: Display_printf(display, 0, 0, "I2C transaction cancelled!"); break; case I2C_STATUS_INVALID_TRANS: Display_printf(display, 0, 0, "I2C transaction invalid!"); break; case I2C_STATUS_ERROR: Display_printf(display, 0, 0, "I2C generic error!"); break; default: Display_printf(display, 0, 0, "I2C undefined error case!"); break; } } /* 硬件I2C的初始化 */ void bsp_Cc2652I2c0Init(void) { I2C_init(); /* Create I2C for usage */ I2C_Params_init(&i2cParams); i2cParams.bitRate = I2C_400kHz; i2cHandle = I2C_open(CC2652RB_I2C0, &i2cParams); if (i2cHandle == NULL) { Display_printf(display, 0, 0, "Error Initializing I2Cn"); while (1); } else { Display_printf(display, 0, 0, "I2C Initialized!n"); } } 使用硬件 I2C 读写传感器寄存器 将数据写入 fxos8700cq 传感器寄存器 /******************************************************************************* * 名 称: bsp_Fxos8700cqWrSingleCmd * 功 能: Fxos8700cq写一个单指令。 * 入口参数: _ucSlaveAddr: i2c 从机地址 * _ucRegAddr: 需要写入的寄存器地址 * _ucRegData: 写入寄存器的数据 * 出口参数: 无 * 作 者: Roger-WY * 创建日期: 2020-12-30 * 修 改: * 修改日期: * 备 注: *******************************************************************************/ int8_t bsp_Fxos8700cqWrSingleCmd(uint8_t _ucSlaveAddr,uint8_t _ucRegAddr,uint8_t _ucRegData) { uint8_t sendBuff[2] = {0}; I2C_Transaction i2cTransaction = {0}; sendBuff[0] = _ucRegAddr; sendBuff[1] = _ucRegData; i2cTransaction.slaveAddress = _ucSlaveAddr; i2cTransaction.writeBuf = sendBuff; i2cTransaction.writeCount = 2; i2cTransaction.readBuf = NULL; i2cTransaction.readCount = 0; if (I2C_transfer(i2cHandle, &i2cTransaction)) { Display_printf(display, 0, 0, "i2c writer reg:%02X value:%02X,succeed!n",_ucRegAddr,_ucRegData); return 0; } else { i2cErrorHandler(&i2cTransaction, display); return -1; } } 从 fxos8700cq 传感器寄存器中读取数据 /******************************************************************************* * 名 称: bsp_Fxos8700cqRdData * 功 能: Fxos8700cq读取寄存器数据。 * 入口参数: _ucSlaveAddr: i2c 从机地址 * _ucRegAddr: 需要读取的寄存器起始地址 * *_pRegData: 读出来的数据缓存数组 * _ucRegCnt : 需要读取的寄存器个数 * 出口参数: 无 * 作 者: Roger-WY * 创建日期: 2020-12-30 * 修 改: * 修改日期: * 备 注: *******************************************************************************/ int8_t bsp_Fxos8700cqRdRegData(uint8_t _ucSlaveAddr,uint8_t _ucRegAddr,uint8_t *_pRegData,uint8_t _ucRegCnt) { I2C_Transaction i2cTransaction = {0}; i2cTransaction.slaveAddress = _ucSlaveAddr; i2cTransaction.writeBuf = &_ucRegAddr; i2cTransaction.writeCount = 1; i2cTransaction.readBuf = _pRegData; i2cTransaction.readCount = _ucRegCnt; if (I2C_transfer(i2cHandle, &i2cTransaction)) { Display_printf(display, 0, 0, "i2c read reg:%02X succeed!n",_ucRegAddr); return 0; } else { i2cErrorHandler(&i2cTransaction, display); return -1; } } 读取 FXOS8700CQ 的数据 请参照 FXOS8700CQ 6轴传感器的介绍与调试 测试工程 /* * ======== mainThread ======== */ void *mainThread(void *arg0) { /* Call driver init functions */ Display_init(); GPIO_init(); //I2C_init(); /* Configure the LED */ GPIO_setConfig(CONFIG_GPIO_LED_0, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW); GPIO_setConfig(CONFIG_GPIO_LED_1, GPIO_CFG_OUT_STD | GPIO_CFG_OUT_LOW); /* Open the UART display for output */ display = Display_open(Display_Type_UART, NULL); if (display == NULL) { while (1); } /* Turn on user LED */ GPIO_write(CONFIG_GPIO_LED_0, CONFIG_GPIO_LED_ON); Display_printf(display, 0, 0, "Starting the fxos8700cq example"); while(bsp_Fxos8700cqInit(FXOS8700CQ_I2C_ADDR) == -1) { I2C_close(i2cHandle); sleep(1); } GPIO_write(CONFIG_GPIO_LED_1, CONFIG_GPIO_LED_ON); for (;;) { bsp_Fxos8700cqClearIntFlag(FXOS8700CQ_I2C_ADDR); bsp_ReadFxos8700cqTemp(FXOS8700CQ_I2C_ADDR,&gTemperature); //读取温度值 bsp_ReadFxos8700cqAcc(FXOS8700CQ_I2C_ADDR,&gAccData); //读取加速度计 bsp_ReadFxos8700cqMagn(FXOS8700CQ_I2C_ADDR,&gMagnData); //读取磁力计 bsp_ReadAccelMagnData(FXOS8700CQ_I2C_ADDR,&gFxosSensorVal[0],&gFxosSensorVal[1]); //读取磁力计 Display_printf(display, 0, 0, "fxos8700cq temp:%d",gTemperature); Display_printf(display, 0, 0, "fxos8700cq accx:%d,accy:%d,accz:%d",gAccData.x,gAccData.y,gAccData.z); Display_printf(display, 0, 0, "fxos8700cq magnx:%d,magny:%d,magnz:%d",gMagnData.x,gMagnData.y,gMagnData.z); /* Sleep for 1 second */ sleep(1); } //Display_printf(display, 0, 0, "I2C closed!"); //return (NULL); } 使用IAR搭建CC2652RB的开发环境 FXOS8700CQ 6轴传感器的介绍与调试 完整的工程下载,请点击 CC2652RB硬件I2C读取FXOS8700CQ加速度传感器,如果您没有积分可以留言,我单独发您。 PS: IAR FOR ARM 需要 8.50.9 及以上版本才能打开. 需要先安装 CC2652RB 的 SDK,且需要安装在默认位置. |
|
|
|
只有小组成员才能发言,加入小组>>
3278 浏览 9 评论
2955 浏览 16 评论
3455 浏览 1 评论
8987 浏览 16 评论
4050 浏览 18 评论
1102浏览 3评论
570浏览 2评论
const uint16_t Tab[10]={0}; const uint16_t *p; p = Tab;//报错是怎么回事?
568浏览 2评论
用NUC131单片机UART3作为打印口,但printf没有输出东西是什么原因?
2301浏览 2评论
NUC980DK61YC启动随机性出现Err-DDR是为什么?
1857浏览 2评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-11-22 02:04 , Processed in 1.243360 second(s), Total 79, Slave 59 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号