- Low Frequency (LF): 0 Hz ~ 899,999 Hz. This incoming frequency can feed through T0 pin of microcontroller. To display the value of incoming frequency is the same of our old project.
- Medium Frequency (MF): 900,000 Hz ~ 7,199,999 Hz. This incoming frequency must feed to T0 pin of microcontroller via a 3-bit counter/divider. To display the value of incoming frequency, the number of pulse that was counted by timer 0 must be multiplied by eight. To maintenance the accuracy, we cut the least significant digit of display. The display format is X,XXX.XX kHz.
- High Frequency (HF): 7,200,000 Hz ~ 99 MHz. This incoming frequency must feed to T0 pin of microcontroller via a 3-bit and 4-bit counter/divider. To display the value of incoming frequency, the number of pulse that was counted by timer 0 must be multiplied by 128. To maintenance the accuracy, we cut the two least significant digits of display. The display format is XX,XXX.X kHz. To display the frequency between 99 MHz and 100 MHz is still on experiment.
99 MHz Frequency Counter
Open-Collector as Selector
We have three states. Each state has its frequency range.
1. High Frequency State.
This state sets HFPin, so the incoming pulse (f) can feed to CP1 (Clock Pulse of counter 1). This state also sets HFPin1, so the output frequency of counter 0 feed to CP0 (Clock Pulse of counter 0). The incoming pulse was isolated to CP0 by resetting MFPin. Next the output frequency of counter 0 was emitted from Q2-0 to timer 0. To do this, we set HMFPin and reset LFPin.
2. Medium Frequency State.
This state resets HFPin and sets MFPin, so incoming pulse feed to CP0 . The 3-bit counter/divider works and emits the pulse at Q2-0.
3. Low Frequency State.
This state sets the LFPin and resets the others. The incoming pulse feed through to timer 0.
The schematic diagram is the same as the schematic diagram of dual channel 920 kHz Frequency Counter project, but before counter 0 pin (P3.4), we must add this divider.
Auto-select Subroutine Program
When counting the incoming pulse (by timer 0) was running, counter/divider that was activated must proper with the incoming frequency. To do this, the Auto-select Subroutine Program will be run before the counting process. This subroutine will determine the counter/divider that must be activated.
This subroutine works as follow.
1. Check if incoming pulse is high frequency.
- Activate HF state: set HFPin, HFPin1, HMFPin; and reset MFPin, LFPin (See table bellow). This will enable output of the 3-bit and 4-bit counter/divider.
- Count incoming pulse during 20 ms intervals.
- If result is grater than 56,250, the incoming pulse must be high frequency, go to step 4.
- Activate MF state: set MFPin, HMFPin; and reset HFPin, HFPin1, LFPin. This will enable output of the 3-bit counter/divider but disable the other.
- Count incoming pulse during 20 ms intervals.
- If result is grater than 112,500, the incoming pulse must be medium frequency. So, and go to step 4.
4. Count incoming pulse during 1 s intervals.
aaaaaHFPin HFPin1 MFPin HMFPin LFPin
aaaaaP0.1 aa P0.2 aaP0.3 aa P0.4 aaP0.5
HF aaaaa1 aaaaa 1 aaaaa0 aaaaa1 aaaaa0
MF aaaaa0 aaaaa 0 aaaaa1 aaaaa1 aaaaa0
LF aaaaaa0 aaaaa0 aaaaa0 aaaaa0 aaaaa1
To do four step above we make some subroutine: PreDelay20ms to prepare timer 2 for delay 20 ms, PreCount20msT0 to count the incoming pulse of timer 0 during 20 ms intervals, CheckIfHF to check if incoming pulse is high frequency, CheckIfMF to check if incoming pulse is medium frequency, CountF to count incoming frequency during 1 s interval, CountHFFrequency to divide the incoming high frequency by 128 and prepare it for display, CountMFFrequency to divide the incoming medium frequency by eight and prepare it for display.
The listing program can be downloaded here.
Dual Channel 920 kHz Frequency Counter
- CmdWrtStart,CmdWrt, DataWrt
This subroutine will display a character on LCD and others facilities to display some properties like initiate LCD, clear screen, go to home, and write some characters. - HexToBcd
This subroutine will convert 20-bits hexadesimal number at tripple of register R1 R2 R3 to 8-digits BCD number at quadruple of register R4 R5 R6 R7. The BCD data can be displayed on LCD. May be only some lowest data that required to display. - DisplayDigit
A subroutine to display 6-digits is enough for displaying digital data accuratly.DisplayDigitThis subroutine will display 6-digits on LCD. Zero will be replaced by space until we find non-zero number. Input data must be BCD format and located at triplle of register R5 R6 R7.
Microcontroller-based 2 Channel Frequency-Meter
This project is to make frequency counter/frequency meter that can measure the frequency of incoming analog signal. This project has two input leads. Up to now, this application can measure the frequency of both leads from 0 Hz to 920 kHz with six digit display. We have calibrated this project with fabrication-made frequency counter. This project has only 0.01 % error. So if we measure a frequency above 100kHz, the error may be happen below 20 Hz. And if we measure the a frequency between 10kHz and 100kHz, the last digit may be toggle between two values.
Overview
Access 4X3 Matrix-Keypad
At the lowest level, keyboards are organized in a matrix of rows and columns. The CPU accesses both rows and column through ports; therefore, with a port of microcontroller, a 4X3 matrix of keys can be connected. When a key pressed, a row and column make a connection; otherwise, there is no connection between row and column.
Scanning and Identifying the Key Pressed
Figure1 shows a 4X3 matrix connected to port 1. The rows (R1 through R4) are connected to an output port and the columns (C1 through C3) are connected to an input port. Note that, we only use 7 pins of port 1.
To detect a pressed key, first, the microcontroller set pin 0 through 7 of port 1 and initiate the variable ‘Digit’ to zero. The value of ‘Digit’ represents the digit of key pressed.
Then it sends 0111 to R1 R2 R3 R4 and it reads the columns. If the data read from the columns is C1 C2 C3 =111, no key has been pressed and the process continues to next step.
If the data read from the columns is C1 C2 C3 =011, this means that a key in the R1 row and C1 column has been pressed. That is ‘1’.
In the subroutine ‘CheckColumn’, the value of ‘Digit’ is increased. The value of ‘Digit’ represents the key pressed. Before leave this subroutine, microcontroller set ‘KeyPressed’ to indicate there is a key pressed.
If the data read from the columns is C1 C2 C3 =101, this means that a key in the R1 row and C2 column has been pressed. That is ‘2’. Table 1 represents the meaning of each combination of data received at C1 C2 C3.
Someone can not press two key at the same time. There is time different between press a key with another key. ‘KeyPressed’ indicates there is a key pressed in this time. Before leave the entire subroutine, if ‘KeyPressed’ has been set, the microcontroller set the variable ‘KeyAlreadyPressed’. This variable will not be cleared until this subroutine detect that there is no key pressed. So, if press more than two key, only the first key will be read.
To see if any key is pressed, the columns are scanned over and over in an infinite loop until one of them has a 0 on it. The listing below is only for example. In fact, it’s no efficient if we scan the keypad without any intervals of time. We usually use the intervals time for scanning more than 10 ms. This interval can be generated by timer.
Listing Program.
Program for counter
A programmable counter can be made by a microcontroller. AT89S51 have two timers that can be operated as independent counter. To do this, we must set c/t bit at TMOD register. This sample will operate timer 0 as mode 2 counter. By loading 155des at TL0 and TH0, timer 0 act as 100des counter.
A simple frequency counter can be made by this counter.
This is the sample program for counting negative edge of input clock at T0 (p3.4). The number of clock will displayed at 2 digit of 7-segment LED common anode.
This program also acts as frequency divider. The clock frequency will be divided by 100 and emitted to P3.2. Press INT0 for pause and INT0 again to resume.
Here is the listing program.
Displaying 2 digits to 7-segment LED
The 2 digit 7-segment display can seem a little complicated. The main idea for this system is to connect both 7-segment cells together in parallel, but in the same time, only one that be powered.
Here is the simple sequence to show the ones at the right cell and tens at the left cell.
1. Sends data ‘ones’ though the ‘PortLED’.
2. Energizes the right cell while left cell is off by resetting P1.0.
3. Wait for a short time delay. In this project, we paused for 8.192 ms that was setted by interrupt from timer 1.
4. Sends data ‘tens’ though the ‘PortLED’.
5. Energizes the left cell while right cell is off by setting P1.0.
6. Wait for 8.192 ms too.
Here is a sample program for displaying 00~99 to 7-Segment LED common anode.Each digit activated by multiplexing every 8128 ms.
The ones will increment in 1 second.
Microcontroller as a frequency generator
The main principle of microcontroller-based clock generator is toggling a output pin with specific time interval.
Here is a sample program for toggling pin 0 through 7 of port 0 with delay routine.
$MOD51
Org 0h
Start: Mov P1,#00001111B
call Delay
Mov P1,#11110000B
call Delay
Jmp Start
;------------------- Delay sub routine -------------------
Delay: Mov R0,#0FFh ;R0=FF (Hex)
Delay1: Mov R1,#0FFh ;R1=FF (hex)
Delay2: nop ;no operation
nop
nop
Djnz R1,Delay2
Djnz R0,Delay1
Ret
End
Here is a sample program will source the clock with its periode 6 ms.
$mod51
MOV P1,#0h
Repeat: CPL P1.0
JMP Repeat
END
Keypad
A computer keyboard usually contains a small numeric keypad with a calculator-style arrangement of buttons duplicating the numeric and arithmetic keys on the main keyboard to allow efficient entry of numerical data. This number pad (commonly abbreviated to "numpad") is usually positioned on the right side of the keyboard because most people are right-handed. Many laptop computers have special function keys which turn part of the alphabetical keyboard into a numerical keypad as there is insufficient space to allow a separate keypad to be built into the laptop's chassis. Separate plug-in keypads can be purchased.
By convention, the keys on calculator-style keypads are arranged such that 123 is on the bottom row. In contrast, a telephone keypad has the 123 keys at the top. It also has buttons labelled * (star) and # (octothorpe, number sign, "pound" or "hash") either side of the zero. Most of the keys also bear letters which have had several auxiliary uses, such as remembering area codes or whole telephone numbers. The keypad of a calculator contains the digits 0 through 9, together with the four arithmetic operations, the decimal point and other more advanced functions. Keypads are a part of mobile phones that are replaceable and sit on a sensor board. Some multimedia mobile phones have a small joystick which has a cap to match the keypad. Keypads are also a feature of some combination locks. This type of lock is often used on doors, such as that found at the main entrance to some offices.
There are some type(s) of keypad: 4X3, 4X4, etc. Each type of those sould be accessed with different manner. Microcontroller must use I/O port to source and sink current to achieve data in which keypad pressed.
Constructing a Matrix Keypad
Martix keypads are well known for their simple architecture and ease of interfacing with any microcontroller. In this part of tutorial we will learn how to interface a 4x4 matrix keypad with AVR and 8051 microcontroller. Also we will see how to program then in Assembly and C.
Constuction of a keypad is really simple. As per the outline shown in the figure below we have four rows and four columns. In between each overlapping row and column line there is a key. Now our keypad is ready, all we have to do is connect the rows and columns to a port of microcontroller and program the controller to read the input.
Scanning a Matrix Keypad
There are many methods depending on how you connect your keypad with your controller, but the basic logic is same. We make the coloums as i/p and we drive the rows making them o/p, this whole procedure of reading the keyboard is called scanning.
In order to detect which key is pressed from the matrix, we make row lines low one by one and read the coloums. Lets say we first make Row1 low, then read the columns. If any of the key in row1 is pressed will make the corrosponding column as low i.e if second key is pressed in Row1, then column2 will give low. So we come to know that key 2 of Row1 is pressed. This is how scanning is done.
So to scan the keypad completely, we need to make rows low one by one and read the columns. If any of the button is pressed in a row, it will take the corrosponding column to a low state which tells us that a key is pressed in that row. If button 1 of a row is pressed then Column 1 will become low, if button 2 then column2 and so on...
Overview of microcontroller
A microcontroller is a miniature of computer. It has CPU, ROM, RAM, I/O port and timer all embedded on single chip, some times its contain ADC, DAC and analogue comparator. It's also known as IBP ("itty-bitty processors"). All computers -- whether we are talking about a personal desktop computer or a large mainframe computer or a microcontroller -- have several things in common:
- All computers have a CPU (central processing unit) that executes programs. If you are sitting at a desktop computer right now reading this article, the CPU in that machine is executing a program that implements the Web browser that is displaying this page.
- The CPU loads the program from somewhere. On your desktop machine, the browser program is loaded from the hard disk.
- The computer has some RAM (random-access memory) where it can store "variables."
- And the computer has some input and output devices so it can talk to people. On your desktop machine, the keyboard and mouse are input devices and the monitor and printer are output devices. A hard disk is an I/O device -- it handles both input and output.
The desktop computer you are using is a "general purpose computer" that can run any of thousands of programs. Microcontrollers are "special purpose computers." Microcontrollers do one thing well. There are a number of other common characteristics that define microcontrollers. If a computer matches a majority of these characteristics, then you can call it a "microcontroller":
- Microcontrollers are "embedded" inside some other device (often a consumer product) so that they can control the features or actions of the product. Another name for a microcontroller, therefore, is "embedded controller."
Microcontrollers are dedicated to one task and run one specific program. The program is stored in ROM (read-only memory) and generally does not change.
- Microcontrollers are often low-power devices. A desktop computer is almost always plugged into a wall socket and might consume 50 watts of electricity. A battery-operated microcontroller might consume 50 milliwatts.
- A microcontroller has a dedicated input device and often (but not always) has a small LED or LCD display for output. A microcontroller also takes input from the device it is controlling and controls the device by sending signals to different components in the device.
For example, the microcontroller inside a TV takes input from the remote control and displays output on the TV screen. The controller controls the channel selector, the speaker system and certain adjustments on the picture tube electronics such as tint and brightness. The engine controller in a car takes input from sensors such as the oxygen and knock sensors and controls things like fuel mix and spark plug timing. A microwave oven controller takes input from a keypad, displays output on an LCD display and controls a relay that turns the microwave generator on and off.
- A microcontroller is often small and low cost. The components are chosen to minimize size and to be as inexpensive as possible.
- A microcontroller is often, but not always, ruggedized in some way.
The microcontroller controlling a car's engine, for example, has to work in temperature extremes that a normal computer generally cannot handle. A car's microcontroller in Alaska has to work fine in -30 degree F (-34 C) weather, while the same microcontroller in Nevada might be operating at 120 degrees F (49 C). When you add the heat naturally generated by the engine, the temperature can go as high as 150 or 180 degrees F (65-80 C) in the engine compartment.
On the other hand, a microcontroller embedded inside a VCR hasn't been ruggedized at all.
The actual processor used to implement a microcontroller can vary widely. For example, the cell phone shown on Inside a Digital Cell Phone contains a Z-80 processor. The Z-80 is an 8-bit microprocessor developed in the 1970s and originally used in home computers of the time. The Garmin GPS shown in How GPS Receivers Work contains a low-power version of the Intel 80386, I am told. The 80386 was originally used in desktop computers.
In many products, such as microwave ovens, the demand on the CPU is fairly low and price is an important consideration. In these cases, manufacturers turn to dedicated microcontroller chips -- chips that were originally designed to be low-cost, small, low-power, embedded CPUs. The Motorola 6811 and Intel 8051 are both good examples of such chips. There is also a line of popular controllers called "PIC microcontrollers" created by a company called Microchip. By today's standards, these CPUs are incredibly minimalistic; but they are extremely inexpensive when purchased in large quantities and can often meet the needs of a device's designer with just one chip.
A typical low-end microcontroller chip might have 1,000 bytes of ROM and 20 bytes of RAM on the chip, along with eight I/0 pins. In large quantities, the cost of these chips can sometimes be just pennies. You certainly are never going to run Microsoft Word on such a chip -- Microsoft Word requires perhaps 30 megabytes of RAM and a processor that can run millions of instructions per second. But then, you don't need Microsoft Word to control a microwave oven, either. With a microcontroller, you have one specific task you are trying to accomplish, and low-cost, low-power performance is what is important.