Upload
sam
View
230
Download
0
Embed Size (px)
Citation preview
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 1/47
Microcontroller And InterfacingMicrocontroller And Interfacing
KantipurKantipur Engineering CollegeEngineering Collegeamra su e ec.e u.npamra su e ec.e u.np
11Samrat SubediSamrat Subedi
[email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 2/47
Contents:Contents: Introductory ConceptIntroductory Concept
-- Introduction to Embedded SystemIntroduction to Embedded System
-- ,,
-- Introduction to Top View SimulatorIntroduction to Top View Simulator
Basic inputs and outputsBasic inputs and outputs
Interfacing Seven segment DisplayInterfacing Seven segment Display
Keypad InterfacingKeypad Interfacing as cs o an ts nter ac ngas cs o an ts nter ac ng
Timer and CounterTimer and Counter
Interrupt HandlingInterrupt Handling
Introduction to ProteusIntroduction to Proteus
22Samrat SubediSamrat Subedi
[email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 3/47
Wh do we need to learnWh do we need to learnMicroprocessors/controllers?Microprocessors/controllers?
The microprocessor is the core of
Nowadays many communication, digital
controlled by them.
components he needs, ways to reduceproduction costs and product reliable..
33Samrat SubediSamrat Subedi
[email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 4/47
The necessary tools for aThe necessary tools for amicroprocessor/controllermicroprocessor/controller
I/O: Input /Output
Memory: RAM & ROM Timer
Interrupt
Serial Port
Parallel Port
44Samrat SubediSamrat Subedi
[email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 5/47
Microprocessors:Microprocessors:
CPU for Com uters
General-purpose microprocessor
No RAM, ROM, I/O on CPU chip itself Example:Intel’s x86, Motorola’s 680x0
CPU Data BusMany chips on mother’s board
General-
Purpose
Micro-RAM ROM I/O
PortTimer
Serial
COM
processor
Address Bus
enera - urpose croprocessor ystem
55Samrat SubediSamrat Subedi
[email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 6/47
Microcontroller :
A smaller computer
On-chip RAM, ROM, I/O ports...
’ ’ ’, ,
RAM ROMCPU
A single chip
I/O
PortTimer
SerialCOM
PortMicrocontroller
66Samrat SubediSamrat Subedi
[email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 7/47
.
croprocessor
CPU is stand-alone, RAM,
ROM, I/O, timer are separate
crocon ro er
• CPU, RAM, ROM, I/O and
timer are all on a single chip
designer can decide on the
amount of ROM, RAM and
I/O orts.
• fix amount of on-chip ROM,
RAM, I/O ports
•
expansive
versatility
,
power and space are critical
• single-purpose
general-purpose
77Samrat SubediSamrat Subedi
[email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 8/47
application. An embedded product uses a microprocessor or microcontroller to do
one as on y.
In an embedded system, there is only one application software that is
typically burned into ROM.
Example:printer, keyboard, video game player
88Samrat SubediSamrat Subedi
[email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 9/47
Three criteria in Choosin a Microcontroller
.
effectively• speed, the amount of ROM and RAM, the number of I/O ports
an mers, s ze, pac ag ng, power consump on
• easy to upgrade
• cost er unit
2. availability of software development tools
• assemblers, debuggers, C compilers, emulator, simulator,
3. wide availability and reliable sources of the microcontrollers.
99Samrat SubediSamrat Subedi
[email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 10/47
External interru ts
On-chip
RAM
On-chip
ROM for
programInterrupt
Control
Timer 1
Timer/Counter
Counter
In uts
CPU
4 I/O PortsSerial
PortOSCBus
Control
TxD RxDP0 P1 P2 P3
ress a a
1010Samrat SubediSamrat Subedi
[email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 11/47
1111Samrat SubediSamrat Subedi
[email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 12/47
Pin Description of the 8051Pin Description of the 8051
12
4039
P1.0P1.1
VccP0.0(AD0)
3
456
38
373635
P1.2
P1.3P1.4P1.5
P0.1(AD1)
P0.2(AD2)P0.3(AD3)P0.4(AD4)
8051
89
1011
3332
3130
.P1.7RST
(RXD)P3.0(TXD)P3.1
.P0.6(AD6)P0.7(AD7)
EA/VPPALE/PROG12131415
29282726
(T0)P3.4(T1)P3.5
(INT0)P3.2(INT1)P3.3
PSENP2.7(A15)P2.6(A14)P2.5(A13)
17181920
24232221
XTAL2XTAL1
GND
(RD)P3.7. .
P2.3(A11)P2.2(A10)P2.1(A9)P2.0(A8)
1212Samrat SubediSamrat Subedi
[email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 13/47
Feature of 8051Feature of 8051
Eight bit CPU with register A and B
t rogram ounter
Internal Ram of 128 bytes
4 Kb Flash Memory
4 I/O Ports
Two 16 bit Timer (T0 and T1)
Six Interrupts
… 1313
Samrat SubediSamrat [email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 14/47
Pins of 8051Pins of 8051(1/41/4))
– Vcc provides supply voltage to the chip.
– The voltage source is +5V.
GND(pin 20):ground XTAL1 and XTAL2(pins 19,18)
1414Samrat SubediSamrat Subedi
[email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 15/47
Fi ur XTAL nn i n 51Fi ur XTAL nn i n 51
Using a quartz crystal oscillator
C2
We can observe the frequency on the XTAL2 pin.
30pF
XTAL2
1
30pF
XTAL1
GND
1515Samrat SubediSamrat Subedi
[email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 16/47
( ) ( )
( ): ):
– It is an input pin and is active high(normally
).
The high pulse must be high at least 2 machine cycles.
– -
Upon applying a high pulse to RST, the
microcontroller will reset and all values in registers
will be lost.
1616Samrat SubediSamrat Subedi
[email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 17/47
--..Vcc
10 uF
+
30 pF
11.0592 MHz
EA/VPPX1
19
30 pF
.X2
RST
18
1717Samrat SubediSamrat [email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 18/47
( ) ( )
p n ex erna access
– There is no on-chip ROM in 8031 and 8032 .
–
stored externally.
– /PSEN& ALE are used for external ROM.– For 8051, /EA pin is connected to Vcc.
– “/” means active low.
/PSEN(pin 29):program store enable– This is an output pin and is connected to the OE pin of the
.
1818Samrat SubediSamrat [email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 19/47
( ) ( )
– It is an output pin and is active high.– 8051 ort 0 rovides both address and data.
– The ALE pin is used for de-multiplexing the address
and data by connecting to the G pin of the 74LS373atc .
I/O port pins
– , , , .– Each port uses 8 pins.
– - ..
1919Samrat SubediSamrat [email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 20/47
The 8051 has four I/O orts
– Port 0(pins 32-39):P0(P0_0~P0_7)– Port 1(pins 1-8) :P1(P1_0~P1_7)
– Port 2 pins 21-28 :P2 P2_0~P2_7
– Port 3(pins 10-17):P3(P3_0~P3_7)
–
Named P0_X(X=0,1,...,7), P1_X, P2_X, P3_X
Ex:P0_0 is the bit 0(LSB)of P0
Ex:P0_7 is the bit 7 MSB of P0 These 8 bits form a byte.
- .⌦
2020Samrat SubediSamrat [email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 21/47
Each in of I/O orts
– Internal CPU bus:communicate with CPU– A D latch store the value of this pin
a c s con ro e y r e o a c
– Write to latch=1:write data into the D latch
– 2 Tri-state buffer: TB1: controlled by “Read pin”
– Read pin=1:really read the data present at the pin
TB2: controlled b “Read latch”
– Read latch=1:read value from internal latch
– A transistor M1 gate
a e= : open
Gate=1: close2121Samrat SubediSamrat [email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 22/47
D LatchD Latch::
2222Samrat SubediSamrat [email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 23/47
Vcc
Load(L1)
Read latchTB2
D QInternal CPU
bus
P1.X
pinP1.X
Clk QWrite to latch M1
Read pin
TB1
⌦P0.x
8051 IC2323Samrat SubediSamrat [email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 24/47
“ ”“ ” ..
Vcc
Load(L1)
Read latch
2. output pin isTB2
D QInternal CPU
bus
P1.X
pinP1.X
1. write a 1 to the pin1
Clk QWrite to latch M10 ou pu
Read pin
TB1
8051 IC2424Samrat SubediSamrat [email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 25/47
“ ”“ ” ..
Vcc
Load(L1)
Read latch
2. output pin isTB2
D QInternal CPU
bus
P1.X
pinP1.X
1. write a 0 to the pin0
Clk QWrite to latch M11 ou pu
Read pin
TB1
8051 IC2525Samrat SubediSamrat [email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 26/47
“ ”“ ”
Vcc
Load(L1)
Read latch 2. MOV A,P1
external pin=High1. write a 1 to the pin MOV
P1,#0FFH
TB2
D QInternal CPU bus P1.X pin
P1.X
1 1
Clk QWrite to latch M10
Read pin
3. Read in=1 Read latch=0
TB1
8051 ICWrite to latch=1
2626Samrat SubediSamrat [email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 27/47
“ ”“ ”
Vcc
Load(L1)
Read latch
2. MOV A,P1external pin=Low1. write a 1 to the pin
TB2
D QInternal CPU bus P1.X pin
P1.X
,
1 0
Clk QWrite to latch M10
Read pin
3. Read in=1 Read latch=0
TB1
8051 ICWrite to latch=1
2727Samrat SubediSamrat [email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 28/47
-, , .
– P1, P2, and P3 are not open drain. -
connects to Vcc inside the 8051.
– P0 is open drain.– Compare the figures of P1.X and P0.X.
However, for a programmer, it is the same to program
P0, P1, P2 and P3. All the ports upon RESET are configured as output.
2828Samrat SubediSamrat [email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 29/47
Read latch
TB2
D QInternal CPU
bus
P0.X
pinP1.X
Clk QWrite to latch M1
Read pin
TB1
P1.x
8051 IC2929Samrat SubediSamrat [email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 30/47
Port 0 with PullPort 0 with Pull--U ResistorsU Resistors
10 K
P0.0P0.1
P0.2
DS5000P o
r t .
P0.4P0.5P0.6
8951 0
.
3030Samrat SubediSamrat Subedi
[email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 31/47
Port 3 Alternate FunctionsPort 3 Alternate FunctionsPinPinFunctionFunctionP3 BitP3 Bit
1010RxDRxDPP33..00
1212INT0INT0P3.2P3.2
..
1515TT11P3.5P3.5
1414T0T0P3.4P3.4
..
1717RDRDP3.7P3.7
1616WRWRP3.6P3.6
3131Samrat SubediSamrat Subedi
[email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 32/47
0000PCReset ValueRegister
0000B
0000ACC
0007SP
0000PSW
0000DPTR
..⌦
3232Samrat SubediSamrat Subedi
[email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 33/47
Registers
A
B
R1
R2
DPH DPL
PC
DPTR
PC
R3
R4
R5
Some 8051 16-bit Register
R7
R6
Some 8-bitt Registers of the
8051
3333Samrat SubediSamrat Subedi
[email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 34/47
ROM memory map in 8051 family
0000H 0000H 0000H
1FFFH8751
AT89C518752
AT89C52
3434Samrat SubediSamrat Subedi
[email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 35/47
memory space a ocat on n t e 8051
Scratch pad RAM
30H
2FH
Bit-Addressable RAM
20H
1FH
17H
18HRegister Bank 3
10H0FH
07H
08H(Stack) Register Bank 1
eg s er an
00HRegister Ban 0
3535Samrat SubediSamrat Subedi
[email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 36/47
The re ister used to access
the stack is called SP (stack
pointer) register.
7FH
Scratch pad RAM
The stack pointer in the 8051is only 8 bits wide, which
means that it can take value
2FH
20H
Bit-Addressable RAM
00 to FFH. When 8051powered up, the SP registercontains value 07.
1FH
17H
10H
0FH
18H
Register Bank 2
Register Bank 3
07H
08H
00HRegister Bank 0
3636Samrat SubediSamrat Subedi
[email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 37/47
Writing Simple ProgramWriting Simple Program#include<at#include<at8989xx5151.h>.h>
void delay(void delay(intint a)a)
floatfloat ii;;
for (for (ii==00;i<;i<100100*a ;*a ;ii++ );++ );
void main()void main()
{{
while(while(11))
PP11_ _11=!P=!P11_ _11;;delay(delay(1010););
Save asSave as ---- c:c:\ \program filesprogram files\ \SDCCSDCC\ \bin>bin>led. hled. h3737
Samrat SubediSamrat [email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 38/47
..
:: program esprogram es n>n>pac xpac x e . x> e . exe . x> e . ex
you will Seeyou will See
packihxpackihx: read: read 107107 lines, wrotelines, wrote 5858: OK.: OK.
3838Samrat SubediSamrat Subedi
[email protected]@gmail.com
And thenAnd then
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 39/47
And thenAnd then
12000
3939Samrat SubediSamrat Subedi
[email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 40/47
4040Samrat SubediSamrat Subedi
[email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 41/47
4141Samrat SubediSamrat Subedi
[email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 42/47
4242
Samrat SubediSamrat Subedi
[email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 43/47
4343
Samrat SubediSamrat Subedi
[email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 44/47
4444
Samrat SubediSamrat Subedi
[email protected]@gmail.com
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 45/47
4545
Samrat SubediSamrat Subedi
[email protected]@gmail.com
Try thisTry this
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 46/47
Try thisTry this..
void delay(void delay(intint a)a)
{{
for (for (ii==00;i<;i<100100*a ;*a ;ii++ );++ );
}}
{{
char in=char in=00xx0404;;
==
while(while(11))
{{
++++
delay(delay(2020););
PP00=in;=in;
}}
4646
Samrat SubediSamrat Subedi
[email protected]@gmail.com
LED ON I/O PORTSLED ON I/O PORTS
8/4/2019 Micro Controller Basics
http://slidepdf.com/reader/full/micro-controller-basics 47/47
LED ON I/O PORTSLED ON I/O PORTS'
above 1mA so the LED will be verydim.
he LED will conduct heavily at about 2Vand the extra 3V has to be dropped in the
.dissipation in the TTL or the LED fails.
.calculated by assuming its voltage is about 2.5V and theTTL output is 0.9V. For 2.2V LED, 1.9V is across the
.
(1.9/220). For 1.7V LED, 2.4V is across the resistor so itwould limit the current to 10.9mA (2.4/220). The resistorshould not less than 100ohm or the LED would fail.
4747
Samrat SubediSamrat Subedi
[email protected]@gmail.com