
;                             FILE SERIAL1.ASM
;              Sample Program Demonstrating Setup and Use
;              of Internal Serial Port Using Mode 1. This
;              program polls the serial port for incoming
;              characters then echoes them back to the 
;              sending program.
;              The program is initially configured for a
;              baud rate of 9600 but other rates can be
;              used by changing the timer reload values.
;
;                     Written By S.C.T.  1/29/99


  $MOD52                        ; Include 8052 Definitions

; ----------  DEFINE Timer 1 Reload Constants ------------

  Rate9600      EQU  0FDh       ; 9600 Baud Setup
  Rate4800      EQU  0FAh       ; 4800 Baud Setup
  Rate2400      EQU  0F4h       ; 2400 Baud Setup
  Rate1200      EQU  0E8h       ; 1200 Baud Setup


; ---------- SET A Particular Baud Rate ------------------

  BaudRateCount EQU  Rate9600  ; Setup Timer 1 Reload Value

  ORG  4000h                    ; Start Program at SDK Code Space Base

; ------ INITIALIZE program variables and registers ------

  CLR   EA                      ; Disable Global Interrupts
  MOV   TMOD, #22h              ; Set Timers 0 and 1 to Mode 2
                                ; For auto reload operation

  MOV   SCON, #50h              ; Setup Serial Port for
                                ; Mode 1 and Enable Receive
  
  MOV   TL1, #BaudRateCount     ; Setup Timer with Desired
  MOV   TH1, #BaudRateCount     ; Baud Rate

  SETB  TR1                     ; Start Timer 1
  SETB  TI                      ; Set Serial Port XMIT Empty

loop1:
  JBC   RI,ProcessChar          ; Go to Character Processing Routine
  JMP   loop1                   ; repeat loop until bit set

ProcessChar:

  MOV   A,SBUF                  ; Retrieve Character From SBUF
loop2:
  JNB   TI,loop2                ; Wait till Transmit Buffer is Empty
  INC   A                       ; Increment A
  MOV   SBUF,A                  ; Load Character to XMIT Buffer and Send
  JMP   loop1                   ; Return to main loop

  END

