Module #7: Serial Port Output Code
; The following "$" commands must be included in every module
$NOMOD51 ; Omit assembler predefined registers.
$INCLUDE(reg515.inc) ; Include 515/535 microcontroller definitions.
NAME SERIAL ; Optional parameter; if no name is provided,
; the filename will be used by default.
PUBLIC SERINIT, SEROUT, ALERT; Lets other modules access this section of code
; from "public domain" utilizing the EXTRN command.
EXTRN CODE (LCDOUT)
; Makes subroutines LCDINIT and LCDOUT in the
; lcd.a51 module available to module main.a51 .
MR1BDAT EQU 00010011B
MR2BDAT EQU 00000111B ;Set stop bit length = 1
;Put registers in memory spaces
ACR EQU 04H ;Auxiliary Control Register
MR1B EQU 08H ;Mode Register B (1-receiver 2-transmitter)
SRB EQU 09H ;Channel B Status Register
CSRB EQU 09H ;Clock Select Register B
CRB EQU 0AH ;Channel A Command Register
THRB EQU 0BH ;Tx holding register
;***********************************************************************************
SERIAL SEGMENT CODE ; Reserve RAM space for the generic code
; segment, MAIN. The name segment name is referred
; to by the following RSEG directive.
RSEG SERIAL ; Selects the MAIN code segement, and makes it
; "active" at this point in assembled code.
; The selected segment remains "active" until
; a different segment is specified.
USING 0 ; Indicates to the assembler that register
; bank 0 will be used, but does not actually
; select register bank 0 .
SERINIT:
MOV A,#01010000B ;Do from this command, down to 00010000
COM_B_RESET:
MOV P2,#CRB
MOVX @R1,A
ADD A,#-16 ;Subtracts 1 from the upper nibble; loop until = 0000
;0101=Reset channel A interrupt
;0100=Reset error status. Clears channel A received break, ; parity error, and overrun error bits.
;0011=Reset transmitter.
;0010=Reset receiver.
;0001=Reset MR pointer. Points MR pointer to MR1.
;0000=No command, exit loop.
JNZ COM_B_RESET ;If the first 4 bits don't equal 0000 jump back to COM_B_RESET.
COM_B_SETUP:
MOV P2,#MR1B ;Points Mode Register 1B to Port 2
MOV A,#MR1BDAT ;Initializes MR1B receiver first in order to
MOVX @R1,A ;initialize MR2B next for transmission.
MOV A,#MR2BDAT ;Stores mode register parameters in acc
MOVX @R1,A ;Move MR2BDAT into MR2B
MOV P2,#ACR ;
MOV A,#80H ;Points 80H into ACR in Port 2
MOVX @R1,A ;Baud Rate Generator Set Select = 1
MOV P2,#CSRB ;Set BAUD rate to 1.8kHz
MOV A,#01000100B
MOVX @R1,A
MOV A,#00000101B ;Points data bits for CRB into Acc
MOV P2,#CRB ;Points CRB into Port 2
MOVX @R1,A ;Points data bits into CRB at Port 2
RET ;Enables the COM B - transmitter and reciever
ALERT:
MOV B,#20 ;WILL TRANSMIT THE NUMBER OF TIMES OF THE
;NUMBER STORED IN B
SEROUT:
MOV A,@R1
;POP ACC ;PUTS THE ID# INTO THE ACC
;MOV A,#37H ;Test data to be sent to the transmitter
SEROUTB:
MOV P2,#SRB
PUSH ACC ; SAVE CHAR for later use
SOUTB1:
MOVX A,@R1 ;Point external Port 2 to Acc
JNB ACC.2,SOUTB1 ;Loop until SRB-bit 2 (TXrdy) is ready to transmit
POP ACC
MOV P2,#THRB ;Send out the serial bit stored
MOVX @R1,A
DJNZ B,SEROUT
RET
END