304
void I2C_USCI_init(unsigned char address)
{
P1DIR &= ~(BIT6 + BIT7);
P1OUT |= (BIT6 + BIT7);
P1SEL2 |= (BIT6 | BIT7);
P1SEL |= (BIT6 | BIT7);
UCB0CTL1 |= UCSWRST;
UCB0CTL0 = (UCMST | UCMODE_3 | UCSYNC);
UCB0CTL1 = (UCSSEL_2 | UCSWRST);
UCB0BR0 = 20;
UCB0I2CSA = address;
UCB0CTL1 &= ~UCSWRST;
}
Off all the functions used in the HW_I2C files, the following are of high importance:
unsigned char I2C_USCI_read_byte(unsigned char address);
unsigned char I2C_USCI_read_word(unsigned char address,unsigned char *value,
unsigned char length);
unsigned char I2C_USCI_write_byte(unsigned char address, unsigned char value);
Their names suggest their functionality. The codes inside them are arranged as such that they take
care of start-stop conditions generation, clock generation, etc. I2C communication needs device
address along side read-write info. Based on read-write info, the host device writes or read I2C bus.
The drawback of using I2C with these functions is the vulnerability to falling inside a loop since loops
are used in these functions widely. This setback can be overcome with timeouts.
Demo
Demo video: https://youtu.be/RkTjCcCLEDg.