;------------------------------------------------- ;Program: MODRs232.ASM ;Update: 15 August 2002 ;Initial: 17 October 1991 ; ;By: Dr. Marcus O. Durham, PhD, PE ; Tulsa, OK, USA ; mod@superb.org ; www.ThewayCorp.com ;Copyright (c)1991, 2002. All rights reserved ; ;Purpose: ; A routine to demonstrate serial communication ; & to flash an LED on MMIO. ; ;Processor: 8031 family ;PROM: 8k (2000H) onboard ;Crystal: 11.059 MHz ;Baud: 9600 ;Handshake: not used at this speed ;Assembler: Intel ASM51 ; ASM51 MODRs232.asm ;################################################# ; ; ASSIGNMENTS ; ;################################################# ;EXTERNAL ADDRESS AdSevS EQU 8000H ;MMIO addr for Seven Seg ;------------------------------------------------- ;BANK 0, USE W/ MATH & LOOP LoopC EQU 07H ;loop counter ;R3 EQU 03H ;loop 2, math ;R2 EQU 02H ;loop, destination size,mat ;################################################# ; ; PROGRAM ; ;################################################# START: ;------------------------------------------------- ; When processor is reset, program control comes ; here. Jump to the first executable address ; after all interrupts reserved locations. ORG 00H LJMP INITIAL ORG 0070H ;Addres past reserve ;------------------------------------------------- INITIAL: ;------------------------------------------------- ; ; The procedure initializes all common routines. ; It directs traffic for initiation messages. ;INITIALIZE MOV SP,#5Fh ;start stack @ 5f+1 LCALL UART ;config & start UART LCALL SERLINE ;CR & LF MOV DPTR,#SerGreet ;get address LCALL BIOSSER ;message headr RS232 ;------------------------------------------------- MAIN: ;------------------------------------------------- ; ; The main procedure directs traffic. ;PROCESS MOV A,#1 ;turn on LCALL MMIOSS ;send to MMIO LCALL DELAY ;wait MOV A,#0 ;turn off LCALL MMIOSS ;send to MMIO LCALL DELAY ;wait MAN9: LJMP MAIN ;Repeat ;************************************************* ; ; DELAY ; ;************************************************* ; ; Delay routine using NOPS and a nested loop. ; Registers R2 & R3 are used for counting loops. ; ; Count the machine cycles for each instruction. ; NOP = 1 ; MOV = 1 ; DJNZ = 2 ; LCALL= 2 ; RET = 2 ; ; The time for each loop is calculated ; MOV = 1 ; NOP = 1*count ; DJNZ = 2*count ; inside = (incount *2) + 1 ; outside= [(inside + 2)outcout] + 1 ; Plus = 4 for call ; ; Each machine cycle requires time to execute. ; This is based on the oscillator frequency. ; Time= (sec/11.059E6 cycle)(12 period/mach cy) ; = 1.08509 E-6 sec ; ;------------------------------------------------- DELAY: ;------------------------------------------------- ; ; Delay routine using NOPS and a nested loop. ; Registers R2 & R3 are used for this. MOV R3,#00H ;Outer loop counter ZDEL1: MOV R2,#00H ;Nested loop counter ZDEL2: NOP ;Delay DJNZ R2,ZDEL2 ;Nested loop, 256 x DJNZ R3,ZDEL1 ;Outer loop, 256 x RET ;Return to call ;************************************************* ; ; SERIAL RS232 ; ;************************************************* UART: ;------------------------------------------------- ; ; Initialize the registers that control the ; serial communications process. ; ; Timer 1 is used in the 8-bit auto-reload mode. ; So TMOD bit M1 is set and bits ; M1, C/T' and GATE are clear. ; With GATE set, INT1' & INT0' control the timer. ; ; Bit SMOD in register PCON can be used to double ; the baud rate when set. It's clear here, K=1. ; If set, then K=2. ; ; SCON is used to define serial communication ; mode 1, 8-bit UART, by setting bit SM1, ; and enable reception with bit REN set. ; ; Bit TR1 in register TCON is set to enable ; timer. ; ; Set TI to indicate serial transmission is ; complete. ; ; Register TH1 contains the count for baud rate. ; TH1 = 256-(K * Osc Freq / 384 * baud rate) ; ; For 11.059 MHz oscillator, ; TH1 is 0FDH (-3D) for 9600 baud. ; TH1 is 0E8H (-24D) for 1200 baud. MOV TMOD,#20H ;Timer 1 Mode 2 ; ANL PCON,#7FH ;SMOD = 0, K=1 MOV TH1,#0FDH ;9600 BAUD @11.05MHz MOV SCON,#50H ;Set SM1 & REN ; SETB TR1 ;Start timer SETB TI ;last xmission thru ; RET ;Return to call ;------------------------------------------------- SERIN: ;------------------------------------------------- ; ; General purpose serial receiver routine. It ; gets a byte from the serial buffer, SBUF, ; converts to the needed binary form by removing ; the leading bit, and clears the RI (receive ; interrupt) flag that is set by the uP after a ; full byte is received. ; ; Clearing RI allows next byte to be received. ; SBUF is the input and register A is the output. MOV A,SBUF ;Get byte from SBUF CLR RI ;Clear RI ANL A,#7FH ;Clr MSB for ASCII RET ;Return to call ;------------------------------------------------- SEROUT: ;------------------------------------------------- ; ; Transfer one byte out to the serial port. ; TI is high while a transmission is happening. ; It goes lo, when a new byte can be sent. JNB TI,$ ;xmit busy so wait. CLR TI ;clr for next xmit MOV SBUF,A ;mov byte to serial RET ;------------------------------------------------- SERLINE: ;------------------------------------------------- ; Send out a carriage & line feed. MOV A,#0Ah ;lf LCALL SEROUT ;send byte MOV A,#0Dh ;cr LCALL SEROUT ;send byte RET ;------------------------------------------------- BIOSSER: ;------------------------------------------------- ; Send serial message. ; This uses the same UART setup as SERIN. ; The first byte is the # of characters to send. MOV A,#0 ;read initial byte MOVC A,@A+DPTR ;input byte MOV LoopC,A ;set up loop MOV B,#2 ;get 1st message BIOR2: MOV A,B ;offset counter MOVC A,@A+DPTR ;input byte LCALL SEROUT ;ship the byte INC B ;restore offset DJNZ LoopC,BIOR2 ;go thru loop RET ;************************************************* ; ; LATCH REGISTERS - MMIO ; ;************************************************* ; ; Latch registers are used to expand the port ; capability on the Matt board. MMIO latches are ; selected by PEEL logic of the address lines. ; ;------------------------------------------------- MMIOSS: ;------------------------------------------------- ; Select a latch using MMIO addressing. ; Data lines D0 - D7 are connected as the byte. MOV DPTR,#AdSevS ;address location MOVX @DPTR,A ;output data RET ;################################################# ; ; TABLES ; ;################################################# ; ; Predefined messages are in code memory. ; These are used in BIOSSER. ; Place at end of program code or ORG out of way ; ; The first byte is the number of characters. ; The second byte is the cursor location for LCD. ; The next bytes are the ASCII message. ;------------------------------------------------- SerGreet: DB 14, 0, 'uC BIOS ModRs232' ;************************************************* END ;Program end