先将串口调试好,便于下一步工作的展开。首先,查看TI提供的代码例子,这个例子就在TI的MSPWare代码包内。注释如下:
//******************************************************************************
// MSP432P401 Demo - eUSCI_A0 UART echo at 9600 baud using BRCLK = 12MHz
//
// Description: This demo echoes back characters received via a PC serialport.
// SMCLK/DCO is used as a clock source and the device is put in LPM3
// Theauto-clock enable feature is used by the eUSCI and SMCLK is turned off
// when theUART is idle and turned on when a receive edge is detected.
// Note thatlevel shifter hardware is needed to shift between RS232 and MSP
// voltagelevels.
//
// Theexample code shows proper initialization of registers
// andinterrupts to receive and transmit data.
// To testcode in LPM3, disconnect the debugger.
//
//
//
// MSP432P401
// -----------------
// /|\| |
// || |
// --|RST |
// | |
// | |
// | P1.3/UCA0TXD|----> PC(echo)
// | P1.2/UCA0RXD|<---- PC
// | |
//
// Wei Zhao
// TexasInstruments Inc.
// October2015 (updated) | June 2014 (created)
// Builtwith Code Composer Studio V6.0
//******************************************************************************
从以上信息我们可以看出,此段例程所要做的事情就是将板载串口配置成9600波特率工作的串口,并原样送回我们发送进去的数据。从示意图中我们可以看出,我们只需要一对收发线即可完成本实验。接下来分析其源代码。
#include "msp.h"
int main(void)
{
WDTCTL =WDTPW | WDTHOLD; // Stopwatchdog timer
CS->KEY= 0x695A; //Unlock CS module for register access
CS->CTL0= 0; // Resettuning parameters
CS->CTL0= CS_CTL0_DCORSEL_3; //Set DCO to 12MHz (nominal, center of 8-16MHz range)
//Select ACLK = REFO, SMCLK = MCLK = DCO
CS->CTL1= CS_CTL1_SELA_2 | CS_CTL1_SELS_3 | CS_CTL1_SELM_3;
CS->KEY= 0; // LockCS module from unintended accesses
//Configure UART pins
P1SEL0 |=BIT2 | BIT3; // set2-UART pin as second function
__enable_interrupt();
NVIC->ISER[0] = 1 << ((EUSCIA0_IRQn) & 31); // EnableeUSCIA0 interrupt in NVIC module
//Configure UART
UCA0CTLW0|= UCSWRST;
UCA0CTLW0|= UCSSEL__SMCLK; // PuteUSCI in reset
// BaudRate calculation
//12000000/(16*9600) = 78.125
//Fractional portion = 0.125
// User'sGuide Table 21-4: UCBRSx = 0x10
// UCBRFx =int ( (78.125-78)*16) = 2
UCA0BR0 =78; //12000000/16/9600
UCA0BR1 =0x00;
UCA0MCTLW= 0x1000 | UCOS16 | 0x0020; //注意这一行的设定
UCA0CTLW0&= ~UCSWRST; //Initialize eUSCI
UCA0IE |=UCRXIE; // EnableUSCI_A0 RX interrupt
__sleep();
__no_operation(); // For debugger
}
// UART interrupt service routine
void EUSCIA0_IRQHandler(void)
{
if(UCA0IFG & UCRXIFG)
{
while(!(UCA0IFG&UCTXIFG));
UCA0TXBUF = UCA0RXBUF;
__no_operation();
}
}
我们首先粗略地研究一下这段测试代码,忽略前半部分关于时钟设置的内容,检查我标记的注意行。UCA0MCTLW0寄存器的设定在芯片TechnicalReference Manual的745页,例程中基本是使用默认串口配置。串口通信波特率已经在前面代码中设定为9600。看明白了上述内容,就掌握了这块开发板串口的基本内容,有精力可以继续探索一下芯片手册,尝试串口的其他功能。重要提示,不要使用盗版ghost win7,这样会导致xds110板载调试器的驱动安装失败。