C51 COMPILER V9.54   UART0                                                                 04/29/2022 17:43:39 PAGE 1   


C51 COMPILER V9.54, COMPILATION OF MODULE UART0
OBJECT MODULE PLACED IN .\Objects\uart0.obj
COMPILER INVOKED BY: C:\Keil_v5\C51\BIN\C51.EXE source\uart0.c OPTIMIZE(8,SPEED) BROWSE DEBUG OBJECTEXTEND PRINT(.\Listi
                    -ngs\uart0.lst) TABS(2) OBJECT(.\Objects\uart0.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 stUART0_Hardware;
  30          
  31          /*_____ F U N C T I O N S __________________________________________________*/
  32          
  33          /**
  34           * @Brief This function is used to initialize the UART0 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   UART0                                                                 04/29/2022 17:43:39 PAGE 2   

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

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

 179           * @return None
 180           */
 181          void ISR_UART0_RX() interrupt ISRUart0rx
 182          {
 183   1        *(stUART0_Hardware.bptrRx_Buf++) = S0BUF;
 184   1        if(stUART0_Hardware.bptrRx_Buf > stUART0_Hardware.bptrRx_EndAdress)
 185   1        {
 186   2          stUART0_Hardware.bptrRx_Buf = stUART0_Hardware.bptrRx_StartAdress;
 187   2          stUART0_Hardware.fRx_Done = 1;
 188   2        }
 189   1        /* Clear UART Interrupt flag of reception. */
 190   1        S0CON &= ~RI0;
 191   1      }
 192          


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