;------------------------------------------------- ;Program: MODTime.ASM ;Update: 21 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 set of routines are provided to perform the ; basic functions of a computer. ; ; These include ports & timers ; ;Processor: 8031 family ;PROM: 8k (2000H) onboard ;Crystal: 11.059 MHz ;Baud: 9600 ;Handshake: not used at this speed ;Assembler: Intel ASM51 ; ; There are three types of processing techniques ; that are used. ; MAIN - is the normal loop processing ; INTERRUPTS- occur based on events, such as ; regular time intervals. ; BUSY - is called for random tasks that ; can be processed during the delay ; in other tasks. Keypad scan ; is a prime example. ; ; ;################################################# ; ; ASSIGNMENTS ; ;################################################# ;CONSTANTS ;------------------------------------------------- ;EXTERNAL ADDRESS AdConR EQU 0FCxxH ;MMIO addr Control Register ;+++++++++++++++++++++++++++++++++++++++++++++++++ ;USER VARIABLES ;------------------------------------------------- ; ; All the RAM locations I used are shown below. ; This is so the user will not overwrite them. ; ; Place any user variables within this range. ; ;------------------------------------------------- ;USER DEFINED VARIABLES TimMin EQU 3EH ;time minutes counter TimSec EQU 3DH ;time seconds counter TimPer EQU 3CH ;time periods counter ;+++++++++++++++++++++++++++++++++++++++++++++++++ ;SPECIAL FUNCTION REGISTERS ;------------------------------------------------- ; ; SFR are listed for information. With limited ; assemblers, remove the ; since the address must ; be defined. ; ; ;SFR DEFINED ;B EQU 0F0H ;second math register ;Acc EQU 0E0H ;accumulator ;PSW EQU 0D0H ;processor status word ;IP EQU 0B8H ;interrupt priority ;P3 EQU 0B0H ;port 3 ;IE EQU 0A8H ;interrupt enable ;P2 EQU 0A0H ;port 2 ;SBUF EQU 99H ;serial buffer ;SCON EQU 98H ;serial control ;P1 EQU 90H ;port 1 ;TH1 EQU 8DH ;timer hi BYTE 1 ;TH0 EQU 8CH ;timer hi BYTE 0 ;TL1 EQU 8BH ;timer lo BYTE 1 ;TL0 EQU 8AH ;timer lo BYTE 0 ;TMOD EQU 89H ;timer mode ;TCON EQU 88H ;timer control ;PCON EQU 87H ;power control reg ;DPH EQU 83H ;data pointer hi ;DPL EQU 82H ;data pointer low ;SP EQU 81H ;stack pointer ;P0 EQU 80H ;port 0 ;------------------------------------------------- ;BIT IN SFR ;P EQU 0D0H ;parity, PSW ;TI EQU 99H ;transmit is complete ;RI EQU 98H ;receive is complete ;TR1 EQU 8EH ;timer 1 start ;TF0 EQU 8DH ;timer 0 overflow ;TR0 EQU 8CH ;timer 0 start ;------------------------------------------------- ;PORT USE P33 EQU 0B3H ;Port33 Hi=PC Busy,uP no xm ;Not always used in prog ;to use, SETB P33 for input P35 EQU 0B5H ;Port35 Hi=uP BUSY,stop ser ;xmit to uP in download mod ;################################################# ; ; 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 ;************************************************* ; ; INTERRUPTS ; ;************************************************* ; ; Interrupt set-up is discussed in a later ; section. It is associated with timer/counters. ; ;------------------------------------------------- ;INTERRUPT-External 0 ;------------------------------------------------- ORG 03H RETI ;------------------------------------------------- ;INTERRUPT-Timer 0 ;------------------------------------------------- ; The procedure provides direction when timer ; completes count. ORG 0BH LJMP TIMECAL ;interrupt processor ;------------------------------------------------- ;INTERRUPT-External 1 ;------------------------------------------------- ORG 13H ;------------------------------------------------- ;INTERRUPT-Timer 1 ;------------------------------------------------- ; Timer 1 is used for serial. So this procedure ; will never be executed. ORG 1BH ;------------------------------------------------- ;INTERRUPT-Serial ;------------------------------------------------- ; This is not used. The bits are polled in ; various other routines. ORG 23H ;------------------------------------------------- ;COPYRIGHT ;------------------------------------------------- ; Federal statues require credit be given for ; the source of any information used. This ; section must be left with any code used from ; this program. ORG 002BH ;Address past vectors DB 0 ;null address = 2Ch DB 18, 1, 'Copyright (C) 2002' DB 25, 1, 'Marcus O. Durham, PhD, PE' DB 14, 1, 'Tulsa, OK USA' DB 0 ;null address = 6Eh ;************************************************* ; ; INITIAL & MAIN ; ;************************************************* ORG 0070H ;Addres past reserve ; ORG 1000H ;Address if external ;------------------------------------------------- INITIAL: ;------------------------------------------------- ; ; The procedure initializes all common routines. ; It directs traffic for initiation messages. ;INITIALIZE MOV SP,#5Fh ;start stack @ 5f+1 MOV IE,#10000000b ;enable all interr LCALL TIMEINIT ;start timer0 CLR TR0 ;stop counting MOV TimMin,#20 ;initial count MOV TimSec,#0 ; ;------------------------------------------------- MAIN: ;------------------------------------------------- ; ; The main procedure directs traffic. ; The main orchestrates execution of the ; subroutines. ; ; The procedures are for a scoreboard (SB). ;PROCESS MOV A, TimePer ;time period CJNE A,#0,MAN9 ;<> 1/2 sec CPL P3.5 ;is 1/2, so flash MAN9: LJMP MAIN ;Repeat ;################################################# ; ; GENERAL ; ;################################################# ; ; The general routines are common to most project ; applications. These include ; Port 3 ; Timers ; Interrupts ;************************************************* ; ; PORT 3 ; ;************************************************* ; ; Port 3 is a SFR at address 0B0h. ; It has multiple uses for external control of ; events. ; ; The port provides serial transmit & receive. ; It also has external memory read 7 write. ; ; Six of the pins are associated with timers' ; external switches, & interrupts. ; ; P3.7 - 0B7h - read data from external memory ; P3.6 - 0B6h - write data to external memory ; P3.5 - 0B5h - #1 count pulses or start timer ; P3.4 - 0B4h - #0 count pulses or start timer ; P3.3 - 0B3h - #1 external interrupt input ; P3.2 - 0B2h - #0 external interrupt input ; P3.1 - 0B1h - serial transmit ; P3.0 - 0B0h - serial receive ;************************************************* ; ; TIMERS ; ;************************************************* ; ; Timers are used in virtually every project. ; The base MCS-51 design has 2 timers. ; Other manufacturers may have additional timers. ; Two registers control the timer functions. ; ; TCON - timer control register, SFR 88h ; TF1 -8Fh -7- set by time overflow from f to 0 ; TR1 -8Eh -6- program set to 1 to start timer ; TF0 -8Dh -5- set by time overflow from f to 0 ; TR0 -8Ch -4- program set to 1 to start timer ; IE1 -8Bh -3- set by hi to lo edge on P3.3 ; IT1 -8Ah -2- program set=edge, clear=level ; IE0 -89h -1- set by hi to lo edge on P3.2 ; IT0 -88h -0- program set=edge, clear=level ; ;------------------------------------------------- ; TMOD - timer mode register, SFR 89h ; Gate -7- #1 prog set to 1 for external start ; C/T' -6- #1 program set to 1 counts P3.5 ; M1 -5- mode M1M0 ; M0 -4- ; Gate -3- #1 prog set to 1 for external start ; C/T' -2- #0 program set to 1 counts P3.4 ; M1 -1- mode ; M0 -0- ; ; The timer modes select 8, 13, or 16 bits. They ; are the same for timer 1 & 0, except in mode 3. ; 00= 13 bit timer ; 01= 16 bit timer ; 10= 8 bit autoreload. Preset is in THx ; 11= Timer 1 stop. ; TL0= 8 bit controlled as timer 0 ; TH0= 8 bit controlled as timer 1 ; ; Timer 1 is used in 8-bit auto-reload mode to ; control serial communications baud rate. ; ;------------------------------------------------- ; TH1 EQU 8DH ;timer hi BYTE 1 ; TH0 EQU 8CH ;timer hi BYTE 0 ; TL1 EQU 8BH ;timer lo BYTE 1 ; TL0 EQU 8AH ;timer lo BYTE 0 ; ; ;------------------------------------------------- ; The timers are simply counting machine cycles. ; Time is calculated by the formula ; ; Sec= [(THx,TLx)(12 osc/machine cy)]/osc freq ; ; Preset values are similarly calculated. ; ; THx,TLx= (Sec * Osc freq)/12 osc/machine cy ; ; These routines set up a timer that counts ; 1 ms, 1/25 sec, 1 sec, 1 min, 20 min. ;+++++++++++++++++++++++++++++++++++++++++++++++++ COUNT0: ;------------------------------------------------- ; Count is actually very simple. This routine is ; for illustrations to use counter 0. Timer 1 ; is used for serial baud. ; ; TH0,TL0 have results of the count. ;INITIAL SETB 0B4h ;count 1 is input MOV TMOD,#00000101b ;counter=0, mode1 SETB TR0 ;start counting RET ;------------------------------------------------- COUNTINT: ;------------------------------------------------- ; An interrupt can be used when count gets to a ; preset value stored in TH0,TL0. ; For example, to interrupt after counting 5 ; events, preload with -5 = 0FFFBh ; ; When count overflows to 0, the interrupt will ; transfer program to address 000Bh for process. ; This requires the interrupt to be enabled. ; ; If an interrupt is not desired, the overflow ; bit, TF0, can be tested in software polling. ;INITIAL SETB 0B4h ;count 1 as input MOV TMOD,#00000101b ;counter=0, mode1 MOV TH0,#0FFh ;minus leading ones MOV TL0,#0FBh ;-5 SETB TR0 ;start counting ;OPTIONAL INTERRUPT ORL IE,#10000010B;enable all & T0 RET ;get outta here ;+++++++++++++++++++++++++++++++++++++++++++++++++ TIMEINIT: ;------------------------------------------------- ; ; Timer 0 is used for the clock, since timer 1 is ; serial baud rate. Mode is 16 bit and soft gate. ; ; An interrupt at address 000Bh is generated when ; the time has elapsed. ; ; Alternately, if interrupt is not desire, the ; overflow bit, TF0, can be polled. ;OPTIONAL INTERRUPT ORL IE,#10000010b ;enable all & T0 ; ;INITIAL MOV TMOD,#00000001b ;timer=0, mode=1 LCALL TIMERUN ;preset & start T0 RET ;MrButler to Atlanta ;------------------------------------------------- TIMERUN: ;------------------------------------------------- ; ; The routine preloads values and starts the ; timer. ; ; The preset time is calculated. The crystal osc. ; frequency is 11.059 MHz. ; ; Preset= (Sec * Osc freq)/12 osc/machine cy ; ; for 1 msec, THxTLx= -921.583= -400h = 0FC66h ; for 1/20 sec, THxTLx= -45954.167 = 4C7Eh ; for 1/30 sec, ThxTlx= -30719.444 = 8801h ;INITIAL MOV TH0,#88h ;4C7Eh = 1/20 sec MOV TL0,#01H ;lo count SETB TR0 ;start counting RET ;MrButler to Atlanta ;------------------------------------------------- TIMECAL: ;------------------------------------------------- ; ; This routine is called from the interrupt ; processor. It calculates seconds and minutes. ; ; First restart the timer. Then see if time is ; elapsed. ; ; SUBROUTINE CALL DEPTH = 1 ; TIMERUN ;INITIAL LCALL TIMERUN ;preset & start T0 ;PERIOD COUNTS INC TimPer ;another time period MOV A,TimPer ; CJNE A,#30,TIMC9 ;periods in a sec ;SECOND COUNTS MOV TimPer,#0 ;reset period INC TimSec ;another sec MOV A,TimSec CJNE A,#60,TIMC9 ;secs in a min ;MINUTE COUNTS MOV TimSec,#0 ;reset sec INC TimMin ;another min TIMC9: RETI ;leave Scarlet OHara ;************************************************* ; ; INTERRUPTS ; ;************************************************* ; ; Interrupts are used to capture events that ; occur at irregular intervals. These are all ; related to the Port 3 connections. ; Low memory is reserved to process these events. ; ORG 0003H ;IE0 external 0 ; ORG 000BH ;TF0 timer ovrflow 0 ; ORG 0013H ;IE1 external 1 ; ORG 001BH ;TF1 timer ovrflow 1 ; ORG 0023H ;Serial RI or TI ; ; On occurrence of an event, the program ; counter moves to the interrupt address. Eight ; bytes are available to process the event. ; If that is not adequate LJMP or LCALL another ; routine. ; ; At the end of the processing, program ; control is returned to the next instruction by ; a single instruction. ; RETI ;back to program ; ; Two registers are used for interrupt set-up. ; ;------------------------------------------------- ; IE - interrupt enable register, SFR 0A8h ; EA -0AFh -7- enable all interrupts, master ; -0AEh -6- ; -0ADh -5- ; ES -0ACh -4- enable serial interrupt ; ET1 -0ABh -3- enable timer 1 interrupt ; EX1 -0AAh -2- enable external 1 interrupt ; ET0 -0A9h -1- enable timer 0 interrupt ; EX0 -0A8h -0- enable external 0 interrupt ; ; When the master enable is set to 1, the other ; interrupts are selectively enabled. When the ; master enable is clear to 0, all interrupts ; are disabled. ; ;------------------------------------------------- ; The interrupts are handled in the priority of ; the list. External 0, has the highest ; priority. If different priority is desired, use ; the next register to make any interrupt go to ; higher priority. ; ; Set the bit for the higher priority. If more ; than one is set, the pecking order is the ; default order among those set. ; ; IP - interrupt priority register, SFR 0B8h ; PS -0BCh -4- priority of serial interrupt ; PT1 -0BBh -3- priority of timer 1 interrupt ; PX1 -0BAh -2- priority external 1 interrupt ; PT0 -0B9h -1- priority of timer 0 interrupt ; PX0 -0B8h -0- priority external 0 interrupt ; ;------------------------------------------------- ; On interrupt, the processor does the following. ; Pushes PC 2 bytes to stack. ; Transfers to PC the interrupt address. ; Executes the process. ; Returns to next instruction by RETI. ; ; An example is shown below at COUNTINT: ;+++++++++++++++++++++++++++++++++++++++++++++++++ EXTIINIT: ;------------------------------------------------- ; ; External Interrupt 1 is used to trigger by ; switch changes on Port3.3. ; The switch is set for falling edge. ; An interrupt at address 0013h is generated when ; the trigger occurs. ;INITIAL ORL TCON,#00000100b ;falling edge typ ORL IE,#10000100b ;enable all & EI1 RET ;MrButler to Atlanta ;************************************************* ; ; 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 with the RD' & WR'. ; There are multiple latches pre-assigned. ; ; 0FCxxh = AdConR = control register latch ; ;------------------------------------------------- LATCON: ;------------------------------------------------- ; Select the control latch. MOV DPTR,#AdConR ;address location MOVX @DPTR,A ;out data RET ;************************************************* END ;Program end