uint8_t gyro_read_reg(uint8_t addr) { uint8_t result2; __disable_irq(); i2c1_start(); i2c1_write_byte(GYRO_I2C2_ADDRESS | I2C1_WRITE); i2c1_wait(); i2c1_get_ack(); i2c1_write_byte(addr); i2c1_wait(); i2c1_get_ack(); i2c1_repeated_start(); i2c1_write_byte(GYRO_I2C2_ADDRESS | I2C1_READ); i2c1_wait(); i2c1_get_ack(); i2c1_set_rx_mode(); i2c1_give_nack(); result2 = i2c1_read_byte(); i2c1_wait(); i2c1_get_ack(); i2c1_stop(); result2 = i2c1_read_byte(); pause(); __enable_irq(); return result2; }
int main(void) { int delay; int ack, temperature; i2c1_init(TC74_CLK_FREQ); while(1) { //start event i2c1_start(); //send write addr ack = i2c1_send(ADDR_WR); //command (read temp) i2c1_send(RTR); //start event i2c1_start(); //send read addr ack += i2c1_send(ADDR_RD); //printInt10(ack); if (ack != 0) { i2c1_stop(); printStr("An error has ocurred, exiting.\n"); } else { printStr("\nReading temperature: "); temperature = i2c1_receive(I2C_NACK); i2c1_stop(); printInt10(temperature); } //delay 250ms delay = readCoreTimer(); while (readCoreTimer() - delay < 5000000); } return 0; }
int getTemperature(int *temperature){ int ack; i2c1_init(TC74_CLK_FREQ); // Send Start event i2c1_start(); // Send Address + WR (ADDR_WR) and copy return value to "ack" variable ...(see exercise 7, function main())... ack = i2c1_send(ADDR_WR); ack += i2c1_send(RTR); i2c1_start(); ack += i2c1_send(ADDR_RD); *temperature = i2c1_receive(I2C_NACK); // Send Stop event i2c1_stop(); return ack; }
void gyro_write_reg(uint8_t addr, uint8_t data) { __disable_irq(); i2c1_start(); i2c1_write_byte( GYRO_I2C2_ADDRESS|I2C1_WRITE); i2c1_wait(); i2c1_get_ack(); i2c1_write_byte(addr); i2c1_wait(); i2c1_get_ack(); i2c1_write_byte(data); i2c1_wait(); i2c1_get_ack(); i2c1_stop(); __enable_irq(); pause(); }