u8 IIC1_MultRead(u8 slaveAddr, u8* pBuffer, u8 readAddr, u16 NumByteToRead) { IIC_Start(); //起始信号 IIC_Send_Byte(slaveAddr); //发送写器件指令 if(FALSE==IIC_Wait_Ack()) { IIC_Stop(); //发送停止信号 return 1; } IIC_Send_Byte(readAddr); //发送寄存器地址 IIC_Wait_Ack(); IIC_Start(); //重新启动 IIC_Send_Byte(slaveAddr+1); //发送读器件指令 IIC_Wait_Ack(); /* While there is data to be read */ while(NumByteToRead) { if(NumByteToRead == 1) { *pBuffer=IIC_Read_Byte(0); //读出寄存器数据不继续再读,发送NACK } else { *pBuffer=IIC_Read_Byte(1); //读出寄存器数据继续读,发送ACK pBuffer++; /* Point to the next location where the byte read will be saved */ } /* Decrement the read bytes counter */ NumByteToRead--; } IIC_Stop(); //停止信号 return 0; }
/**************************实现函数******************************************** *函数原型: bool i2cWrite(uint8_t addr, uint8_t reg, uint8_t data) *功 能: *******************************************************************************/ int i2cRead(uint8_t addr, uint8_t reg, uint8_t len, uint8_t *buf) { if (!IIC_Start()) return 1; IIC_Send_Byte(addr << 1); if (!IIC_Wait_Ack()) { IIC_Stop(); return 1; } IIC_Send_Byte(reg); IIC_Wait_Ack(); IIC_Start(); IIC_Send_Byte((addr << 1)+1); IIC_Wait_Ack(); while (len) { if (len == 1) *buf = IIC_Read_Byte(0); else *buf = IIC_Read_Byte(1); buf++; len--; } IIC_Stop(); return 0; }
u8 IIC1_MultWrite(u8 slaveAddr, u8* pBuffer, u8 wrtieAddr, u16 NumByteToWrite) { IIC_Start(); //起始信号 IIC_Send_Byte(slaveAddr); //发送设备地址+写信号 if(FALSE==IIC_Wait_Ack()) { IIC_Stop(); //发送停止信号 return 1; } IIC_Send_Byte(wrtieAddr); //内部寄存器地址 IIC_Wait_Ack(); while(NumByteToWrite) { IIC_Send_Byte(*pBuffer); //内部寄存器数据 if(FALSE==IIC_Wait_Ack()) { IIC_Stop(); //发送停止信号 return 1; } pBuffer++; NumByteToWrite--; } IIC_Stop(); //发送停止信号 return 0; }
void main(void) { int i=0; EnableInterrupts; /* enable interrupts */ /* include your code here */ InitClock(); IIC1_Init(); configIIC(); /* IIC_Start(); IIC_write_byte(0x3D); IIC_write_byte(0x03); IIC_Stop(); */ //iic1=IIC_read_byte(); for(;;) { IIC_Start(); IIC_write_byte(0x3C); IIC_write_byte(0x03); IIC_Stop(FALSE); IIC_write_byte(0x3D); while(i<6){ iic1=IIC_read_byte(); i++; } i=0; IIC_Stop(FALSE); //__RESET_WATCHDOG(); /* feeds the dog */ } /* loop forever */ /* please make sure that you never leave main */ }
//IIC写一个字节 //reg:寄存器地址 //data:数据 //返回值:0,正常 // 其他,错误代码 u8 MPU_Write_Byte(u8 reg,u8 data) { IIC_Start(); IIC_Send_Byte((MPU_ADDR<<1)|0);//发送器件地址+写命令 if(IIC_Wait_Ack()) //等待应答 { printf("in MPU_Write_Byte: failed at 1,reg=%X,data=%X\n",reg,data); IIC_Stop(); return 1; } IIC_Send_Byte(reg); //写寄存器地址 if(IIC_Wait_Ack()){ printf("in MPU_Write_Byte: failed at 2,reg=%X,data=%X\n",reg,data); IIC_Stop(); return 1; } //等待应答 IIC_Send_Byte(data);//发送数据 if(IIC_Wait_Ack()) //等待ACK { printf("in MPU_Write_Byte: failed at 3,reg=%X,data=%X\n",reg,data); IIC_Stop(); return 1; } IIC_Stop(); return 0; }
uint8_t IIC_Read_Bytes(uint8_t dev, uint8_t reg, uint8_t length, uint8_t *data) { uint8_t count = 0; uint8_t temp; IIC_Start(); IIC_Send_Byte(dev); IIC_Wait_Ack(); IIC_Send_Byte(reg); IIC_Wait_Ack(); IIC_Start(); IIC_Send_Byte(dev + 1); IIC_Wait_Ack(); for(count = 0; count < length; count++) { if(count != (length - 1) ) temp = IIC_Receive_Byte(1); else temp = IIC_Receive_Byte(0); data[count] = temp; } IIC_Stop(); return count; }
//写数据,成功返回0,失败返回0xff int IIC_WriteData(u8 dev_addr,u8 reg_addr,u8 data) { IIC_Start(); IIC_WriteBit(dev_addr); if(IIC_WaitAck() == 0xff) { printf("error 2B\r\n"); return 0xff; } IIC_WriteBit(reg_addr); if(IIC_WaitAck() == 0xff) { printf("error 2C\r\n"); return 0xff; } IIC_WriteBit(data); if(IIC_WaitAck() == 0xff) { printf("error 2D\r\n"); return 0xff; } IIC_Stop(); return 0; }
uint8_t IIC_Wait_Ack(void) { uint8_t ucErrTime = 0; SDA_IN(); IIC_SDA(1); delay_us(1); IIC_SCL(1); delay_us(1); while(READ_SDA) { ucErrTime++; if(ucErrTime>250) { IIC_Stop(); return 1; } } IIC_SCL(0); return 0; }
void WM8960_WriteData(Uint8 channel, Uint8 Register, Uint16 Data) { Uint8 HightByte,LowByte; Uint8 I2C_Status; HightByte = Data >> 8; HightByte = HightByte & 0x01; HightByte |= (Register << 1); LowByte = Data; //bit 7 - 0 IIC_Start(channel); IIC_Delay(); if(!(IIC_WriteByte(channel,WM8960_Write_ADD))) { I2C_Status = IIC_WriteByte(channel,HightByte); I2C_Status = IIC_WriteByte(channel,LowByte); IIC_Delay(); IIC_Stop(channel); #if DebugVerbosity > 1 if(I2C_Status == 0) { UART_TxStr("Channel = "); UART_TxUint8(channel); UART_TxStr("Register = "); UART_TxUint8(Register); UART_TxStr(" Value = "); UART_TxUint8(Data>>8); UART_TxUint8(LowByte); UART_TxStr("\n\r"); }
//等待应答信号到来 //返回值:1,接收应答失败 // 0,接收应答成功 u8 IIC_Wait_Ack(void) { u8 ucErrTime=0; //SDA_IN(); //SDA设置为输入 GPIO_InitTypeDef GPIO_InitStructure; // RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOB, ENABLE); GPIO_InitStructure.GPIO_Pin = GPIO_Pin_14; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN; GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ; GPIO_Init(GPIOB, &GPIO_InitStructure); IIC_SDA_1;delay_us(1); IIC_SCL_1;delay_us(1); while(READ_SDA) { ucErrTime++; if(ucErrTime>250) { IIC_Stop(); return 1; } } IIC_SCL_0;//时钟输出0 return 0; }
void SetUpADS1110(unsigned char x) { IIC_Start(); IIC_Send_Byte(ADS1110_ADDR & 0xFE); IIC_Send_Byte(x); IIC_Stop(); }
/****************************************************************************** 函数(模块)名称:unsigned char IIC_Page_Write(unsigned char W_addr, unsigned char *WriteData_Addr, unsigned char WriteData_Cnt) 功能: IIC页写 输入参数: 要写的地址,要写的数据指针,要写的字节数 输出参数: 1 写成功 0 写失败 其它说明: 要写的字节数,不能大于当前写字节地址到当前页最大地址之差。 *******************************************************************************/ unsigned char IIC_Page_Write(unsigned char W_addr,unsigned char *WriteData_Addr, unsigned char WriteData_Cnt) { unsigned char try_time=define_try_time; //用来定义出现通讯错误时尝试的错误 while(1) { if(!try_time--) //用来防止因硬件错误或其它原因程序进入死循环 { return 0; } IIC_Start(); //步骤1:主机产生一个启动信号 IIC_Send_Byte(Slave_Addres&(~BIT0)); //步骤2:主机发送从机地址及读写位 if(ReadAck()) continue; //步骤3:主机接收从机应答;没有应答,忽略下面语句,重新开始 //应答,则开始发送地址 IIC_Send_Byte(W_addr); //步骤4:主机发送读写地址 if(ReadAck()) continue; //步骤5:主机接收从机应答 没有应答,忽略下面语句,重新开始 while(WriteData_Cnt--) { IIC_Send_Byte(*(WriteData_Addr++)); if(!ReadAck()) continue; //收到应答(0),则继续发送数据 return 0; //没收到应答,错误返回 } break; } IIC_Stop(); //步骤8:主机发送停止信号,启动写周期 Delay(5000); //需要延时一段时间,等待写周期完成 return 1; }
/****************************************************************************** 函数(模块)名称:unsigned char IIC_Single_Write(unsigned char W_addr,unsigned char Date_byte) 功能: IIC总线单字节写 输入参数: 要写的地址,要写的数据 输出参数: 1 写成功 0 写失败 其它说明: *******************************************************************************/ unsigned char IIC_Single_Write(unsigned char W_addr,unsigned char Date_byte) { unsigned char try_time=define_try_time; //用来定义出现通讯错误时尝试的错误 while(1) { if(!try_time--) //用来防止因硬件错误或其它原因程序进入死循环 { return 0; } IIC_Start(); //步骤1:主机产生一个启动信号 IIC_Send_Byte(Slave_Addres&(~BIT0)); //步骤2:主机发送从机地址及读写位 if(ReadAck()) continue; //步骤3:主机接收从机应答;没有应答,忽略下面语句,重新开始 //应答,则开始发送地址 IIC_Send_Byte(W_addr); //步骤4:主机发送读写地址 if(ReadAck()) continue; //步骤5:主机接收从机应答 没有应答,忽略下面语句,重新开始 IIC_Send_Byte(Date_byte); //步骤6:主机发送数据 //应答,则开始发送要写入的数据 if(ReadAck()) //步骤7:机接收从机应答 continue; //没有应答,忽略下面语句,重新开始 break; //应答,则跳出循环 } IIC_Stop(); //步骤8:主机发送停止信号,启动写周期 Delay(5000); //需要延时一段时间,等待写周期完成 return 1; }
/****************************************************************************** 函数(模块)名称:unsigned char IIC_Single_Read(unsigned int R_addr,unsigned char *R_word) 功能: 单次读/随机读 输入参数: 要读的数据地址,输出数据保存的字节指针 输出参数: 1 读成功 0 读失败 其它说明: *******************************************************************************/ unsigned char IIC_Single_Read(unsigned char R_addr,unsigned char *R_word) { unsigned char try_time=define_try_time; //用来定义出现通讯错误时尝试的错误 while(1) { if(!try_time--) //用来防止因硬件错误或其它原因程序进入死循环 { return 0; } IIC_Start(); //步骤1:产生一个启动信号 IIC_Send_Byte(Slave_Addres&(~BIT0)); //步骤2:发送控制命令 if(ReadAck()) continue; //没有应答,忽略下面语句,重新开始 //应答,则开始发送地址 IIC_Send_Byte(R_addr); if(ReadAck()) continue; //没有应答,忽略下面语句,重新开始 //应答,则开始发送启动信号 IIC_Start(); //启动信号 IIC_Send_Byte(Slave_Addres|BIT0); //发送控制命令 if(ReadAck()) continue; //没有应答,忽略下面语句,重新开始 break; //应答,则跳出循环,开始接收数据 } *R_word=IIC_Receive_Byte(); //从总线上接收一个字节数据 WriteAck(1); //发送 非应答信号 IIC_Stop(); //发送停止信号 return 1; }
/****** busrt one byte via I2C *****/ void I2C_WriteByte(uint8_t SlaveAddress,uint8_t REG_Address,uint8_t REG_data) { IIC_Start(); //起始信号 IIC_SendByte(SlaveAddress); //发送设备地址+写信号 IIC_SendByte(REG_Address); //内部寄存器地址, //请参考中文pdf22页 IIC_SendByte(REG_data); //内部寄存器数据, //请参考中文pdf22页 IIC_Stop(); //发送停止信号 }
//IIC写命令 -- 命令操作很少,对速度影响不大 void CN_SSD1306::Write_IIC_Command(unsigned char IIC_Command) { IIC_Start(); Write_IIC_Byte(0x78); //Slave address,SA0=0 Write_IIC_Byte(0x00); //write command Write_IIC_Byte(IIC_Command); IIC_Stop(); }
/********************************************** // IIC Write Data **********************************************/ void Write_IIC_Data(unsigned char IIC_Data) { IIC_Start(); Write_IIC_Byte(0x78); //D/C#=0; R/W#=0 Write_IIC_Byte(0x40); //write data Write_IIC_Byte(IIC_Data); IIC_Stop(); }
/*********************************************** 函数名:void MPU6050_WirteByte(u8 reg, u8 data) 说明:MPU6050单次写字节 入口:u8 reg 寄存器地址 u8 data 需要写入的字节 出口:无 备注:无 ***********************************************/ void MPU6050_WirteByte(u8 reg, u8 data) { IIC_Start(); IIC_Send_Byte(MPU6050_DEVICE); // 发送器件地址+写信号 IIC_Send_Byte(reg); // 寄存器地址 IIC_Send_Byte(data); // 需要写入的数据 IIC_Stop(); }
void Single_Write_IIC(u8 SlaveAddress,u8 REG_Address,u8 REG_data) { IIC_Start(); IIC_SendByte(SlaveAddress); IIC_SendByte(REG_Address); IIC_SendByte(REG_data); IIC_Stop(); }
void OLED_WrCmd(unsigned char IIC_Command) { IIC_Start(); Write_IIC_Byte(0x78); //Slave address,SA0=0 Write_IIC_Byte(0x00); //write command Write_IIC_Byte(IIC_Command); IIC_Stop(); }
void configIIC(void){ IIC_Start(); IIC_write_byte(0x3C); IIC_write_byte(0x02); IIC_write_byte(0x00); IIC_Stop(TRUE); }
void OLED_WrDat(unsigned char IIC_Data) { IIC_Start(); Write_IIC_Byte(0x78); Write_IIC_Byte(0x40); //write data Write_IIC_Byte(IIC_Data); IIC_Stop(); }
void HAL_I2C_Stop(void) { #ifdef USE_I2C_OLD I2C_Stop(); #else IIC_Stop(); #endif }
/*********************************************************\ * IIC Write Register \*********************************************************/ void IIC_RegWrite(uint8_t address, uint8_t reg,uint8_t val) { IICC_TX = 1; // Transmit Mode IIC_Start(); // Send Start IIC_CycleWrite(address); // Send IIC "Write" Address IIC_CycleWrite(reg); // Send Register IIC_CycleWrite(val); // Send Value IIC_Stop(); // Send Stop }
void SetPWM(uint8_t num, uint16_t on, uint16_t off) { IIC_Start(); IIC_SendByte(LED0_ON_L+num*4); IIC_SendByte(on); IIC_SendByte(on>>8); IIC_SendByte(off); IIC_SendByte(off>>8); IIC_Stop(); }
/******************************************************************************* **函 数: IIC_WriteByte **功 能: IIC往指定地址写一字节数据 **参 数: Addr --地址 ** data --发送的数据 **返 回: void *******************************************************************************/ void IIC_WriteByte(u8 Addr,u8 data) { IIC_Start(); IIC_SendByte(DEVICE_ADDR); //WriteDeviceAddr IIC_WaitAck(); IIC_SendByte(Addr); IIC_WaitAck(); IIC_SendByte(data); IIC_WaitAck(); IIC_Stop(); }
//写ADXL345寄存器 //addr:寄存器地址 //val:要写入的值 //返回值:无 void ADXL345_WR_Reg(u8 addr,u8 val) { IIC_Start(); IIC_Send_Byte(ADXL_WRITE); //发送写器件指令 IIC_Wait_Ack(); IIC_Send_Byte(addr); //发送寄存器地址 IIC_Wait_Ack(); IIC_Send_Byte(val); //发送值 IIC_Wait_Ack(); IIC_Stop(); //产生一个停止条件 }
void IIC_Write_One_Byte(u8 daddr,u8 addr,u8 data) { IIC_Start(); IIC_Send_Byte(daddr);//器件地址+写信号 IIC_Wait_Ack(); IIC_Send_Byte(addr);//寄存器 IIC_Wait_Ack(); IIC_Send_Byte(data);//数据 IIC_Wait_Ack(); IIC_Stop(); }
//IIC写一个字节 //reg:寄存器地址 //data:数据 //返回值:0,正常 // 其他,错误代码 uint8_t MPU_Write_Byte(uint8_t reg,uint8_t data) { IIC_Start(); IIC_Send_Byte((MPU_ADDR<<1)|0);//发送器件地址+写命令 if(IIC_Wait_Ack()) //等待应答 { IIC_Stop(); return 1; } IIC_Send_Byte(reg); //写寄存器地址 IIC_Wait_Ack(); //等待应答 IIC_Send_Byte(data);//发送数据 if(IIC_Wait_Ack()) //等待ACK { IIC_Stop(); return 1; } IIC_Stop(); return 0; }
/********************************************** // IIC Write Data **********************************************/ void Write_IIC_Data(unsigned char IIC_Data) { #ifndef USE_I2C_LIB IIC_Start(); Write_IIC_Byte(0x78); //D/C#=0; R/W#=0 IIC_Wait_Ack(); Write_IIC_Byte(0xC0); //write data IIC_Wait_Ack(); Write_IIC_Byte(IIC_Data); IIC_Wait_Ack(); IIC_Stop(); #else IIC_Start(); Write_IIC_Addr7(0x78); Write_IIC_Byte(0xC0); //write command Write_IIC_Byte(IIC_Data); IIC_Stop(); #endif }