Beruflich Dokumente
Kultur Dokumente
PC based system
• An embedded product uses a • A PC based system can be used
microprocessor or for any number of applications
microcontroller to do one task such that word processor, print-
only. server, video game player,
network server; internet
• A printer is an example of terminal etc.
embedded system since the
processor inside it performs
one task only; namely getting
the data and printing it.
• Other examples:
stereo equipments, washing
machine, VCRs, micro-wave
oven, photocopiers, computer
peripherals (IBM PC Key board),
mobiles etc
Micro-Processor vs. Micro-Controller
Hardware architecture Hardware architecture
• micro-processor is a single- • a micro-controller contains in a
chip CPU, requires external single IC, a CPU and much of
ROM,RAM and other circuitry the remaining circuitry of a
complete microcomputer
system, like RAM, ROM, a serial
Applications
interface, a parallel interface,
• Microprocessors are most timers, interrupts circuitry
commonly used as the CPU in
Applications
microcomputer system (PC) in
• In a microcontroller based
order to process large
embedded system, there is only
information like word
one application software that is
processor, print-server, video
typically burned in to ROM.
game player, network server;
internet terminal etc.
Micro-Processor vs. Micro-Controller
Instruction set features Instruction set features
• • Micro-controllers on the other
Microprocessors instruction set
are “processing intensive”, hand, have instruction sets
implying they have powerful catering to the control of inputs
addressing modes with and outputs. The interface too
instructions catering to many inputs and outputs uses
operations on large volume of a single bit.
data. Their instructions operate • For example, a motor may be
on byte, words, double words, turn on and off by a solenoid
array of data, address pointers energized by a single-bit
and offsets etc output port.
Short birth history
8051, which was originated by Intel, several
companies also currently produce the 8051 micro-
controller. These companies include: Intel, Atmel,
Philips / Signetics, AMD, Siemens, Matra, and Dallas
semiconductor.
• In 1971 first 8080 μ-Processor introduce by INTEL
• In 1976 first 8048 μ-Controller introduce by INTEL
• In 1980 8051 μ-Controller introduce by INTEL
MCS-51 Family Overview
The generic MCS-51 IC is the 8051, the first device in
the family. Its features are summarized below.
• 4K bytes ROM (factory masked programmed)
• 128 bytes RAM
• Four 8-bit I/O (Input/Output) ports
• Two 16-bit Timers
• Serial interface
• 64K external code memory space
• 64K external data memory space
• Boolean processor (operates on single bits)
• 210 bit-addressable locations
• 4 μ-sec multiply/divide
Comparison of MCS-51 ICs
Port 0
• Port 0 is a dual-purpose port of the 8051 IC. It can be used
as a general purpose I/O port, as well as this port becomes
a multiplexed address and data bus when interfacing
external memory with the 8051 IC.
Port 1
• Port 1 is a dedicated I/O port of the 8051 IC. No alternate
function is available for 8051 device, but in case of 8052 /
89C52 IC’s, P1.0 and P1.1 either as I/O lines or as external
inputs to the third timer.
Port 2
• Port 2 is a dual-purpose port of the 8051 IC. It can be used as a general
purpose I/O port, as well as this port becomes a high byte of address
bus and when interfacing external memory with the 8051 IC.
BIT Name Alternate
Function
P3.0 RXD Receive data for
serial port
VCC
VCC
C2
R1 C1
SW1
RESET(Pin#9) RESET(Pin#9)
R2 R4
The states of all the 8051 registers after a system reset is as summarized
in below table
Registers Contents
Program counter 0000H
Accumulator ( ACC ) 00H
B register 00H
PSW 00H
SP (Stack Pointer) 00H
DPTR 0000H
PORT(0…..3) FFH
IP (8031/8051) XXX00000B
IP (8032/8052) XX000000B
IE (8031/8051) 0XX00000B
IE (8032/8052) 0X000000B
Timer register 00H
SCON (Serial control register) 00H
SBUF (serial buffer register) 00H
PCON (Power control register) 0XXX000B
External Access (EA)
Power Connections
• The 8051 operates from single +5 Volt supply. The VCC
connection is on pin 40, and the VSS or GND on 20.
64K bytes
128 bytes
(SFR)
7F
4K bytes
IRAM
128 bytes
Figure 1
Program Memory
74LS373
EA
D
0
Q A0..A7
ALE
G EPROM
PORT_2 A8....A15
PSEN OE
AD0....AD7 D0..D7
VCC 74LS373
EA
D
Q A0..A7
ALE G
RAM
1K Byte
P2.0 A8
P2.1
A9
NC
PSEN
CS
RD OE
WR WR 0
Q0 Q1 Q2 L o g ic 1 O v e rflo w F L A G
1 = set
D Q
D Q D Q D Q
C lo c k in
Q
Q Q Q C le a r
C lo c k in
Q0 0 1 0 1 0 1 0 1 0
Q1 0 0 1 1 0 0 1 1 0
Q2 0 0 0 0 1 1 1 1 0
O v e r flo w F la g
0 1 2 3 4 5 6 7 0
O v e rflo w
C o n d itio n
TIMMER OPERATION
• Since each successive stage divide-by-2, a timer with n stages
divides the input clock frequency by 2n.
• The 8051 has two internal 16- bit hardware Timer/Counters,
therefore, the 16th or last stage divides the input clock
frequency by 216 = 65,536
• Each Timer/Counter can be configured in various modes,
typically based on 8-bit or 16-bit operation.
• The 8052 product has an additional (third) Timer/Counter.
• The timer can be used as (a) interval timing, (b) event counting,
or (c) baud-rate generation for the built-in serial port.
Timer special function registers
TMOD Mode No
Event counting
• If C/T = 1, the timer is clocked from external source.
• Event counting is used to determine the number of
occurrences of an event.
• An “event” is any external stimulus that provides a 1-to-0
transition to pins(T0-P3.4 or T1-P3.5) on the 8051 IC.
Baud rate generation
• If C/T = 0, timer is clocked from the on-chip oscillator, can also
provide the baud rate clock for the 8051’s internal serial port.
Symbol Position Description
TF1 TCON.7 Timer 1 overflow flag. Set by hardware when the Timer/Counter 1
overflows. Cleared by hardware as the processor vectors to the
interrupt service routine.
TR1 TCON.6 Timer 1 run control bit. Set/cleared by software to turn
Timer/Counter 1 ON/OFF.
TF0 TCON.5 Timers 0 overflow flag. Set by hardware when the Timer/Counter
0 overflows. Cleared by hardware as the processor vectors to
the service routine.
TR0 TCON.4 Timer 0 run control bit. Set/cleared by software to turn
Timer/Counter 0 ON/OFF.
IE1 TCON.3 External Interrupt 1 edge flag. Set by hardware when the
External Interrupt edge is detected.
Cleared by hardware when the interrupt is processed.
IT1 TCON.2 Interrupt 1 type control bit. Set/cleared by software to specify
falling edge/low level triggered External Interrupt.
IE0 TCON.1 External Interrupt 0 edge flag. Set by hardware when External
Interrupt edge detected. Cleared by hardware when interrupt is
processed.
IT0 TCON.0 Interrupt 0 type control bit. Set/cleared by software to specify
falling edge/low level triggered External Interrupt.
Starting, stopping, and controlling
the timers
T im er
Tim er R egister
C lock
0 = U p (tim er stop p ed )
1 = D ow n (tim er start)
TR0 = 1;
TR0 = 0;
Step to program in mode 1 (16-bit timer)
To generate a time delay, using the timer’s mode 1, the
Following steps are taken,
1. It is the 16-bit timer mode; load the TMOD value register
indicating which timer (timer 0 or timer 1 or both) is to be used.
2. Load registers TL and TH with initial count values.
3. Start the timer.
4. Keep monitoring the timer flag (TF) with while(TF==0) if it is
raised. Take action, and get out of the loop when TF becomes
high.
5. Stop the timer.
6. Clear the TF flag for the next round.
7. Go back to step 2 to load TH and TL again.
• Example: write a program to create 1kHz square wave of 50% duty cycle on P1.0
crystal frequency is 12MHz
Solution:
Calculations: 1kHz=1000uSec
for 50% duty cycle: HT/ON=500uSec, LT/OFF=500uSec
Here Desired Time=500uSec
Timer Clock=cyrstal Freq/12 12Mhz/12= 1Mhz
Timer clock=1/12Mhz= 1uSec
Now divide the desired time delay by timer clock
n=desired time/timer clock=500uSec/1uSec=500
Perform 65536 -n we get 65536 -500=65036 (Timer reload value)
Convert this value in hex that is FE0C
Since the interval is greater than 256uSec so 16 bit timer is used.
So the values for THx=FEh and TLx=0Ch
#include <REGX51.H>
void main()
{
TMOD = 0x01; //timer0,mode1 16bit
while(1)
{
TH0 = 0xFE; //high value of Sq wave=500uSec
TL0 = 0x0C; //500=FE0Ch
TR0=1; //Start timer0
while(TF0!=1) // wait for TF0
{}
TR0=0; //Stop Timer0
TF0=0; //clear TF0
}
}
Step to program in mode 2 (8-bit timer)
To generate a time delay, using the timer’s mode 2, the following
steps are taken,
1. It is the 8-bit timer mode (auto-reload); load the TMOD value
register indicating which timer (timer 0 or timer 1 or both) is
to be used.
2. Load registers TH with initial count values.
3. Start the timer.
4. Keep monitoring the timer flag (TF) with while(TF==0) if it is
raised. Take action, and get out of the loop when TF
becomes high.
5. Clear the TF flag for the next round.
6. Go back to step 4.
Basics of serial comunication
• Serial communication standards
RS-485 RS-422 RS-232
Transmitter Receiver
Receiver Transmitter
Half Duplex
Transmitter Receiver
Transmission only
Transmitter Receiver Receiver Transmitter
simplex
Full Duplex
•Asynchronous Serial Communications
TXD
RXD
(P3.1)
(P3.2)
clk SBUF Q D
Shift register
(write only)
clk
Baud rate
Clock Baud rate
transmit Clock
receive SBUF
(Read only)
THE 8051 UART
One 1
________
Macine Baud - rate
Cycle
Data in
( RXD ) D0 D1 D2 D3 D4 D 5 D6 D 7 Stop
Bit
Shift
Clock
( TXD )
RI =1
SBUF is Full
Figure : Serial port receive timing for mode 0
One 1
________
Macine Baud - rate
Data out Cycle
( RXD )
D 0 D 1 D2 D 3 D4 D 5 D6 D 7 Stop
Bit
Shift
Clock
( TXD )
TI =1
SBUF is
Figure : Serial port Transmits timing for mode 0 Empty
• Mode 0 has a fixed baud rate, which is 1/12 of the oscillator
frequency. To run the serial port in this mode, none of the
Timer/Counters need to be set up. Only the SCON register
needs to be defined.
Mode 0 has a fixed baud rate, which is 1/12 of the oscillator frequency. To
run the serial port in this mode, none of the Timer/Counters need to be set
up. Only the SCON register needs to be defined.
Mode 1 has a variable baud rate. The baud rate can be generated by either
Timer 1 or Timer 2
Using Timer1 to Generate Baud Rate
• For this purpose, Timer 1 is used in mode 2(Auto-Reload).
#include <AT89X51.H>
void main (void)
{ unsigned cha MyByte;
TMOD=0x20;
TH1=0xFD; //baud rate 9600 crystal = 11.059Mhz
SCON=0x50; // mode1
TR1=1;
while(1)
{ RI=0;
while(RI==0)
MyByte = SBUF;
P0= MyByte ;
}
}
RS 232 (Serial Port)
IBM PC DB – 9 Signal
Pin# Description
1 Data Carrier Detect
TxD RxD
(DCD)
RxD TxD
2 Received Data (RxD)
Ground 3 Transmitted Data (TxD)
4 Data Terminal Ready
(DTR)
5 Signal grounding (GND)
6 Data Set Ready (DSR)
7 Request To Send (RTS)
8 Clear to Send (CTS)
9 Ring Indicator (RI)
Interrupt
• An interrupt is the occurrence of a condition; an
event that cause a temporary suspension of a
program whiles the condition is serviced by another-
program.
• When an interrupt occurs, the main program
temporarily suspends execution and branches to the
ISR.
• The program that deals with an interrupt is called an
interrupt service routine (ISR) or interrupt handler.
• There are five interrupt sources on the 8051; two
external interrupts, two timer interrupts, and a serial
port interrupt.
IE (Interrupt enable register)
-- IP.7 reserved
Ground all Wait for Wait for Wait for Wait for
Columns key key key key
denounce denounce denounce denounce
Wait for
key Read all Read all Read all Read all
denounce Rows Rows Rows Rows
Read all NO NO
NO
Rows
which key is which key is which key is which key is
pressed ? pressed ? pressed ? pressed ?
NO '1 ','4','7','A' ''2 ','5','8','0 ' '3 ','6 ','9 ','B' 'F','E','D','C '
All keys
open ? YES YES YES YES
VCC
10 nF
10 K 1 2 3 F
P2.4
10nF 4 5 6 E
VCC
INPUT
10K
P2.5 7 8 9 D
10nF
VCC
A 0 B C
10K
P2.6 10nF
VCC
10K
P2.7
P2.0 P2.1 P2.2 P2.3
OUTPUT
Basics of serial comunication