22
CONTENTS:CONTENTS:
IntroductionIntroductionBlock Diagram and Block Diagram and Pin Description of the 8051Pin Description of the 8051RegistersRegistersMemory mapping in 8051 Memory mapping in 8051 Stack in the 8051Stack in the 8051I/O Port ProgrammingI/O Port ProgrammingTimerTimer
33
1. meeting the computing needs of the task efficiently and cost effectively
• speed, the amount of ROM and RAM, the number of I/O ports and timers, size, packaging, power consumption
• easy to upgrade
• cost per unit
2. availability of software development tools
• assemblers, debuggers, C compilers, emulator, simulator, technical support
3. wide availability and reliable sources of the microcontrollers.
Introduction : Three criteria in Choosing a Microcontroller:
8 bit controllers – more 8 bit controllers – more requirementsrequirements
44
Embedded System :Embedded System :
55
Fall 2004Fall 2004 Embedded Systems
Development processDevelopment processPlanning of tasks & interactions
edit
taskcode1.A51 taskcode2.c
Assembler C-compiler
misc.LIB Linker/Locator
Burn in EPROM
Download to board
Revise !
Debugging ???
8051 Microcontroller Kit Layout8051 Microcontroller Kit Layout
77
General Purpose Microprocessor General Purpose Microprocessor v/s Microcontrollerv/s Microcontroller
88
Block Diagram : 8051 Block Diagram : 8051
99
1010
Block DiagramBlock Diagram
CPU
On-chip RAM
On-chip ROM for program code
4 I/O Ports
Timer 0
Serial PortOSC
Interrupt Control
External interrupts
Timer 1
Timer/Counter
Bus Control
TxD RxDP0 P1 P2 P3
Address/Data
Counter Inputs
PC Interface Lab.
Stepper motor etc. Lab
Delay Generation Labs
1111
ROMLike Brain
OscillatorLike Heart
Data flow Like BloodFlow
Ports likeHands/Legs
InternalDataBus
Prepares the sequence of operations
8051 Block Diagram8051 Block Diagram
1212
1313
1414
1515
ROMLike Brain
OscillatorLike Heart
Data flow Like BloodFlow
Ports likeHands/Legs
InternalDataBus
Prepares the sequence of operations
SignalSignalPins Pins
Figure
8051 pinouts & Functions
8051 Block Diagram
Fetch CycleFetch Cycle
1818
8051 Family State Sequence8051 Family State Sequence
1919
2020
8051 Architecture: Salient features8051 Architecture: Salient features 8 bit CPU with registers A and B8 bit CPU with registers A and B 16 bit Program Counter and data pointer DPTR16 bit Program Counter and data pointer DPTR 8 bit Program Status Word8 bit Program Status Word 8 bit Stack Pointer8 bit Stack Pointer Internal RAM 128 bytesInternal RAM 128 bytes
4 Registers banks, each containing 8 Registers4 Registers banks, each containing 8 Registers16 bytes, may be addressed at the bit level16 bytes, may be addressed at the bit level8 bytes of General Purpose memory8 bytes of General Purpose memory
Internal ROM 4KInternal ROM 4K 32 I/O pins as 4 eight bit ports: P0-P332 I/O pins as 4 eight bit ports: P0-P3 2 sixteen bit timer/counters: T0 and T12 sixteen bit timer/counters: T0 and T1 Full duplex serial data receiver/transmitter: SBUFFull duplex serial data receiver/transmitter: SBUF Control registers: TCON, TMOD, SCON, PCON, IP AND IEControl registers: TCON, TMOD, SCON, PCON, IP AND IE 2 external and 3 internal interrupt sources2 external and 3 internal interrupt sources Oscillator and clock circuits.Oscillator and clock circuits.
2121
Pin Description of the 8051Pin Description of the 8051
2222
8051 Hardware Connections : Crystal Connection to 80518051 Hardware Connections : Crystal Connection to 8051
C2
30pF
C1
30pF
XTAL2
XTAL1
GND
Using a quartz crystal oscillator ( Frequency 11.0592 MHz) We can observe the frequency on the XTAL2 pin.
Pin 18
Pin 20
Pin 19
2323
Pins of 8051Pins of 8051 Vcc ( pin 40 ):
– Vcc provides supply voltage to the chip.
– The voltage source is +5V. GND ( pin 20 ): ground XTAL1 and XTAL2 ( pins 19,18 ) RST ( pin 9 ): reset
– It is an input pin and is active high ( normally low ) . The high pulse must be high at least 2 machine cycles.
– It is a power-on reset. Upon applying a high pulse to RST, the microcontroller will
reset and all values in registers will be lost.
2424
Pins of 8051Pins of 8051
/EA ( pin 31 ): external access
– There is no on-chip ROM in 8031 and 8032 .
– The /EA pin is connected to GND to indicate the code is 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 ROM.
2525
Pins of 8051Pins of 8051
ALE ( pin 30 ): address latch enable– It is an output pin and is active high.– 8051 port 0 provides both address and data.– The ALE pin is used for de-multiplexing the address
and data by connecting to the G pin of the 74LS373 latch.
I/O port pins– The four ports P0, P1, P2, and P3.– Each port uses 8 pins.– All I/O pins are bi-directional..
2626
Pins of I/O PortPins of I/O Port
The 8051 has four I/O ports– 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 )– Each port has 8 pins.
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.
Each port can be used as input or output (bi-direction).
2727
Power-On RESET CircuitPower-On RESET Circuit
30 pF
30 pF
8.2 K
10 uF
+
Vcc
11.0592 MHz
EA/VPPX1
X2
RST
31
19
18
9
2828
Hardware Structure of I/O PinHardware Structure of I/O Pin
Each pin of I/O ports
– Internal CPU bus : communicate with CPU
– A D latch store the value of this pin D latch is controlled by “Write to latch”
– 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 by “Read latch”
– Read latch = 1 : read value from internal latch
– A transistor M1 gate Gate=0: open Gate=1: close
P89V51 RD2 : P89V51 RD2 : Electrical Specs.( Threshold Electrical Specs.( Threshold
Voltages)Voltages)
2929
3030
P89V51RD2 : Timing Specs.P89V51RD2 : Timing Specs.
3131
3232
A Pin of Port 1A Pin of Port 1
8051 IC
D Q
Clk Q
Vcc
Load(L1)
Read latch
Read pin
Write to latch
Internal CPU bus
M1
P1.X pinP1.X
TB1
TB2
P0.x
3333
Writing “1” to Output Pin P1.XWriting “1” to Output Pin P1.X
D Q
Clk Q
Vcc
Load(L1)
Read latch
Read pin
Write to latch
Internal CPU bus
M1
P1.X pinP1.X
8051 IC
2. output pin is Vcc1. write a 1 to the pin
1
0 output 1
TB1
TB2
3434
Writing “0” to Output Pin P1.XWriting “0” to Output Pin P1.X
D Q
Clk Q
Vcc
Load(L1)
Read latch
Read pin
Write to latch
Internal CPU bus
M1
P1.X pinP1.X
8051 IC
2. output pin is ground1. write a 0 to the pin
0
1 output 0
TB1
TB2
3535
Reading “High” at Input PinReading “High” at Input Pin
D Q
Clk Q
Vcc
Load(L1)
Read latch
Read pin
Write to latch
Internal CPU bus
M1
P1.X pin
P1.X
8051 IC
2. MOV A,P1
external pin=High1. write a 1 to the pin MOV
P1,#0FFH
1
0
3. Read pin=1 Read latch=0 Write to latch=1
1
TB1
TB2
3636
Reading “Low” at Input PinReading “Low” at Input Pin
D Q
Clk Q
Vcc
Load(L1)
Read latch
Read pin
Write to latch
Internal CPU bus
M1
P1.X pin
P1.X
8051 IC
2. MOV A,P1
external pin=Low1. write a 1 to the pin
MOV P1,#0FFH
1
0
3. Read pin=1 Read latch=0 Write to latch=1
0
TB1
TB2
You can not Get Logic 1 withYou can not Get Logic 1 withHeavy Load and Weak Pull-upHeavy Load and Weak Pull-up
3737
Is it too much?Is it too much?- it’s the beginning -- it’s the beginning -
3838
LOAD
LOADINGEFFECT( SOURCE IS INPROBLEM
3939
A Pin of Port 0A Pin of Port 0
8051 IC
D Q
Clk Q
Read latch
Read pin
Write to latch
Internal CPU bus
M1
P0.X pinP1.X
TB1
TB2
P1.x
4040
Port 0 with Pull-Up ResistorsPort 0 with Pull-Up Resistors
P0.0P0.1P0.2P0.3P0.4P0.5P0.6P0.7
DS5000
8751
8951
Vcc10 K
Port 0
4141
AddressLines
Bi-DirectionalData Lines
4242
Instruction:MOV A,# 3CH
743C
Data 5Chis in FlashMemory
4343
Instruction:MOV A,3CH
Data is 7F hex
At RAM Address3C hex - -
ACC data will be 7F h
E53C
4444
8580A0
Instruction: MOV p2,p0Mov address, address85
4545
Port 3 Alternate FunctionsPort 3 Alternate Functions
8051 PROG. MEMORY8051 PROG. MEMORY
4646
8051 DATA MEMORY8051 DATA MEMORY
4747
4848
Registers of 8051
4949
RAM memory space allocation in the 8051
7FH
30H
2FH
20H
1FH
17H
10H
0FH
07H
08H
18H
00HRegister Bank 0
(Stack )Register Bank 1
Register Bank 2
Register Bank 3
Bit-Addressable RAM
Scratch pad RAM
Prof. Cherrice TraverProf. Cherrice TraverEE/CS-152: Microprocessors and Microcontrollers
Address ModesAddress Modes Bit-Oriented Data TransferBit-Oriented Data Transfer – transfers between individual bits. – transfers between individual bits. SFRs with addresses ending in 0 or 8 are bit-addressable. (80, 88, 90, 98, SFRs with addresses ending in 0 or 8 are bit-addressable. (80, 88, 90, 98,
etc)etc) Carry flag (C) (bit 7 in the PSW) is used as a single-bit accumulatorCarry flag (C) (bit 7 in the PSW) is used as a single-bit accumulator RAM bits in addresses 20-2F are bit addressableRAM bits in addresses 20-2F are bit addressable
Examples of bit transfers of special function register bits:Examples of bit transfers of special function register bits:mov C, P0.0mov C, P0.0 ; C ; C bit 0 of P0 bit 0 of P0
Bit addressable Area of RAMBit addressable Area of RAM
mov R5,0fahmov R5,0fah ANL A,#08HANL A,#08H mov c,23h.2 mov c,23h.2 ; Data from RAM location ; Data from RAM location
; 23hex bit .2 is transferred to carry ; 23hex bit .2 is transferred to carry
mov 35.7,cmov 35.7,c ; Data from Carry Flag bit is transferred ; Data from Carry Flag bit is transferred ; to RAM location 35 decimal bit .7 i.e. ; to RAM location 35 decimal bit .7 i.e.
; 23H; 23H Mov p0.1 , c Mov p0.1 , c ; Carry bit is transferred to Port 0 bit .1; Carry bit is transferred to Port 0 bit .1 EndEnd ( NOTE : RAM Loactions 20 hex to 2f hex are Bit ( NOTE : RAM Loactions 20 hex to 2f hex are Bit
Addressable Area ).Addressable Area ).5151
Prof. Cherrice TraverProf. Cherrice TraverEE/CS-152: Microprocessors and Microcontrollers
Bit Addressable MemoryBit Addressable Memory20h – 2Fh (16 locations X 8-bits = 128 bits)
7F 78
1A
10
0F 08
07 06 05 04 03 02 01 00
27
26
25
24
23
22
21
20
2F
2E
2D
2C
2B
2A
29
28
Bit addressing:mov C, 1Ahormov C, 23h.2
5353
Stack in the 8051Stack in the 8051
The register used to access the stack is called SP (stack pointer) register.
The stack pointer in the 8051 is only 8 bits wide, which means that it can take value 00 to FFH. When 8051 powered up, the SP register contains value 07.
7FH
30H
2FH
20H
1FH
17H10H
0FH
07H
08H
18H
00HRegister Bank 0
(Stack )Register
Bank 1
Register Bank 2
Register Bank 3
Bit-Addressable RAM
Scratch pad RAM
Prof. Cherrice TraverProf. Cherrice TraverEE/CS-152: Microprocessors and Microcontrollers
StacksStacks
pushpop
stack
stack pointer
Go do the stack exercise…..
5555
TimersTimers
There are TWO general purpose 16 bit timersThere are TWO general purpose 16 bit timers
TIMER MODES:TIMER MODES:MODE 0MODE 0
MODE 1MODE 1
MODE 2MODE 2
MODE 3MODE 3
5656
TIMER MODE 0TIMER MODE 0
Setting timer X mode bits to 00 in the TMOD Setting timer X mode bits to 00 in the TMOD registers results in using THX register as an registers results in using THX register as an 8 bit counter and TLX as a 5 bit counter; 8 bit counter and TLX as a 5 bit counter; pulse input is divided by 32d in TL so that pulse input is divided by 32d in TL so that TH counts the original oscillator frequency TH counts the original oscillator frequency by a total 384d. by a total 384d.
5757
TIMER MODE 1TIMER MODE 1
SIMILAR TO MODE 0 EXCEPT tlx IS SIMILAR TO MODE 0 EXCEPT tlx IS CONFIGURED AS A FULL 8 BIT CONFIGURED AS A FULL 8 BIT COUNTERCOUNTER
5858
TIMER MODE 2TIMER MODE 2
The TLX is used as an 8 bit counter and The TLX is used as an 8 bit counter and THX is used to hold a value that is loaded THX is used to hold a value that is loaded into TLX every time TLX overflows from into TLX every time TLX overflows from FFH to 00H.FFH to 00H.
5959
TIMER MODE 3TIMER MODE 3
Timer 0 in mode 3 becomes two completely Timer 0 in mode 3 becomes two completely separate 8 bit counters. Time 1 may work in separate 8 bit counters. Time 1 may work in Mode 0, 1 and 2.Mode 0, 1 and 2.
6060
6161
TMOD RegisterTMOD Register: :
Gate : When set, timer only runs while INT(0,1) is high.
C/T : Counter/Timer select bit.
M1 : Mode bit 1. M0 : Mode bit 0.
6262
The Timer Control (TCON), Special Function Register:The Timer Control (TCON), Special Function Register:
TF1: Timer 1 overflow flag. Set when timer rolls from all 1 s to 0
TR1: Timer 1 run control bit. Set to 1 by program to enable count.
TF0: Timer 0 overflag. Set when timer rolls from all 1 s to 0
TR0: Timer 0 run control bit. Set to 1 by program to enable count.
6363
IE1: External interrupt 1 edge flag. Set to 1 when H to L IE1: External interrupt 1 edge flag. Set to 1 when H to L edge signal is received on port 3 pin 3.3 ( INT1)edge signal is received on port 3 pin 3.3 ( INT1)
IT1: External interrupt 1 signal type control bit. Set to 1 by IT1: External interrupt 1 signal type control bit. Set to 1 by program to enable external interrupt 1 to be triggered by a program to enable external interrupt 1 to be triggered by a falling edge signal. Set to 0 by by program to enable a low falling edge signal. Set to 0 by by program to enable a low level signal on external interrupt 1 to generate and level signal on external interrupt 1 to generate and interrupt.interrupt.
IE0: External interrupt 0 edge flag. External interrupt 1 IE0: External interrupt 0 edge flag. External interrupt 1 edge flag. edge flag. Set to 1 when H to L edge signal is received Set to 1 when H to L edge signal is received on port 3 pin on port 3 pin 3.2(INT0)3.2(INT0)
IT0: External interrupt 0 signal type control bitIT0: External interrupt 0 signal type control bit
TCON
PCB: 89c51 Microcontroller PortsPCB: 89c51 Microcontroller Ports
6464
Port 3 Port 1
Port 2 Port 0
ATMEL 89c51 Microcontroller
PCB: 89c51 Microcontroller CardPCB: 89c51 Microcontroller Card
6565
ResistorArray (10K)
Micro-controller
Crystal 11.0592 MHz
Burge StripHeader
Capacitors30 pF
Port 3 Port 1
Port 2 Port 0
ATMEL 89c51 Microcontroller
PCB: 89c51 Microcontroller CardPCB: 89c51 Microcontroller Card
6666
ResistorArray (10K)
Micro-controller
Crystal 11.0592 MHz
Burge StripHeader
Capacitors30 pF
Port 3 Port 1
Port 2 Port 0
ATMEL 89c51 Microcontroller
6767
PCB: 89c51 Microcontroller PCB: 89c51 Microcontroller CardCard
Pin 1 of 89c51
Pin 20 Of 89c51
Pin 21Of89c51
Pin 40 of 89c51
ATMEL 89c51 Microcontroller
Where is Clock Circuitry ?Where is Clock Circuitry ?
6868
Clock Circuitrysection
ATMEL 89c51 Microcontroller
Clock circuitry componentsClock circuitry components
6969
Crystal 11.0592 MHz
Two Capacitors33 pF
ATMEL 89c51 Microcontroller
LED Array Card : To Port 0LED Array Card : To Port 0
7070
LED Array card to PORT 0
ATMEL 89c51 Microcontroller
Interfacing DIP Switch & LED ArrayInterfacing DIP Switch & LED Array
7171
PORT 0DIP Switch
PORT 1LED Array
ATMEL Microcontroller 89c51
Stepper Motor InterfacingStepper Motor Interfacing
7272
Stepper Motor InterfacingP1.0 to P1.3PORT 1 thru’ULN 2003 Driver
StepperMotor
ATMEL 89c51 Microcontroller
P.C. Serial(RS-232) Interfacing with uC 89c51P.C. Serial(RS-232) Interfacing with uC 89c51
7373
Pin 10,11 to MAX232
RS232 port of P.C.
ATMEL 89c51 Microcontroller
Embedded system-Spiral ModelEmbedded system-Spiral Model
7474
7575
What is Embedded System?What is Embedded System?
7676
Microcontroller W/S Microcontroller W/S
LAB. EXPERIMENTSLAB. EXPERIMENTS
7777
OVERVIEW DATA TRANSFERSOVERVIEW DATA TRANSFERS
InstructionSOURCE OF DATA
DESTINATIONOF DATA
ADDRESSING MODE
MOV A,#56H FLASH MEMORY
ACCUMULATOR
IMMEDIATE
MOV A,56H RAM LOCATION56H
ACCUMULATOR
DIRECT
MOV A,R1 RAM REGISTER R1
ACCUMULATOR
REGISTER
MOV A,@ 56H RAM LOCATION WHOSEADDRESS IS STORED AT LOCATION 56H
ACCUMULATOR
REGISTER INDIRECT
Tuesday, April 11, 2023Tuesday, April 11, 2023
Mahdi Hassanpour
Prof. Cherrice TraverProf. Cherrice TraverEE/CS-152: Microprocessors and Microcontrollers
Infinite LoopsInfinite Loops
Start: mov C, p3.7Start: mov C, p3.7
mov p1.6, Cmov p1.6, C
sjmp Startsjmp Start
Microcontroller application programs are almost always infinite loops!
Program for Blinking LEDs on Program for Blinking LEDs on PORT 0PORT 0
Include 89c51.mcInclude 89c51.mc Loop:Loop: Mov a,#55hMov a,#55h Mov p0,aMov p0,a Jmp loopJmp loop
8080
LED In Sinking & Sourcing ModeLED In Sinking & Sourcing Mode
8181
Lab. Experiment:Lab. Experiment:Alternately Blink LEDs at PORT 1 considering DelayAlternately Blink LEDs at PORT 1 considering Delay
MOV A,#55HMOV A,#55H MOV P1,AMOV P1,A LOOP:LOOP: MOV R5,#0FFH MOV R5,#0FFH REPEAT3: REPEAT3: MOV R3,#0FFHMOV R3,#0FFH REPEAT2:REPEAT2: Delay LoopDelay Loop DJNZ R3,REPEAT2DJNZ R3,REPEAT2 DJNZ R5,REPEAT3DJNZ R5,REPEAT3 CPL ACPL A MOV P1,AMOV P1,A JMP LOOPJMP LOOP
8282
Lab. Experiment: Lab. Experiment: Connect LED Array to PORT 1. Scroll LEDs Connect LED Array to PORT 1. Scroll LEDs
one by one , in sinking mode one by one , in sinking mode MOV A,#80HMOV A,#80H MOV P1,AMOV P1,A LOOP: LOOP: MOV R5,#0FFHMOV R5,#0FFH REPEAT3: REPEAT3: MOV R3,#0FFHMOV R3,#0FFH REPEAT2:REPEAT2: Delay LoopDelay Loop DJNZ R3,REPEAT2DJNZ R3,REPEAT2 DJNZ R5,REPEAT3DJNZ R5,REPEAT3 RR ARR A MOV P1,AMOV P1,A JMP LOOPJMP LOOP
8383
Experiment 2 : DIP Switch & LED array InterfacingExperiment 2 : DIP Switch & LED array Interfacing
Assumption : DIP Switch interfaced to Assumption : DIP Switch interfaced to PORT 0 & LED Array interfaced to PORT 1.PORT 0 & LED Array interfaced to PORT 1.
MOV A,#0FFHMOV A,#0FFH AGAIN: AGAIN: MOV A,P0MOV A,P0 MOV P1,AMOV P1,A JMP AGAINJMP AGAIN
8484
Lab. Experiment : Generation of Square Lab. Experiment : Generation of Square Wave Wave
LOOP:LOOP: SETB P1.0SETB P1.0 MOV R5,#3FHMOV R5,#3FH CALL DELAYCALL DELAY CLR P1.0CLR P1.0 MOV R5,#0FFHMOV R5,#0FFH CALL DELAYCALL DELAY JMP LOOPJMP LOOP
8585
DELAY:NOPDJNZ R5,AGAINRET
ULN 2003 DRIVER : For Driving ULN 2003 DRIVER : For Driving Stepper MotorStepper Motor
8686
Lab. Experiment : Stepper Motor ControllerLab. Experiment : Stepper Motor Controller
C1 EQU P1.0C1 EQU P1.0 C2 EQU P1.1C2 EQU P1.1 C3 EQU P1.2C3 EQU P1.2 C4 EQU P1.3C4 EQU P1.3 DATA EQU P1DATA EQU P1
MOV DATA , #00HMOV DATA , #00H MOV A,#88HMOV A,#88H ENDLESS:ENDLESS: RR ARR A MOV DATA,AMOV DATA,A CALL DELAYCALL DELAY JMP ENDLESSJMP ENDLESS
8787
DELAY:MOV R7,#0FFHAGAIN:NOPNOP
DJNZ R7,AGAIN
RET
8888
A simple project using A simple project using AT89C51AT89C51
8989
9090
Bit addressable Area of RAMBit addressable Area of RAM
mov R5,0fahmov R5,0fah ANL A,#08HANL A,#08H mov c,23h.2 mov c,23h.2 ; Data from RAM location ; Data from RAM location
; 23hex bit .2 is transferred to ; 23hex bit .2 is transferred to carry carry
mov 35.7,cmov 35.7,c ; Data from Carry Flag bit is transferred ; Data from Carry Flag bit is transferred ; to RAM location 35 decimal bit .7; to RAM location 35 decimal bit .7
Mov p0.1 , c Mov p0.1 , c ; Carry bit is transferred to Port 0 bit .1; Carry bit is transferred to Port 0 bit .1 EndEnd ( NOTE : RAM Loactions 20 hex to 2f hex are Bit ( NOTE : RAM Loactions 20 hex to 2f hex are Bit
Addressable Area ).Addressable Area ). 9191
MicrocontrollersMicrocontrollers
9292
8051 Pin-out8051 Pin-out
9393
RST – ResetRST – ResetHIGH for 2 clock cyclesHIGH for 2 clock cycles
Resets registers and Resets registers and
program counter (to 0000h) program counter (to 0000h)
for an orderly startup.for an orderly startup.
Program Status Word (PSW)Program Status Word (PSW)
Pulse width modulatorPulse width modulator Generates pulses with Generates pulses with
specific high/low timesspecific high/low times Duty cycle: % time highDuty cycle: % time high
– Square wave: 50% Square wave: 50% duty cycleduty cycle
Common use: control Common use: control average voltage to average voltage to electric deviceelectric device– Simpler than DC-Simpler than DC-
DC converter or DC converter or digital-analog digital-analog converterconverter
– DC motor speed, DC motor speed, dimmer lightsdimmer lights
Another use: encode Another use: encode commands, receiver commands, receiver uses timer to decodeuses timer to decode
9696
clk
pwm_o
25% duty cycle – average pwm_o is 1.25V
clk
pwm_o
50% duty cycle – average pwm_o is 2.5V.
Fall 2004Fall 2004
Stepper motor controllerStepper motor controller
Red AWhite A’Yellow B
Black B’
MC3479P 1
5
4
3
2
7
8
6
16
15
14
13
12
11
10
9
Vd
A’
A
GND
Bias’/Set
Clk
O|C
Vm
B
B’
GND
Phase A’
CW’/CCW
Full’/Half Step
Sequence A B A’ B’1 + + - -2 - + + -3 - - + +4 + - - +5 + + - -
Stepper motor: rotates Stepper motor: rotates fixed number of degrees fixed number of degrees when given a “step” signalwhen given a “step” signal– In contrast, DC motor just In contrast, DC motor just
rotates when power applied, rotates when power applied, coasts to stopcoasts to stop
Rotation achieved by Rotation achieved by applying specific voltage applying specific voltage sequence to coilssequence to coils
Controller greatly Controller greatly simplifies this simplifies this
Fall 2004Fall 2004
Stepper motor with controller Stepper motor with controller (driver)(driver)
2 A’ 3 A
10 7
B 15B’ 14
MC3479PStepper Motor Driver 8051
P1.0P1.1
StepperMotor
CLK
CW’/CCW
The output pins on the stepper motor driver do not provide enough current to drive the stepper motor. To amplify the current, a buffer is needed. One possible implementation of the buffers is pictured to the left. Q1 is an MJE3055T NPN transistor and Q2 is an MJE2955T PNP transistor. A is connected to the 8051 microcontroller and B is connected to the stepper motor.
Q2
1K
1KQ1
+V
A B
void main(void){
*/turn the motor forward */ cw=0; /* set direction */ clk=0; /* pulse clock */ delay(); clk=1;
/*turn the motor backwards */ cw=1; /* set direction */ clk=0; /* pulse clock */ delay(); clk=1;
}
/* main.c */
sbit clk=P1^1;sbit cw=P1^0;
void delay(void){ int i, j; for (i=0; i<1000; i++) for ( j=0; j<50; j++) i = i + 0;}
Fall 2004Fall 2004
Stepper motor without controller Stepper motor without controller (driver)(driver)
StepperMotor
8051
GND/ +VP2.4
P2.3P2.2P2.1P2.0
A possible way to implement the buffers is located below. The 8051 alone cannot drive the stepper motor, so several transistors were added to increase the current going to the stepper motor. Q1 are MJE3055T NPN transistors and Q3 is an MJE2955T PNP transistor. A is connected to the 8051 microcontroller and B is connected to the stepper motor.
Q2
+V
1KQ1
1K
+V
A
B
330
/*main.c*/sbit notA=P2^0;sbit isA=P2^1;sbit notB=P2^2;sbit isB=P2^3;sbit dir=P2^4;
void delay(){ int a, b; for(a=0; a<5000; a++) for(b=0; b<10000; b++) a=a+0;}
void move(int dir, int steps) {int y, z; /* clockwise movement */ if(dir == 1){ for(y=0; y<=steps; y++){ for(z=0; z<=19; z+4){ isA=lookup[z]; isB=lookup[z+1]; notA=lookup[z+2]; notB=lookup[z+3]; delay(); } } }
/* counter clockwise movement */ if(dir==0){ for(y=0; y<=step; y++){ for(z=19; z>=0; z - 4){ isA=lookup[z]; isB=lookup[z-1]; notA=lookup[z -2]; notB=lookup[z-3]; delay( ); } } }}void main( ){ int z; int lookup[20] = { 1, 1, 0, 0, 0, 1, 1, 0, 0, 0, 1, 1, 1, 0, 0, 1, 1, 1, 0, 0 }; while(1){ /*move forward, 15 degrees (2 steps) */ move(1, 2); /* move backwards, 7.5 degrees (1step)*/ move(0, 1); }}
4 register banks4 register banks
General user RAMGeneral user RAM
Special Function Registers (SFR)Special Function Registers (SFR)
100100
Intel 8051 RAM
8051 SFRs8051 SFRs
Blue: related to the I/O ports Blue: related to the I/O ports
Yellow: control the configuration of 8051 Yellow: control the configuration of 8051
Green: auxiliary SFRs Green: auxiliary SFRs
101101
102102
103103
Block Diagram of 8051 Block Diagram of 8051 MicrocontrollerMicrocontroller
104104
Schematic for Microcontroller Schematic for Microcontroller BoardBoard
105105
106106
8051 Block Diagram8051 Block Diagram
107107
8051 Family State Sequence8051 Family State Sequence
108108
RAM RAM
109109
110110
111111
Using 7 Segment DisplayUsing 7 Segment Display
112112
113113
114114
115115
Bit addressable Area of RAMBit addressable Area of RAM
mov R5,0fahmov R5,0fah ANL A,#08HANL A,#08H mov c,23h.2 mov c,23h.2 ; Data from RAM location ; Data from RAM location
; 23hex bit .2 is transferred to ; 23hex bit .2 is transferred to carry carry
mov 35.7,cmov 35.7,c ; Data from Carry Flag bit is transferred ; Data from Carry Flag bit is transferred ; to RAM location 35 decimal bit .7; to RAM location 35 decimal bit .7
Mov p0.1 , c Mov p0.1 , c ; Carry bit is transferred to Port 0 bit .1; Carry bit is transferred to Port 0 bit .1 EndEnd ( NOTE : RAM Loactions 20 hex to 2f hex are Bit ( NOTE : RAM Loactions 20 hex to 2f hex are Bit
Addressable Area ).Addressable Area ). 116116
Rotating Universal /DC Motor in Clockwise & Anticlockwise Rotating Universal /DC Motor in Clockwise & Anticlockwise using Microcontroller by Sensing positionusing Microcontroller by Sensing position
117117
Micro-Con-troller
89c51
TANK
2 C.O. /
DPDT Relay
DC Motor
REEDSensor
Assembly Code for Sensing PositionAssembly Code for Sensing Position
MOV P1,#FFh ; port1 as a input portMOV P1,#FFh ; port1 as a input port MOV A,P1MOV A,P1 LOOP:LOOP: MOV R1,P1MOV R1,P1
IF R1 = # FEh then ; 1111 1110IF R1 = # FEh then ; 1111 1110 SETB P2.0SETB P2.0 END IFEND IF MOV R2,P1MOV R2,P1
IF R2=#FDh then ; 1111 1101IF R2=#FDh then ; 1111 1101 CLR P2.0CLR P2.0 END IFEND IF
JMP LOOPJMP LOOP118118
Micro-Controller89c51
Pin 1P1.0
Pin 2P1.1
P 2.0
From sensor 1st
From sensor 2nd
Top Related