我设置的slaveAddr = 0x80; 可通过示波器看的时候第一个字节是0x00,后来改变slaveAddr的值,对比发现只是第一位没有发送。
Viki Shi:
如果只有第一位发不出来,怀疑是时序问题。试下I2C起始信号段延时是否正常
Magic:
回复 Viki Shi:
I2C起始信号段延时 在哪里设置?怎样看是否正常?
Alvin Chen:
回复 Magic:
/** Copyright (c) 2016-2017, Texas Instruments Incorporated* All rights reserved.** Redistribution and use in source and binary forms, with or without* modification, are permitted provided that the following conditions* are met:** *Redistributions of source code must retain the above copyright*notice, this list of conditions and the following disclaimer.** *Redistributions in binary form must reproduce the above copyright*notice, this list of conditions and the following disclaimer in the*documentation and/or other materials provided with the distribution.** *Neither the name of Texas Instruments Incorporated nor the names of*its contributors may be used to endorse or promote products derived*from this software without specific prior written permission.** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,* THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.*//**======== i2ctmp007.c ========*/ #include <stdint.h> #include <stddef.h> #include <unistd.h>/* Driver Header files */ #include <ti/drivers/GPIO.h> #include <ti/drivers/I2C.h> #include <ti/display/Display.h> #include <ti/drivers/i2c/I2CCC26XX.h> /* Example/Board Header files */ #include "Board.h"#define TASKSTACKSIZE640 #define slaveAddr0x68 #define DEVICE_ID0x75 static Display_Handle display; I2C_Handlei2cHandle; I2C_Paramsi2cParams; static uint8_t buffer[32];#define Board_I2C0_SDA1IOID_10 #define Board_I2C0_SCL1IOID_11 #define SENSOR_I2C_11 static const I2CCC26XX_I2CPinCfg pinCfg1 = {// Pin configuration for I2C interface 1.pinSDA = Board_I2C0_SDA1,.pinSCL = Board_I2C0_SCL1 }; void My_I2cinit();bool BoardI2C_readReg(uint8_t addr, uint8_t *pBuf, uint8_t nBytes); bool BoardI2C_writeReg(uint8_t addr, uint8_t *pBuf, uint8_t nBytes); bool BoardI2C_read(uint8_t *data, uint8_t len); bool BoardI2C_write(uint8_t *data, uint8_t len); uint8_t AD_RD_Reg(uint8_t addr); void AD_WR_Reg(uint8_t addr,uint8_t val); /**======== mainThread ========*/ void *mainThread(void *arg0) {/* Call driver init functions */Display_init();GPIO_init();I2C_init();/* Open the HOST display for output */display = Display_open(Display_Type_UART, NULL);if (display == NULL) {while (1);}/* Turn on user LED */GPIO_write(Board_GPIO_LED0, Board_GPIO_LED_ON);Display_printf(display, 0, 0, "Starting the example\n");My_I2cinit();if(AD_RD_Reg(DEVICE_ID)==0X68) //读取器件ID{Display_printf(display, 0, 0, "ok!!!!!!\n");}return (NULL); } void My_I2cinit() {/* Create I2C for usage */I2C_Params_init(&i2cParams);i2cParams.bitRate = I2C_400kHz;i2cParams.custom=(uintptr_t)&pinCfg1;i2cHandle = I2C_open(Board_I2C_TMP, &i2cParams);if (i2cHandle == NULL) {Display_printf(display, 0, 0, "Error Initializing I2C\n");while (1);}else{Display_printf(display, 0, 0, "Initializing I2C\n");}} bool BoardI2C_write(uint8_t *data, uint8_t len) {I2C_Transaction masterTransaction;masterTransaction.writeCount= len;masterTransaction.writeBuf= data;masterTransaction.readCount= 0;masterTransaction.readBuf= NULL;masterTransaction.slaveAddress = slaveAddr;return I2C_transfer(i2cHandle, &masterTransaction) == TRUE; }/******************************************************************************* * @fnBoardI2C_writeSingle * * @briefSingle byte write to an I2C device * * @paramdata - byte to write * * @returntrue if success */ bool BoardI2C_writeSingle(uint8_t data) {uint8_t d;d = data;return BoardI2C_write(&d, 1); }/******************************************************************************* * @fnBoardI2C_read * * @briefBurst read from an I2C device * * @paramdata - pointer to data buffer * @paramlen - number of bytes to write * * @returntrue if success */ bool BoardI2C_read(uint8_t *data, uint8_t len) {I2C_Transaction masterTransaction;masterTransaction.writeCount = 0;masterTransaction.writeBuf = NULL;masterTransaction.readCount = len;masterTransaction.readBuf = data;masterTransaction.slaveAddress = slaveAddr;return I2C_transfer(i2cHandle, &masterTransaction) == TRUE; }/******************************************************************************* * @fnBoardI2C_writeRead * * @briefBurst write/read from an I2C device * * @paramwdata - pointer to write data buffer * @paramwlen - number of bytes to write * @paramrdata - pointer to read data buffer * @paramrlen - number of bytes to read * * @returntrue if success */ bool BoardI2C_writeRead(uint8_t *wdata, uint8_t wlen, uint8_t *rdata, uint8_t rlen) {I2C_Transaction masterTransaction;masterTransaction.writeCount = wlen;masterTransaction.writeBuf = wdata;masterTransaction.readCount = rlen;masterTransaction.readBuf = rdata;masterTransaction.slaveAddress = slaveAddr;if(I2C_transfer(i2cHandle, &masterTransaction) == TRUE){return 1;}else{return 0;} }/******************************************************************************* * @fnBoardI2C_readReg * * @briefThis function implements the I2C protocol to read from a sensor. *The sensor must be selected before this routine is called. * * @paramaddr - which register to read * @parampBuf - pointer to buffer to place data * @paramnBytes - number of bytes to read * * @returnTRUE if the required number of bytes are received ******************************************************************************/ bool BoardI2C_readReg(uint8_t addr, uint8_t *pBuf, uint8_t nBytes) {return BoardI2C_writeRead(&addr,1,pBuf,nBytes); }/******************************************************************************* * @fnBoardI2C_writeReg * @briefThis function implements the I2C protocol to write to a sensor. *The sensor must be selected before this routine is called. * * @paramaddr - which register to write * @parampBuf - pointer to buffer containing data to be written * @paramnBytes - number of bytes to write * * @returnTRUE if successful write */ bool BoardI2C_writeReg(uint8_t addr, uint8_t *pBuf, uint8_t nBytes) {uint8_t i;uint8_t *p = buffer;/* Copy address and data to local buffer for burst write */*p++ = addr;for (i = 0; i < nBytes; i++){*p++ = *pBuf++;}nBytes++;/* Send data */return BoardI2C_write(buffer,nBytes); } void AD345_WR_Reg(uint8_t addr,uint8_t val) {BoardI2C_writeReg(addr,&val,1); }uint8_t AD_RD_Reg(uint8_t addr) {uint8_t temp=0;BoardI2C_readReg(addr,&temp,1);return temp;//返回读到的值 }这个程序是我写的一个小demo,你可以参考一下.
Eggsy Pang:
I2C地址是7位的,不存在0x80. 你会不会想输入的是0x40吧