- //UART
- #include "includeH.h"
- void InitUART(void) //初始化UART
- {
- unsigned int ucGeneralCnt;
- //WDTCTL=WDTPW+WDTHOLD; //close watch dog
- //DCOCTL &= ~DCO2;
- BCSCTL1|=XTS; //MCLK时钟源 Select LFXTCLK high frequency
- BCSCTL2|=SELM_3; //MCLK时钟源 Select LFXTCLK //+DIVM_3
- do //等待时钟稳定
- { IFG1 &= ~OFIFG;
- for(ucGeneralCnt=0;ucGeneralCnt<0xff;ucGeneralCnt++);
- }while ((IFG1 & OFIFG)==OFIFG);
- UCTL0=0X00;
- UCTL0|=SWRST; //disable soft reset
- UCTL0|=CHAR; //8 bits
- //UCTL0|=LISTEN; //OPEN LISTEN
- UTCTL0=SSEL0; //BaudRate generate select ACLK
- UBR10=0X03;//0x1a;//0X03;//0X02; 0x03;//0X00; //BaudRate==9600
- UBR00=0X41;//0x0a;//0X41;//0X2b; 0x41;//0X49;
- UMCTL0=0X00;
- UCTL0&=~SWRST; //enable soft reset USART
- ME1|=URXE0+UTXE0; //enable TXD and RXD
- IE1|=URXIE0; //enable RX interrupt
- IE1|=UTXIE0; //enable TX interrupt
- IFG1=0X00; //clear IFG1
- P3SEL|=0x30;//0x00;//0X30; //P3.4 P3.5 for USART
- P3DIR|=BIT4; //P3.4 tx OUTPUT
- delay800ms();
- for(ucGeneralCnt=0;ucGeneralCnt<255;ucGeneralCnt++)
- {
- aucUartRxBuf[ucGeneralCnt]=0; //clear RX buffer
- }
- _EINT(); //enable interrupt
- }//InitUART()
- void UartStart(void) //启动发送
- {
- //ATcmdDelay();
- //ATcmdDelay();
- IE1 |= UTXIE0;
- gpucTxBufPnt = 0;
- gwTxNum = 4;
- while((UTCTL0&BIT0) != BIT0); //TXEPT: 1 empty 0 full
- TXBUF0 = aucUartTxBuf[0];
- gucTxBufCnt = 1;
- //IFG1 |= UTXIFG0; //Entry TX interrupt
- }
- void UartReceive(void) //receive
- {
- gucTimeOverflowCount=0;//clear Rx Fax time flowover
- gwTimeOverflowCount=0; //clear Rx Fax time flowover
- aucUartRxBuf[gucUartRxCnt]=RXBUF0;
- gucUartRxCnt+=1;
- gdwRxQueueRearCnt++; //Queue length inc
- gpucQueueRear++; //entry Queue Rear Point inc
- }//UartReceive
- void UartSend(void) //transmit
- {
- gpucTxBufPnt+=1; //TxBufCnt point to the second data.The second data will be sent now
- if(gpucTxBufPnt<gwTxNum)//check whether send next data
- {
- TXBUF0=aucUartTxBuf[gucTxBufCnt];
- gucTxBufCnt++;
- //if(gucTxBufCnt>22){gucTxBufCnt=0;}
- }
- else
- {
- gpucTxBufPnt=0;
- gucTxBufCnt=0;
- }
- }
- //**************************************************************/
- #pragma vector=USART0TX_VECTOR
- __interrupt void intUartTx(void)//发送中断响应
- {
- UartSend();
- }
- #pragma vector=USART0RX_VECTOR
- __interrupt void intUartRx(void)//接收中断响应
- {
- gucGeneralStatus |= bitModemActive;
- UartReceive();
- }