C51 COMPILER V9.54   UART2                                                                 04/29/2022 17:43:39 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/04
   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/04/28  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          /**
  34           * This function is used to initialize the UART1 function and set the UART
  35           * mode and baud rate in mode 1. 
  36           *
  37           * @param bUartBaudRate The baud rate selection. Mode1 supports an asynchronous
  38           *                      8-bit UART with variable baud rate. The baud rate clock 
  39           *                      source is from baud rate generator or from Timer 1. Here
  40           *                      is the selections below. \n
  41           *                      - eBAUD_RATE_TIMER1
  42           *                      - eBAUD_RATE_4800
  43           *                      - eBAUD_RATE_9600
  44           *                      - eBAUD_RATE_19200
  45           *                      - eBAUD_RATE_38400
  46           *                      - eBAUD_RATE_56000
  47           *                      - eBAUD_RATE_57600
  48           *                      - eBAUD_RATE_115200
  49           *                      - eBAUD_RATE_128000
  50           *                      - eBAUD_RATE_250000
  51           *  
  52           * @note If you set baud rate clock source from Timer1, you need to configure timer1
  53           *       before initializing UART.
  54           *
C51 COMPILER V9.54   UART2                                                                 04/29/2022 17:43:39 PAGE 2   

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

 117   2        }
 118   1      
 119   1        /* Init UART status. */
 120   1        if(bRxBufAddress !=0)
 121   1        {
 122   2          stUART2_Hardware.fRx_Done = 0;
 123   2          stUART2_Hardware.bptrRx_StartAdress = bRxBufAddress;
 124   2          stUART2_Hardware.bptrRx_EndAdress = bRxBufAddress + (bLength-1);  
 125   2          stUART2_Hardware.bptrRx_Buf = stUART2_Hardware.bptrRx_StartAdress;  
 126   2        }
 127   1        
 128   1        /* Clear Interrupt flags. */ 
 129   1        S2CON &= ~RI2;
 130   1        S2CON &= ~TI2;
 131   1      }
 132          
 133          
 134          /**
 135           * Transmit an amount of data via UART. The data to be sent is stored in the transmission
 136           * FIFO that is implemented by software.
 137           *
 138           * @param pData Points to a data array to be sent.
 139           * @param bLength The length of the data to be sent.
 140           * @return None
 141           */
 142          void UART2_TransmitData(uint8_t *bptrData, uint8_t bLength)
 143          {
 144   1        while(bLength--)
 145   1        {
 146   2          /* Write bytes to S1BUF and Polling transmit. */
 147   2          S2BUF = *(bptrData++);
 148   2          while(!(S2CON & TI2));
 149   2          S2CON &= ~TI2;
 150   2        }
 151   1        
 152   1      }
 153          
 154          /**
 155           * @Brief   Check UART received done or not
 156           * @param   bLength, bptrData address
 157           * @return  True or Flase
 158           */
 159          bit UART2_ReceivedDone(void)
 160          {
 161   1        if(stUART2_Hardware.fRx_Done)
 162   1        {
 163   2          stUART2_Hardware.fRx_Done = 0;    
 164   2          return 1;
 165   2        }
 166   1        else
 167   1        {
 168   2          return 0;
 169   2        }
 170   1      }
 171          
 172          /**
 173           * Received interrupt service routine.
 174           *
 175           * @param None
 176           * @return None
 177           */
 178          void ISR_UART2_RX() interrupt ISRUart2rx
C51 COMPILER V9.54   UART2                                                                 04/29/2022 17:43:39 PAGE 4   

 179          {
 180   1        *(stUART2_Hardware.bptrRx_Buf++) = S0BUF;
 181   1        if(stUART2_Hardware.bptrRx_Buf > stUART2_Hardware.bptrRx_EndAdress)
 182   1        {
 183   2          stUART2_Hardware.bptrRx_Buf = stUART2_Hardware.bptrRx_StartAdress;
 184   2          stUART2_Hardware.fRx_Done = 1;
 185   2        }
 186   1        /* Clear UART Interrupt flag of reception. */
 187   1        S2CON &= ~RI2;
 188   1      }
 189          
 190          


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =    301    ----
   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)
