{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Lecture13_I2C_handout - I2C_lecture Coding Example Repeat...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
I2C_lecture October 10, 2007 1 19 October 10, 2007 Lecture #13 - Blimp Coding Example void i2c_write_data(unsigned char dev_addr, unsigned char start_reg, unsigned char *buffer, unsigned char num_bytes) { unsigned char j; i2c_start(); //start i2c i2c_write(dev_addr&~0x01); //identify target and send 0 in R/W bit i2c_write(start_reg); //Specify device register to be written for(j=0; j<num_bytes-1; j++) //write all but last data byte i2c_write(buffer[j]); i2c_write_and_stop(buffer[num_bytes-1]); //write last data byte and stop } Repeat data byte for num_bytes-1 times Followed by last write with a stop bit. 20 October 10, 2007 Lecture #13 - Blimp Coding Example – you write this one, work sheet #8 void i2c_read_data(unsigned char addr, unsigned char start_reg,unsigned char *buffer, unsigned char num_bytes) { Start I2C transfer Write the address of the device to read from Stop and write the first register to be read Start I2C transfer Write address again, this time indicating a read operation For (0 to num_bytes – 2) Set acknowledge bit Read data, save it in the buffer Clear acknowledge bit Read the last byte and stop, save it in the buffer }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon