热度 1|
/******************************************************************************
本程序是单片机MSP430G2553
P1.2----------------------------UCA0TXD 串口发送端
P1.1----------------------------UCA0RXD 串口接受端
P1.0----------------------------红灯指示
P1.6----------------------------绿灯指示
利用主辅时钟为1MHZ,串口波特率9600,
波特率:1000000/9600=104
UCA0BR0 = 104; // 1MHz 9600
UCA0BR1 = 0; // 1MHz 9600
现象:
功能(1)看到 P1.0---红灯指示;P1.6---绿灯指示 循环闪烁。
同时电脑串口调试助手(设置波特率:9600,无校验,8位数据位,1位停止位)
不断显示,0x00,0x01,0x40,0x41。
功能(2)打开电脑串口调试助手,发送区有数据待发送时,在接受区将看到相应的数据。
******************************************************************************/
#include "msp430g2553.h"
#define uint unsigned int
#define uchar unsigned char
void Delay_us(uint us) // 延时us 前提是f=1MHZ(不建议用该延时,不是很精确)
{
uint i;
for(i=0;i<us;i++)
__delay_cycles(1);
}
void Delay_ms(uint ms) // 延时ms 前提是f=1MHZ
{
uint j;
for(j=0;j<ms;j++)
__delay_cycles(1000);
}
void main(void)
{
uchar a=0;
uchar TX[4]={0x00,0x01,0x40,0x41};
WDTCTL = WDTPW + WDTHOLD; // 关闭看门狗
BCSCTL1 = CALBC1_1MHZ; // 设置时钟
DCOCTL = CALDCO_1MHZ;
P1DIR = 0xFF; // All P1.x outputs
P1OUT = 0; // All P1.x reset
P1SEL |= BIT1 + BIT2 + BIT4; // P1.1 = RXD, P1.2=TXD
P1SEL2 |= BIT1 + BIT2; // P1.4 = SMCLK, others GPIO
P2DIR = 0xFF; // All P2.x outputs
P2OUT = 0; // All P2.x reset
UCA0CTL1 |= UCSSEL_2; // SMCLK
UCA0BR0 = 104; // 1MHz 9600
UCA0BR1 = 0; // 1MHz 9600
UCA0MCTL = UCBRS2 + UCBRS0; // Modulation UCBRSx = 5
UCA0CTL1 &= ~UCSWRST; // **Initialize USCI state machine**
IE2 |= UCA0RXIE; // 串口使能开启
__bis_SR_register( GIE); // 总中断使能
P1DIR |= (BIT0+BIT6);
P1OUT |= (BIT0+BIT6);
while(1)
{
//a=0x41时,红绿灯全亮;a=00时,红绿灯全灭;
//a=0x01时,红灯亮;绿灯灭;a=0x10时,绿灯亮;红灯灭;
UCA0TXBUF=TX[a];
Delay_ms(500);
P1OUT = UCA0TXBUF;
a++;
if(a>3)
a=0;
}
}
// Echo back RXed character, confirm TX buffer is ready first
#pragma vector=USCIAB0RX_VECTOR
__interrupt void USCI0RX_ISR(void) //接受中断
{
//uchar b;
while (!(IFG2&UCA0TXIFG)); // USCI_A0 TX buffer ready?
//判断串口的发送是否正在工作,
//因为要使用发送功能,所以得判断发送是否忙碌,
//只有在不忙碌的时候才可以启用发送。
//这一句就是判断发送是否在工作,如果是在工作,那么一直等待到不工作为止。
UCA0TXBUF = UCA0RXBUF; // TX -> RXed character
}
硬件连接图: