Uart.c
上传用户:xhjmsc
上传日期:2019-09-13
资源大小:389k
文件大小:4k
源码类别:

传真(Fax)编程

开发平台:

C/C++

  1. //UART
  2. #include "includeH.h"
  3. void InitUART(void)                    //初始化UART
  4. {      
  5.        unsigned int ucGeneralCnt;
  6.        //WDTCTL=WDTPW+WDTHOLD;              //close watch dog
  7.        //DCOCTL &= ~DCO2;
  8.        BCSCTL1|=XTS;     //MCLK时钟源  Select LFXTCLK high frequency
  9.        BCSCTL2|=SELM_3;     //MCLK时钟源  Select LFXTCLK   //+DIVM_3 
  10.        
  11.        do                                  //等待时钟稳定    
  12.        {   IFG1 &= ~OFIFG;
  13.            for(ucGeneralCnt=0;ucGeneralCnt<0xff;ucGeneralCnt++);    
  14.        }while ((IFG1 & OFIFG)==OFIFG);
  15.        
  16.        UCTL0=0X00;
  17.        UCTL0|=SWRST;                       //disable soft reset  
  18.        UCTL0|=CHAR;                         //8 bits
  19.        //UCTL0|=LISTEN;                       //OPEN LISTEN    
  20.        UTCTL0=SSEL0;                  //BaudRate generate select ACLK    
  21.        UBR10=0X03;//0x1a;//0X03;//0X02;  0x03;//0X00; //BaudRate==9600
  22.        UBR00=0X41;//0x0a;//0X41;//0X2b;  0x41;//0X49;    
  23.        UMCTL0=0X00;    
  24.        UCTL0&=~SWRST;                      //enable soft reset USART    
  25.        ME1|=URXE0+UTXE0;                   //enable TXD and RXD    
  26.        IE1|=URXIE0;                        //enable RX interrupt
  27.        IE1|=UTXIE0;                        //enable TX interrupt    
  28.        IFG1=0X00;                          //clear IFG1    
  29.        P3SEL|=0x30;//0x00;//0X30;                       //P3.4 P3.5 for USART    
  30.        P3DIR|=BIT4;                         //P3.4 tx OUTPUT 
  31.        
  32.        delay800ms();
  33.        for(ucGeneralCnt=0;ucGeneralCnt<255;ucGeneralCnt++)
  34.        { 
  35.             aucUartRxBuf[ucGeneralCnt]=0;    //clear RX buffer
  36.        }                      
  37.        _EINT();                             //enable interrupt
  38.        
  39. }//InitUART()
  40.     
  41.     
  42.     
  43. void UartStart(void)                   //启动发送
  44. {    
  45.        //ATcmdDelay();
  46.        //ATcmdDelay();  
  47.        IE1 |= UTXIE0;
  48.        gpucTxBufPnt = 0;
  49.        gwTxNum = 4;
  50.        while((UTCTL0&BIT0) != BIT0);        //TXEPT: 1 empty 0 full 
  51.        TXBUF0 = aucUartTxBuf[0];    
  52.        gucTxBufCnt = 1;
  53.            
  54.        //IFG1 |=  UTXIFG0;                //Entry TX interrupt
  55.            
  56. }
  57.            
  58.            
  59.        
  60. void UartReceive(void)                 //receive
  61. {   
  62.        gucTimeOverflowCount=0;//clear Rx Fax time flowover
  63.        gwTimeOverflowCount=0; //clear Rx Fax time flowover  
  64.        aucUartRxBuf[gucUartRxCnt]=RXBUF0;
  65.        gucUartRxCnt+=1;
  66.        gdwRxQueueRearCnt++;    //Queue length inc
  67.        gpucQueueRear++;       //entry Queue Rear Point inc
  68.              
  69. }//UartReceive
  70.                
  71.                
  72. void UartSend(void)                    //transmit
  73. {   
  74.        gpucTxBufPnt+=1; //TxBufCnt point to the second data.The second data will be sent now
  75.        if(gpucTxBufPnt<gwTxNum)//check whether send next data      
  76.        {   
  77.           TXBUF0=aucUartTxBuf[gucTxBufCnt];          
  78.           gucTxBufCnt++;
  79.           //if(gucTxBufCnt>22){gucTxBufCnt=0;}               
  80.        }    
  81.        else     
  82.           {     
  83.              gpucTxBufPnt=0;                        
  84.              gucTxBufCnt=0;                                      
  85.           }
  86. }
  87.             
  88. //**************************************************************/            
  89.             
  90. #pragma vector=USART0TX_VECTOR            
  91. __interrupt void intUartTx(void)//发送中断响应
  92. {
  93.        UartSend();
  94. }
  95. #pragma vector=USART0RX_VECTOR
  96. __interrupt  void intUartRx(void)//接收中断响应
  97. {
  98.     gucGeneralStatus |= bitModemActive;
  99.     UartReceive();