/* ********************************************************************************************************* * 函 数 名: SI4730_PowerUp_FM_Revice * 功能说明: 配置Si4703为FM接收模式, 模拟模式(非数字模式) * 形 参:无 * 返 回 值: 0 失败, 1 成功 ********************************************************************************************************* */ uint8_t SI4730_PowerUp_FM_Revice(void) { uint8_t ack; uint8_t status; /* AN332 page = 277 Powerup in Analog Mode CMD 0x01 POWER_UP ARG1 0xC0 Set to FM Receive. Enable interrupts. ARG2 0x05 Set to Analog Audio Output STATUS →0x80 Reply Status. Clear-to-send high. */ i2c_Start(); i2c_SendByte(I2C_ADDR_SI4730_W); ack = i2c_WaitAck(); i2c_SendByte(0x01); ack = i2c_WaitAck(); i2c_SendByte(0xC0); ack = i2c_WaitAck(); i2c_SendByte(0x05); ack = i2c_WaitAck(); i2c_Stop(); /* 等待器件返回状态 0x80 */ { uint32_t i; for (i = 0; i < 2500; i++) { i2c_Start(); i2c_SendByte(I2C_ADDR_SI4730_R); /* 读 */ ack = i2c_WaitAck(); status = i2c_ReadByte(); i2c_NAck(); i2c_Stop(); if (status == 0x80) { break; } } /* 实测 535 次循环应该正常退出 */ if (i == 2500) { return 0; } } return 1; }
unsigned int mem_Write(unsigned int addr,unsigned char *data, unsigned int data_len) { unsigned int i=0; unsigned char b; i2c_Start(); i2c_Write(MEM_ADDRESS); if (i2c_Timeout()) goto Timeout; b = addr>>8; i2c_Write(b); if (i2c_Timeout()) goto Timeout; b = addr&0xFF; i2c_Write(b); if (i2c_Timeout()) goto Timeout; for (i=0; i<data_len; i++) { i2c_Write(data[i]); if (i2c_Timeout()) goto Timeout; } i2c_Stop(); return i; Timeout: return i; }
/*! * Start I2C Transmision * @param SlaveID is the 7 bit Slave Address * @param Mode sets Read or Write Mode */ void IIC_StartTransmission (unsigned char SlaveID, unsigned char Mode) { if(Mode == MWSR) { /* set transmission mode */ MasterTransmission = MWSR; } else { /* set transmission mode */ MasterTransmission = MRSW; } /* shift ID in right possition */ SlaveID = (unsigned char) ACCEL_I2C_ADDRESS << 1; /* Set R/W bit at end of Slave Address */ SlaveID |= (unsigned char)MasterTransmission; /* send start signal */ i2c_Start(); /* send ID with W/R bit */ i2c_write_byte(SlaveID); }
/*FUNCTION*------------------------------------------------------------------- * * Function Name : max3353_WriteReg * Returned Value : * Comments : Write data to max3353 register * * *END*----------------------------------------------------------------------*/ bool max3353_WriteReg ( uint8_t i2c_channel, uint8_t regAdd , uint8_t regValue ) { /* Send I2C slave address to bus*/ i2c_Start(i2c_channel); i2c_WriteByte(i2c_channel,(MAX3353_SLAVE_ADDR <<1) | 0); i2c_Wait(i2c_channel); /* Send register address of MAX3353 */ i2c_WriteByte(i2c_channel, regAdd); i2c_Wait(i2c_channel); /* Send data to MAX3353*/ i2c_WriteByte(i2c_channel, regValue); i2c_Wait(i2c_channel); /* Send stop signal */ i2c_Stop(i2c_channel); Pause(); return TRUE; }
////////////////////////////////////////////////////////////////////////// // I2C access start. // // Arguments: ucSlaveAdr - slave address // trans_t - I2C_TRANS_WRITE/I2C_TRANS_READ ////////////////////////////////////////////////////////////////////////// BOOL i2c_AccessStart(BYTE ucSlaveAdr, I2C_Direction trans_t) { BYTE ucDummy; // loop dummy if (trans_t == I2C_READ) // check i2c read or write ucSlaveAdr = I2C_DEVICE_ADR_READ(ucSlaveAdr); // read else ucSlaveAdr = I2C_DEVICE_ADR_WRITE(ucSlaveAdr); // write ucDummy = I2C_ACCESS_DUMMY_TIME; while (ucDummy--) { i2c_Delay(); if (i2c_Start() == FALSE) continue; if (i2c_SendByte(ucSlaveAdr) == I2C_ACKNOWLEDGE) // check acknowledge return TRUE; //printf("ucSlaveAdr====%x", ucSlaveAdr); i2c_Stop(); Delay1ms(1); } return FALSE; }
uint8_t my_i2c_read_nbytes(I2Cn_e i2cn, uint8_t addr, uint8_t reg, int length, uint8_t * pdata) { int i = 0; uint8_t dump; i2c_Start(i2cn); if(!my_i2c_write_byte(i2cn, ( addr << 1 ) | MWSR)) { return 0; } if(!my_i2c_write_byte(i2cn, reg)) { return 0; } i2c_RepeatedStart(i2cn); if(!my_i2c_write_byte(i2cn, ( addr << 1 ) | MRSW)) { return 0; } i2c_EnterRxMode(i2cn); dump = I2C_D_REG(I2C0_BASE_PTR); if(!my_i2c_Wait(i2cn)) { return 0; } for(i = 0; i < length; i++) { if(i == length - 1) { i2c_PutinRxMode(i2cn); *(pdata + i) = I2C_D_REG(I2C0_BASE_PTR); } else { *(pdata + i) = I2C_D_REG(I2C0_BASE_PTR); } if(!my_i2c_Wait(i2cn)) { return 0; } } i2c_Stop(i2cn); return 1; }
/*! * @brief 读取I2C设备指定地址寄存器的数据 * @param I2Cn_e I2C模块(I2C0、I2C1) * @param SlaveID 从机地址(7位地址) * @param reg 从机寄存器地址 * @return 读取的寄存器值 * @since v5.0 * Sample usage: uint8 value = i2c_read_reg(I2C0, 0x1D, 1); */ uint8 i2c_read_reg(I2Cn_e i2cn, uint8 SlaveID, uint8 reg) { //先写入寄存器地址,再读取数据,因此此过程是 I2C 的复合格式,改变数据方向时需要重新启动 uint8 result; ASSERT((SlaveID & 0x80) == 0); //断言,我们要求的7位地址的值仅仅是7bit,不是通信时要求的高7位 //有些手册,给出的7位地址指的是8bit里的高7位 //有些手册,给出的7位地址指的是7bit //请自行确认,可以尝试是否通信正常来确认 i2c_Start(i2cn); //发送启动信号 i2c_write_byte(i2cn, ( SlaveID << 1 ) | MWSR); //发送从机地址和写位 i2c_write_byte(i2cn, reg); //发送从机里的寄存器地址 i2c_RepeatedStart(i2cn); //复合格式,发送重新启动信号 i2c_write_byte(i2cn, ( SlaveID << 1) | MRSW ); //发送从机地址和读位 i2c_PutinRxMode(i2cn); //进入接收模式(不应答,只接收一个字节) result = I2C_D_REG(I2CN[i2cn]); //虚假读取一次,启动接收数据 i2c_Wait(i2cn); //等待接收完成 i2c_Stop(i2cn); //发送停止信号 result = I2C_D_REG(I2CN[i2cn]); //读取数据 Pause(); //必须延时一下,否则出错 return result; }
/* ********************************************************************************************************* * 函 数 名: SI4730_SendCmd * 功能说明: 向Si4730发送CMD * 形 参: _pCmdBuf : 命令数组 * _CmdLen : 命令串字节数 * 返 回 值: 0 失败(器件无应答), 1 成功 ********************************************************************************************************* */ uint8_t SI4730_SendCmd(uint8_t *_pCmdBuf, uint8_t _ucCmdLen) { uint8_t ack; uint8_t i; i2c_Start(); i2c_SendByte(I2C_ADDR_SI4730_W); ack = i2c_WaitAck(); if (ack != 0) { goto err; } for (i = 0; i < _ucCmdLen; i++) { i2c_SendByte(_pCmdBuf[i]); ack = i2c_WaitAck(); if (ack != 0) { goto err; } } i2c_Stop(); return 1; err: i2c_Stop(); return 0; }
//----------------------------------------------- long _24c01_read_4byte(short adr) { long temp; //char temp1; long temp_[4]; i2c_Start(); i2c_SendByte(0xa0); if(i2c_ReadAcknowledge()) goto Stop_label_24c01_read_4byte; i2c_SendByte((char)adr); if(i2c_ReadAcknowledge()) goto Stop_label_24c01_read_4byte; i2c_Restart(); i2c_SendByte(0xa1); if(i2c_ReadAcknowledge()) goto Stop_label_24c01_read_4byte; temp_[0]=i2c_ReadByte(); i2c_SendAcknowledge(1); temp_[1]=i2c_ReadByte(); i2c_SendAcknowledge(1); temp_[2]=i2c_ReadByte(); i2c_SendAcknowledge(1); temp_[3]=i2c_ReadByte(); i2c_SendAcknowledge(0); Stop_label_24c01_read_4byte: i2c_Stop(); temp_[0]&=0x000000ff; temp_[1]<<=8; temp_[1]&=0x0000ff00; temp_[2]<<=16; temp_[2]&=0x00ff0000; temp_[3]<<=24; temp_[3]&=0xff000000; temp=temp_[0]+temp_[1]+temp_[2]+temp_[3]; return temp; }
void i2c_slave_command(uint8_t slave_address, uint8_t address, uint8_t command) { i2c_Start(); // send Start i2c_Address(slave_address, I2C_WRITE); // Send slave address with write operation i2c_Write(address); // Servo Speed i2c_Write(command); // 2 seconds on servo 0 i2c_Stop(); // send Stop }
/* ********************************************************************************************************* * 函 数 名: SI4730_GetRevision * 功能说明: 读取器件、固件信息。 返回8字节数据 * 形 参:_ReadBuf 返回结果存放在此缓冲区,请保证缓冲区大小大于等于8 * 返 回 值: 0 失败, 1 成功 ********************************************************************************************************* */ uint8_t SI4730_GetRevision(uint8_t *_ReadBuf) { uint8_t ack; uint8_t status; uint32_t i; /* AN223 page = 67 */ /* 发送 0x10 命令 */ i2c_Start(); i2c_SendByte(I2C_ADDR_SI4730_W); ack = i2c_WaitAck(); i2c_SendByte(0x10); ack = i2c_WaitAck(); i2c_Stop(); /* 等待器件状态为 0x80 */ for (i = 0; i < 50; i++) { i2c_Start(); i2c_SendByte(I2C_ADDR_SI4730_R); /* 读 */ ack = i2c_WaitAck(); status = i2c_ReadByte(); if (status == 0x80) { break; } } /* 实测 2 次循环应该正常退出 */ if (i == 50) { i2c_NAck(); i2c_Stop(); return 0; } /* 连续读取8个字节的器件返回信息 */ for (i = 0; i < 8; i++) { i2c_Ack(); _ReadBuf[i] = i2c_ReadByte(); } i2c_NAck(); i2c_Stop(); return 1; }
/******************************************************************************* * Function Name: i2c_I2CCyBtldrCommStart ******************************************************************************** * * Summary: * Starts the I2C component and enables its interrupt. * Every incoming I2C write transaction is treated as a command for the * bootloader. * Every incoming I2C read transaction returns 0xFF until the bootloader * provides a response to the executed command. * * Parameters: * None * * Return: * None * *******************************************************************************/ void i2c_I2CCyBtldrCommStart(void) { i2c_I2CSlaveInitWriteBuf(i2c_slWriteBuf, i2c_I2C_BTLDR_SIZEOF_WRITE_BUFFER); i2c_I2CSlaveInitReadBuf (i2c_slReadBuf, 0u); i2c_SetCustomInterruptHandler(&i2c_I2CResposeInsert); i2c_applyBuffer = 0u; i2c_Start(); }
uint8_t i2c_Read(uint8_t anbt_dev_addr, uint8_t anbt_reg_addr, uint8_t anbt_i2c_len, uint8_t *anbt_i2c_data_buf) { i2c_Start(); i2c_SendByte(anbt_dev_addr << 1 | I2C_Direction_Transmitter); //发送陀螺仪写地址 i2c_SendByte(anbt_reg_addr); //发送陀螺仪ID地址 i2c_Start(); i2c_SendByte(anbt_dev_addr << 1 | I2C_Direction_Receiver); //发送陀螺仪读地址 // while (anbt_i2c_len) { if (anbt_i2c_len==1) *anbt_i2c_data_buf =i2c_ReadByte(); else *anbt_i2c_data_buf =i2c_ReceiveByte_WithACK(); anbt_i2c_data_buf++; anbt_i2c_len--; } i2c_Stop(); return 0x00; }
uint8_t i2c_Write(uint8_t anbt_dev_addr, uint8_t anbt_reg_addr, uint8_t anbt_i2c_len, uint8_t *anbt_i2c_data_buf) { uint8_t i; i2c_Start(); i2c_SendByte(anbt_dev_addr << 1 | I2C_Direction_Transmitter); //发送陀螺仪写地址 i2c_SendByte(anbt_reg_addr); //发送陀螺仪PWM地址 for (i=0;i<anbt_i2c_len;i++) i2c_SendByte(anbt_i2c_data_buf[i]); //发送陀螺仪PWM值 i2c_Stop(); return 0x00; }
//to-do write in comments what these do. void init9axis(){ i2c_Init(); i2c_ResetBus(); i2c_Start(acc_i2c_addr,I2C_WRITE); i2c_Write(0x20); i2c_Write(0x2F); i2c_ResetBus(); // i2c_Start(acc_i2c_addr,I2C_WRITE); // i2c_Write(0x23); // i2c_Write(0xC0); // i2c_ResetBus(); i2c_Start(mag_i2c_addr,I2C_WRITE); i2c_Write(0x00); i2c_Write(0x08); i2c_ResetBus(); i2c_Start(mag_i2c_addr,I2C_WRITE); i2c_Write(0x01); i2c_Write(0x60); i2c_ResetBus(); i2c_Start(mag_i2c_addr,I2C_WRITE); i2c_Write(0x02); i2c_Write(0x00); i2c_ResetBus(); i2c_Start(gyr_i2c_addr,I2C_WRITE); i2c_Write(0x16); i2c_Write(0x18); i2c_ResetBus(); //Setup Timer4 to be used as a trigger for information collection TMR4 =0; T4CON=0; T4CONbits.TCKPS = 0b11; // 1:8 for now _T4IP = 0x01; // Set Timer4 Interrupt Priority Level IFS1bits.T4IF = 0; // Clear Timer4 Interrupt Flag IEC1bits.T4IE = 1; // Enable Timer4 interrupt T4CONbits.TON = 1; // turn on T4 }
/* ********************************************************************************************************* * 函 数 名: MPU6050_ReadByte * 功能说明: 读取 MPU-6050 寄存器的数据 * 形 参: _ucRegAddr : 寄存器地址 * 返 回 值: 无 ********************************************************************************************************* */ uint8_t MPU6050_ReadByte(uint8_t _ucRegAddr) { uint8_t ucData; i2c_Start(); /* 总线开始信号 */ i2c_SendByte(MPU6050_SLAVE_ADDRESS); /* 发送设备地址+写信号 */ i2c_WaitAck(); i2c_SendByte(_ucRegAddr); /* 发送存储单元地址 */ i2c_WaitAck(); i2c_Start(); /* 总线开始信号 */ i2c_SendByte(MPU6050_SLAVE_ADDRESS+1); /* 发送设备地址+读信号 */ i2c_WaitAck(); ucData = i2c_ReadByte(); /* 读出寄存器数据 */ i2c_NAck(); i2c_Stop(); /* 总线停止信号 */ return ucData; }
/************************************************************************* * 野火嵌入式开发工作室 * * 函数名称:I2C_StartTransmission * 功能说明:启动 I2C 传输 * 参数说明:I2Cn 模块号(I2C0、I2C1) * SlaveID 7位从机地址 * MSmode 读写模式( MWSR 或 MRSW ) * 函数返回:无 * 修改时间:2012-1-20 * 备 注: *************************************************************************/ void I2C_StartTransmission (I2Cn i2cn, u8 SlaveID, MSmode Mode) { //ASSERT(Mode == MWSR || Mode == MRSW); //使用断言,检测 Mode 是否为 读 或 写 SlaveID = ( SlaveID << 1 ) | Mode ; //确定写地址和读地址 /* send start signal */ i2c_Start(i2cn); /* send ID with W/R bit */ i2c_write_byte(i2cn, SlaveID); }
unsigned int mem_Read(unsigned int addr,unsigned char *data, unsigned int data_len) { unsigned int i=0; unsigned char b; i2c_Start(); i2c_Write(MEM_ADDRESS); if (i2c_Timeout()) goto Timeout; b = addr>>8; i2c_Write(b); if (i2c_Timeout()) goto Timeout; b = addr&0xFF; i2c_Write(b); if (i2c_Timeout()) goto Timeout; //i2c_Stop(); i2c_Start(); i2c_Write(MEM_ADDRESS | 1); if (i2c_Timeout()) goto Timeout; for (i=0; i<data_len; i++) { data[i] = i2c_Read(); clear_CPU_operation_detection(); if (i<data_len-1) i2c_Ack(); else i2c_Nack(); } i2c_Stop(); return i; Timeout: return i; }
//----------------------------------------------- void _24c01_write_2byte(short adr,short data) { i2c_Start(); i2c_SendByte(0xa0); if(i2c_ReadAcknowledge()) goto Stop_label_24c01_write_2byte; i2c_SendByte((char)adr); if(i2c_ReadAcknowledge()) goto Stop_label_24c01_write_2byte; i2c_SendByte(data); i2c_ReadAcknowledge(); i2c_SendByte(data>>8); i2c_ReadAcknowledge(); Stop_label_24c01_write_2byte: i2c_Stop(); }
/*FUNCTION*------------------------------------------------------------------- * * Function Name : IIC_StartTransmission * Returned Value : None * Comments : Start I2C Transmission * * *END*----------------------------------------------------------------------*/ void IIC_StartTransmission (unsigned char SlaveID, unsigned char Mode) { /* shift ID in right position */ SlaveID = (unsigned char) SlaveID << 1; /* Set R/W bit at end of Slave Address */ SlaveID |= (unsigned char)Mode; /* send start signal */ i2c_Start(); /* send ID with W/R bit */ i2c_write_byte(SlaveID); }
/* ********************************************************************************************************* * 函 数 名: i2c_CheckDevice * 功能说明: 检测I2C总线设备,CPU向发送设备地址,然后读取设备应答来判断该设备是否存在 * 形 参:_Address:设备的I2C总线地址 * 返 回 值: 返回值 0 表示正确, 返回1表示未探测到 ********************************************************************************************************* */ uint8_t i2c_CheckDevice(uint8_t _Address) { uint8_t ucAck; i2c_Cfg(); /* 配置GPIO及速度*/ i2c_Start(); /* 发送启动信号 */ /* 发送设备地址+读写控制bit(0 = w, 1 = r) bit7 先传 */ i2c_SendByte(_Address | I2C_WR); ucAck=i2c_WaitAck(); i2c_Stop(); /* 发送停止信号 */ return ucAck; }
void i2c_write_reg(I2Cn_e i2cn, uint8 SlaveID, uint8 reg, uint8 Data) { i2c_Start(i2cn); //发送启动信号 i2c_write_byte(i2cn, ( SlaveID << 1 ) | MWSR); //发送从机地址和写位 i2c_write_byte(i2cn, reg); //发送从机里的寄存器地址 i2c_write_byte(i2cn, Data); //发送需要写入的数据 i2c_Stop(i2cn); Pause(); //延时太短的话,可能写出错 }
/* ********************************************************************************************************* * 函 数 名: MPU6050_WriteByte * 功能说明: 向 MPU-6050 寄存器写入一个数据 * 形 参: _ucRegAddr : 寄存器地址 * _ucRegData : 寄存器数据 * 返 回 值: 无 ********************************************************************************************************* */ void MPU6050_WriteByte(uint8_t _ucRegAddr, uint8_t _ucRegData) { i2c_Start(); /* 总线开始信号 */ i2c_SendByte(MPU6050_SLAVE_ADDRESS); /* 发送设备地址+写信号 */ i2c_WaitAck(); i2c_SendByte(_ucRegAddr); /* 内部寄存器地址 */ i2c_WaitAck(); i2c_SendByte(_ucRegData); /* 内部寄存器数据 */ i2c_WaitAck(); i2c_Stop(); /* 总线停止信号 */ }
/******************************************************************************* * 名 称: i2c_CheckDevice * 功 能: 检测I2C总线设备,CPU向发送设备地址,然后读取设备应答来判断该设备是否存在 * 入口参数: _Address:设备的I2C总线地址 * 出口参数: 返回值 0 表示正确, 返回1表示未探测到 * 作 者: Roger-WY * 创建日期: 2016-05-20 * 修 改: * 修改日期: * 备 注: *******************************************************************************/ uint8_t i2c_CheckDevice(uint8_t _Address) { uint8_t ucAck; Bsp_InitI2C(); if (I2C_SDA_READ() && I2C_SCL_READ()) { i2c_Start(); /* 发送启动信号 */ /* 发送设备地址+读写控制bit(0 = w, 1 = r) bit7 先传 */ i2c_SendByte(_Address | I2C_WR); ucAck = i2c_WaitAck(); /* 检测设备的ACK应答 */ i2c_Stop(); /* 发送停止信号 */ return ucAck; } return 1; /* I2C总线异常 */ }
/* ********************************************************************************************************* * 函 数 名: SI4730_WaitStatus80 * 功能说明: 读取Si4730的状态,等于0x80时返回。 * 形 参: _uiTimeOut : 轮询次数 * _ucStopEn : 状态0x80检测成功后,是否发送STOP * 返 回 值: 0 失败(器件无应答), > 1 成功, 数字表示实际轮询次数 ********************************************************************************************************* */ uint32_t SI4730_WaitStatus80(uint32_t _uiTimeOut, uint8_t _ucStopEn) { uint8_t ack; uint8_t status; uint32_t i; /* 等待器件状态为 0x80 */ for (i = 0; i < _uiTimeOut; i++) { i2c_Start(); i2c_SendByte(I2C_ADDR_SI4730_R); /* 读 */ ack = i2c_WaitAck(); if (ack == 1) { i2c_NAck(); i2c_Stop(); return 0; /* 器件无应答,失败 */ } status = i2c_ReadByte(); if ((status == 0x80) || (status == 0x81)) /* 0x81 是为了执行0x23指令 读取信号质量 */ { break; } } if (i == _uiTimeOut) { i2c_NAck(); i2c_Stop(); return 0; /* 超时了,失败 */ } /* 成功了, 处理一下第1次就成功的情况 */ if (i == 0) { i = 1; } /* 因为有些命令还需要读取返回值,因此此处根据形参决定是否发送STOP */ if (_ucStopEn == 1) { i2c_NAck(); i2c_Stop(); } return i; }
// Read a char from servo ic uint8_t i2c_slave_read(uint8_t slave_address, uint8_t address) { uint8_t read_byte; // Read one byte (i.e. servo pos of one servo) i2c_Start(); // send Start i2c_Address(slave_address, I2C_WRITE); // Send slave address with write operation i2c_Write(address); // Set register for servo 0 i2c_Restart(); // Restart i2c_Address(slave_address, I2C_READ); // Send slave address with read operation read_byte = i2c_Read(0); // Read one byte // If more than one byte to be read, (0) should // be on last byte only // e.g.3 bytes= i2c_Read(1); i2c_Read(1); i2c_Read(0); i2c_Stop(); // send Stop return read_byte; // return byte. If reading more than one byte // store in an array }
/*FUNCTION*------------------------------------------------------------------- * * Function Name : max3353_ReadReg * Returned Value : * Comments : Read data from max3353 register * * *END*----------------------------------------------------------------------*/ bool max3353_ReadReg ( uint8_t i2c_channel, uint8_t regAdd, uint8_t* p_regValue ) { uint8_t result; uint32_t j; /* Send Slave Address */ i2c_Start(i2c_channel); i2c_WriteByte(i2c_channel, (MAX3353_SLAVE_ADDR <<1) | 0); i2c_Wait(i2c_channel); /* Write Register Address */ i2c_WriteByte(i2c_channel, regAdd); i2c_Wait(i2c_channel); /* Do a repeated start */ i2c_RepeatedStart(i2c_channel); /* Send Slave Address */ i2c_WriteByte(i2c_channel, (MAX3353_SLAVE_ADDR << 1) | 0x01); i2c_Wait(i2c_channel); /* Put in Rx Mode */ i2c_SetRXMode(i2c_channel); /* Turn off ACK */ i2c_GiveNACK(i2c_channel); /* Dummy read */ result = i2c_ReadByte(i2c_channel); for (j=0; j<5000; j++){}; i2c_Wait(i2c_channel); /* Send stop signal */ i2c_Stop(i2c_channel); result = i2c_ReadByte(i2c_channel); Pause(); *p_regValue = result; return TRUE; }
void my_i2c_write_reg(I2Cn_e i2cn, uint8 SlaveID, uint8 reg, uint8 Data) { i2c_Start(i2cn); //发送启动信号 if(!my_i2c_write_byte(i2cn, ( SlaveID << 1 ) | MWSR)) { return; } //发送从机地址和写位 if(!my_i2c_write_byte(i2cn, reg)) { return; } //发送从机里的寄存器地址 if(!my_i2c_write_byte(i2cn, Data)) { return; } //发送需要写入的数据 i2c_Stop(i2cn); DELAY_US(500);//延时太短的话,可能写出错 }
//----------------------------------------------- void _24c01_read_nbyte(short adr, char* buff, char n) { char i; i2c_Start(); i2c_SendByte(0xa0); if(i2c_ReadAcknowledge()) goto Stop_label_24c01_read_nbyte; i2c_SendByte((char)adr); if(i2c_ReadAcknowledge()) goto Stop_label_24c01_read_nbyte; i2c_Restart(); i2c_SendByte(0xa1); if(i2c_ReadAcknowledge()) goto Stop_label_24c01_read_nbyte; for(i=0;i<(n-1);i++) { buff[i]=i2c_ReadByte(); i2c_SendAcknowledge(1); } buff[i]=i2c_ReadByte(); i2c_SendAcknowledge(0); Stop_label_24c01_read_nbyte: i2c_Stop(); }
//----------------------------------------------- short _24c01_read_2byte(short adr) { short temp; char temp1; i2c_Start(); i2c_SendByte(0xa0); if(i2c_ReadAcknowledge()) goto Stop_label_24c01_read_2byte; i2c_SendByte((char)adr); if(i2c_ReadAcknowledge()) goto Stop_label_24c01_read_2byte; i2c_Restart(); i2c_SendByte(0xa1); if(i2c_ReadAcknowledge()) goto Stop_label_24c01_read_2byte; temp1=i2c_ReadByte(); i2c_SendAcknowledge(1); temp=i2c_ReadByte(); i2c_SendAcknowledge(0); Stop_label_24c01_read_2byte: i2c_Stop(); temp<<=8; temp+=temp1; return temp; }