305
USCI I2C – Interfacing DS1307 Real Time Clock (RTC)
This part shows another example of I2C implementation using MSP430’s USCI hardware. This time the
I2C device that is connected with a MSP430 is the popular DS1307 real time clock (RTC). This is the
last USCI hardware example.
Code Example
HW_I2C.h
#include <msp430.h>
void I2C_USCI_init(unsigned char address);
void I2C_USCI_set_address(unsigned char address);
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);
HW_I2C.c
#include "HW_I2C.h"
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;