;
;##### Receive and transmit #####
;##### strings of data over UART #####
serTx DATA 30H
serRx DATA 31H
byte1 DATA 32h
byte2 DATA 33h
byte3 DATA 34h
byte4 DATA 35h
ORG 00h
;##### Main program #####
ACALL SetUART ;Set up UART
;set up pointers to load UART data string to RAM
loop: MOV R0,#serRx ;source pointer
MOV R1,#byte1 ;destination pointer
MOV R3,#4 ;counte
ACALL Rx_ram ;input UART string to ram
;set up pointers to load RAM data to UART and output string
MOV R0,#byte1 ;source pointer
MOV R1,#serTx ;destination pointer
MOV R3,#4 ;counte
ACALL ram_Tx ;output string of data from ram over UART
ACALL loop ;keep doing it in a loop
;####################################
;##### RAM Loops ######
;####################################
Rx_ram: ACALL Rx ;Get data from UART
MOV A,@R0 ;get a byte from source
MOV @R1,A ;copy it to destination
INC R1 ;increment destination pointer
DJNZ R3,Rx_ram ;keep doing till all bytes are received
;setb P0.4 ;turn off p0.4 LED after all bytes are sent
RET
ram_Tx: MOV A,@R0 ;get a byte from source
MOV @R1,A ;copy it to destination
INC R0 ;increment source pointer
ACALL Tx ;transmit data byte
DJNZ R3,ram_Tx ;keep doing till all bytes are sent
RET
;####################################
;##### UART Initialization ######
;##### & ######
;##### transmit and receive ######
;####################################
setUART: MOV PCON,#0x80 ;x2 serial baud rate
MOV TH1,#0xF4 ;reload value 250 for 9600bps w/11.059MHz xtal
MOV TL1,#0xF4
MOV TMOD,#0x20 ;timer 1,mode 2(auto reload)
MOV TCON,#0x40 ;start timer 1
MOV SCON,#0x50 ;8-bit, 1 stop, REN enabled
RET
TstOK: MOV serRx,SBUF
CLR RI ;clear RI flag
RET
;timer to keep sync
Rx: MOV R4,#20 ;delay time multiplier
AGAIN: MOV R5,#20 ;delay time
HERE: JB RI,TstOK ;get byte from SBUF if IR is set
NOP
NOP
DJNZ R5,HERE
DJNZ R4,AGAIN
ACALL st_flt ;exit if there is no receive in time period
;transmit UART
Tx: MOV SBUF,serTx ;saving incoming byte in A
TxH: JNB TI,TxH ;wait for the last bit
CLR TI ;get ready to receive next byte
RET ;Return to main program
st_flt: NOP
;CLR P0.4
ACALL loop ;start loop again
END