CS 4101 Introduction to Embedded Systems Lab 7
CS 4101 Introduction to Embedded Systems Lab 7: Serial Communication Prof. Chung-Ta King Department of Computer Science National Tsing Hua University, Taiwan National Tsing Hua University
Introduction • In this lab, we will learn - Communication peripherals of MSP 430 Launch. Pad - Implementation of a software UART on MSP 430 Launch. Pad - Communication between MSP 430 Launch. Pad and PC through RS 232 interface National Tsing Hua University 1
Recall Pins for Comm P 1. 1 TXD P 1. 2 RXD National Tsing Hua University 2
Pin Connections TXD RXD #define TXD 0 x 02 // TXD #define RXD 0 x 04 // RXD P 1 SEL |= TXD + RXD; // P 1 DIR = 0 x. FF & ~RXD; // P 1 OUT = 0 x 00; // National Tsing Hua University Use TACCR 0 Use TACCR 1 on P 1. 1 (Timer 0_A. OUT 0) on P 1. 2 (Timer 0_A. CCI 1 A) Enable TXD/RXD pins Set pins to output Initialize all GPIO 3
For Transmission For Receive RXD pin National Tsing Hua University TXD pin Latch allows sampling at precise time, regardless of ISR latency 4
Receive of Software UART by Timer_A • Between transmissions, CCR 1 waits in Capture mode for a falling edge on its input. • When a falling edge is detected, TACCR 1 captures the count in TAR and raises an interrupt. CCR 1 is switched to Compare mode and TACCR 1 is set to fire an interrupt after 1. 5 of the bit period from now. • The next interrupt occurs and SCCI contains the value of LSB. ISR saves it. Next compare event is set up to occur after a further bit period. • The above procedure is repeated until all 8 bits of data have been received. National Tsing Hua University 5
Transmission of Software UART Use Capture/Compare Block 0 (TACCR 0) • OUTMOD_0: OUT 0 signal is defined by OUT bit • OUTMOD_2: OUT 0 signal is reset when the timer counts to TACCR 0 Use OUTMOD_0 to send a 1, OUTMOD_2 for 0 OUT 0 National Tsing Hua University 6
TA 0 CCTLx National Tsing Hua University 7
TA 0 CCTLx (cont’d) National Tsing Hua University 8
Sample Code (msp 430 g 2 xx 3_ta_uart 9600) • Software UART, using Timer 0_A, 9600 baud, echo, full duplex, SMCLK at 1 MHz - Main loop readies UART to receive one character and waits in LPM 3 with all activity interrupt driven. - TA 0 CCR 0 and TA 0 CCR 1 may interrupt at any time and in an interleaved way - TA 0 R keeps an independent time reference, while CCR 0 and CCR 1 handle time intervals National Tsing Hua University 9
Sample Code (msp 430 g 2 xx 3_ta_uart 9600) #include "msp 430. h“ #define UART_TXD 0 x 02 // TXD on P 1. 1 (Timer 0_A. OUT 0) #define UART_RXD 0 x 04 // RXD on P 1. 2 (Timer 0_A. CCI 1 A) #define UART_TBIT_DIV_2 (1000000 / (9600 * 2)) #define UART_TBIT (1000000 / 9600) unsigned int tx. Data; // UART internal TX variable unsigned char rx. Buffer; // Received UART character void Timer. A_UART_init(void); void Timer. A_UART_tx(unsigned char byte); void Timer. A_UART_print(char *string); National Tsing Hua University 10
Sample Code (msp 430 g 2 xx 3_ta_uart 9600) void main(void) { WDTCTL = WDTPW + WDTHOLD; // Stop watchdog timer DCOCTL = 0 x 00; // Set DCOCLK to 1 MHz BCSCTL 1 = CALBC 1_1 MHZ; DCOCTL = CALDCO_1 MHZ; P 1 OUT = 0 x 00; // Initialize all GPIO P 1 SEL = UART_TXD + UART_RXD; // Use TXD/RXD pins P 1 DIR = 0 x. FF & ~UART_RXD; // Set pins to output __enable_interrupt(); National Tsing Hua University 11
Sample Code (msp 430 g 2 xx 3_ta_uart 9600) Timer. A_UART_init(); // Start Timer_A UART Timer. A_UART_print("G 2 xx 3 Timer. A UARTrn"); Timer. A_UART_print("READY. rn"); for (; ; ) { // Wait for incoming character __bis_SR_register(LPM 0_bits); // Echo received character Waken up by Timer. A_UART_tx(rx. Buffer); Timer_A 1_ISR } } void Timer. A_UART_print(char *string) { while (*string) Timer. A_UART_tx(*string++); } National Tsing Hua University 12
Sample Code (msp 430 g 2 xx 3_ta_uart 9600) void Timer. A_UART_init(void) { TA 0 CCTL 0 = OUT; // Set TXD idle as '1' TA 0 CCTL 1 = SCS + CM 1 + CAP + CCIE; // CCIS 1 = 0 // Set RXD: sync, neg edge, capture, interrupt TA 0 CTL = TASSEL_2 + MC_2; // SMCLK, continuous mode } void Timer. A_UART_tx(unsigned char byte) { while (TACCTL 0 & CCIE); // Ensure last char TX'd TA 0 CCR 0 = TAR; // Current state of TA counter TA 0 CCR 0 += UART_TBIT; // One bit time till 1 st bit TA 0 CCTL 0 = OUTMOD 0 + CCIE; // Set TXD on EQU 0, Int tx. Data = byte; // Load global variable tx. Data |= 0 x 100; // Add stop bit to TXData tx. Data <<= 1; // Add start bit } National Tsing Hua University 13
Sample Code (msp 430 g 2 xx 3_ta_uart 9600) #pragma vector = TIMER 0_A 0_VECTOR __interrupt void Timer_A 0_ISR(void) { static unsigned char tx. Bit. Cnt = 10; TA 0 CCR 0 += UART_TBIT; // Set TACCR 0 for next intrpt if (tx. Bit. Cnt == 0) { // All bits TXed? TA 0 CCTL 0 &= ~CCIE; // Yes, disable intrpt tx. Bit. Cnt = 10; // Re-load bit counter } else { if (tx. Data & 0 x 01) {// Check next bit to TX TA 0 CCTL 0 &= ~OUTMOD 2; // TX '1’ using OUTMODE 0 } else { TA 0 CCTL 0 |= OUTMOD 2; } // TX '0‘ tx. Data >>= 1; tx. Bit. Cnt--; } } National Tsing Hua University 14
Sample Code (msp 430 g 2 xx 3_ta_uart 9600) #pragma vector = TIMER 0_A 1_VECTOR __interrupt void Timer_A 1_ISR(void) { static unsigned char rx. Bit. Cnt = 8; static unsigned char rx. Data = 0; switch (__even_in_range(TA 0 IV, TA 0 IV_TAIFG)) { case TA 0 IV_TACCR 1: // TACCR 1 CCIFG - UART RX TA 0 CCR 1 += UART_TBIT; // Set TACCR 1 for next int if (TA 0 CCTL 1 & CAP) { // On start bit edge TA 0 CCTL 1 &= ~CAP; // Switch to compare mode TA 0 CCR 1 += UART_TBIT_DIV_2; // To middle of D 0 } else { // Get next data bit rx. Data >>= 1; National Tsing Hua University 15
Sample Code (msp 430 g 2 xx 3_ta_uart 9600) if (TA 0 CCTL 1 & SCCI) { // Get bit from latch rx. Data |= 0 x 80; } rx. Bit. Cnt--; if (rx. Bit. Cnt == 0) { // All bits RXed? rx. Buffer = rx. Data; // Store in global rx. Bit. Cnt = 8; // Re-load bit counter TACCTL 1 |= CAP; // Switch to capture __bic_SR_register_on_exit(LPM 0_bits); // Clear LPM 0 bits from 0(SR) } } break; Wake up main loop } } National Tsing Hua University 16
Interrupt Source Interrupt Flag Power-up/external reset/Watchdog Timer+/flash key viol. /PC out-of-range NMI/Oscillator Fault/ Flash access viol. Timer 1_A 3 Comparator_A+ Watchdog Timer+ Timer 0_A 3 PORIFG RSTIFG WDTIFG KEYV NMIIFG/OFIFG/ ACCVIFG TA 1 CCR 0 CCFIG TA 1 CCR 1/2 CCFIG, TAIFG CAIFG WDTIFG TA 0 CCR 0 CCIFG Timer 0_A 3 TA 0 CCR 1/2 CCIFG, TAIFG System Interrupt Word Address Priority Reset 0 FFFEh 31 (highest) Non-maskable 0 FFFCh 30 maskable maskable 0 FFFAh 0 FFF 8 h 0 FFF 6 h 0 FFF 4 h 0 FFF 2 h 29 28 27 26 25 maskable 0 FFF 0 h 24 0 FFEEh 0 FFECh 0 FFEAh same 23 22 21 0 FFE 8 h 20 0 FFE 6 h 19 0 FFE 4 h 18 0 FFE 2 h 0 FFE 0 h 0 FFDEh 0 FFCDh 17 16 ADC 10 IFG maskable Three sources of interrupts cause the interrupt vector. Which one(s) cause the I/O Port P 2 (8) to P 2 IFG. 7 maskable interrupt? P 2 IFG. 0 check TAIV register ADC 10 I/O Port P 1 (8) Unused P 1 IFG. 0 to P 1 IFG. 7 National Tsing Hua University maskable 1517 - 0
TAIV (Timer_A Interrupt Vector) • On an interrupt, TAIV contains a number indicating the highest priority enabled interrupt: TACCR 1_CCIFG, TACCR 2_CCIFG, TAIFG - Any access of TAIV resets the highest pending interrupt flag. If another interrupt flag is set, another interrupt is immediately generated National Tsing Hua University 18
Setting PC for Serial Communication • In the Debug perspective of CCS IDE, click [View] -> [Other…] and, in the Show View window, click the + next to Terminal. • Select Terminal below that and click OK. National Tsing Hua University 19
Setting PC for Serial Communication • A Terminal pane will appear on the screen. Click the Settings button in the Terminal pane and make the selections (set the serial communication setting) National Tsing Hua University 20
Basic 1: Temperature Sensing System • Basic 1 a: - Read temperature from ADC every second. - If the temperature is higher than 737, then flash the red LED at 5 Hz and send a string “Alarm!” to PC every second. - Otherwise flash the green LED at 1 Hz and send a string “Normal” to PC every second. - Use Timer 0_A alternatively for timing 1 sec and UART • Basic 1 b: - Whenever PC receives a string “Alarm!”, it responds with the string “Ack!”. - Observe how program behaves and explain the reasons. National Tsing Hua University 21
Basic 2 • Read temperature from ADC every second • If the temperature is higher than 737, then - Flash the red LED at 5 Hz - Send ONE string “Alarm!” to PC using format 7 -n-2 (7 -bit data, no parity bit, 2 stop bits) - When PC receives “Alarm!”, it responds with “Ack!” • If the temperature is lower than 737, then - Flash the green LED at 1 Hz - Send ONE string “Normal!” to PC using format 7 -n-2 (7 -bit data, no parity bit, 2 stop bits) - When PC receives “Normal!”, it responds with “Ack!” National Tsing Hua University 22
Bonus • Modify the sample code so that Basic 1 b can work properly, if it is not. • Modify Basic 2 to use the format of 7 -E-2 (7 -bit data, even parity bit, 2 stop bits). National Tsing Hua University 23
Bonus • Modify the basic lab so that when MSP 430 receives the character ” 0”, it stops temperature sensing. • While MSP 430 is not sensing temperature, if it receives the character “R”, it turns on the red LED; if it receives the character “G”, turns on the green LED; and if it receives other characters, no LED is on. • When MSP 430 receives the character ” 1”, it goes back to basic lab. • Use 4800 baud, 8 -bit of data, and 2 stop bits. National Tsing Hua University 24
- Slides: 25