MSP430 Teaching Materials: Communications
MSP430 Teaching Materials: Communications
UBI
Chapter 14
Communications
USI Module
Pedro Dinis Gaspar, António Espírito Santo, Bruno Ribeiro, Humberto Santos
University of Beira Interior, Electromechanical Engineering Department
www.msp430.ubi.pt
www.msp430.ubi.pt
Contents
UBI
Quiz
I2C:
I (on ‘15x/’16x only)
2C:
I2C:
- Simplified interrupt usage
- Master and Slave Modes - SW state machine needed
- Master and Slave Modes
- up to 400kbps - Master and Slave Modes
- up to 400kbps
Copyright 2009 Texas Instruments
>> Contents All Rights Reserved
4
www.msp430.ubi.pt
USI module introduction (1/2)
UBI
I2C mode:
• START/STOP detection;
• Arbitration lost detection.
Interrupt driven;
USI initialization:
Reset USISWRST;
Set USIPEx bits (USI function for the pin and maintains the
PxIN and PxIFG functions for the pin):
• Port input levels can be read via the PxIN register by
software;
SPI interrupts:
One interrupt vector associated with the USI module;
One interrupt flag, USIIFG:
• Set when bit counter counts to zero;
• Generates an interrupt request when USIIE = 1;
• Cleared when USICNTx > 0 (USIIFGCC = 0), or directly
by software;
• Stops clock when set.
I2C master:
USIMST = 1 and USII2C = 1;
Select clock source (output to SCL line while USIIFG = 0).
I2C slave:
USIMST = 0;
SCL is held low if USIIFG=1, USISTTIFG=1 or if
USICNTx=0.
I2C transmitter:
Data value is first loaded into USISRL;
USIOE= 1: Enable output and start transmission (writes 8 into
USICNTx);
Send Start (or repeated Start);
Define address and set R/W;
Slave ACK: (Data TX/RX + ACK for N bytes);
SCL is generated in master mode or released from being held
low in slave mode;
USIIFG is set after the transmission of all 8 bits (stops clock
signal on SCL in master mode or held low at the next low
phase in slave mode);
Stop (or repeated Start).
Copyright 2009 Texas Instruments
>> Contents All Rights Reserved
16
www.msp430.ubi.pt
USI operation: I2C mode (4/10)
UBI
I2C receiver:
Clear USIOE (disable output);
SDA configuration:
Direction;
Used for TX/RX, ACK/NACK handling and START/STOP
generation;
USIGE: Output latch control;
USIOE: Data output enable.
START condition:
(high-to-low transition on SDA while SCL is high);
Clear MSB of the shift register;
USISTTIFG set on start (Sources USI interrupt).
STOP condition:
(low-to-high transition on SDA while SCL is high):
Clear the MSB in the shift register and loads 1 into USICNTx
(finishes the acknowledgment bit and pulls SDA low);
USISTP set on stop (CPU-accessible flag).
I2C Interrupts:
One interrupt vector associated with the USI;
Each interrupt flag has its own interrupt enable bit, USIIE
and USISTTIE;
Example:
Procedure for I2C communication between a Master TX and a
Slave RX.
Master TX Slave RX
1: Send Start, Address and R/W bit 1: Detect Start, receive address and
R/W
2: Receive (N)ACK 2: Transmit (N)ACK
3: Test (N)ACK and handle TX data 3: Data RX
4: Receive (N)ACK 4: Transmit (N)ACK
5: Test (N)ACK and prepare Stop 5: Reset for next Start
6: Send Stop
Bit Description
7 USIPE7 USI SDI/SDA port enable:
SPI mode Input
I2C mode Input or open drain output
USIPE7 = 0 USI function disabled
USIPE7 = 1 USI function enabled
6 USIPE6 USI SDO/SCL port enable:
SPI mode Output
I2C mode Input or open drain output
USIPE6 = 0 USI function disabled
USIPE6 = 1 USI function enabled
5 USIPE5 USI SCLK port enable:
SPI slave mode Input
SPI master mode Output
I2C mode Input
USIPE5 = 0 USI function disabled
USIPE5 = 1 USI function enabled
Copyright 2009 Texas Instruments
>> Contents All Rights Reserved
24
www.msp430.ubi.pt
USI registers (SPI and I2C modes) (2/8)
UBI
4 USILSB LSB first select (direction of the receive and transmit shift
register):
USILSB = 0 MSB first
USILSB = 1 LSB first
3 USIMST Master select:
USIMST = 0 Slave mode
USIMST = 1 Master mode
2 USIGE Output latch control:
USIGE = 0 Output latch enable depends on shift clock
USIGE = 1 Output latch always enabled and transparent
1 USIOE Data output enable:
USIOE = 0 Output disabled
USIOE = 1 Output enabled
0 USIWRST USI software reset:
USIWRST = 0 USI released for operation
USIWRST = 1 USI logic held in reset state
Copyright 2009 Texas Instruments
>> Contents All Rights Reserved
25
www.msp430.ubi.pt
USI registers (SPI and I2C modes) (3/8)
UBI
Bit Description
7 USICKPH Clock phase select:
USICKPH = 0 Data is changed on the first SCLK edge and
captured on the following edge
USICKPH = 1 Data is captured on the first SCLK edge and
changed on the following edge
6 USII2C I2C mode enable:
USII2C = 0 I2C mode disabled
USII2C = 1 I2C mode enabled
5 USISTTIE START condition interrupt-enable:
USISTTIE = 0 Interrupt on START condition disabled
USISTTIE = 1 Interrupt on START condition enabled
4 USIIE USI counter interrupt enable:
USIIE = 0 Interrupt disabled
USIIE = 1 Interrupt enabled
Copyright 2009 Texas Instruments
>> Contents All Rights Reserved
26
www.msp430.ubi.pt
USI registers (SPI and I2C modes) (4/8)
UBI
Bit Description
7 USISCLREL SCL line release from low to idle:
USISCLREL = 0 SCL line is held low if USIIFG is set
USISCLREL = 1 SCL line is released
6 USI16B 16-bit shift register enable:
USI16B = 0 8-bit shift register mode. (Uses USISRL low byte)
USI16B = 1 16-bit shift register mode (Uses both USISRx bytes)
5 USIIFGCC USI interrupt flag clear control:
USIIFGCC = 0 USIIFG automatically cleared on USICNTx update
USIIFGCC = 1 USIIFG is not cleared automatically
4-0 USICNTx USI bit count (Number of bits to be received or transmitted)
Bit Description
7-0 USISRLx Contents of the USI low byte shift register
Bit Description
7-0 USISRHx Contents of the USI high byte shift register
Project files:
C source files: Chapter 14 > Lab10 > Lab10b1_student.
Chapter 14 > Lab10 > Lab10b2_student.c
Solution files: Chapter 14 > Lab10 > Lab10b1_solution.c
Chapter 14 > Lab10 > Lab10b2_solution.c
Overview:
This laboratory explores the USCI and USI communication
interfaces in SPI mode;
Overview (continued):
MSP430FG4618: Master reads the current state of the slave,
and drives it to the new desired state;
MSP430F2013: Slave commanded by the Master.
A. Resources:
USCI module: MSP430FG4618;
USI module: MSP430F2013;
Both units operate in SPI mode;
Basic Timer1 of the master device is programmed to switch
the status of the slave device once every 2 seconds;
The slave is notified of the arrival of information through the
end of counting interrupt of the USI module.
A. Resources (continued):
• USCI module;
• USI module;
• Basic Timer1;
• Interrupts;
• I/O ports.
Copyright 2009 Texas Instruments
>> Contents All Rights Reserved
34
www.msp430.ubi.pt
Lab10b: Echo test using SPI mode (4/17)
UBI
MASTER SLAVE
SOMI
USCI USI USI
SIMO
RX SPI SPI ISR
SCLK
Main TX
RX
Master Task
TX
C. Configuration:
C. Configuration (continued):
UCB0CTL0 = _______________;
UCB0CTL1 = _______________;
C. Configuration (continued):
UCB0BR0= _______________;
UCB0BR1= _______________;
Copyright 2009 Texas Instruments
>> Contents All Rights Reserved
39
www.msp430.ubi.pt
Lab10b: Echo test using SPI mode (9/17)
UBI
C. Configuration (continued):
P3SEL = __________________;
C. Configuration (continued):
C. Configuration (continued):
USICTL0 = _______________;
USICTL1 = _______________;
D. Analysis of operation:
D. Analysis of operation:
Verification:
• Once the program code is running in the two
microcontrollers, monitor LED3 of the Experimenter’s
board. It will blink at a rate of 4 flashes per second.
MSP-EXP430FG4618 SOLUTION
(master)
Using USCI module in SPI mode included in the FG4618 (configured as master) of the
Experimenter’s board, establish a connection to the F2013 by its USI module in SPI
mode. The data exchanged is displayed by the LED blinking.
MSP-EXP430F2013 SOLUTION
(slave)
Using the USCI module in SPI mode included in the FG4618 (configured as
master) of the Experimenter’s board, establish a connection to the F2013 by
its USI module in SPI mode. The data exchanged is displayed by the LED
blinking.
USICTL1 = 0x10;
//USICTL1= USICKPH|USII2C|USISTTIE|USIIE|USIAL|USISTP|USISTTIFG|USIIFG
//USICKPH (Clock phase select) = 0b -> Data is changed on the first
// SCLK edge and captured on the following edge
//USII2C (I2C mode enable) = 0b -> I2C mode disabled
//USISTTIE (START condition interrupt) = 0b -> Not used
//USIIE (USI counter) = 1b -> Interrupt enabled
//USIAL (Arbitration lost) = 0b -> Not used
//USISTP (STOP condition received) = 0b -> Not used
//USISTTIFG (START condition int. flag) = 0b -> Not used
//USIIFG (USI counter int. flag) = 0b -> No int. pending
Project files:
C source files: Chapter 14 > Lab10 > Lab10c1_student.c
Chapter 14 > Lab10 > Lab10c2_student.c
Solution files: Chapter 14 > Lab10 > Lab10c1_solution.c
Chapter 14 > Lab10 > Lab10c2_solution.c
Overview:
This laboratory explores the USCI and USI communication
interfaces in I2C mode;
It uses the two MSP430 devices included on the
Experimenter’s board: MSP430FG4618 as the master and
the MSP430F2013 as slave;
The master receives a single byte from the slave as soon as
a button connected to P1.0 is pressed.
Copyright 2009 Texas Instruments
>> Contents All Rights Reserved
49
www.msp430.ubi.pt
Lab10c: Echo test using I2C mode (2/21)
UBI
A. Resources:
This laboratory uses the USCI module of the MSP430FG4618
device and the USI module included in the MSP430F2013.
Both units operate in I2C mode;
A. Resources:
• USCI module;
• USI module;
• Interrupts;
• I/O ports.
Software architecture:
MASTER SLAVE
RX Buffer Slave Data
SDA
USCI USCI USI USI
RX SCL
ISR I2C I2C ISR
RX
P1 TX TX
ISR
P1.0 S1 LED3
Status
RX
4 Adress
7
Process
Adress 3
1- START condition TX
2- Adress received
5
3- Read command
4 - Adress not match RX
(N)ACK
5 – Data sent
6 – (N)ACK reveived 6
7 – END
Process
ACK
C. Configuration:
C. Configuration (continued):
UCB0BR0= _______________;
UCB0BR1= _______________;
Copyright 2009 Texas Instruments
>> Contents All Rights Reserved
56
www.msp430.ubi.pt
Lab10c: Echo test using I2C mode (9/21)
UBI
C. Configuration (continued):
P3SEL = __________________;
C. Configuration (continued):
C. Configuration (continued):
USICTL0 |=________________;
USISRL =_________________;
USICNT |=________________;
D. Analysis of operation:
Once the USCI module is configured in accordance with the
previous steps, to initiate the experiment, complete the files:
• Lab10c1_student.c (master – MSP430FG4618)
• Lab10c2_student.c (slave – MSP430F2013)
Compile them and run them on the Experimenter’s board;
D. Analysis of operation:
Verification:
• The slave data values are sent and incremented from
0x00 with each transmitted byte, and are verified by the
Master;
D. Analysis of operation:
Verification:
MSP-EXP430FG4618 SOLUTION
(master)
Using USCI module in I2C mode included in the FG4618 (configured as
master) of the Experimenter’s board, establish a connection to the F2013 by
its USI module in I2C mode. The master receives a single byte from the slave
as soon as a button connected on P1.0 is pressed.
UCB0CTL1 = 0x81;
//UCB0CTL1 =
//UCSSELx|Unused|UCTR|UCTXNACK|UCTXSTP|UCTXSTT|UCSWRST|
//UCSSELx (USCI clock source select) = 10b -> SMCLK
//Unused
//UCTR (Transmitter/Receiver) = 0b -> Receiver
//UCTXNACK (Transmit a NACK) = 0b -> Ack normally
//UCTXSTP (Transmit STOP condition) = 0b -> No STOP
//UCTXSTT (Transmit START condition) = 0b -> No START
//UCSWRST (Software reset) = 1b -> Enabled
Data rate:
// DATA RATE
// data rate -> fSCL = SMCLK/11 = 95.3 kHz
UCB0BR0 = 0x0B; // fSCL = SMCLK/11 = 95.3 kHz
UCB0BR1 = 0x00;
Configure ports:
P3SEL |=0x06; // Assign I2C pins to USCI_B0
MSP-EXP430F2013 SOLUTION
(slave)
Using USCI module in I2C mode included in the FG4618 (configured as
master) of the Experimenter’s board, establish a connection to the F2013 by
its USI module in I2C mode. The master receives a single byte from the slave
as soon as a button connected on P1.0 is pressed.
//USICTL1 =
//|USICKPH|USII2C|USISTTIE|USIIE|USIAL|USISTP|USISTTIFG|USIIFG|
//USICKPH (Clock phase select) = 0b -> Data is changed
// on the first SCLK edge and captured on the following edge.
//USII2C (I2C mode enable) = 1b -> I2C mode enabled
//USISTTIE = 1b -> Interrupt on START condition enabled
//USIIE = 1b -> USI counter interrupt enable
//USIAL (Arbitration lost) = 0b -> Not used
//USISTP (STOP condition received) = 0b -> Not used
//USISTTIFG (START condition int. flag) = 0b -> Not used
//USIIFG (USI counter int. flag) = 0b -> No int. pending
USICNT |= 0x20;
//USICNT =
//USISCLREL| USI16B |USIIFGCC |USICNTx|
Answers: