Advertisement  

Tuesday, 21 May 2019
     
 
Main Menu
Home Home
Shop Shop
News News
BASCOM-AVR BASCOM-AVR
BASCOM-8051 BASCOM-8051
Products Products
Application Notes Application Notes
Publications Publications
Links Links
Support Center Support Center
Downloads Downloads
Forum Forum
Resellers Resellers
Contact Us Contact Us
Updates Updates
MCS Wiki MCS Wiki
Online Help
BASCOM-AVR Help BASCOM-AVR Help
BASCOM-8051 Help BASCOM-8051 Help
Contents in Cart
Show Cart
Your Cart is currently empty.
Search the Shop

Products Search

User Login
Username

Password

If you have problem after log in with disappeared login data, please press F5 in your browser

RSS News
 
     
 

 
   
     
 
AN #35 - Low cost LCD frequency meter Print
by K.S.Sankar - Mostek Electronics ( www.mostek.biz )
Originaly published in ELECTRONICS FOR YOU magazine ( INDIA - W W W . E F Y M A G . C O M )

Frequency meters have always been expensive tools for the average hobbyists. Now, with microcontrollers and liquid-crystal displays (LCDs) having become very economical and popular, it is possible to build a compact and low-cost LCDbased frequency meter that can measure up to 250 kHz.
A sample photo of the LCD module is shown in Fig. 1. These modules are available in 14- and 16-pin configurations.
The 16-pin module has a backlight option. Popular brands are Lampex and Hantronix. Note the pin numbers
before soldering to the circuit. In this project, the LCD module used is Lampex LM16200 with 16 alphanumeric
characters and two lines with backlight option. Pin details of this module are given in Table I. A
functional diagram of the module is shown in Fig. 2. However, you may use any branded or unbranded 2-line, 16-character LCD module for this project. The 10k potentiometer, which controls the contrast of the LCD module, works best when its wiper contact is nearer to ground potential.

Circuit description
Fig. 3 shows the circuit of the frequency counter including the power supply. The microcontroller used is
AT89C2051, which features 2 kB of Flash, 128 bytes of RAM, 15 input/output (I/O) lines, two 16-bit timer/
counters, a five-vector two-level interrupt architecture, a full-duplex serial port, a precision analogue comparator, an on-chip oscillator and clock circuitry.


Fig. 1: 2×16 LCD module

Fig. 2: Functonal diagram of LCD module

Table I : Pin details of LM1620


Port-1 is used to drive the LCD in 4-bit mode with 10-kilo-ohm pull-up resistors. The 24MHz crystal used
gives a processing speed of 2 mega-instructions per second (MIPS). Timer 0 is used as an external counter to count the input pulses. Transistor T1 amplifies the input signal, while non-inverting gate NI (1/6 CD4050) serves as a buffer for coupling the amplified pulses to input pin 8 (P2.4) of timer-0.
A software gate of one-second duration is used to count the number of pulses corresponding to the frequency
of the input signal source. The count value is read and displayed on the 2-line, 16-character LCD module. The
flow-chart of the frequency counting routine is shown in Fig. 4. A conventional power supply circuit comprising a step-down transformer followed by a bridge rectifier,smoothing capacitor and 5V regulator is used to power the circuit. Capacitor C2 (0.1μF) filters ripples in the output of the regulator and LED1 shows the supply status. To test the circuit, connect any pulse generator output to the probe and check the frequency displayed
on the LCD screen.
The LCD module is used in the 4-bit data interface mode, wherein only data pins for DB4 through DB7 are
used for data transfer.  An actual-size, single-side PCB for the LCD frequency meter (Fig. 3) is shown in Fig. 5 and its component layout in Fig. 6.



Fig. 3: Circuit diagram of frequency meter

Fig. 5: Actual-size, single-side PCB layout for frequency meter

Fig. 6: Component layout for the PCB


The software

The software is compiled using the demo version of BASCOM-8051, which can be downloaded from website
‘www.mcselec.com.’ Syntax of some of the important instructions used in the program is shown in the box along with examples. The BASCOM compiler provides special instructions for use and display of
data on the LCD module.
The source code file EFY80FM24.BAS for this LCD frequency meter in BASCOM-51 is given at the end of this article. The same may be modified to meet your specific requirements.

Fig. 4: Flowchart

