uint8 hal_dev_mag3110_read_reg(uint8 addr) { uint8 result; i2c_start(I2C_MAG); i2c_write_byte(I2C_MAG, MAG3110_I2C_ADDRESS | I2C_WRITE); i2c_wait(I2C_MAG); i2c_get_ack(I2C_MAG); i2c_write_byte(I2C_MAG, addr); i2c_wait(I2C_MAG); i2c_get_ack(I2C_MAG); i2c_repeated_start(I2C_MAG); i2c_write_byte(I2C_MAG, MAG3110_I2C_ADDRESS | I2C_READ); i2c_wait(I2C_MAG); i2c_get_ack(I2C_MAG); i2c_set_rx_mode(I2C_MAG); i2c_give_nack(I2C_MAG); result = i2c_read_byte(I2C_MAG); i2c_wait(I2C_MAG); i2c_stop(I2C_MAG); result = i2c_read_byte(I2C_MAG); pause(); return result; }
uint8_t I2C_ReadOneByte(uint8_t SlaveAddr, uint8_t RegAddr) { uint8_t result; i2c_start(I2C0_B); i2c_write_byte(I2C0_B, (SlaveAddr<<1) | I2C_WRITE); i2c_wait(I2C0_B); i2c_get_ack(I2C0_B); i2c_write_byte(I2C0_B, RegAddr); i2c_wait(I2C0_B); i2c_get_ack(I2C0_B); i2c_repeated_start(I2C0_B); i2c_write_byte(I2C0_B, (SlaveAddr<<1) | I2C_READ); i2c_wait(I2C0_B); i2c_get_ack(I2C0_B); i2c_set_rx_mode(I2C0_B); i2c_give_nack(I2C0_B); result = i2c_read_byte(I2C0_B); i2c_wait(I2C0_B); i2c_stop(I2C0_B); result = i2c_read_byte(I2C0_B); pause(40); return result; }
uint8 hal_dev_mma8451_read_reg(uint8 addr) { uint8 result; int i; i2c_start(I2C0_B); i2c_write_byte(I2C0_B, MMA8451_I2C_ADDRESS | I2C_WRITE); i2c_wait(I2C0_B); i2c_get_ack(I2C0_B); i2c_write_byte(I2C0_B, addr); i2c_wait(I2C0_B); i2c_get_ack(I2C0_B); i2c_repeated_start(I2C0_B); for(i=0;i<1000;i++) for( i=0;i<1000;i++); i2c_write_byte(I2C0_B, MMA8451_I2C_ADDRESS | I2C_READ); i2c_wait(I2C0_B); i2c_get_ack(I2C0_B); i2c_set_rx_mode(I2C0_B); i2c_give_nack(I2C0_B); result = i2c_read_byte(I2C0_B); i2c_wait(I2C0_B); i2c_stop(I2C0_B); result = i2c_read_byte(I2C0_B); pause(); for(i=0;i<1000;i++) for( i=0;i<1000;i++); return result; }
void i2c_stop(I2C_MemMapPtr p) { i2c_set_slave_mode(p); i2c_set_rx_mode(p); }