di2616.txt ;************************************************************************************************** ; LISTING 1 - FREQUENCY-COUNTER ROUTINE FOR 89C2051 ; ; "Microcontroller makes effective frequency counter," EDN, Nov 23, 2000, pg 164 ; http://www.ednmag.com/ednmag/reg/2000/11232000/designideas.htm#24di6 ; *************************************************************************************************** ; Program counts the pulses and transmits through p3.1 pin only without handshaking signal. .Equ Frqflg,0fh ;use a bit flag to signal main program .Equ Baudnum,0f3h ;number loaded in TH1 for 2403.8 baud Freq: .Org 0000h Mov SP, #30h ;set stack above register/bit area Sjmp over ;jump over T1 interrupt address .Org 000bh Inc r2 Reti .Org 001bh ;T1 overflow flag interrupt to here Setb psw.3 ;swich to register bank1 Inc r0 ;count R0 up until overflow at 00h Cjne r0, #00h,Checktime ;check to see if time is up Inc r1 ;or inc R1 when R0 rolls over Checktime: Cjne r1, #27h, Goback ;check R1 for terminal count Cjne r0, #10h, Goback ;check R0 for terminal count Clr tr0 ;stop T0 Clr tr1 ;stop T1 time before T0 stopped = Setb Frqflg ;signal main program that T0 =freq. Goback: Clr psw.3 ;return to bank 0 registers Reti ;return to main program Over: Mov tcon, #00h ;all timers stopped - flags reset Set psw.3 ;select register bank 1 and reset R0, R1 Mov r0, #00h Mov r1, #00h Clr psw.3 ;return to bank 0 Mov tmod, #25h ;T1 a mode 2 timer, T0 mode 1 counter Mov tl1, #9ch ;start TL1 at 9ch Mov th1, #9ch ;TH1 = 156d, overflows in 100 clocks Mov tl0, #00h ;zero T0 Mov th0, #00h Clr Frqflg ;reset the freq. measured flag Mov tcon, #50h ;start timer T1 and counter T0 Mov ie, #88h ;enable global and T1 overflow interrupts Simulate: Jbc Frqflg, Getfrq ;have main program test "frqflg" Sjmp Simulate ;loop here until freq.is measured Getfrq: Anl pcon, #7fh ;set SMOD bit to 0 for Baud * 32 rate Anl tmod, #30h ;alter timer T1 configuration only Orl tmod, #20h ;set timer T1 as an 8 bit autoload Mov th1, #Baudnum Setb tr1 ;run T1 Mov scon, #40h Mov sbuf, #'L' Wait: Jbc ti, Next Sjmp Wait Next: Mov sbuf, r2 ;set UART to mode1 Wait1: Jbc ti, Next1 Sjmp Wait1 Next1: Mov sbuf, th0 ;transmit content of the th0 Wait2: Jbc ti, Next2 ;wait for T1set before next transmission Sjmp Wait2 ;else poll flag again Next2: Mov sbuf, tl0 ;now transmit tl0 ;and wait until the ti flag is set Wait3: Jbc ti, Over Sjmp Wait3 . End