C51 COMPILER V9.54   UART2                                                                 05/19/2022 10:31:24 PAGE 1   


C51 COMPILER V9.54, COMPILATION OF MODULE UART2
OBJECT MODULE PLACED IN .\Objects\uart2.obj
COMPILER INVOKED BY: C:\Keil_v5\C51\BIN\C51.EXE source\uart2.c OPTIMIZE(8,SPEED) BROWSE DEBUG OBJECTEXTEND PRINT(.\Listi
                    -ngs\uart2.lst) TABS(2) OBJECT(.\Objects\uart2.obj)

line level    source

   1          /******************** (C) COPYRIGHT 2020 SONiX *******************************
   2          * COMPANY:  SONiX
   3          * DATE:     2022/05
   4          * AUTHOR:   Eden
   5          * IC:       
   6          * DESCRIPTION:
   7          *
   8          *   This sample code demonstrates how to use the UART function.
   9          *
  10          *____________________________________________________________________________
  11          * REVISION  Date        User  Description
  12          * 1.0       2022/05/04  Eden   First release
  13          *
  14          *____________________________________________________________________________
  15          * THE PRESENT SOFTWARE WHICH IS FOR GUIDANCE ONLY AIMS AT PROVIDING CUSTOMERS
  16          * WITH CODING INFORMATION REGARDING THEIR PRODUCTS TIME TO MARKET.
  17          * SONiX SHALL NOT BE HELD LIABLE FOR ANY DIRECT, INDIRECT OR CONSEQUENTIAL 
  18          * DAMAGES WITH RESPECT TO ANY CLAIMS ARISING FROM THE CONTENT OF SUCH SOFTWARE
  19          * AND/OR THE USE MADE BY CUSTOMERS OF THE CODING INFORMATION CONTAINED HEREIN 
  20          * IN CONNECTION WITH THEIR PRODUCTS.
  21          *****************************************************************************/
  22          
  23          /*_____ I N C L U D E S ____________________________________________________*/
  24          #include "uart.h"
  25          
  26          /*_____ D E F I N I T I O N S ______________________________________________*/
  27          
  28          /*_____ D E C L A R A T I O N S ____________________________________________*/
  29          UART_Hardware_st stUART2_Hardware;
  30          
  31          /*_____ F U N C T I O N S __________________________________________________*/
  32          
  33          /*_____ M A C R O S _____________________________________*/
  34          
  35          /**
  36           * This function is used to initialize the UART1 function and set the UART
  37           * mode and baud rate in mode 1. 
  38           *
  39           * @param bUartBaudRate The baud rate selection. Mode1 supports an asynchronous
  40           *                      8-bit UART with variable baud rate. The baud rate clock 
  41           *                      source is from baud rate generator or from Timer 1. Here
  42           *                      is the selections below. \n
  43           *                      - eBAUD_RATE_TIMER1
  44           *                      - eBAUD_RATE_4800
  45           *                      - eBAUD_RATE_9600
  46           *                      - eBAUD_RATE_19200
  47           *                      - eBAUD_RATE_38400
  48           *                      - eBAUD_RATE_56000
  49           *                      - eBAUD_RATE_57600
  50           *                      - eBAUD_RATE_115200
  51           *                      - eBAUD_RATE_128000
  52           *                      - eBAUD_RATE_250000
  53           *  
  54           * @note If you set baud rate clock source from Timer1, you need to configure timer1
C51 COMPILER V9.54   UART2                                                                 05/19/2022 10:31:24 PAGE 2   

  55           *       before initializing UART.
  56           *
  57           */
  58          void UART2_Init(uint8_t bUartBaudRate, uint8_t* bRxBufAddress, uint8_t bLength)
  59          {
  60   1        /* Disable SWAT */
  61   1        DEGCMD = 0x00;
  62   1        _SET_COMBOUART_INPUT_
  63   1        
  64   1        /* Notice: Enable UART1 TX/RX module first. */
  65   1        S0CON2 |= TEN2;
  66   1        S2CON  |= REN2;
  67   1        
  68   1        /* Setting UART Mapping Matrix */
  69   1        S0CON2 |= mskUART2_COMBO_URX;
  70   1        
  71   1        /* S1CON: UART MODE Setting */
  72   1        S2CON |= mskUART_MODE1;
  73   1          
  74   1        if (bUartBaudRate == eBAUD_RATE_TIMER1)
  75   1        {
  76   2          /* Baud rate generator selection (mode1, 3) (0 : Timer1 overflow period) */
  77   2          S0CON2 &= ~BD2; 
  78   2        }
  79   1        else
  80   1        {
  81   2          /* Baud rate generator selection (mode1, 3) (1 : Controlled by S0REL) */
  82   2          S0CON2 |= BD2;  
  83   2          S2RELH = 0x03;
  84   2          PCON |= SMOD2;
  85   2          
  86   2          switch(bUartBaudRate) {
  87   3            case eBAUD_RATE_4800:
  88   3              S2RELL = BD_4800 & 0x00FF;
  89   3              PCON &= ~SMOD1;
  90   3              break;
  91   3                    
  92   3            case eBAUD_RATE_9600:
  93   3              S2RELL = BD_9600 & 0x00FF;
  94   3              PCON &= ~SMOD1;
  95   3              break;
  96   3            
  97   3            case eBAUD_RATE_19200:
  98   3              S2RELL = BD_19200 & 0x00FF;
  99   3              break;
 100   3            
 101   3            case eBAUD_RATE_38400:
 102   3              S2RELL = BD_38400 & 0x00FF;
 103   3              break;
 104   3            
 105   3            case eBAUD_RATE_56000:
 106   3              S2RELL = BD_56000 & 0x00FF;
 107   3              break;
 108   3            
 109   3            case eBAUD_RATE_115200:
 110   3              S2RELL = BD_115200 & 0x00FF;
 111   3              break;
 112   3            
 113   3            case eBAUD_RATE_128000:
 114   3              S2RELL = BD_128000 & 0x00FF;
 115   3              break;
 116   3            
C51 COMPILER V9.54   UART2                                                                 05/19/2022 10:31:24 PAGE 3   

 117   3            case eBAUD_RATE_250000:
 118   3              S2RELL = BD_250000 & 0x00FF;
 119   3              break;
 120   3            
 121   3            default:
 122   3              break;
 123   3          }
 124   2        }
 125   1      
 126   1        /* Init UART status. */
 127   1        if(bRxBufAddress !=0)
 128   1        {
 129   2          stUART2_Hardware.fRx_Done = 0;
 130   2          stUART2_Hardware.bptrRx_StartAdress = bRxBufAddress;
 131   2          stUART2_Hardware.bptrRx_EndAdress = bRxBufAddress + (bLength-1);  
 132   2          stUART2_Hardware.bptrRx_Buf = stUART2_Hardware.bptrRx_StartAdress;  
 133   2        }
 134   1        
 135   1        /* Clear Interrupt flags. */ 
 136   1        S2CON &= ~RI2;
 137   1        S2CON &= ~TI2;
 138   1        /* Enable UART Interrupt. */
 139   1        UART2_RX_ISR_EN();
 140   1      }
 141          
 142          
 143          /**
 144           * Transmit an amount of data via UART. The data to be sent is stored in the transmission
 145           * FIFO that is implemented by software.
 146           *
 147           * @param pData Points to a data array to be sent.
 148           * @param bLength The length of the data to be sent.
 149           * @return None
 150           */
 151          void UART2_TransmitData(uint8_t *bptrData, uint8_t bLength)
 152          {
 153   1        
 154   1        S0CON2 |= TEN2;
 155   1        S0CON2 &= ~0x18; //**clear URAT2MX
 156   1        S0CON2 |= mskUART2_COMBO_UTX;
 157   1        _SET_COMBOUART_OUTPUT_  
 158   1        
 159   1        while(bLength--)
 160   1        {
 161   2          /* Write bytes to S1BUF and Polling transmit. */
 162   2          S2BUF = *(bptrData++);
 163   2          while(!(S2CON & TI2));
 164   2          S2CON &= ~TI2;
 165   2        }
 166   1        
 167   1        S2CON  |= REN2;
 168   1        S0CON2 &= ~0x18; //**clear URAT2MX
 169   1        S0CON2 |= mskUART2_COMBO_URX;
 170   1        _SET_COMBOUART_INPUT_
 171   1        
 172   1      }
 173          
 174          /**
 175           * @Brief   Check UART received done or not
 176           * @param   bLength, bptrData address
 177           * @return  True or Flase
 178           */
