Microcontroller as a frequency generator

Many electronic experiences require clock generator. In common, analog clock generator can build clock signal with 5 % error, and digital clock generator can build it with 0.1 % error. Microcontroller-based clock signal generator has crystal accuracy. The useful frequency range that can be generated is between 1 Hz and 1 MHz when the microcontroller operating at a 24 MHz clock frequency. The 1 MHz frequency output can be generated with continuing to execute a one-cycle-instruction to complement the pin output. To generate a specific frequency below 1 MHz, the other instruction(s) such as no-operation can be inserted to delay the next instruction. The maximum error of 0.1 % can be generated at frequency output below 2 kHz and the maximum error of 1 % at frequency below 20 kHz. The selected frequency signal with 0.1 % or less error also can be generated for frequency below 1 MHz.
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 keypad is a set of buttons arranged in a block which usually bear digits and other symbols but not a complete set of alphabetical letters. If it mostly contains numbers then it can also be called a numeric keypad. Keypads are found on many alphanumeric keyboards and on other devices such as calculators, combination locks and telephones which require largely numeric input.
Figure 1 A 4X3 matrix 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.
Figure 3 Keypad Switch
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.