i o ports and timers of 8051

31
I/O PORTS AND TIMERS I/O PORTS AND TIMERS IN 8051 MICRO IN 8051 MICRO CONTROLLER CONTROLLER Dr. C. Saritha Dr. C. Saritha Lecturer in Electronics Lecturer in Electronics SSBN Degree & PG College SSBN Degree & PG College Anantapur - 515001 Anantapur - 515001

Upload: saritha-reddy

Post on 12-May-2015

3.672 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: I o ports and timers of 8051

I/O PORTS AND TIMERSI/O PORTS AND TIMERSIN 8051 MICRO CONTROLLERIN 8051 MICRO CONTROLLER

Dr. C. SarithaDr. C. SarithaLecturer in ElectronicsLecturer in Electronics

SSBN Degree & PG CollegeSSBN Degree & PG CollegeAnantapur - 515001 Anantapur - 515001

Page 2: I o ports and timers of 8051

Input/Output Ports (I/O Ports)Input/Output Ports (I/O Ports)

All 8051 microcontrollers have 4 I/O ports All 8051 microcontrollers have 4 I/O ports each comprising 8 bits which can be each comprising 8 bits which can be configured as inputs or outputs. configured as inputs or outputs.

All the ports upon reset are configured as All the ports upon reset are configured as output, ready to use it as output port.output, ready to use it as output port.

To use any of these ports as an input port, To use any of these ports as an input port, it must be programmed.it must be programmed.

Page 3: I o ports and timers of 8051

Port0 occupies a total of 8 pins (pins Port0 occupies a total of 8 pins (pins 32-39).32-39).

It can be used for input or output.It can be used for input or output.To use the pins of port o as both input To use the pins of port o as both input

and output ports, each pin must be and output ports, each pin must be connected externally to 10k-ohm pull up connected externally to 10k-ohm pull up resistor.resistor.

This is due to fact that p0 is an open This is due to fact that p0 is an open drain with external pull up resistors drain with external pull up resistors connected upon the reset.connected upon the reset.

Port 0Port 0

Page 4: I o ports and timers of 8051

Port0 with pull up resistorPort0 with pull up resistor

Page 5: I o ports and timers of 8051

Port0 is configured as o/p port:-Port0 is configured as o/p port:- BACK MOV A,#55HBACK MOV A,#55H MOV PO,AMOV PO,A ACALL DELAYACALL DELAY CPL ACPL A SJMP BACKSJMP BACK

Continue…Continue…

Page 6: I o ports and timers of 8051

Continue…Continue…

To make the port o as i/p port, the port To make the port o as i/p port, the port must be programmed by writing 1 to all the must be programmed by writing 1 to all the bits.bits.

MOV A,#FFHMOV A,#FFH

MOV PO,AMOV PO,A

BACK MOV A,POBACK MOV A,PO

SJMP BACKSJMP BACK

Page 7: I o ports and timers of 8051

Dual role of port 0Dual role of port 0

Port0 is also designated as ADO-Port0 is also designated as ADO-AD7,allowing it to be used for both address AD7,allowing it to be used for both address and data .and data .

When connecting 8051 to an external When connecting 8051 to an external memory, port0 provides both address and memory, port0 provides both address and datadata

The 8051 multiplexes address and data The 8051 multiplexes address and data through port0 to save pins.through port0 to save pins.

Page 8: I o ports and timers of 8051

Continue…Continue…

When ALE=0, it provides data D0-D7 but When ALE=0, it provides data D0-D7 but when ALE=1 it has address A0-A7.when ALE=1 it has address A0-A7.

ALE is used for multiplexing address and ALE is used for multiplexing address and data with help of 74LS73.data with help of 74LS73.

..

Page 9: I o ports and timers of 8051

Port1 Port1 Port 1 occupies a total of 8 pins, it can be Port 1 occupies a total of 8 pins, it can be

used as input or output.used as input or output.

This port does not need any pull-up This port does not need any pull-up resistors since it already has pull-up resistors since it already has pull-up resistor internally.resistor internally.

Upon reset ,port1 is configured as an o/p Upon reset ,port1 is configured as an o/p port.port.

Page 10: I o ports and timers of 8051

Continue…Continue…

Port1 is configured as o/p port:-Port1 is configured as o/p port:-

BACK MOV A,#55HBACK MOV A,#55H

