Professional Documents
Culture Documents
Kick Start Board
Kick Start Board
Board
Powered by:
The 8051 is the name of a big family of microcontrollers. The device which we
are going to use along this tutorial is the 'AT89V51RD2' which is a typical 8051
microcontroller manufactured by Atmel™. Note that this part doesn't aim to
explain the functioning of the different components of a 89V51RD2
microcontroller, but rather to give you a general idea of the organization of the
chip and the available features, which shall be explained in detail along this
tutorial.
This figure shows the main features and components that the designer can
interact with. You can notice that the 89V51RD2 has 4 different ports, each one
having 8 Input/output lines providing a total of 32 I/O lines. Those ports can be
used to output DATA and orders do other devices, or to read the state of a
sensor, or a switch.
Figure: 1
Some of the features that have made the 8051 popular are:
8051 models may also have a number of special, model-specific features, such as
UARTs, ADC, OpAmps, etc...
8051 chips are used in a wide variety of control systems, telecom applications, robotics
as well as in the automotive industry. By some estimation, 8051 family chips make up
over 50% of the embedded chip market.
Figure: 2
Basic Pins
PIN 9: PIN 9 is the reset pin which is used to reset the microcontroller‟s internal
registers and ports upon starting up. (Pin should be held high for 2 machine cycles.)
PINS 18 & 19: The 8051 has a built-in oscillator amplifier hence we need to only connect
a crystal at these pins to provide clock pulses to the circuit.
PIN 40 and 20: Pins 40 and 20 are VCC and ground respectively. The 8051 chip needs
+5V 500mA to function properly, although there are lower powered versions like the
Atmel 2051 which is a scaled down version of the 8051 which runs on +3V.
PINS 29, 30 & 31: As described in the features of the 8051, this chip contains a built-in
flash memory. In order to program this we need to supply a voltage of +12V at pin 31. If
external memory is connected then PIN 31, also called EA/VPP, should be connected to
ground to indicate the presence of external memory. PIN 30 is called ALE (address
latch enable), which is used when multiple memory chips are connected to the
controller and only one of them needs to be selected. We will deal with this in depth in
the later chapters. PIN 29 is called PSEN. This is "program store enable". In order to
use the external memory it is required to provide the low voltage (0) on both PSEN and
EA pins.
PORTS
Port P1 (Pins 1 to 8): The port P1 is a general purpose input/output port which can be
used for a variety of interfacing tasks. The other ports P0, P2 and P3 have dual roles or
additional functions associated with them based upon the context of their usage.
Port P3 (Pins 10 to 17): Port P3 acts as a normal IO port, but Port P3 has additional
functions such as, serial transmit and receive pins, 2 external interrupt pins, 2 external
counter inputs, read and write pins for memory access.
Port P2 (pins 21 to 28): Port P2 can also be used as a general purpose 8 bit port when
no external memory is present, but if external memory access is required then PORT P2
will act as an address bus in conjunction with PORT P0 to access external memory.
PORT P2 acts as A8-A15.
Port P0 (pins 32 to 39): Port P0 can be used as a general purpose 8 bit port when no
external memory is present, but if external memory access is required then PORT P0
acts as a multiplexed address and data bus that can be used to access external memory
in conjunction with PORT P2. P0 acts as AD0-AD7.
All four ports in the 80C51 are bidirectional. Each consists of a latch (Special Function
Registers P0 through P3), an output driver, and an input buffer. The output drivers of
Ports 0 and 2, and the input buffers of Port 0, are used in accesses to external memory.
In this application, Port 0 outputs the low byte of the external memory address, time-
multiplexed with the byte being written or read. Port 2 outputs the high byte of the
external memory address when the address is 16 bits wide. Otherwise, the Port 2 pins
continue to emit the P2 SFR content.
All the Port 3 pins are multifunctional. They are not only port pins, but also serve the
functions of various special features as listed below:
POWER
ON/OFF
Microcontroller
To
RS232
supply
connect the
DC(5volt
kickstartboar
s)
d To
computer
Figure: 3
About flash magic
Flash magic is software used to burn the instruction in microcontrollers and also
perform a lot of task.
Figure: 4
NOTE – Flash magic support only those microcontroller which have the ISP(In
System Programming) feature.
One thing is also note that before working in flash magic we have to enable
“Assert DTR and RTS while COM Port open”. For enable this function follow this
steps-
1. Choose “Advance
Option” from “Option”
Menu
Figure: 5
Step 2. Follow the procedure shown in figure 3.
3. Put ON
the
Switch
1. To
POWER
Supply
2. To
Comput
er
Figure: 6
Step 3. Open the flash magic and choose the proper communication port from
device manager and
microcontroller device as shown in
figure: 7.
NXP89V51RD2
1. Select proper
device of
Microcontroller
family & COM Port
Figure: 7
Step 4. Open the flash magic and choose the reset option from ISP menu of
menu bar as shown in figure: 8.
Figure: 8
Step 5 . After choosing reset option a dialogue box appears, follow this
procedure as shown in figure 9.
1. After clicking
start this dialogue
box appear
2. Press”RESET”
Figure: 9
2. Choose the
programme
file
3. Click on
“Start”
Figure: 10
Step 7 . Follow the procedure as shown in figure:11.
1. After clicking
start this dialogue
box appear
2.Press “RESET”
button
3.finished status
shows that the
instructions have
burn in
Microcontroller
Figure: 11
Today, embedded systems are found in cell phones, digital cameras, camcorders,
portable video games, calculators, and personal digital assistants, microwave ovens,
answering machines, home security systems, washing machines, lighting systems, fax
machines, copiers, printers, and scanners, cash registers, alarm systems, automated
teller machines, transmission control, cruise control, fuel injection, anti-lock brakes,
active suspension and many other devices/ gadgets.
Definition
A precise definition of embedded systems is not easy. Simply stated, all computing
systems other than general purpose computer (with monitor, keyboard, etc.) are
embedded systems.
Characteristics
a) Embedded systems are application specific & single functioned; application is
known apriori, the programs are executed repeatedly.
c) Embedded systems are typically designed to meet real time constraints; a real
time system reacts to stimuli from the controlled object/ operator within the time
interval dictated by the environment. For real time systems, right answers arriving
too late (or even too early) are wrong.
Non Recurring Engineering (NRE) Cost: Money invested in R&D and developing
first, functional and tested prototype.
Unit Cost: Cost of producing one unit
Electrical Power
Performance - System Throughput, Computational Power, response time
Functional Correctness
Dependability; Fault Tolerance, Reliability, Maintainability, Availability
Physical Size and Weight
Time to prototype
Time to market
Safety: It should not cause harm to others.
Maintenance, Ease of Use
These metrics compete amongst themselves; increasing one may affect others. Hence
optimization of these metrics is a challenge for an embedded system designer.
Hardware
General purpose microprocessors are single chip semi conductor device which is a
computer on chip, but not a complete computer. Its CPU contains an Arithmetic & Logic
Unit(ALU), a Program Counter(PC), a Stack Pointer(SP), registers, a clock and
interrupts circuit on a single chip. To make complete micro computer, one must add
memory usually ROM and RAM, memory decoder, an oscillator, a number of serial and
parallel ports
A general-purpose processor is designed to cater for large amount of applications and
hence is produced in bulk. Using it in an embedded system offers various benefits.
Design time is low as only software is to be developed, no digital design is involved.
Typical characteristics of a general purpose processors are relatively high cost, high
speeds, higher Power consumption, large architecture, large memory size, onboard
flash and cache, an external bus interface for greater memory usage.
Examples: Motorola‟s 680x0, Intel„s x86
8051 Family
Intel fabricated the original 8051 which is known as MCS-51. The other two members of
the 8051 family are:
8052 – This microcontroller has 256 bytes of RAM and 3 timers. In addition to the
standard features of 8051, this microcontroller has an added 128 bytes of RAM and
timer. It has 8K bytes of on chip program ROM. The programs written for projects
using 8051 microcontroller can be used to run on the projects using 8052
microcontroller as 8051 is a subset of 8052.
8031 – This microcontroller has all the features of 8051 except for it to be ROM-less.
An external ROM that can be as large as 64 K bytes should be programmed and
added to this chip for execution. The disadvantage of adding external ROM is that 2
ports (out of the 4 ports) are used. Hence, only 2 ports are left for I/O operations
which can also be added externally if required for execution.
Comparison of 8051 family members:
ROM 4K 8K 0K
Timers 2 3 2
Serial port 1 1 1
I/O pins 32 32 32
Interrupt sources 6 8 6
8051 microcontrollers use two different kinds of memory such as UV- EPROM, Flash
and NV-RAM. Hence 8051 will not be seen in the part number even though it is the most
popular member of the 8051 family.
i. 8751 – This microcontroller is the UV-EPROM version of 8051. This chip has
only 4K bytes of UV-EPROM. It is required to have access to the PROM
burner and the UV-EPROM eraser to erase the contents inside the chip
before it is programmed again. The disadvantage of using this memory is
the waiting time of around 20 minutes to erase the contents in order to
program it again. Due to this limitation, manufacturers fabricated flash and
NV-RAM versions of 8051.
ii. AT89C51 from Atmel Corporation – Atmel fabricated the flash ROM version
of 8051 which is popularly known as AT89C51 („C‟ in the part number
indicates CMOS). The flash memory can erase the contents within seconds
which is best for fast growth. Therefore, 8751 is replaced by AT89C51 to
eradicate the waiting time required to erase the contents and hence
expedite the development time. To build up a microcontroller based system
using AT89C51, it is essential to have ROM burner that supports flash
memory. Note that in Flash memory, entire contents must be erased to
program it again. The contents are erased by the ROM burner. Atmel is
working on a newer version of AT89C51 that can be programmed using the
serial COM port of IBM PC in order to get rid of the ROM burner.
iii. DS5000 from Dallas Semiconductor – Dallas Semiconductor fabricated the
NV-RAM version of the 8051 which is known as DS5000. The PC serial port
is utilized to load the program onto the in-built ROM. The advantage of NV-
RAM memory is the facility to erase the contents one byte at a time.
iv. One - Time - Programmable (OTP) versions of the 8051 – This version of
microcontroller is cheaper and available from various manufacturers. The
manufacturers use OTP microcontroller for mass production because the
price per unit is very cheap.
Microcontroller structure
CPU
Serial ports
Serial ports provide various serial interfaces between microcontroller and other
peripherals like parallel ports.
Timers/counters
ADC converters are used for converting the analog signal to digital form. The
input signal in this converter should be in analog form (e.g. sensor output) and
the output from this unit is in digital form. The digital output can be use for
various digital applications (e.g. measurement devices).
DAC perform reversal operation of ADC conversion. DAC convert the digital
signal into analog format. It usually used for controlling analog devices like DC
motors, various drives, etc.
Interrupt control
The interrupt control used for providing interrupt (delay) for a working program
.The interrupt may be external (activated by using interrupt pin) or internal (by
using interrupt instruction during programming).
Special functioning block
Some microcontrollers used only for some special applications (e.g. space
systems and robotics) these controllers containing additional ports to perform
such special operations.
This considered as special functioning block.
Features of 8051
This microcontroller is also called as “System on a chip” because it has all the features
on a single chip. The Block Diagram of 8051 Microcontroller is as shown in Figure
Memory Architecture
i. Internal RAM – This memory is located from address 0 to 0xff. The memory
locations from 0x00 to 0x7F are accessed directly. The bytes from 0x20 to
0x2F are bit-addressable. Loading R0 and R1 the memory location from 0x80
to 0xFF can easily accessed.
ii. Special Function Registers (SFR) – Located from address 0x80 to 0xFF of the
memory location. The same instructions used for lower half of Internal RAM
can be used to access SFR‟s. The SFR‟s are bit addressable too.
iii. Program Memory – This is read only memory which is located at address 0.
With the help of 16 bit Special Function Register DPTR, this memory can also
save the tables of constants.
iv. External Data Memory – Located at address 0. The Instruction MOVX (Move
External) should be used to access the external data memory.
Intel Corporation fabricated the 8 – bit microcontroller which was referred as MCS-51 in
1981. This microcontroller was also referred as “system on a chip” because it has 128
bytes of RAM, 4Kbytes of ROM, 2 Timers, 1 Serial port, and four ports on a single chip.
The CPU can work for only 8bits of data at a time because 8051 is an 8-bit processor. In
case the data is larger than 8 bits then it has to be broken into parts so that the CPU can
process conveniently. Most manufacturers have put 4Kbytes of ROM even though the
quantity of ROM can be exceeded up to 64 K bytes.
Intel permitted other manufacturers to fabricate different versions of 8051 but with the
limitation that code compatibility should be maintained. This has added advantage that
if the program is written then it can be used for any version of 8051 despite of
manufacturer.
As years passed by, the quality of technology surpassed the expectation of the greatest
minds, with gadgets becoming smaller, sleeker and more efficient. Microcontrollers
were seen as the answer to the requirements raised in advanced electronics. This is the
reason why manufacturers have now focused their production around the following
main developmental aspects:
i. Ease-of-use
ii. Market availability
iii. Less power usage
iv. Smaller processing power
v. More integrated features like RF and USB
vi. Smaller form factors
Applications
The 8051 has been in use in a wide number of devices, mainly because it is easy to
integrate into a project or build a device around. The following are the main areas of
focus:
iv. Medical Devices: Portable medical devices such as blood pressure and glucose
monitors use microcontrollers will to display data, thus providing higher
reliability in providing medical results.
1. Program to turn ON the LED.
#include<reg52.h>
sbit d1=P1^6;
sbit d2=P3^0;
sbit d3=P3^1;
/*||||||||||||||||||||||||||||||||||||||
SENSORS:
+ VCC
- GND
S3 AD4
S2 AD5
S1 AD6
S0 AD7
NOTE: This port is also used as 8 bit/10 bit ADC input channel.
LCD:
LCD CONTROL PIN(RS) (PORT1, BIT4)
void delay()
{
unsigned int i,j;
for(i=0;i<500;i++)
{
for(j=0;j<400;j++);
}
}
void main()
{
d1=0;
d2=0;
d3=0; //LED1,LED2,LED3 TURN OFF
delay();
while(1)
{
}
2. Program to toggle the LED.
#include<reg52.h>
sbit d1=P1^6;
sbit d2=P3^0;
sbit d3=P3^1;
/*||||||||||||||||||||||||||||||||||||||
SENSORS:
+ VCC
- GND
S3 AD4
S2 AD5
S1 AD6
S0 AD7
NOTE: This port is also used as 8 bit/10 bit ADC input channel.
LCD:
LCD CONTROL PIN(RS) (PORT1, BIT4)
LCD CONTROL PIN(RW) GND
LCD CONTROL PIN(EN) (PORT1, BIT5)
LCD DATA PINS (PORT1, BIT0-3)
LED‟S:
LED1(D1) (PORT1, BIT6)
LED2(D2) (PORT3, BIT0)
LED3(D3) (PORT3, BIT1)
LED4(D6) Programming LED
LED5(PWR-ON) Power supply indication
void main()
{
d1=0;
d2=0;
d3=0; /*TURN OFF LED1, LED2, LED3 */
delay();
while(1)
{
d1=1;
d2=1;
delay();
d1=0;
d2=0;
delay();
}
3. PROGRAM TO DISPLAY SLOGAN1 ON FIRST LINE AND DISPLAY SLOGAN2 ON
SECOND LINE OF THE LCD
#include<reg52.h>
#include"lcd.h"
sbit rs=P1^4;
sbit lcde=P1^5;
sbit d4=P1^0;
sbit d5=P1^1;
sbit d6=P1^2;
sbit d7=P1^3;
sbit motor1=P2^0;
code const unsigned char slogan1[]="THINNKWARE ";
code const unsigned char slogan2[]=" WELCOME ";
/*||||||||||||||||||||||||||||||||||||||
SENSORS:
+ VCC
- GND
S3 AD4
S2 AD5
S1 AD6
S0 AD7
NOTE: This port is also used as 8 bit/10 bit ADC input channel.
LCD:
LCD CONTROL PIN(RS) (PORT1, BIT4)
LCD CONTROL PIN(RW) GND
LCD CONTROL PIN(EN) (PORT1, BIT5)
LCD DATA PINS (PORT1, BIT0-3)
LED‟S:
LED1(D1) (PORT1, BIT6)
LED2(D2) (PORT3, BIT0)
LED3(D3) (PORT3, BIT1)
LED4(D6) Programming LED
LED5(PWR-ON) Power supply indication
clr_lcd();
rs=0;
delay();
lcde=1;
delay();
data2=xch(data1);
datatr(data2);
lcde=0;
delay();
lcde=1;
datatr(data1);
delay();
lcde=0;
delay();
rs=1;
rs=1;
delay();
lcde=1;
delay();
data2=xch(data1);
datatr(data2);
lcde=0;
delay();
lcde=1;
datatr(data1);
delay();
lcde=0;
delay();
rs=0;
}
void delay()
{
unsigned char i,j;
for(i=0;i<20;i++)
{
for(j=0;j<100;j++)
{}
}}
void next_line()
{
send_command(0xc0);
delay();
}
/****COMMAND FOR CLEARING LCD AND BRINGING LCD CURSOR ON FIRST LINE****/
void clr_lcd()
{
send_command(0x01);
delay();
send_command(0x02);
delay();
}
void main()
{
delay();
delay();
#include<reg52.h>
sbit d1=P1^6;
sbit d2=P3^0;
sbit d3=P3^1;
sbit sw1=P3^2;
sbit sw2=P3^3;
sbit sw3=P3^4;
/*||||||||||||||||||||||||||||||||||||||
SW1 (PORT3, BIT2)
SW2 (PORT3, BIT3)
SENSORS:
+ VCC
- GND
S3 AD4
S2 AD5
S1 AD6
S0 AD7
NOTE: This port is also used as 8 bit/10 bit ADC input channel.
LCD:
LCD CONTROL PIN(RS) (PORT1, BIT4)
LCD CONTROL PIN(RW) GND
LCD CONTROL PIN(EN) (PORT1, BIT5)
LCD DATA PINS (PORT1, BIT0-3)
LED‟S:
LED1(D1) (PORT1, BIT6)
void main()
{
d1=0;
d2=0;
d3=0;
while(1) //MAIN BODY OF THE LOOP
{
if(sw1==0)
d1=1;
else
d1=0; //IF SWITCH1 PRESSED,LED1 GLOWS ELSE LED1 TURNED OFF
if(sw2==0)
d2=1;
else
d2=0; //IF SWITCH2 PRESSED,LED2 GLOWS ELSE LED2 TURNED OFF
if(sw3==0)
d3=1;
else
d3=0; //IF SWITCH3 PRESSED,LED3 GLOWS ELSE LED3 TURNED OFF
#include<reg52.h>
sbit sw1=P3^2;
sbit motor1_plus=P2^0;
sbit motor1_minus=P2^1;
sbit motor2_plus=P2^2;
sbit motor2_minus=P2^3;
/*||||||||||||||||||||||||||||||||||||||
SENSORS:
+ VCC
- GND
S3 AD4
S2 AD5
S1 AD6
S0 AD7
NOTE: This port is also used as 8 bit/10 bit ADC input channel.
LCD:
LCD CONTROL PIN(RS) (PORT1, BIT4)
LCD CONTROL PIN(RW) GND
LCD CONTROL PIN(EN) (PORT1, BIT5)
LCD DATA PINS (PORT1, BIT0-3)
LED‟S:
LED1(D1) (PORT1, BIT6)
LED2(D2) (PORT3, BIT0)
LED3(D3) (PORT3, BIT1)
LED4(D6) Programming LED
LED5(PWR-ON) Power supply indication
/* MAIN LOOP*/
void main()
{
while(1)
{
if(sw1==0) /*MOTOR1,MOTOR2 TURNS ON BASED ON SWITCH1 PRESS*/
{
motor1_plus=1;
motor1_minus=0;
motor2_plus=1;
motor2_minus=0;
}
else /*MOTOR1,MOTOR2 TURNS OFF BASED ON SWITCH1 NOT PRESSED */
{
motor1_plus=1;
motor1_minus=1;
motor2_plus=1;
motor2_minus=1;
}
}}
6. PROGRAM TO DEMONSTRATE STEPPER MOTOR'S MOVEMENT IN FORWARD
AND REVERSE DIRECTIONS
#include<reg52.h>
#include"stepper.h"
sbit coil_a=P2^0;
sbit coil_b=P2^1;
sbit coil_c=P2^2;
sbit coil_d=P2^3;
sbit sw1=P3^2;
/*||||||||||||||||||||||||||||||||||||||
SENSORS:
+ VCC
- GND
S3 AD4
S2 AD5
S1 AD6
S0 AD7
NOTE: This port is also used as 8 bit/10 bit ADC input channel.
LCD:
LCD CONTROL PIN(RS) (PORT1, BIT4)
LCD CONTROL PIN(RW) GND
LCD CONTROL PIN(EN) (PORT1, BIT5)
LCD DATA PINS (PORT1, BIT0-3)
LED‟S:
LED1(D1) (PORT1, BIT6)
LED2(D2) (PORT3, BIT0)
LED3(D3) (PORT3, BIT1)
LED4(D6) Programming LED
LED5(PWR-ON) Power supply indication
MOTOR DRIVER IC:
MOTOR DRIVER 1 (PORT 2,BIT(0-3))
MOTOR DRIVER 2 (PORT 2,BIT(4-7)) */
void main()
{
void fwdstepper()
{
coil_a=0;
coil_b=1;
coil_c=1;
coil_d=1;
stepperdelay();
coil_a=1;
coil_b=0;
coil_c=1;
coil_d=1;
stepperdelay();
coil_a=1;
coil_b=1;
coil_c=0;
coil_d=1;
stepperdelay();
coil_a=1;
coil_b=1;
coil_c=1;
coil_d=0;
stepperdelay();
}
void revstepper()
{
coil_a=1;
coil_b=1;
coil_c=1;
coil_d=0;
stepperdelay();
coil_a=1;
coil_b=1;
coil_c=0;
coil_d=1;
stepperdelay();
coil_a=1;
coil_b=0;
coil_c=1;
coil_d=1;
stepperdelay();
coil_a=0;
coil_b=1;
coil_c=1;
coil_d=1;
stepperdelay();
}
7. PROGRAM TO RUN A COUNTER COUNTING FROM 0-99 ON THE SEVEN SEGMENT
#include<reg51.h>
sbit com0=P3^0;
sbit com1=P3^7;
#define segport P2
unsigned char degdata[]={0x81,0xE7,0x92,0xC2,0xE4,0xC8,0x8C,0xE3,0x80,0xE0,0Xff};
unsigned char unit,tens,increment_count;
/*||||||||||||||||||||||||||||||||||||||
SW1 (PORT3, BIT2)
SW2 (PORT3, BIT3)
SW3 (PORT3, BIT4)
SENSORS:
+ VCC
- GND
S3 AD4
S2 AD5
S1 AD6
S0 AD7
NOTE: This port is also used as 8 bit/10 bit ADC input channel.
LCD:
LCD CONTROL PIN(RS) (PORT1, BIT4)
LCD CONTROL PIN(RW) GND
LCD CONTROL PIN(EN) (PORT1, BIT5)
LCD DATA PINS (PORT1, BIT0-3)
LED‟S:
LED1(D1) (PORT1, BIT6)
LED2(D2) (PORT3, BIT0)
LED3(D3) (PORT3, BIT1)
LED4(D6) Programming LED
LED5(PWR-ON) Power supply indication
void increment()
{
increment_count++;
if(unit<9)
{
if(increment_count==20)
{
increment_count=0;
unit++; //increment unit counter
}}
else if(increment_count==20)
{
increment_count=0;
unit=0;
if(tens<9)
tens++; //increment tens counter
else
tens=0;
}
}
void display()
{
com0=1; //turn on segment1 display
com1=0;
segport=0xff;
segdelay();
segport=degdata[unit]; //display unit count on seven segment display
segdelay();
com1=1;
com0=0; //turn on segment2 display
segport=0xff;
segdelay();
segport=degdata[tens]; //display tens count on seven segment display
segdelay();
}
void main()
{
unit=0;
tens=0;
while(1)
{
display(); //seven segment display function
increment(); //0-99 counter function
}
}
#include<reg52.h>
#include"matrix_keypad.h"
#include"lcd.h"
sbit rs=P1^4;
sbit lcde=P1^5;
sbit d4=P1^0;
sbit d5=P1^1;
sbit d6=P1^2;
sbit d7=P1^3;
sbit row0=P2^0;
sbit row1=P2^1;
sbit row2=P2^2;
sbit row3=P2^3;
sbit col0=P2^4;
sbit col1=P2^5;
sbit col2=P2^6;
sbit col3=P2^7;
bit flag;
unsigned keyno;
/*||||||||||||||||||||||||||||||||||||||
SW1 (PORT3, BIT2)
SW2 (PORT3, BIT3)
SW3 (PORT3, BIT4)
SENSORS:
+ VCC
- GND
S3 AD4
S2 AD5
S1 AD6
S0 AD7
NOTE: This port is also used as 8 bit/10 bit ADC input channel.
LCD:
LCD CONTROL PIN(RS) (PORT1, BIT4)
LCD CONTROL PIN(RW) GND
LCD CONTROL PIN(EN) (PORT1, BIT5)
LCD DATA PINS (PORT1, BIT0-3)
LED‟S:
LED1(D1) (PORT1, BIT6)
LED2(D2) (PORT3, BIT0)
LED3(D3) (PORT3, BIT1)
LED4(D6) Programming LED
LED5(PWR-ON) Power supply indication
clr_lcd();
p++;
}
}
rs=0;
delay();
lcde=1;
delay();
data2=xch(data1);
datatr(data2);
lcde=0;
delay();
lcde=1;
datatr(data1);
delay();
lcde=0;
delay();
rs=1;
rs=1;
delay();
lcde=1;
delay();
data2=xch(data1);
datatr(data2);
lcde=0;
delay();
lcde=1;
datatr(data1);
delay();
lcde=0;
delay();
rs=0;
}
void delay()
{
unsigned char i,j;
for(i=0;i<100;i++)
{
for(j=0;j<20;j++)
{}
}}
void next_line()
{
send_command(0xc0);
delay();
}
void clr_lcd()
{
send_command(0x01);
delay();
send_command(0x02);
delay();
}
void keychk()
{
row0=0;
row1=1;
row2=1;
row3=1; //CLEAR ROWO PORT PIN AND SET ROW1,ROW2,ROW3 PORT PINS
if(col0==0)
{
keyno=0x00; //MAKE KEYNO=00H IF ROW0=0,COL0=0
return;
}
if(col1==0)
{
keyno=0x01; //MAKE KEYNO=01H IF ROW0=0,COL1=0
return;
}
if(col2==0)
{
keyno=0x02; //MAKE KEYNO=02H IF ROW0=0,COL2=0
return;
}
if(col3==0)
{
keyno=0x03; //MAKE KEYNO=03H IF ROW0=0,COL3=0
return;
}
row0=1;
row1=0;
row2=1;
row3=1; //CLEAR ROW1 PORT PIN AND SET ROW0,ROW2,ROW3 PORT PINS
if(col0==0)
{
keyno=0x04; //MAKE KEYNO=04H IF ROW1=0,COL0=0
return;
}
if(col1==0)
{
keyno=0x05; //MAKE KEYNO=05H IF ROW1=0,COL1=0
return;
}
if(col2==0)
{
keyno=0x06; //MAKE KEYNO=06H IF ROW1=0,COL2=0
return;
}
if(col3==0)
{
keyno=0x07; //MAKE KEYNO=07H IF ROW1=0,COL3=0
return;
}
row0=1;
row1=1;
row2=0;
row3=1; //CLEAR ROW2 PORT PIN AND SET ROW0,ROW1,ROW3 PORT PINS
if(col0==0)
{
keyno=0x08; //MAKE KEYNO=08H IF ROW2=0,COL0=0
return;
}
if(col1==0)
{
keyno=0x09; //MAKE KEYNO=09H IF ROW2=0,COL1=0
return;
}
if(col2==0)
{
keyno=0x10; //MAKE KEYNO=10H IF ROW2=0,COL2=0
return;
}
if(col3==0)
{
keyno=0x11; //MAKE KEYNO=11H IF ROW2=0,COL3=0
return;
}
row0=1;
row1=1;
row2=1;
row3=0; //CLEAR ROW3 PORT PIN AND SET ROW0,ROW1,ROW2 PORT PINS
if(col0==0)
{
keyno=0x12; //MAKE KEYNO=12H IF ROW3=0,COL0=0
return;
}
if(col1==0)
{
keyno=0x13; //MAKE KEYNO=13H IF ROW3=0,COL1=0
return;
}
if(col2==0)
{
keyno=0x14; //MAKE KEYNO=14H IF ROW3=0,COL2=0
return;
}
if(col3==0)
{
keyno=0x15; //MAKE KEYNO=15H IF ROW3=0,COL3=0
return;
}
flag=0;
keyno=0x50; //MAKE KEYNO=50H AND flag=0 for no key press
}
void keyaction()
{
if(keyno==0x50)
return;
if(flag==1)
return;
else
{
flag=1; //key-bouncing protection code
if(keyno==0x00)
senddata(48);
if(keyno==0x01)
senddata(49);
if(keyno==0x02)
senddata(50);
if(keyno==0x03)
senddata(51);
if(keyno==0x04)
senddata(52);
if(keyno==0x05)
senddata(53);
if(keyno==0x06)
senddata(54);
if(keyno==0x07)
senddata(55);
if(keyno==0x08)
senddata(56);
if(keyno==0x09)
senddata(57);
if(keyno==0x10)
senddata('A');
if(keyno==0x11)
senddata('B');
if(keyno==0x12)
senddata('C');
if(keyno==0x13)
senddata('D');
if(keyno==0x14)
senddata('E');
if(keyno==0x15)
senddata('F'); //display 0-f if keyno=00h-15h
}}
void main()
{
lcdinit();
delay();
next_line();
dispslogan("keypad_display");
while(1)
{
keychk(); //key sense function
send_command(0x80);
keyaction(); //function to display 0-f on lcd based on key press detection
}}