O'Reilly logo

Microcontroller Programming and Interfacing Texas Instruments MSP430 by Daniel J. Pack, Steven F. Barrett

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

296 9. COMMUNICATION SYSTEMS
{
WDTCTL = WDTPW+WDTHOLD; //Stop watchdog timer
while(!(P3IN&0x01)); //If clock sig from master
//stays low, it is not yet in
//SPI mode
P3SEL |= 0x31; //P3.5,4,0 option select
UCA0CTL1 |= UCSWRST; //Put state machine in reset
UCA0CTL0 |= UCSYNC+UCCKPL+UCMSB; //3-pin, 8-bit SPI slave,
//Clock polarity high, MSB
UCA0CTL1 &= ˜UCSWRST; //Init USCI state machine
UCA0IE |= UCRXIE; //Enable USCI_A0 RX interrupt
__bis_SR_register(LPM4_bits + GIE);
//Enter LPM4, enable interrupts
}
// Echo character
#pragma vector=USCI_A0_VECTOR
__interrupt void USCI_A0_ISR(void)
{
switch(__even_in_range(UCA0IV,4))
{
case 0:break; //Vector0-nointerrupt
case 2: //Vector 2 - RXIFG
while (!(UCA0IFG&UCTXIFG)); //USCI_A0 TX buffer ready?
UCA0TXBUF = UCA0RXBUF;
break;
case 4:break; //Vector 4 - TXIFG
default: break;
}
}
//***********************************************************************
9.5 INTER-INTEGRATED COMMUNICATION - I
2
C
MODULE
The I
2
C is one of prominent communication modes used when multiple serial devices are inter-
connected using a serial bus. The I
2
C bus is a two-wire bus with the serial data line (SDL) and the
9.5. INTER-INTEGRATED COMMUNICATION - I
2
C MODULE 297
serial clock line (SCL). By configuring devices connected to the common I
2
C line as either a master
device or a slave device, multiple devices can share information using the common bus.
The difference between a master device and a slave device is that a master device initiates
a communication by means of either requesting data from another device or sending data to a
designated device. A master device must also provide a clock signal (SCL).
The MSP430’s universal serial communication interface (USCI) can be programmed to op-
erate in the I
2
C communication mode. As seen earlier in this chapter, the USCI_Ax ports are
programmed to operate in the UART, IrDA, and SPI communication mode while the USCI_Bx
ports are used for the I
2
C and SPI serial communication modes.
I
2
C communication is initiated by sending an address of a desired destination device connected
toacommonI
2
C bus.The device address can be either a 7-bit number or a 10-bit number, depending
on the number of devices connected to the bus. Of course, all devices on the same bus must use the
same addressing mode and program accordingly.
One of the reasons the I
2
C serial communication became popular is its flexibility to allow
multiple master devices to co-exist on a same bus. The MSP430 USCI device allows its I
2
Ccom-
munication unit to operate either in the standard mode (100 kbps) or in the fast mode (400 kbps).
We now show how a USCI_Bx port is programmed to operate on an I
2
C bus. To initialize
a USCI_Bx port as an I
2
C communication port, you must: (1) set the UCSWRST bit in the
UCxCTL1 register to one, (2) configure the I
2
C mode of operation by setting UCMODEx bits to
11 and initialize the USCI registers, and (3) set up an actual port with a pull up resistor. As soon as
the UCSWRST bit is cleared, the I
2
C communication of MSP430 can commence.
ELWDGGUHVVLQJPRGH
FORFNV
'DWDRQ6'$OLQH6$5:$FN'$FN'$FN3
ELWDGGUHVVLQJPRGH
FORFNV
'DWDRQ6'$OLQH6$5:$FN$$FN'$FN'$FN3
66WDUWFRQGLWLRQ
$6ODYHDGGUHVVELWDGGUHVVLQJPRGH
$06%VODYHDGGUHVV[[ELWDGGUHVVLQJPRGH
$/6%VODYHDGGUHVV
5:5HDGRU:ULWH
''DWD
$FN$FNQRZOHGJHPHQW
36WRSFRQGLWLRQ
Figure 9.7: Data format for both 7-bit and 10-bit addressing modes.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required