MOV P1,AMOV P1,A

ACALL DELAYACALL DELAY

CPL ACPL A

SJMP BACKSJMP BACK

Page 11: I o ports and timers of 8051

Continue…Continue…

To make port1 an i/p port, it must be To make port1 an i/p port, it must be programmed as such writing 1 to all its bitsprogrammed as such writing 1 to all its bits

MOV A,#FFHMOV A,#FFH

MOV P1,AMOV P1,A

BACK MOV A,P1BACK MOV A,P1

SJMP BACKSJMP BACK

Page 12: I o ports and timers of 8051

PORT 2 PORT 2

Port2 occupies a total of 8 pins, it can be Port2 occupies a total of 8 pins, it can be used as i/p or o/p.used as i/p or o/p.

Just like p1,port2 does not need any pull-Just like p1,port2 does not need any pull-up resistor since it has pull-up resistor up resistor since it has pull-up resistor internally.internally.

Upon reset, port2 configured as an o/p.Upon reset, port2 configured as an o/p.

Page 13: I o ports and timers of 8051

Continue…Continue…

Port2 is configured as o/p port:-Port2 is configured as o/p port:-

BACK MOV A,#55HBACK MOV A,#55H

MOV P2,AMOV P2,A

ACALL DELAYACALL DELAY

CPL ACPL A

SJMP BACKSJMP BACK

Page 14: I o ports and timers of 8051

Continue…Continue…

To make port2 an i/p port, it must be To make port2 an i/p port, it must be programmed as such writing 1 to all its bitsprogrammed as such writing 1 to all its bits

MOV A,#FFHMOV A,#FFH

MOV P2,AMOV P2,A

BACK MOV A,P2BACK MOV A,P2

SJMP BACKSJMP BACK

Page 15: I o ports and timers of 8051

Dual role of port2Dual role of port2

Port2 must be used along with p0 to Port2 must be used along with p0 to provide the 16 bit address for the external provide the 16 bit address for the external memory.memory.

Port2 is designated as A8-A15,indicating Port2 is designated as A8-A15,indicating its dual functionits dual function

Page 16: I o ports and timers of 8051

PORT 3PORT 3 port3 occupies a total of 8 pins. It can be port3 occupies a total of 8 pins. It can be

used as i/p or o/p .used as i/p or o/p .P3 does not need any pull up resistor same P3 does not need any pull up resistor same

as p1 and p2 did not.as p1 and p2 did not.Port3 is configured as o/p port upon reset .Port3 is configured as o/p port upon reset .Port3 has the additional function of providing Port3 has the additional function of providing

some extremely important signals such as some extremely important signals such as interrupts interrupts

Page 17: I o ports and timers of 8051

Alternate Pin-functionsAlternate Pin-functions

Page 18: I o ports and timers of 8051

Timers/countersTimers/counters

Timers/Counters

Timer0/Counter0 Timer1/Counter1

Page 19: I o ports and timers of 8051

Continue…Continue…

They can be used as:They can be used as:

1 Timer1 Timer:: Timers are used as time delay generator.Timers are used as time delay generator. The clock source is the internal crystal The clock source is the internal crystal

frequency of the 8051.frequency of the 8051.

2 counter2 counter:: Which counts number of events.Which counts number of events. External input from input pin to count the External input from input pin to count the

number of events on registernumber of events on register

Page 20: I o ports and timers of 8051

Registers used in Timer/counterRegisters used in Timer/counter Register pairs (TH0,TH1),(TL0,TL1) form Register pairs (TH0,TH1),(TL0,TL1) form

16-bit Timer/Counter registers.16-bit Timer/Counter registers.

These are instructions for reading and These are instructions for reading and writing and they are bit wisewriting and they are bit wise

One 8 bit mode register-TMOD.One 8 bit mode register-TMOD.

One 8 bit control register-TCON.One 8 bit control register-TCON.

Page 21: I o ports and timers of 8051

TMOD Register TMOD Register

Both timer 0 and timer 1 use the same Both timer 0 and timer 1 use the same register called TMOD.register called TMOD.

It is an 8-bit register in which the lower 4 It is an 8-bit register in which the lower 4 bits are set aside for timer 0 and the upper 4 bits are set aside for timer 0 and the upper 4 bits are set aside for timer 1bits are set aside for timer 1