C51 COMPILER V9.54   UART2                                                                 05/19/2022 10:31:24 PAGE 4   

 179          bit UART2_ReceivedDone(void)
 180          {
 181   1        
 182   1        if(stUART2_Hardware.fRx_Done)
 183   1        {
 184   2          stUART2_Hardware.fRx_Done = 0;    
 185   2          return 1;
 186   2        }
 187   1        else
 188   1        {
 189   2          return 0;
 190   2        }
 191   1      }
 192          
 193          /**
 194           * Received interrupt service routine.
 195           *
 196           * @param None
 197           * @return None
 198           */
 199          void ISR_UART2_RX() interrupt ISRUart2rx
 200          {
 201   1        
 202   1        *(stUART2_Hardware.bptrRx_Buf++) = S2BUF;
 203   1        if(stUART2_Hardware.bptrRx_Buf > stUART2_Hardware.bptrRx_EndAdress)
 204   1        {
 205   2          stUART2_Hardware.bptrRx_Buf = stUART2_Hardware.bptrRx_StartAdress;
 206   2          stUART2_Hardware.fRx_Done = 1;
 207   2        
 208   2        }
 209   1        /* Clear UART Interrupt flag of reception. */
 210   1        S2CON &= ~RI2;
 211   1      }
 212          
 213          


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    351    ----
   CONSTANT SIZE    =   ----    ----
   XDATA SIZE       =   ----    ----
   PDATA SIZE       =   ----    ----
   DATA SIZE        =     10       8
   IDATA SIZE       =   ----    ----
   BIT SIZE         =   ----    ----
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)
