static unsigned char read_reg(unsigned char addr) { unsigned char r; i2c_start_cond(); i2c_write(0x40); i2c_write(addr); i2c_start_cond(); i2c_write(0x41); r = i2c_read(0); i2c_stop_cond(); return r; }
static void write_reg(unsigned char addr, unsigned char val) { i2c_start_cond(); i2c_write(0x40); i2c_write(addr); i2c_write(val); i2c_stop_cond(); }
void set_dac_level(int level) { if(!i2c_init()) { printf("I2C init failed\n"); return; } i2c_start_cond(); if(!i2c_write(0x90)) printf("DAC not detected\n"); i2c_write(0x2f); i2c_write((level & 0xff0) >> 4); i2c_write((level & 0x00f) << 4); i2c_stop_cond(); }
// Write a byte to I2C bus. Return 0 if ack by the slave. bool i2c_write_byte(bool send_start, bool send_stop, unsigned char byte) { unsigned bit; bool nack; if (send_start) { i2c_start_cond(); } for (bit = 0; bit < 8; bit++) { i2c_write_bit((byte & 0x80) != 0); byte <<= 1; } nack = i2c_read_bit(); if (send_stop) { i2c_stop_cond(); } return nack; }