The lower 2 bits are used to set the timer The lower 2 bits are used to set the timer and the upper 2 bits to specify the operation.and the upper 2 bits to specify the operation.

Page 22: I o ports and timers of 8051

Timer Mode (TMOD)

GATEGATE C/TC/T M1M1 M0M0 GATEGATE C/TC/T M1M1 M0M0

Timer 1 Timer 0

Page 23: I o ports and timers of 8051

GATE :-GATE :-

Gating control when set, the Gating control when set, the timer/counter is enabled only while the INTX timer/counter is enabled only while the INTX

Pin is high and the TRX control pin is set.Pin is high and the TRX control pin is set.

C/T :-C/T :-

This bit in the TMOD register is used This bit in the TMOD register is used to decide whether the timer is used as a to decide whether the timer is used as a delay generation or an event counter.delay generation or an event counter.

If C/T=0,it is used as a delay If C/T=0,it is used as a delay generator.generator.

Page 24: I o ports and timers of 8051

Continue…Continue…

M1: M1: Mode bit 1Mode bit 1

M0: M0: Mode bit 0Mode bit 0

M0 and M1 select the timer mode, there M0 and M1 select the timer mode, there are three modes i.e., mode 0,mode 1, are three modes i.e., mode 0,mode 1, mode 2.mode 2.

Page 25: I o ports and timers of 8051

Modes of timers/countersModes of timers/counters

Mode 0 : 5 bit prescaler(13 bit )Mode 1 : 16 bit timerMode 2 : 8 bit Auto reloadMode 3 : TL 0 and TH0 are independent

but TH0 controlled by TR1

and TF1.

Page 26: I o ports and timers of 8051

TCON TCON RegisterRegister This is an 8 bit register.

The upper 4 bits of the register is used to store the TF and TR bits of both timer 0 and timer 1.

The lower 4 bits are set aside for controlling the interrupt bit.

Page 27: I o ports and timers of 8051

Timer Control Register (TCON)

TF1TF1 TR1TR1 TF0TF0 TR0TR0 IE1IE1 IT1IT1 IE0IE0 IT0IT0

Page 28: I o ports and timers of 8051

TF1 [Timer1 over flow flag]TF1 [Timer1 over flow flag] :- Set by :- Set by hardware when timer 1 counters over hardware when timer 1 counters over flows, cleared by hardware as the flows, cleared by hardware as the processor vectors to the service routine.processor vectors to the service routine.

TR1 [Timer1 run control bit]TR1 [Timer1 run control bit] :- Set/cleared :- Set/cleared by software to turn timer1 counter1 on/off.by software to turn timer1 counter1 on/off.

TF0 [Timer0 over flow flag]TF0 [Timer0 over flow flag] :- Set by :- Set by hardware when timer /counters over flows, hardware when timer /counters over flows, cleared by hardware as the processor cleared by hardware as the processor vectors to the service routine.vectors to the service routine.

Page 29: I o ports and timers of 8051

TR0TR0 [Timer0 run control bit][Timer0 run control bit] :- Set/cleared by :- Set/cleared by software to turn timer/counter on/off.software to turn timer/counter on/off.

IE1 IE1 [External interrupt1 edge flag][External interrupt1 edge flag] :- Set by :- Set by CPU when the external interrupt edge is CPU when the external interrupt edge is detected, cleared by the CPU when the detected, cleared by the CPU when the interrupt is processed.interrupt is processed.

IT1 IT1 [Interrupt 1 type control bit][Interrupt 1 type control bit] :- :- Set/cleared by software to specify falling Set/cleared by software to specify falling edge/low level triggered external interrupt.edge/low level triggered external interrupt.

Page 30: I o ports and timers of 8051

IE0 IE0 [External interrupt0 edge flag][External interrupt0 edge flag] :- Set by :- Set by CPU when the external interrupt edge is CPU when the external interrupt edge is detected, cleared by the CPU when the detected, cleared by the CPU when the interrupt is processed.interrupt is processed.

IT0 IT0 [Interrupt 0 type control bit][Interrupt 0 type control bit] :- :- Set/cleared by software to specify falling Set/cleared by software to specify falling edge/low level triggered external interrupt.edge/low level triggered external interrupt.

Page 31: I o ports and timers of 8051

THANK YOUTHANK YOU THANK YOUTHANK YOU