Ex. No : 6 VERIFY TIMER/ COUNTER.
Date :
Aim
To Write an ALP using 8051 for Timer / Counter and to Find the value for TMOD
if we want to program Timer 0 in mode 2, use 8051 XTAL for the clock source, and use
instructions to start and stop the timer.
And also
Algorithm
Solution
The role of the TMOD register, we will look at the timer’s modes and how they are
programmed
to create a time delay.
Because modes 1 and 2 are so widely used, we describe each of them in detail.
Mode 1 programming
The following are the characteristics and operations of mode 1:
1. It is a 16-bit timer; therefore, it allows values of 0000 to FFFFH to be
loaded
into the timer’s registers TL and TH.
2. After TH and TL are loaded with a 16-bit initial value, the timer must be
started.
This is done by “SETB TRO” for Timer 0 and “SETB TR1″ for Timer 1.
3. After the timer is started, it starts to count up. It counts up until it reaches
its limit of
FFFFH.
4. When it rolls over from FFFFH to 0000, it sets high a flag bit called TF
(timer flag).
This timer flag can be monitored. When this timer flag is raised, one option
would be to
stop the timer with the instructions “CLR TRO” or “CLR TR1″, for Timer
0 and
Timer 1, respectively.
Again, it must be noted that each timer has its own timer flag: TFO for Timer 0, and
TF1 for Timer 1.
1. After the timer reaches its limit and rolls over, in order to repeat the
process the registers
2. TH and TL must be reloaded with the original value, and TF must be
reset to 0.
Steps to program in mode 1
To generate a time delay, using the timer’s mode 1, the following steps are taken.
● Load the TMOD value register indicating which timer (Timer 0 or Timer 1) is to be
used and which timer
mode (0 or 1) is selected.
1. Load registers TL and TH with initial count values.
2. Start the timer.
● Keep monitoring the timer flag (TF) with the “JNB TFx, target”
instruction to see if it
is raised. Get out of the loop when TF becomes high.
3. Stop the timer.
4. Clear the TF flag for the next round.
5. Go back to Step 2 to load TH and TL again.
To calculate the exact time delay and the square wave frequency generated on pin
PI .5, we need to
know the XTAL frequency. See Example 9-5.
Develop a formula for delay calculations using mode 1 (16-bit) of the timer for a
crystal frequency of
XTAL = 11.0592 MHz. This is given in Figure 9-4. The scientific calculator in the
Accessories
directory of Microsoft Windows can help you to find the TH, TL values.
This calculator supports decimal, hex, and binary calculations.
(a) in hex
(FFFF – YYXX + 1) X 1.085 us where YYXX are TH, TL initial values respectively.
Notice that values YYXX are in hex.
(b) in decimal
Convert YYXX values of the TH,TL register to decimal to get a NNNNN decimal
number,
then (65536 – NNNNN) x 1.085 mircosec
Timer Delay Calculation for XTAL = 11.0592 MHz
Timer
The 8051 uses two registers to handle and control the working of the Timer registers. One
of these registers is
TMOD. The other is TCON.
● TMOD (Timer mode register) – The timer registers can operate in four
different modes.
We’ll take a look at all these modes in a jiffy. But the point is, the selection of
the functioning
mode is done in this register. Additionally, the overall function of the timer
registers, whether
to work as timers or counters, is done here too.
● TCON (Timer Control Register) – At the risk of a slight oversimplification
that we will
address later, we only use half of the TCON register for timing and counting
purposes.
This register is used to initialize the counting and to indicate when the timers
have reached
their counting limit.
Timer modes in 8051 microcontrollers
Mode 0:
Mode 1:
Mode 2:
Mode 3:
Result
EX. NO : 7 INTERRUPT HANDLING
Date :
Aim
Write an ALP using 8051 for Interrupt Handling and to Find the amount of time
delay in the DELAY subroutine generated by the timer. Assume that XTAL = 11.0592
MHz.
And also, to explain in detail about the Interrupt handling in 8051 microcontroller
Algorithm
Solution
● Interrupts are the events that temporarily suspend the main program, pass the control to
the external
sources and execute their task. It then passes the control to the main program where it
had left off.
● 8051 has 5 interrupt signals, i.e. INT0, TFO, INT1, TF1, RI/TI.
Each interrupt can be enabled or disabled by setting bits of the IE register and the
whole interrupt
system can be disabled by clearing the EA bit of the same register.
IE (INTERRUPT ENABLE) REGISTER
This register is responsible for enabling and disabling the interrupt.
EA register is set to one for enabling interrupts and set to 0 for disabling
the interrupts.
Its bit sequence and their meanings are shown in the following figure.
It disables all interrupts. When EA = 0 no interrupt will
EA IE.7 be acknowledged and EA = 1 enables the interrupt
individually.
- IE.6 Reserved for future use.
- IE.5 Reserved for future use.
ES IE.4 Enables/disables serial port interrupt.
ET1 IE.3 Enables/disables timer1 overflow interrupt.
EX1 IE.2 Enables/disables external interrupt1.
ET0 IE.1 Enables/disables timer0 overflow interrupt.
EX0 IE.0 Enables/disables external interrupt0.
IP (INTERRUPT PRIORITY) REGISTER
We can change the priority levels of the interrupts by changing the
corresponding bit in the
Interrupt Priority (IP) register as shown in the following figure.
● A low priority interrupt can only be interrupted by the high priority interrupt,
but not interrupted by another low priority interrupt.
● If two interrupts of different priority levels are received simultaneously, the
request of higher priority level is served.
● If the requests of the same priority levels are received simultaneously, then the
internal polling sequence determines which request is to be serviced.
- IP.6 Reserved for future use.
- IP.5 Reserved for future use.
PS IP.4 It defines the serial port interrupt priority
level.
PT1 IP.3 It defines the timer interrupt of 1 priority.
PX1 IP.2 It defines the external interrupt priority level.
PT0 IP.1 It defines the timer0 interrupt priority level.
PX0 IP.0 It defines the external interrupt of 0 priority
level.
TCON Register TCON register specifies the type of external interrupt to the microcontroller.