Click here to download source code and article in PDF

'--------------------------------------------------------
' file: efy20fm24.BAS 25-12-05
' Frequency Meter Program using AT89c2051 micro controller
' written using bascom-51 from www.mcselec.com holland
' an embedded visual basic compiler for 8051 micro
' controllers
' by K.S.Sankar Web: www.mostek.biz
'------------------------------------------------------
' Connect the timer0 input P3.4 to a frequency generator
' with 24 mhz xtal accuracy ok upto 250khz

' define crystal speed and include file
$regfile = "89c2051.dat"
$crystal = 24000000

' define variables used
Dim A As Byte
Dim C As Long , D As Long
Dim Count As Word
Dim T0ic As Long
Dim Delayword As Word


' Initialize variables
Count = 0
T0ic = 0
D = 0

' initialize ports
P1 = 0
P3 = 255

' configure lcd display
Config Lcd = 16 * 2
Config Lcdpin = Pin , Db4 = P1.4 , Db5 = P1.5 , Db6 = P1.6 , Db7 = P1.7 , E = P1.3 , Rs = P1.2
Cls
 
'clear the LCD display
Lcd " EFY Freq Meter "

' define timer0
Config Timer0 = Counter , Gate = Internal , Mode = 1
'Timer0 = counter : timer0 operates as a counter
'Gate = Internal : no external gate control
'Mode = 1 : 16-bit counter

' set t0 internal interrupt
On Timer0 Timer_0_overflow_int
' interrupt will be generated on every 65536 count
Priority Set Timer0
Enable Interrupts
Enable Timer0


Counter0 = 0
'clear counter
Start Counter0
'enable the counter to count

Do
'set up a 1 sec accurate DO NOTHING loop
Enable Interrupts
'wait 1 as per BASCOM-51 is not accurate

For Delayword = 1 To 45440
Next Delayword

Disable Interrupts
C =
Counter0
'get counter value
D = T0ic * 65536

Lowerline
C = C + D
T0ic = 0
Lcd "  "
Lowerline
' show the frequency
Lcd "f=" ; C ; " Hz"
Waitms 255
Waitms 255

C = 0

Counter0 = 0
Start Counter0
're-start it because it was stopped by accessing the COUNTER
Loop



' timer0 int subroutine
Timer_0_overflow_int:
Rem timer0 overflow ( 65535 ) interrupt comes here
' increment the variable
Incr T0ic
Return
End
' end of program
' uses 1106 bytes of program memory


On the Application Note #35,"AN #35 - Low cost LCD frequency meter", there might be a problem.

In the program, there are following a few lines:
 
Enable Interrupts
'wait 1 as per BASCOM-51 is not accurate

For Delayword = 1 To 45440
Next Delayword

Disable Interrupts
C =
Counter0
 

The "Disable Interrupts" instruction stops the interrupts, in this case, the interrupt is only the Timer0's overflow (65536), but the Timer0 itself has not been stopped, if right after this instruction is finished, a falling edge of count signal on P3.4 (T0) comes, the hardware counter inside the 2051 can still increment, because stopping interrupts does not stop a counter or counters. If 65535 is just the number in Timer0,the counter value will increase from 65535 to 65536 which is overflow,  Timer0 can not store it,  so Timer0 will become 0 and Timer0's overflow subroutine will not be able run as at this time, the interrupt was stopped already, meaning: variable T0ic will not increase by 1, so the following instruction, C=Counter, will get a result- value of 0, same for any counts which are N*65536-1, N is a integer. As a result, 65536Hz maybe read as 0Hz, 2*65536Hz maybe read as 65536Hz and so on. A similar issue was addressed by a website http://www.8052.com/tuttimer.phtml

In this article, the issue was with TH0 and TL0, but here with this AN#35, it was with the overflow bit and the rest of the value of Timer0 (counter0).
 
The possible solution might be to stop the counter, which will also stop this interrupt casued by Timer0 overflow as the counter will not change if being stopped, then read the counter value, in this case, put C=Counter0 before Disable interrupts, because C=Counter0 stops counter0 and then read counter0, then followed by disable interrupts, actually, this disable interrupts is not important at all, the reason is the counter has been stopped at this time, there will be no interrupts for this application -- there is only one interrupt which can only be caused by Timer0(counter0).