Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.
below is the code for frequency counter...make necessary ammendment according to your requirment
;****************************************************************
;this is for memory Placement
Prog equ 0000h ;Starting address for the program
BitVar equ 0 ;where the Bitsegment starts
DataVar equ 024h ;were the Data Segment starts
;some Constants
T0_MASK equ 0F0h
T1_MASK equ 00Fh
;Symbol definitions used in "main" to control the LCD display.
;not used at all
LCD_CLR equ 01AH ;clear LCD command
LCD_LF equ 0AH ;LCD line feed (next line)
LCD_CR equ 0DH ;LCD carriage return
;some port definitions to make the code more readable
RAM_A16 Bit P5.0
EEP_ENA Bit P5.2
bMode Bit P5.3 ;Check this PortPin to read Mode of op
;Place for data vars
;**************************************************************
DSEG at DataVar ;start at DataSeg
GateTime: DS 1 ;the GateOVCounter
OverFlow: DS 1 ;
frequency: DS 4 ;actually a UINT32 Value
bufindex: DS 1 ;
outbuffer: DS 16
T0Vect: DS 2 ;for saving the actual Irq Vect
T2Vect: DS 2 ;
STACK: DS 1 ;stack starts here
;**************************************************************
BSEG at BitVar ;place first bit here
bReady: dBit 1 ;1: ready for output
;Code starts here
;*****************************************************************
CSEG at Prog ;opens the code segment
ORG Prog + 0
LJMP Init ;should be placed here
CSEG at PROG +0Bh ;Timer 0 overflow interrupt
PUSH T0Vect ;Attention this is very tricky
PUSH T0Vect+1
RET ;ret starts the IRQ
CSEG at PROG+02Bh ;Timer 2 overflow interrupt
PUSH T2Vect ;both Functions need 6 clocks
PUSH T2Vect+1 ;to start the IRQ
RET
CSEG at PROG +100h
Init:
CLR EAL
; SETB RAM_A16 ;Make bit A16 of 128K Ram "high".
; CLR EEP_ENA ;Disable EEPROM.
; CLR P5.1
MOV SP,#STACK-1 ;init the stack V0.1 STACK-1
;since we have a stack now Rest should be placed in Sub routines
Main:
; CALL INIT_LCD ;make all neccesarry LCD Inits
; JB bMode,Main1 ;0 Frequ Mode 1 Period Mode
SETB TR0 ;both Timers are runing
SETB T2I0
SETB EAL
MainLoop:
JNB bReady,$ ;wait for Result
CALL HandleFreqOutput
JMP MainLoop
;DispalyHandler when executing Frequency
HandleFreqOutput:
CLR A
MOV TL0,A
MOV TH0,A ;clear the counter
MOV Overflow,A
MOV GateTime,#20
MOV TL2, # Low (-49999) ;V0.2 new ReloadValue -49999
MOV TH2, # High(-49999)
SETB T2I0
SETB TR0 ;restart both Timers
CLR bReady
;now you have 1 s to format the output as you want
;you will need some 32 Bit Division Functions
;here a short example for doing hex out to a buffer
MOV bufindex,#OutBuffer
CALL BufHexOut
RET
;*****************************************************************************
;subroutines
InitT0Freq:
MOV A, TMOD ;initialize Timer 0 to Counter mode
ANL A, # T0_MASK ;change only T0
ORL A, # 5 ;Mode 1 16 Bit Counter
MOV TMOD,A
CLR A
MOV TL0, A
MOV TH0, A ;Timer0 to zero
SETB ET0 ;allow T0 Irq
RET
This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
By continuing to use this site, you are consenting to our use of cookies.