/* 连续读出ADXL345内部加速度数据,地址范围0x32~0x37 */ void Multiple_read(short *x,short *y,short *z) { unsigned char i; start_i2c(); //起始信号 sendbyte(ADXL_WRITE); //发送设备地址+写信号 i2c_Wait_Ack(); sendbyte(0x32); i2c_Wait_Ack(); start_i2c(); //起始信号 sendbyte(ADXL_READ); //发送设备地址+读信号 i2c_Wait_Ack(); for (i=0; i<6; i++) //连续读取6个地址数据,存储中BUF { ADXL_BUF[i] = rcvbyte(); //BUF[0]存储0x32地址中的数据 if (i == 5) { send_ack(1); //最后一个数据需要回NOACK } else { send_ack(0); //回应ACK } } stop_i2c( ); //停止信号 *x=(short)(((u16)ADXL_BUF[1]<<8)+ADXL_BUF[0]); *y=(short)(((u16)ADXL_BUF[3]<<8)+ADXL_BUF[2]); *z=(short)(((u16)ADXL_BUF[5]<<8)+ADXL_BUF[4]); }
u8 Multiple_read_BH1750(void) { u8 i; start_i2c(); //起始信号 sendbyte(SlaveAddress+1); //发送设备地址+读信号 i2c_Wait_Ack(); for (i=0; i<3; i++) //连续读取2个地址数据,存储中BUF { BUF[i] = rcvbyte(); //BUF[0]存储0x32地址中的数据 if (i == 3) { send_ack(1); //最后一个数据需要回NOACK } else { send_ack(0); //回应ACK } } stop_i2c(); //停止信号 delay_ms(5); return 1; }
unsigned char Single_Read(unsigned char REG_Address) { unsigned char REG_data; start_i2c(); //起始信号/ sendbyte(ADXL_WRITE); //发送设备地址+写信号 i2c_Wait_Ack(); sendbyte(REG_Address); //发送存储单元地址,从0开始 i2c_Wait_Ack(); start_i2c(); //起始信号 sendbyte(ADXL_READ); //发送设备地址+读信号 i2c_Wait_Ack(); REG_data=rcvbyte(); //读出寄存器数据 send_ack(1); stop_i2c( ); //停止信号 return REG_data; }
void Single_Write(unsigned char REG_Address,unsigned char REG_data) { start_i2c(); //起始信号 sendbyte(ADXL_WRITE); //发送设备地址+写信号 i2c_Wait_Ack(); sendbyte(REG_Address); //内部寄存器地址 i2c_Wait_Ack(); sendbyte(REG_data); //内部寄存器数据 i2c_Wait_Ack(); stop_i2c(); //发送停止信号+-+/ }
int Single_Write_BH1750(u8 REG_Address) { start_i2c(); //起始信号 sendbyte(SlaveAddress); //发送设备地址+写信号 i2c_Wait_Ack(); sendbyte(REG_Address); //内部寄存器地址, i2c_Wait_Ack(); stop_i2c(); //发送停止信号 return 0; }
void read_adc() { start_i2c(); shout_i2c(0x90); //device address and write mode ack_i2c(); shout_i2c(0x01); //control byte ack_i2c(); start_i2c(); shout_i2c(0x91); //device address and read mode ack_i2c(); adc=shin_i2c(); nack_i2c(); stop_i2c(); }
static void write_i2c(uint8_t address, uint8_t value) { printf("writing 0x%02X at address 0x%02X\n", value, address); output(1, 0, 1); usleep(100); /* Init */ output(1, 1, 1); usleep(2000); start_i2c(); write_byte_i2c(I2C_DEVSEL_WR); write_byte_i2c(address); write_byte_i2c(value); usleep(2000); stop_i2c(); usleep(2000); }