Escolar Documentos
Profissional Documentos
Cultura Documentos
Difference between
and microcontroller
microprocessor
Microprocessor is an IC which has only the CPU inside them i.e. only
the processing powers such as Intels Pentium 1,2,3,4, core 2 duo, i3, i5
etc. These microprocessors dont have RAM, ROM, and other peripheral
on the chip. A system designer has to add them externally to make them
functional. Application of microprocessor includes Desktop PCs,
Laptops, notepads etc.
But this is not the case with Microcontrollers. Microcontroller has a
CPU, in addition with a fixed amount of RAM, ROM and other
peripherals all embedded on a single chip. At times it is also termed as a
mini computer or a computer on a single chip. Today different
manufacturers produce microcontrollers with a wide range of features
available in different versions. Some manufacturers are ATMEL,
Microchip, TI, Freescale, Philips, Motorola etc.
ii.
iii.
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.
FEATURES OF 8051
8051 have 128 bytes of RAM
8051 have 128 user defined flags
It consist of 16 bit address bus
It consist of 3 internal and two external interrupts
less power usage in 8051 with respect to other microcontroller
It consist of 16-bit program counter and data pointer
8051 can process 1 million one-cycle instructions per
second
It consist of 32 general purpose registers each of 8 bits
ROM on 8051 is 4 Kbytes in size
It also consist of Two 16 bit Timer/ Counter
We can connect 64 kb of external RAM
SFRSS
As you can see, although the address range of 80h through FFh offer 128
possible addresses, there are only 21 SFRs in a standard 8051. All other
addresses in the SFR range (80h through FFh) are considered invalid.
SFR Types
As mentioned in the chart itself, the SFRs that have a blue background
are SFRs related to the I/O ports. The 8051 has four I/O ports of 8 bits,
for a total of 32 I/O lines. Whether a given I/O line is high or low and
the value read from the line are controlled by the SFRs in green.
The SFRs with yellow backgrouns are SFRs which in some way control
the operation or the configuration of some aspect of the 8051. For
example, TCON controls the timers, SCON controls the serial port.
The remaining SFRs, with green backgrounds, are "other SFRs." These
SFRs can be thought of as auxillary SFRs in the sense that they don't
directly configure the 8051 but obviously the 8051 cannot operate
without them. For example, once the serial port has been configured
using SCON, the program may read or write to the serial port using the
SBUF register.
// Programming Tip: The SFRs whose names appear in red in the chart
above are SFRs that may be accessed via bit operations (i.e., using the
SETB and CLR instructions). The other SFRs cannot be accessed using
bit operations. As you can see, all SFRs that whose addresses are
divisible by 8 can be accessed with bit operations.
SFR Descriptions
This section will endeavor to quickly overview each of the standard
SFRs found in the above SFR chart map. It is not the intention of this
section to fully explain the functionality of each SFR--this information
will be covered in separate chapters of the tutorial. This section is to just
give you a general idea of what each SFR does.
TMOD (Timer Mode, Addresses 89h): The Timer Mode SFR is used
to configure the mode of operation of each of the two timers. Using this
SFR your program may configure each timer to be a 16-bit timer, an 8bit autoreload timer, a 13-bit timer, or two separate timers. Additionally,
you may configure the timers to only count when an external pin is
activated or to count "events" that are indicated on an external pin.
TL0/TH0 (Timer 0 Low/High, Addresses 8Ah/8Ch): These two SFRs,
taken together, represent timer 0. Their exact behavior depends on how
the timer is configured in the TMOD SFR; however, these timers always
count up. What is configurable is how and when they increment in value.
TL1/TH1 (Timer 1 Low/High, Addresses 8Bh/8Dh): These two SFRs,
taken together, represent timer 1. Their exact behavior depends on how
the timer is configured in the TMOD SFR; however, these timers always
count up. What is configurable is how and when they increment in value.
SBUF (Serial Control, Addresses 99h): The Serial Buffer SFR is used
to send and receive data via the on-board serial port. Any value written
to SBUF will be sent out the serial port's TXD pin. Likewise, any value
which the 8051 receives via the serial port's RXD pin will be delivered
to the user program via SBUF. In other words, SBUF serves as the
output port when written to and as an input port when read from.
P2 (Port 2, Address A0h, Bit-Addressable): This is input/output port 2.
Each bit of this SFR corresponds to one of the pins on the
microcontroller. For example, bit 0 of port 2 is pin P2.0, bit 7 is pin P2.7.
Writing a value of 1 to a bit of this SFR will send a high level on the
corresponding I/O pin whereas a value of 0 will bring it to a low level.
// Programming Tip: While the 8051 has four I/O port (P0, P1, P2, and
P3), if your hardware uses external RAM or external code memory (i.e.,
your program is stored in an external ROM or EPROM chip or if you are
using external RAM chips) you may not use P0 or P2. This is because
the 8051 uses ports P0 and P2 to address the external memory. Thus if
you are using external RAM or code memory you may only use ports P1
and P3 for your own use.
will be able to interrupt the serial interrupt routine since the serial
interrupt routine has the highest priority.
PSW (Program Status Word, Addresses D0h, Bit-Addressable): The
Program Status Word is used to store a number of important bits that are
set and cleared by 8051 instructions. The PSW SFR contains the carry
flag, the auxiliary carry flag, the overflow flag, and the parity flag.
Additionally, the PSW register contains the register bank select flags
which are used to select which of the "R" register banks are currently
selected.
// Programming Tip: If you write an interrupt handler routine, it is a very
good idea to always save the PSW SFR on the stack and restore it when
your interrupt is complete. Many 8051 instructions modify the bits of
PSW. If your interrupt routine does not guarantee that PSW is the same
upon exit as it was upon entry, your program is bound to behave rather
erradically and unpredictably--and it will be tricky to debug since the
behavior will tend not to make any sense.
ACC (Accumulator, Addresses E0h, Bit-Addressable): The
Accumulator is one of the most-used SFRs on the 8051 since it is
involved in so many instructions. The Accumulator resides as an SFR at
E0h, which means the instruction MOV A,#20h is really the same as
MOV E0h,#20h. However, it is a good idea to use the first method since
it only requires two bytes whereas the second option requires three
bytes.
B (B Register, Addresses F0h, Bit-Addressable): The "B" register is
used in two instructions: the multiply and divide operations. The B
register is also commonly used by programmers as an auxiliary register
to temporarily store values.
Other SFRs
The chart above is a summary of all the SFRs that exist in a standard
8051. All derivative microcontrollers of the 8051 must support these
basic SFRs in order to maintain compatability with the underlying
MSCS51 standard.
A common practice when semiconductor firms wish to develop a new
8051 derivative is to add additional SFRs to support new functions that
exist in the new chip. For example, the Dallas Semiconductor
DS80C320 is upwards compatible with the 8051. This means that any
program that runs on a standard 8051 should run without modification
on the DS80C320. This means that all the SFRs defined above also
apply to the Dallas component.
However, since the DS80C320 provides many new features that the
standard 8051 does not, there must be some way to control and
configure these new features. This is accomplished by adding additional
SFRs to those listed here. For example, since the DS80C320 supports
two serial ports (as opposed to just one on the 8051), the SFRs SBUF2
and SCON2 have been added. In addition to all the SFRs listed above,
the DS80C320 also recognizes these two new SFRs as valid and uses
their values to determine the mode of operation of the secondary serial
port. Obviously, these new SFRs have been assigned to SFR addresses
that were unused in the original 8051. In this manner, new 8051
derivative chips may be developed which will run existing 8051
programs.
// Programming Tip: If you write a program that utilizes new SFRs that
are specific to a given derivative chip and not included in the above SFR
list, your program will not run properly on a standard 8051 where that
SFR does not exist. Thus, only use non-standard SFRs if you are sure
that your program wil only have to run on that specific microcontroller.
Likewise, if you write code that uses non-standard SFRs and
TIMERS
What is a Timer
Configuring a Timer
A register called TMOD is used for configuring the Timers for the
desired operation. TMOD is an 8-bit register with following bit
configuration:
The lower four bits (TMOD.0 TMOD.3) are used to configure Timer 0
while the higher four bits (TMOD.4 TMOD.7) are for Timer 1. When
GATE is high, the corresponding Timer is enabled only when there is an
interrupt at corresponding INTx pin of AT89C51 controller and Timer
control bit is high. Otherwise only setting Timer control bit is sufficient
to start the Timer.
Programming 8051 Timers
The programming of 8051 Timers can be done by using either polling
method or by using interrupt. In polling, the microcontroller keeps
monitoring the status of Timer flag. While doing so, it does no other
operation and consumes all its processing time in checking the Timer
flag until it is raised on a rollover. In interrupt method controller
responds to only when the Timer flag is raised. The interrupt method
prevents the wastage of controllers processing time unlike polling
method.
Polling is mostly used for time delay generation and interrupt method is
more useful when waveforms are to be generated or some action has to
be repeated in fixed delays.
(i) Polling Method
The polling method involves the following algorithm:
1.
Configure the Timer mode by passing a hex value into the TMOD
register. This will tell the controller about which Timer is be used; the
mode of Timer; operation (to be used as timer or counter); and whether
external interrupt is required to start Timer.
2.
Load the initial values in the Timer low TLx and high THx byte. (x
= 0/1)
3.
Start the Timer by setting TRx bit.
4. Wait while the Timer flag TFx is raised.
5.
Clear the Timer flag. The Timer flag is raised when Timer rolls
over from FFFFH to 0000H. If the Timer is not stopped, it will start
updating from 0000H in case of modes 0 & 1 while with initial value in
case of mode 2. If TFx is not cleared, controller will not be able to detect
next rollover.
6.
Stop the Timer by clearing TRx bit. If TRx bit is not cleared the
Timer will restart updating from 0000H after the rollover in case of
modes 0 and 1 while with initial value in case of mode 2.
(ii) Interrupt Method
The interrupt method makes use of a register called Interrupt Enable (IE)
register. An 8051 microcontroller has 6 hardware interrupts. The
interrupts refer to a notification, communicated to the controller, by a
hardware device or software, on receipt of which controller skips
temporarily whatsoever it was doing and responds to the interrupt.
When EA (IE.7) is set (=1), interrupts are enabled. When clear (EA=0),
interrupts are disabled and controller does not respond to any interrupts.
ET0, ET1 & ET2 (IE.3, IE.4 & IE.5) are Timer interrupt bits. When set
(high) the timer are enabled and when cleared (low) they are disabled.
(8052 controllers have three Timers, so ET2 is its Timer 2 interrupt bit.)
The ISR corresponding to these interrupts are executed when the TFx
flags of respective Timers are raised. For more details on other IE
register bits, refer Interrupts with 8051.
Note that IE register is bit addressable.
Timer programming using Timer interrupts involves following
algorithm.
1.
Configure the Timer mode by passing a hex value to TMOD
register.
2.
Load the initial values in the Timer low TLx and high THx byte.
3.
Enable the Timer interrupt by passing hex value to IE register or
setting required bits of IE register. For example,
IE = 0x82;
IE = 0x88;
or
EA = 1;
ET0 = 1;
IE^7 = 1;
IE^3 = 1;
4.
Start the Timer by setting TRx bit.
5.
Write Interrupt Service Routine (ISR) for the Timer interrupt. For
example,
ISR definition for Timer 0 :
void ISR_Timer0(void) interrupt 1
{
<Body of ISR>
}
ISR definition for Timer 1 :
void ISR_Timer1(void) interrupt 3
{
<Body of ISR>
}
6.
If the Timer has to be stopped after once the interrupt has occurred,
the ISR must contain the statement to stop the Timer.
For example,
void ISR_Timer1(void) interrupt 3
{
<Body of ISR>
TR1 =0;
}
7.
If a routine written for Timer interrupt has to be repeated again and
again, the Timer run bit need not be cleared. But it should be kept in
mind that Timer will start updating from 0000H and not the initial values
in case of mode 0 and 1. So the initial values must be reloaded in the
interrupt service routine.
For example,
void ISR_Timer1(void) interrupt 3
{
<Body of ISR>
TH1 =0XFF;
TL1 = 0xFC;
}
// Toggle LED
// Call delay
TMOD = 0x01;
TH0= 0xFC;
1millisecond delay
TL0 = 0x66;
TR0 = 1;
while(TF0 == 0);
TR0 = 0;
TF0 = 0;
}
// Mode1 of Timer0
// FC66 evaluated hex value for
// Start Timer
// Using polling method
// Stop Timer
// Clear flag
Example code
Time delay in Mode1 using interrupt method
// Use of Timer mode 1 for blinking LED with interrupt method
// XTAL frequency 11.0592MHz
#include<reg51.h>
sbit LED = P1^0;
// LED connected to 1st pin of
port P1
void Timer(void) interrupt 1
// Interrupt No.1 for Timer
0
{
led=~led;
// Toggle LED on interrupt
}
main()
{
TMOD = 0x01;
TH0=0x00;
TL0=0x00;
IE = 0x82;
TR0=1;
while(1);
}
// Mode1 of Timer0
// Initial values loaded to Timer
// Enable interrupt
// Start Timer
// Do nothing
// Mode1 of Timer0
// Initial value loaded to Timer
// Start Timer
// Polling for flag bit
// Stop Timer
// Clear flag
}
(iv) Mode 3 : Split Timer
In mode 3, also known as split mode, the Timer breaks into two 8-bit
Timers that can count from 00H up to FFH. The initial values are loaded
into the higher byte and lower byte of the Timer. In this case the start
and flag bits associated with the other Timer are now associated with
high byte Timer. So one cannot start or stop the other Timer. The other
Timer can be used in modes 0, 1 or 2 and is updated automatically for
every machine cycle.
For example, if Timer0 is used in split mode, TH0 and TL0 will become
separate Timers. The start and flag bits associated with Timer1 will now
be associated with the TH0. Timer 1 cannot be stopped or started, but
will be updated for every machine cycle.
Split mode is useful when two Timers are required in addition to a baud
rate generator.
INTERRUPTS
Interrupt is one of the most important and powerful concepts and
features in microcontroller/processor applications. Almost all the real
world and real time systems built around microcontrollers and
microprocessors make use of interrupts.
What is Interrupt
Pin
9
12
13
-
Flag clearing
Auto
Auto
Auto
Auto
Auto
Programmer clears it
To enable any of the interrupts, first the EA bit must be set to 1. After
that the bits corresponding to the desired interrupts are enabled. ET0,
ET1 and ET2 bits are used to enable the Timer Interrupts 0, 1 and 2,
respectively. In AT89C51, there are only two timers, so ET2 is not used.
EX0 and EX1 are used to enable the external interrupts 0 and 1. ES is
used for serial interrupt.
EA bit acts as a lock bit. If any of the interrupt bits are enabled but
EA bit is not set, the interrupt will not function. By default all the
interrupts are in disabled mode.
Note that the IE register is bit addressable and individual interrupt bits
can also be accessed.
For example
IE = 0x81; enables External Interrupt0 (EX0)
IE = 0x88; enables Serial Interrupt
Setting the bits of IE register is necessary and sufficient to enable the
interrupts. Next step is to specify the controller what to do when an
interrupt occurs. This is done by writing a subroutine or function for the
interrupt. This is the ISR and gets automatically called when an interrupt
occurs. It is not required to call the Interrupt Subroutine explicitly in the
code.
An important thing is that the definition of a subroutine must have the
keyword interrupt followed by the interrupt number. A subroutine for a
particular interrupt is identified by this number. These subroutine
numbers corresponding to different interrupts are tabulated below.
Number
0
1
2
3
4
5
Interrupt
External0
Timer0
External1
Timer1
Serial
Timer2
Symbol
EX0
IT0
EX1
IT1
ES
ET2
<Body of ISR>
}
Note that the interrupt subroutines always have void return type. They
never return a value.
1. Programming Timer Interrupts
The timer interrupts IT0 and IT1 are related to Timers 0 and 1,
respectively. (Please refer 8051 Timers for details on Timer registers and
modes.) The interrupt programming for timers involves following steps :
1.
Configure TMOD register to select timer(s) and its/their
mode.
2.
Load initial values in THx and TLx for mode 0 and 1; or in
THx only for mode 2.
3.
Enable Timer Interrupt by configuring bits of IE register.
4.
Start timer by setting timer run bit TRx.
5.
Write subroutine for Timer Interrupt. The interrupt number
is 1 for Timer0 and 3 for Timer1.
Note that it is not required to clear timer flag TFx.
6.
To stop the timer, clear TRx in the end of subroutine.
Otherwise it will restart from 0000H in case of modes 0 or 1 and from
initial values in case of mode 2.
7.
If the Timer has to run again and again, it is required to
reload initial values within the routine itself (in case of mode 0 and 1).
Otherwise after one cycle timer will start counting from 0000H.
Example code :
Timer interrupt to blink an LED; Time delay in mode1 using interrupt
method
// Use of Timer mode0 for blinking LED using interrupt method
// XTAL frequency 11.0592MHz
#include<reg51.h>
sbit LED = P1^0;
//LED connected to D0 of port 1
Setting the IT0 and IT1 bits make the external interrupt 0 and 1 edge
triggered respectively. By default these bits are cleared and so external
interrupt is level triggered.
Note : For a level trigger interrupt, the INTx pin must remain low until
the start of the ISR and should return to high before the end of ISR. If
the low at INTx pin goes high before the start of ISR, interrupt will not
be generated. Also if the INTx pin remains low even after the end of
ISR, the interrupt will be generated once again. This is the reason why
level trigger interrupt (low) at INTx pin must be four machine cycles
long and not greater than or smaller than this.
Following are the steps for using external interrupt :
1.
Enable external interrupt by configuring IE register.
2.
Write routine for external interrupt. The interrupt number is 0 for
EX0 and 2 for EX1 respectively.
Example code
//Level trigger external interrupt
void main()
{
IE = 0x81;
while(1);
}
void ISR_ex0(void) interrupt 0
{
<body of interrupt>
}
Example code
//Edge trigger external interrupt
void main()
{
IE = 0x84;
IT1 = 1;
while(1);
}
void ISR_ex1(void) interrupt 2
{
<body of interrupt>
}
TI = 0;
}
else
RI = 0;
}
Example code
// Receive data from serial port through interrupt
// XTAL frequency 11.0592MHz
void main()
{
TMOD = 0x20;
TH1 = -1;
SCON = 0x50;
TR1 = 1;
IE = 0x90;
while(1);
}
void ISR_sc(void) interrupt 4
{
unsigned char val;
if(TI==1)
{
TI = 0;
}
else
{
val = SBUF;
RI = 0;
}
}
First two MSBs are reserved. The remaining bits are the priority bits for
the available interrupts.
Bit
PX0
PT0
PX1
PT1
PS
PT2
Interrupt
External0
Timer0
External1
Timer1
Serial
Timer2
Symbol
EX0
ET0
EX1
ET1
ES
ET2
IP = 0x08
Changed Priority
ET1
EX0
EX1
ET1
ES
ET2
ET0
EX1
ES
ET2
More than one bit in IP register can also be set. In such a case, the higher
priority interrupts will follow the sequence as they follow in default
case.
For example, IP = 0x0A; will make Timer0 and Timer1 priorities higher.
So the interrupt priority order will change as follows (in descending
order):
Default Priority
EX0
ET0
EX1
ET1
ES
ET2
IP = 0x0A
Changed Priority
ET0
ET1
EX0
EX1
ES
ET2
Note that the Timer 0 and 1 have been assigned higher priorities in the
same sequence as they follow in default case.
Example code
// Toggle LEDs by Timer and External Interrupt
#include<reg51.h>
sbit led_timercontrol = P1^0;
sbit led_externalcontrol = P1^0;
void external(void) interrupt 0
{
led_externalcontrol = ~ led_externalcontrol;
}
void timer(void) interrupt 1
{
led_timercontrol = ~ led_timercontrol;
}
main()
{
TMOD = 0x01;
TH0 = 0x00;
TL0 = 0x00;
IE = 0x83;
TR0 = 1;
while(1);
}
SERIAL
COMMUNICATION
Serial Communication is a form of I/O in which the bits of a byte being
transferred appear one after other in a timed sequence on a single wire.
Serial Communication uses two methods, asynchronous and
///////////////////////////////////////
Setting the Serial Port Baud Rate
Once the Serial Port Mode has been configured, as explained above, the program
must configure the serial ports baud rate. This only applies to Serial Port modes 1 and
3. The Baud Rate is determined based on the oscillators frequency when in mode 0
and 2. In mode 0, the baud rate is always the oscillator frequency divided by 12. This
means if youre crystal is 11.059Mhz, mode 0 baud rate will always be 921,583 baud.
In mode 2 the baud rate is always the oscillator frequency divided by 64, so a
11.059Mhz crystal speed will yield a baud rate of 172,797.
In modes 1 and 3, the baud rate is determined by how frequently timer 1 overflows.
The more frequently timer 1 overflows, the higher the baud rate. There are many
ways one can cause timer 1 to overflow at a rate that determines a baud rate, but the
most common method is to put timer 1 in 8-bit auto-reload mode (timer mode 2) and
set a reload value (TH1) that causes Timer 1 to overflow at a frequency appropriate to
generate a baud rate.
To determine the value that must be placed in TH1 to generate a given baud rate, we
may use the following equation (assuming PCON.7 is clear).
TH1 = 256 - ((Crystal / 384) / Baud)
If PCON.7 is set then the baud rate is effectively doubled, thus the equation becomes:
TH1 = 256 - ((Crystal / 192) / Baud)
For example, if we have an 11.059Mhz crystal and we want to configure the serial
port to 19,200 baud we try plugging it in the first equation:
TH1 = 256 - ((Crystal / 384) / Baud)
TH1 = 256 - ((11059000 / 384) / 19200 )
TH1 = 256 - ((28,799) / 19200)
TH1 = 256 - 1.5 = 254.5
As you can see, to obtain 19,200 baud on a 11.059Mhz crystal wed have to set TH1 to
254.5. If we set it to 254 we will have achieved 14,400 baud and if we set it to 255 we
will have achieved 28,800 baud. Thus were stuck...
But not quite... to achieve 19,200 baud we simply need to set PCON.7 (SMOD).
When we do this we double the baud rate and utilize the second equation mentioned
above. Thus we have:
TH1 = 256 - ((Crystal / 192) / Baud)
TH1 = 256 - ((11059000 / 192) / 19200)
TH1 = 256 - ((57699) / 19200)
TH1 = 256 - 3 = 253
Here we are able to calculate a nice, even TH1 value. Therefore, to obtain 19,200
baud with an 11.059MHz crystal we must:
1. Configure Serial Port mode 1 or 3.
2. Configure Timer 1 to timer mode 2 (8-bit auto-reload).
3. Set TH1 to 253 to reflect the correct frequency for 19,200 baud.
4. Set PCON.7 (SMOD) to double the baud rate.
/
**********************************************************
**********
>Example program to send some characters serially at 9600 bps.
>Make all connection according to the schematic given above.
>Serial port Mode 1 is used with 8bit data, 1 stop bit, 1 start bit
>One important thing is that all calulations for baud rate generation
using Timer1 are made for Timer1 8 bit auto reload mode
**********************************************************
**********/
#include>at89x52.h<
void main(void)
{
/*********************************************************
***********
>Example program to send a string serially at 9600 bps.
>Make all connection according to the schematic given above.
>Serial port Mode 1 is used with 8bit data, 1 stop bit, 1 start bit
>One important thing is that all calulations for baud rate generation
using Timer1 are made for Timer1 8 bit auto reload mode
**********************************************************
**********/
#include>at89x52.h<
void serial(unsigned char x)
{
SBUF=x;
while(TI==0);
TI=0;
}
void rs_puts(char *aaa)
{
unsigned int i;
for(i=0;aaa[i]!=0;i++)
{
serial(aaa[i]);
}
}
void main(void)
{
TMOD=0x20; // Timer1 Mode2 8 bit auto reload
TH1=0xFD; // 9600 bps
SCON=0x50; // 8 Data bit, 1 start bit, 1 stop bit
TR1=1; // Timer1 ON
while(1==1)
{
rs_puts("www.electrroons.com\n\r");
}
}
Now its your turn to make a program to receive data from UART and
show it on LCD. Poll RI (Receive Flag) and as RI goes 1; retrive your
data from serial buffer, and call lcd_data(c=SBUF); .
LCD
Interfacing 162 LCD with 8051
In this discussion we are going to see how to interface 162 LCD
module to AT89C51which is a 8051 family microcontroller. We use
LCD display for the messages for more interactive way to operate the
system or displaying error messages etc. interfacing LCD to
microcontroller is very easy if you understanding the working of LCD,
in this session I will not only give the information of LCD and also
provide the code in C language which is working fine without any
errors.
Also get an idea about Interfacing LEDs with 8051 Mirocontroller
Interfacing 162 LCD with 8051 Circuit Diagram:
PI
N
NO
NAME
VSS
Ground pin
VCC
VEE
RS
RS is the register select pin used to write display data to the LCD (characters), this pin has
to be high when writing the data to the LCD. During the initializing sequence and other
commands this pin should low.
R/W
Reading and writing data to the LCD for reading the data R/W pin should be high (R/W=1)
to write the data to LCD R/W pin should be low (R/W=0)
Enable pin is for starting or enabling the module. A high to low pulse of about 450ns pulse
is given to this pin.
DB0
DB1
DB2
10
DB3
11
DB4
12
DB5
13
DB6
14
DB7
15
LED+
16
LED-
FUNCTION
DB0-DB7 Data pins for giving data(normal data like numbers characters or command
data) which is meant to be displayed
So by reading the above table you can get a brief idea how to display a
character. For displaying a character you should enable the enable pin
(pin 6) by giving a pulse of 450ns, after enabling the pin6 you should
select the register select pin (pin4) in write mode. To select the register
select pin in write mode you have to make this pin high (RS=1), after
selecting the register select you have to configure the R/W to write mode
that is R/W should be low (R/W=0).
Follow these simple steps for displaying a character or data
Function
0F
Return home.
Decrement cursor
Increment cursor
80
C0
38
83
Command
Function
3C
0C3
0C1
To get the detailed information, Click Here and Download the Datasheet
Circuit Explanation:
The crystal oscillator is connected to XTAL1 and XTAL2 which will
provide the system clock to the microcontroller the data pins and
remaining pins are connected to the microcontroller as shown in the
circuit. The potentiometer is used to adjust the contrast of the LCD. You
can connect data pins to any port. If you are connecting to port0 then
you have to use pull up registers. The enable, R/W and RS pins are
should be connected to the 10, 11 and 16 (P3.3, P3.4 and P3.5).
Programming LCD to 8051:
Coming to the programming you should follow these steps:
The program given below will use above functions and display the
complete string which is given by the programmer to display the data.
You have provided two demo codes working properly and easy to
understand.
#include <REGX51.H>
#include <string.h>
#include <stdio.h>
sfr LCD=0xa0;
sbit EN=P3^6;
sbit RS=P3^7;
voidnop(void);
void delay_1s(unsigned char t);
voidinitial_lcd(void);
void delay(void);
voidstring_to_lcd(unsigned char *s);
voidwrite_lcd(unsigned char dat,unsignedint com);
void delay_50ms(unsigned char x);
void main()
{
P0=0xff;
P1=0xff;
P3=0xff;
delay_50ms(4);
initial_lcd();
write_lcd(0x80,0);
string_to_lcd( WELCOME TO );
write_lcd(0xc0,0);
string_to_lcd(INNOVATE ENG SOL);
}
voidnop(void)
{
unsigned char n;
for(n=0;n<20;n++);
}
/delay routine..//
void delay_1s(unsigned char t)
{
unsigned char i,j;
for(i=0;i<t;i++)
{
for(j=0;j<20;j++)
{
TMOD=0x01;
TH0=0x3c;
//for 12MHz (12/12MHZ)=1u>per cycle operation
TL0=0xb0;
//50ms delay get (50m/1u)=50000;
TR0=1;
//Load value is =65536-50000=15536(=3cb0H)
while(TF0!=1); //wait for overflow flag
TF0=0;
}
}
}
voidinitial_lcd(void)
{
write_lcd(0x38,0);
write_lcd(0x0c,0);
write_lcd(0x01,0);
}
voidwrite_lcd(unsigned char dat,unsignedint com)
{
RS=com;
LCD=dat;nop();
EN=1;nop();
EN=0;
nop();
}
voidstring_to_lcd(unsigned char *s)
{
for(i=0;i<msec;i++)
for(j=0;j<1275;j++);
}
voidlcdcmd(unsigned char item) //Function to send command to LCD
{
dataport = item;
rs= 0;
rw=0;
e=1;
delay(1);
e=0;
}
voidlcddata(unsigned char item) //Function to send data to LCD
{
dataport = item;
rs= 1;
rw=0;
e=1;
delay(1);
e=0;
}
void main()
{
lcdcmd(0x38); //for using 8-bit 2 row mode of LCD
delay(100);
lcdcmd(0x0E); //turn display ON for cursor blinking
delay(100);
lcdcmd(0x01); //clear screen
delay(100);
lcdcmd(0x06); //display ON
delay(100);
lcdcmd(0x86); //bring cursor to position 6 of line 1
delay(100);
lcddata(A);
}
KEYPAD
NXPs 8051 (AT89V51RD2), 8051 Slicker Kit is proposed to smooth
the progress of developing and debugging of various designs
encompassing of speed 8-bit Microcontrollers.
Keypad
A keypad is a set of buttons arranged in a block or "pad" which usually
bear digits, symbols and usually a complete set of alphabetical letters. If
it mostly contains numbers then it can also be called a numeric keypad.
Here we are using 4 X 4 matrix keypad.
Interfacing keypad
Fig. 1 shows how to interface the 4 X 4 matrix keypad to two ports in
microcontroller. The rows are connected to an output port and the
columns are connected to an input port.
To detect a pressed key, the microcontroller grounds all rows by
providing 0 to the output latch, and then it reads the columns. If the data
read from the columns is D3-D0=1111, no key has been pressed and the
process continues until a key press is detected. However, if one of the
column bits has a zero, this means that a key press has occurred. For
example, if D3-D0=1101, this means that a key in the D1 column has
been pressed.
After a key press is detected, the microcontroller will go through the
process of identifying the key. Starting with the top row, the
microcontroller grounds it by providing a low to row D0 only; then it
reads the columns.
If the data read is all 1s, no key in that row is activated and the process is
moved to the next row. It grounds the next row, reads the columns, and
checks for any zero. This process continues until the row is identified.
After identification of the row in which the key has been pressed, the
next task is to find out which column the pressed key belongs to.
4x4
Lines
ROW
COLUM
N
Matrix
8051 Lines
ROW-0
P2.0
ROW-1
P2.1
ROW-2
P2.2
ROW-4
P2.3
COLUMN-0
P2.4
COLUMN-1
P2.5
COLUMN-2
P2.6
COLUMN-3
P2.7
Source Code
The Interfacing keypad with 8051 program is very simple and straight
forward that scan akeypad rows and columns. When the rows and
columns are detected then it will display in PC through UART0. The C
programs are developed in Keil software.
C Program to 4 X 4 matrix keypad using 8051
**********************************************************
Title : Program to keypad interfacing
**********************************************************
#include <stdio.h>
Functions
//Define I/O
#include <reg51.h>
Registers
//Define 8051
#define DATA P1
Port1
//Define DATA to
void lcd_init(void);
//LCD Initialization
void Key_Scan(void);
//KeyScan Function
void DelayMs(int);
//DelayMs Function
sbit RS
//Register Select
= P3^5;
sbit RW = P3^6;
//LCD Read/Write
//LCD Enable
{'C','D','E','F',
'8','9','A','B',
//Matrix Keypad
//Initialization
'4','5','6','7',
'0','1','2','3',};
//Display
//in LCD
//------------------------------//
Main Program
//------------------------------void main()
{
lcd_init();
DelayMs(20);
lcd_cmd(0x01);
lcd_e=0;
lcd_cmd(0x80);
while(1)
{
Key_Scan();
lcd_e=1;
DelayMs(5);
ch = Key[R][C];
//Assign Key
DelayMs(5);
//and Row
value to ch;
Value of Keypad
lcd_display(ch);
lcd_cmd(d++);
DelayMs(10);
P2=0xFF;
}
}
//------------------------------//
//------------------------------void Key_Scan(void)
{
unsigned int i = 0;
//Scanning for Row Value
P2 = 0x0F;
while(P2 == 0x0F);
if(P2 == 0x0E)
R = 0;
else if(P2 == 0x0D)
R = 1;
else if(P2 == 0x0B)
R = 2;
else if(P2 == 0x07)
R = 3;
P2 = 0xF0;
while(P2 == 0xF0);
if(P2 == 0xE0)
C = 0;
else if(P2 == 0xD0)
C = 1;
else if(P2 == 0xB0)
C = 2;
else if(P2 == 0x70)
C = 3;
DelayMs(20);
}
//------------------------------//
//LCD Enable
DATA = dat;
RS = 1;
//RS:Register Select
RW = 0;
//RW:Read/Write
lcd_e = 1;
DelayMs(15);
lcd_e = 0;
}
//------------------------------//
LCD Initialization
//------------------------------void lcd_init(void)
{
unsigned char i;
lcd_cmd(0x38);
dot
DelayMs(15);
lcd_cmd(0x0c);
DelayMs(15);
lcd_cmd(0x06);
DelayMs(15);
lcd_cmd(0x01);
DelayMs(15);
//-----------------------------------//
//-----------------------------------lcd_cmd(0x80);
DelayMs(15);
i=0;
while(msg[i]!='\0')
{
lcd_display(msg[i]);
i++;
}
DelayMs(20);
//-----------------------------------// Second Line Message Display
//-----------------------------------lcd_cmd(0xc0);
DelayMs(15);
i=0;
while(msg1[i]!='\0')
{
lcd_display(msg1[i]);
i++;
}
DelayMs(30);
}
//-------------------------//
DelayMs Function
//-------------------------void DelayMs(int k)
{
unsigned int a,b;
for(a=0;a<=k;a++)
for(b=0;b<1275;b++);
}
ADC
Introduction
is for output enable (READ). The EOC and OE are the same as the
INTR and RD pins respectively.
Features of ADC 0808
1.
Inbuilt 8 analog channels with multiplexer
2.
Zero or full scale adjustment is not required
3.
0 to 5V input voltage range with a single polarity 5V supply
4.
Output is TTL compatible
5.
High speed
6.
Low conversion time (100micro second)
7.
High accuracy
8.
8-bit resolution
9.
Low power consumption (less than 15mW)
10.
Easy to interface with all microprocessor
11.
Minimum temperature dependence
Pin Configuration of ADC 0808
Description
Clock - External clock pulses are to be given to the ADC; this can
be given either from LM 555 in Astable mode or the controller can also
be used to give the pulses.
D0 D7 - D0- D7 are the digital data output pins since ADC0808
is a parallel ADC chip. These are tri state buffered and the converted
data is accessed only when CS=0 and RD is forced low. To calculate the
output voltage, we can use following formula
Dout = Vin / step size
Where Dout = digital data output, Vin = analog input voltage, and step
size is the smallest input change.
ALGORITHM :
1.
2.
Start.
Select the channel.
3.
A Low High transition on ALE to latch in the address.
4.
A Low High transition on Start to reset the ADCs SAR.
5.
A High Low transition on ALE.
6.
A High Low transition on start to start the conversion.
7.
Wait for End of cycle (EOC) pin to become high.
8.
Make Output Enable pin High.
9.
Take Data from the ADCs output
10. Make Output Enable pin Low.
11. Stop
The total numbers of lines required are:
data lines: 8
ALE: 1
START: 1
EOC:1
Output Enable:1
I.e. total 12 lines. You can directly connect the OE pin to Vcc. Moreover
instead of polling for EOC just put some delay so instead of 12 lines you
will require 10 lines.
You can also provide the clock through the controller thus eliminating
the need of external circuit for clock.
Calculating Step Size
ADC 0808 is an 8 bit ADC i.e. it divides the voltage applied at Vref+ &
Vref- into 28 i.e. 256 steps.
Step Size = (Vref+ -Vref-)/256
Suppose Vref+ is connected to Vcc i.e. 5V & Vref- is connected to the
Gnd then the step size will be
Step size= (5 - 0)/256= 19.53 mV.
Calculating Dout
The data we get at the D0 - D7 depends upon the step size & the Input
voltage i.e. Vin.
Dout = Vin /step Size.
If you want to interface sensors like LM35 which has output 10mv/C
then I would suggest that you set the Vref+ to 2.56v so that the step size
will be
Step size= (2.56 - 0)/256= 10 mV.
So now whatever reading that you get from the ADC will be equal to the
actual temperature.
Interfacing
channel
8-bit
ADC
to
8051
OR
In lot of embedded systems microcontrollers needs to take analog input.
Most of the sensors & transducers such as temperature, humidity,
pressure, are analog. For interfacing these sensors to microcontrollers we
require to convert the analog output of these sensors to digital so that the
controller can read it. Some microcontrollers have built in Analog to
Digital Convertor (ADC) so there is no need of external ADC. For
microcontrollers that dont have internal ADC external ADC is used.
One of the most commonly used ADC is ADC0808. ADC 0808 is a
Successive approximation type with 8 channels i.e. it can directly access
8 single ended analog signals.
I/O Pins
ADDRESS LINE A, B, C
PROGRAM
bit
bit
bit
bit
bit
bit
p2.0
p2.1
p2.2
p2.3
p2.4
P2.5
;Select Channel 0
;ale pin high
;start pin high
;ale pin low
;start pin low
nop
nop
nop
nop
djnz r0,l1_delay_small
ret
delay_long:
mov r0,#40
l1_delay_long:
cpl adc_clk
nop
nop
nop
nop
nop
djnz r0,l1_delay_long
ret
End