Tutorial Microcontroller MCS-51 ATMEL ISP

Microcontroller Kits
Programmer and Target 89s51

Simple Mikrokontroller 89s51 Trainer

Mikrokontroller 89s51 Trainer

Super Mikrokontroller Trainer 89s51


All Item Include



uC 8051 makes effective frequency counter

by: Fazal Pathan, Physical Research Laboratory, Ahmedabad, India


Figure 1 shows an efficient and costeffective frequency counter using an Atmel 89C2051 mC Reference 1). The design can use any mC of the 8051 family. The circuit counts frequency and sends the count to a PC via the serial port. The signal connects to pin 3.4 of the mC. The TTL-compatible output of the mC drives the 1488, which converts the output to RS-232 voltage levels. The output of the 1488 connects to the RxD pin on the serial port of the PC. In this design, only unidirectional communication exists between the mC and the PC. But you can use the TxD line to control another device.The assembly routine in Listing 1 shows initialization of the timer, counter, and interrupts. The main program sets up T0 to count external pulses and T1 to count time in the autoreload mode 2 (Reference 2). The main program monitors the main-program flag, frqflg, until the flag is set. Then, the main program sends the counted frequency to the PC’s port using mode 1. First, it sends the special character “L” to recognize the following 3 bytes as valid data. Then it sends the value of register R2, which is the most significant byte of the counter value. This value increments every time the counteroverflows.Then, the main program sends the values of registers TH0 and TL0 to the PC’s port. After sending the data, the program again jumps to the main routine, in which it clears the timer and counter and reinitializes them, and then starts counting again. The mC counts the value in hexadecimal format; the PC can then convert it to decimal format. In this example, we used a 12-MHz crystal and a baud rate of 2400 bps.However, you can use higher clock frequencies and baud rates, because the mC can operate to 24 MHz.We use the system in Figure 1 for photon counting in astronomical instrumentation.We give thanks to the Department of Space,Government of India, for material support and BG Anandarao,PhD, for encouragement.


( Download File : frequency1.zip )


;program counts the pulse and transmite trhough p3.1 pin only 
;without handshaking ;signal.
Frqflg equ 0fh ; use a bit flag to signal main program
Baudnum equ 0f3h ; number loaded in TH1 for 2043,8 baudrate
Org 0h
Mov sp,#30h ;set stack above register/ bit area
Sjmp over ;jump over T1 interrupt address
org 0bh
Inc r2
org 1bh ;T1 overflow flag interrupt to here
setb psw.3 ;swicht 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
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
clr psw.3 ;return to bank 0 register
Reti ;return to main program
Over: mov tcon,#00h ;all timers stopped-flags reset
Setb 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=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
Mov tcon,#50h
Mov ie,#88h
Jbc Frqflg,getfrq
Sjmp simulate
anl pcon,#7fh
Anl tmod,#30h
Orl tmod,#20h ;set timer T1 as an 8 bit autoreload
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 T1 set before nect transmission
sjmp wait2 ;else poll flag again
next2: mov sbuf,tl0 ;now transmit TL0
wait3: jbc ti,over ;and wait until the ti flag is set
sjmp wait3

Comments, questions and discussion about this topic