Friday, January 27, 2006

8051: Example 14

Data reception via UART

In order to enable successful UART serial communication, it is necessary to meet specific rules of the RS232 standard. It primarily refers to voltage levels required by this standard. Accordingly, -10V stands for logic one (1) in the message, while +10V stands for logic zero (0). The microcontroller converts accurately data into serial format, but its power supply voltage is only 5V. Since it is not easy to convert 0V into 10V and 5V into -10V, this operation is on both transmit and receive side left to a specialized IC circuit. Here, the MAX232 by MAXIM is used because it is widespread, cheap and reliable.
This example shows how to receive message sent by a PC. Timer T1 generates boud rate. Since the 11.0592 MHz quartz crystal is used here, it is easy to obtain standard baud rate which amouts to 9600 bauds. Each received data is immediately transferred to port P1 pins.
Receiving data via serial communication UART
;************************************************************************
;* PROGRAM NAME : UartR.ASM
;* DESCRIPTION: Each data received from PC via UART appears on the port
;* P1.
;*
;************************************************************************

;BASIC DIRECTIVES

$MOD53
$TITLE(UARTR.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING

;STACK
         DSEG     AT     03FH
STACK_START:      DS     040H

;RESET VECTORS
         CSEG     AT     0
         JMP      XRESET                ; Reset vector
         ORG      023H                  ; Starting address of UART interrupt routine
         JMP      IR_SER

         ORG      100H

XRESET:  MOV      IE,#00                ; All interrupts are disabled
         MOV      SP,#STACK_START       ; Initialization of Stack pointer
         MOV      TMOD,#20H             ; Timer1 in mode2
         MOV      TH1,#0FDH             ; 9600 baud rate at the frequency of
                                        ; 11.0592MHz
         MOV      SCON,#50H             ; Receiving enabled, 8-bit UART
         MOV      IE,#10010000B         ; UART interrupt enabled
         CLR      TI                    ; Clear transmit flag
         CLR      RI                    ; Clear receive flag
         SETB     TR1                   ; Start Timer1

LOOP:    SJMP     LOOP                  ; Remain here

IR_SER:  JNB      RI,OUTPUT             ; If any data is received,
                                        ; move it to the port
         MOV      A,SBUF                ; P1
         MOV      P1,A
         CLR      RI                    ; Clear receive flag
OUTPUT   RETI

         END                            ; End of program

Data transmission via UART

This program describes how to use UART to transmit data. A sequence of numbers (0-255) is transmitted to a PC at 9600 baud rate. The MAX 232 is used as a voltage regulator.
;************************************************************************
;* PROGRAM NAME : UartS.ASM
;* DESCRIPTION: Sends values 0-255 to PC.
;************************************************************************

;BASIC DIRECTIVES

$MOD53
$TITLE(UARTS.ASM)
$PAGEWIDTH(132)
$DEBUG
$OBJECT
$NOPAGING

;STACK
         DSEG    AT    03FH
STACK_START:     DS    040H

;RESET VECTORS
         CSEG    AT    0
         JMP     XRESET               ; Reset vector

         ORG     100H

XRESET:  MOV     IE,#00               ; All interrupts are disabled
         MOV     SP,#STACK_START      ; Initialization of Stack pointer
         MOV     TMOD,#20H            ; Timer1 in mode 2
         MOV     TH1,#0FDH            ; 9600 baud rate at the frequency of
                                      ; 11.0592MHz
         MOV     SCON,#40H            ; 8-bit UART
         CLR     TI                   ; Clear transmit bit
         CLR     RI                   ; Clear receive flag
         MOV     R3,#00H              ; Reset caunter
         SETB    TR1                  ; Start Timer 1

START:   MOV     SBUF,R3              ; Move number from counter to a PC
LOOP1:   JNB     TI,LOOP1             ; Wait here until byte transmission is
                                      ; complete
         CLR     TI                   ; Clear transmit bit
         INC     R3                   ; Increment the counter value by 1

         CJNE    R3,#00H,START        ; If 255 bytes are not sent return to the
                                      ; label START

LOOP:    SJMP    LOOP                 ; Remain here

         END                          ; End of program