|
//=============================================================//
// //
// document:5438A(UART3.c) //
// statement:MPS430开发板(加外部晶振) //
// writer :haochaoxue //
// time :2013.6.1 //
// //
//=============================================================//
#include "MSP430F5438A.h"
void init_clock(void);
void init_uart3(void);
void main(void)
{
WDTCTL = WDTPW + WDTHOLD;
init_clock();
init_uart3();
__bis_SR_register(LPM0_bits + GIE); //enter LPM0,interrupt enabled
__no_operation();
}
void init_clock(void)
{
unsigned int i;
WDTCTL = WDTPW + WDTHOLD;
//director:设置基础时钟,调整之后分频,计算正确的波特率
P11DIR = BIT0 + BIT2 + BIT4;
P11SEL |=BIT0 + BIT2 + BIT4;
P5SEL |=0x0C;//director:选择外部时钟(port select XT2)
UCSCTL6 &=~ XT2OFF; //director:enable XT2 if not used
UCSCTL3 |= SELREF_2; //director:FLLref = REFO
UCSCTL4 |= SELA__XT2CLK;//director:ACLK from XT2 4M
UCSCTL4 |= SELS_5 + SELM_5;//director:SMCLK=MCLK=XT2=4M
_NOP();
UCSCTL5 = DIVM_2 + DIVS_4 + DIVA_4;//director:XT2-4M,SMCLK-1M,ACLK-1M;
//END
//director:wait for Osc working
do
{
UCSCTL7 &=~ (XT2OFFG + XT1LFOFFG + DCOFFG);//clear XT2,XT1,DCO fault flags
SFRIFG1 &=~ OFIFG;//director:Clear fault flags
for(i=0;i<0xFFFF;i++);//director:wait for Osc to stabilize
}while(SFRIFG1&OFIFG);//director:test Osc fault flag
}
//END
void init_uart3(void)
{
P10SEL |= 0x30;//director:p10.4TXD,p10.5RXD
UCA3CTL1 |= UCSWRST;
UCA3CTL1 |= UCSSEL__SMCLK;
//director:波特率设定
UCA3BR0 = 0x0A; //1M 9600
UCA3BR1 = 0x00; //1M 9600
//END
UCA3MCTL |= UCBRS_3 + UCBRF_0;//director:set USCI state machine
UCA3CTL1 &=~ UCSWRST;//director:init USCI state machine
UCA3IE |= UCRXIE;//director:enable USCI_A3 RX interrupt
}
#pragma vector=USCI_A3_VECTOR
__interrupt void USCI_A3_ISR(void)
{
switch(__even_in_range(UCA3IV,4))
{
case 0:break;//director:Vector 0_no interrupt
case 2:
while(!(UCA3IFG&UCTXIFG));//director:判空
UCA3TXBUF = UCA3RXBUF;//director:将接收到的东西返还给发送者
break;
case 4:break;
default:break;
}
}
|
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|