C51 COMPILER V9.54   UART0                                                                 05/19/2022 10:32:59 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/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 stUART0_Hardware;
  30          
  31          /*_____ F U N C T I O N S __________________________________________________*/
  32          
  33          /*_____ M A C R O S _____________________________________*/
  34          #define OB_IO_OUTPUT_LOW      P00=0;  P0M |=0x01; 
  35          #define OB_IO_OUTPUT_HIGH     P00=1;  P0M |=0x01;
  36          /**
  37           * @Brief This function is used to initialize the UART0 function and set the UART
  38           *        mode and baud rate in mode 1. 
  39           *
  40           * @param bUartBaudRate The baud rate selection. Mode1 supports an asynchronous
  41           *                      8-bit UART with variable baud rate. The baud rate clock 
  42           *                      source is from baud rate generator or from Timer 1. Here
  43           *                      is the selections below. \n
  44           *                      - eBAUD_RATE_TIMER1
  45           *                      - eBAUD_RATE_4800
  46           *                      - eBAUD_RATE_9600
  47           *                      - eBAUD_RATE_19200
  48           *                      - eBAUD_RATE_38400
  49           *                      - eBAUD_RATE_56000
  50           *                      - eBAUD_RATE_57600
  51           *                      - eBAUD_RATE_115200
  52           *                      - eBAUD_RATE_128000
  53           *                      - eBAUD_RATE_250000
  54           *  
C51 COMPILER V9.54   UART0                                                                 05/19/2022 10:32:59 PAGE 2   

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

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

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


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)
