Вы находитесь на странице: 1из 48

Demonstration of Embedded C Program based on Timer / Counter

Prepared By: Prof. Ankit D. Shah Assistant Professor, EC Department, IDS, NU


1

SIGNIFICANCE OF TIMER/COUNTER:
Many microcontroller applications required the counting of external events, such as the frequency of a pulse train, object counter or the generation of precise interval time delays between computer actions or in time delay program used in digital clock or any other application where precise time delay is required.
Saturday, February 23, 2013 Prof. Ankit D. shah 2

What is a Timer/Counter?

A counter is a device that generates binary numbers in a specified count sequence when triggered by an incoming clock pulse. Timers are counters that count pulses. If the pulses are Internal clock pulses, then the timers count time.
Saturday, February 23, 2013

Prof. Ankit D. shah

Cont.

The 8051 comes with two 16-bit timers/counters, both of which may be controlled, set, read, and configured individually. The 8051 timers have three general functions:

Keeping time and/or calculating the amount of time between events (Timer Mode). Counting the events themselves (Event Counter Mode) Generating baud rates for the serial port.

Always count UP irrespective of function as a timer or a counter.


Saturday, February 23, 2013 Prof. Ankit D. shah 4

Difference between Timer and Counter:


When the incoming clock frequency is known, we can generate a fixed period of time known to the designer by setting a preloaded value. This is called a timer. It is also called an interval timer since it is measuring the time of the interval between two events. When the incoming clock is irregular and we are only interested in the number of occurrences of the pulse, this is called a counter. Since we are counting events, is also known as an event counter. In 8051, timer gets its clock from the oscillator (crystal that connected to the CPU) frequency (1/12 of it). Counter gets the clock from an external pin: P3.4 for timer 0, and P3.5 for timer 1.
Saturday, February 23, 2013 Prof. Ankit D. shah 5

Cont.

Timer mode (if the timer control bit is set to timer, the timer uses the system clock) Increments by 1 every machine cycle. A single machine cycle consists of 12 clock pulses. Each counter may be programmed to count internal clock pulsing, acting as a timer Event mode (if the timer control bit is set to event, the timer/counter uses a port bit) Increments by 1 for each pulse on P3.4 for Timer 0, and P3.5 for Timer 1, respectively. Each counter may be programmed to count external pulses as a counter.
Prof. Ankit D. shah 6

Saturday, February 23, 2013

Applications of Timers / Counters:


Generating time delays. Measuring pulse duration or timing intervals. Counting pulses or events. Generating baud rate clock for the internal 8051 serial I/O port. Generating interrupts.
Saturday, February 23, 2013 Prof. Ankit D. shah 7

Software Technique V/S Timer/Counter:


Both of these task can be accomplished using software technique, but software loops for counting or timing keep the processor occupied so that other, perhaps more important, function are not done. To relieve the processor of this time consuming task, two 16-bit up counter, named T0 (Timer 0) and T1 (Timer 1), are provided for the general use of the programmer.
Saturday, February 23, 2013 Prof. Ankit D. shah 8

Calculation of clock frequency when timer/counter operates as a timer:

Find the timers clock frequency and period for 8051-based systems with the following crystal frequencies:

12 MHz

1/12 x 12 MHz = 1 MHz => T = 1 us 1/12 x 16 MHz = 1.333 MHz => T = .75 us

16 MHz

Saturday, February 23, 2013

Prof. Ankit D. shah

Timer/Counter SFR:
The counters are divided into two 8-bit registers called the timer low (TL0 & TL1) and high (TH0 & TH1) bytes. All counter action is controlled by bit states in the timer mode control register (TMOD), time / counter control register (TCON) and certain program instructions. TMOD is dedicated solely to the two timers and can be considered to be two duplicate 4-bit registers, each of which controls the action of one of the timer. TCON has control bits and flags for the timers in the upper nibble and control bits and flags for the external interrupts in the lower nibble.
Saturday, February 23, 2013 Prof. Ankit D. shah 10

Cont.
Timer Special Function Registers (SFRs) As mentioned before, the 8051 has two timers that essentially function the same way. One timer is called TIMER0 (T0) and the other is TIMER1(T1). The two timers share two SFRs (TMOD and TCON) that control the timers. Each timer also has two SFRs dedicated solely to itself (TH0/TL0 and TH1/TL1). It is common practice to use given SFR names to refer to them, but in reality an SFR has a numeric address. When you enter the name of an SFR into an assembler or compiler, it internally converts it to the specified SFR address.
Saturday, February 23, 2013 Prof. Ankit D. shah 11

Cont.
Internal 1 byte address of Timer SFRs SFR Name Description Address TH0 Timer 0 High Byte 8CH TL0 Timer 0 Low Byte 8AH TH1 Timer 1 High Byte 8DH TL1 Timer 1 Low Byte 8BH TCON Timer Control 88H TMOD Timer Mode 89H
Saturday, February 23, 2013 Prof. Ankit D. shah 12

Structure of TCON and TMOD SFR:


Timer Control Register (TCON)

TCON has the following functions:

Turns Timer 0 and Timer 1 on or off. (Timer Run Control Bit) Sets trigger type (edge or level) for interrupt 0 and interrupt 1. (Interrupt type bit) Sets flags when timer 0 or timer 1 overflow. Sets flags when negative edge triggered external interrupt 0 or external interrupt 1 occurs on pin P3.2 or P3.3 respectively.
Prof. Ankit D. shah 13

Saturday, February 23, 2013

Structure or Format of TCON SFR: D7 D6 D5 D4


TF1 TR1 TF0 TR0

D3
IE1 IT1

D2
IE0

D1
IT0

D0

Cont.

Bit D7 D6 D5 D4 D3

Symbol TF1 TR1 TF0 TR0 IE1

Function Timer 1 Overflow Flag. Set when all the bits of timer 1 roll from 1s to 0s. Cleared when processor vectors to execute interrupt service routine located at program address 001B H. Timer 1 Run control bit. Set to 1 by program to enable timer to count; cleared to 0 by program to halt timer. Does not reset timer. Timer 0 Overflow Flag. Set when all the bits of timer 0 roll from 1s to 0s. Cleared when processor vectors to execute interrupt service routine located at program address 000B H. Timer 0 Run control bit. Set to 1 by program to enable timer to count; cleared to 0 by program to halt timer. Does not reset timer. External Interrupt 1 Edge flag. Set to 1 when a high to low edge is received on port 3 pin 3.3. (INT 1). Cleared when processor vectors to interrupt service routine located at program address 0013H. Not related to timer operation. External Interrupt 1 signal Type control bit. Set to 1 by program to enable external interrupt to be triggered by a falling edge signal. Set to 0 by program to enable a low level signal on external interrupt 1 to generate an interrupt. External Interrupt 0 Edge flag. Set to 1 when a high to low edge is received on port 3 pin 3.2. (INT 0). Cleared when processor vectors to interrupt service routine located at program address 0003H. Not related to timer operation. External Interrupt 0 signal Type control bit. Set to 1 by program to enable external interrupt to be triggered by a falling edge signal. Set to 0 by program to enable a low level signal on external interrupt 1 to generate an interrupt.

D2

IT1

D1

IE0

D0

IT0

Only the upper 4 bits are related to timers; the lower 4 bits have nothing to do with timers - they have to do with interrupts. TCON is a bitbit-addressable register.
Saturday, February 23, 2013 Prof. Ankit D. shah 14

Cont..
Timer Mode Control (TMOD) Register

TMOD controls timer 0 and timer 1 functions. The registers upper nibble controls Timer 1 and the lower nibble controls timer 0. The four bits for each timer control a gate function, selection of counter or timer, and timer mode. The timer or counter function is selected by control bits C/T; these two timers/counters have 4 operating modes, selected by appropriate M0 and M1 bits in the TMOD register. TMOD is not a bit-addressable register. It has to be manipulated as a byte.
Saturday, February 23, 2013 Prof. Ankit D. shah 15

Cont.
(MSB) GATE
C/ T

M1

M0

GATE

C/ T

M1

(LSB) M0

Timer 1 GATE If GATE = 1, timer x will run only when TRx = 1 and INTx = 1. If GATE = 0, timer x will run whenever TRx = 1.
C/ T

Timer 0 M1 0 0 1 M0 0 1 0 Operating Mode 13-bit timer mode (8048 mode). 16-bit timer mode. 8-bit auto-reload mode. THx holds a value which is to be reloaded into TLx each time it overflows. (Timer 0) TL0 is an 8-bit Timer/Counter controlled by the standard Timer 0 control bits. TH0 is an 8-bit timer only controlled by Timer 1 control bits. (Timer 1) Timer/Counter 1 is stopped.
16

Timer mode select. If C/ T = 1, timer x runs in counter mode taking its input from Tx pin. If C/ T = 0 , timer x runs in timer mode taking its input from the system clock.

Saturday, February 23, 2013

Prof. Ankit D. shah

Cont.

Indicate which mode and which timer are selected for each of the following:

TMOD = 0000 0001, mode 1 of timer 0, mode 0 of timer 1 TMOD = 0010 0000, , mode 0 of timer 0, mode 2 of timer 1 TMOD = 0001 0010, mode 2 of timer 0, and mode 1 of timer 1 are selected

Saturday, February 23, 2013

Prof. Ankit D. shah

17

Timer / Counter Control Logic:


The following diagram shows the control logic for timer / counter pulse generation which will become the clock pulse for timer / counter.
Timer Oscillator Frequency % 12
C/T = 0 (TMOD timer operation)

S2

S1 Counter T1 / 0 Input Pin


C/ T = 1 (TMOD counter operation)

To Timer Stage OR Timer Clock

TR1/0 Bit in TCON

Gate Bit in TMOD INT1/ 0 Input Pin

Saturday, February 23, 2013

Prof. Ankit D. shah

18

Various modes of Timer/Counter:


As we know that the timers may operate in any one of four modes that are determined by the mode bits, M1 and M0, in the TMOD register.

Saturday, February 23, 2013

Prof. Ankit D. shah

19

Mode 0 13 Bit Timer / Counter:


TH, TL = XXXXXXXX, 000XXXXX 8 bits from TH 5 bits from LSB of TL For compatibility with older microcontroller (8048), not very useful now. Range = 0 to 8191.
Timer clock TLx (5 bits) THx (8 bits)

TFx Overflow flag

8051 Timer Mode 0


Saturday, February 23, 2013 Prof. Ankit D. shah 20

Timer Mode 1 16 Bit Timer / Counter:


8 bits from TH, another 8 bits from TL. Each byte loaded separately Counts UP (so initialize with negative value). The counters start counting when enabled (by a bit in the TCON). Counts time (when acting as timer) until a preset number as specified by the THx and TLx registers is reached. Generates an overflow (interrupt) when timed out. In order to repeat the process the registers TH and TL must be reloaded with the original value and the timer overflow flag must be reset under the control of the program.

Saturday, February 23, 2013

Prof. Ankit D. shah

21

XTAL OSC.
Timer clock TLx (8 bits) THx (8 bits)

Cont.
TFx Overflow flag

/12 Example
#include ankit.h ankit.h void DELAY (void); void main (void) { TMOD = 0X01; while (1) { TL0 = 0X0F2; TH0 = 0XFF; P1.5 = ~P1.5; DELAY (); } } Saturday, February 23, 2013

TRx

Example (cont)
{ TR0 = 1;

8051 Timer Mode 1


FFF2,FFF3,FFF4,,FFFE,FFFF,0000 12 MHz, Timer clock period = 1s 14 counts Half pulse = 14 s Full period = 28 s Frequency = 1/28 s = 35.714 kHz

void DELAY (void)

while (TF0 == 0); TR0 = 0; TF0 = 0; }

Prof. Ankit D. shah

22

Finding load count value:

Assume that we know the desired amount of timer delay, tdelay. To calculate the values to be loaded into the TL and TH registers, using crystal frequency of Xcrystal MHz, the following steps are needed:
ActualCount

t delay X crystal 12

t delay timer clock period

Step Step Step Step

1: 2: 3: 4:

find n, where n = Perform 65536 - n Convert step 2 result to hex, assume this result is yyxx Set TL = xx and TH = yy.

Saturday, February 23, 2013

Prof. Ankit D. shah

23

Cont.

Assuming a desired time delay of 5 ms and Xcrystal = 11.0592 MHz, determine the values of TL and TH in hex.
Actualcoun n 5 10 Loadcount TL 00 H TH EEH
3

t delay X crystal 12
6

t delay timer clock period

11 . 0592 10

/ 12 4608 1200 H

0000 H 1200 H EE 00 H

Saturday, February 23, 2013

Prof. Ankit D. shah

24

Programming in Moe 1:

To generate a time delay using timer mode 1, the following steps are taken. 1. Load the TMOD value register indicating which timer (0 or 1) is to be used and which timer mode is selected. 2. Load registers TL and TH with initial count values. 3. Start the timer by the statements TR0 = 1; for timer 0 and TR1 = 1; for timer 1. 4. Keep monitoring the timer flag (TF) with the while (TFx == 0); statement to see if it is raised. Get out of the loop when TF becomes high.
Saturday, February 23, 2013 Prof. Ankit D. shah 25

Cont..
5. Stop the timer with the statements TR0 = 0; or TR1 = 0;, for timer 0 and timer 1, respectively. 6. Clear the TF flag for the next round with the statements TF0 = 0; or TF1 = 0;, for timer 0 and timer 1, respectively. 7. Go back to step 2 to load TH and TL again. The programming techniques mentioned here are also applicable to counter/timer mode 0. The only difference is in the number of bits of the initialization value.
Saturday, February 23, 2013 Prof. Ankit D. shah 26

Cont.

Embedded C program to continuously generate a square wave of 2 kHz frequency on pin P1.5 using timer 1. Assume the crystal oscillator frequency to be 12 MHz. Solution: Initial calculations are as follows. The period of the square wave is T = 1/(2 kHz) = 500 s. Each half pulse = 250 s. The value n for 250 s is: 250 s /1 s = 250 65536 - 250 = FF06H. TL = 06H and TH = 0FFH.

Saturday, February 23, 2013

Prof. Ankit D. shah

27

Square Wave Generation on Pin P1.5


#include ankit.h ankit.h void main (void) { TMOD = 0X10; Initialize timer 1 in mode 1 P1.5 = 0; To start square wave from low period while (1) { TL1 = 0X06; To load lower 88-bit of count TH1 = 0XFF; To load higher 88-bit of count TR1 = 1; To start timer 1 while (TF1 == 0); To check whether timer 1 ;has completed the count or not TR1 = 0; To stop timer 1 P1.5 = ~P1.5; Complement P1.5 to make it ;from low to high and high to low ` TF1 = 0; To clear the timer 1 flag } }In this program we have to load count value again as mode -1 is not auto reload.
Saturday, February 23, 2013 Prof. Ankit D. shah 28

Mode 2 8 Bit Auto Reload:


TL0 or TL1 is an 8 bit counter/timer TH0 or TH1 holds an initialization value TL reloaded from TH on 255 to 0 transition The reload leaves TH unchanged. Used to generate UART baud rate clock; a periodic flag or interrupt.
Timer clock TLx (8 bits) TFx Overflow flag Reload THx (8 bits)

8051 Timer Mode 2


Saturday, February 23, 2013 Prof. Ankit D. shah 29

Programming in Moe 2:
1. Load the TMOD value register indicating which timer (0 or 1) is to be used; select timer mode 2. 2. Load TH register with the initial count value as well as first time load TL with the same value. As it is an 8-bit timer, the valid range is from 00 to FFH. 3. Start the timer. 4. Keep monitoring the timer flag (TFx) to see if it is raised. Get out of the loop when TFx goes high. 5. Clear the TFx flag. 6. Go back to step 4, since mode 2 is auto-reload.

Saturday, February 23, 2013

Prof. Ankit D. shah

30

Cont.
Embedded C program segment that uses timer 1 in mode 2 to toggle P1.0 once whenever the counter reaches a count of 100. Assume the timer clock is taken from external source P3.5 (T1). Solution:
Gate C/T M1 M0 Gate C/T M1 M0

1 Timer 1

0 Timer 0

The TMOD value is 60H The initialization value to be loaded into TH1 is 256 - 100 = 156 = 9CH
Saturday, February 23, 2013 Prof. Ankit D. shah 31

Cont.
#include ankit.h ankit.h void main (void) { TMOD = 0X60; Counter 1 in mode 2, C/T = 1 P3.5 = 1; To make T1 pin as an input pin TH1 = 0X9C; To load count value TL1 = 0X9C; To load count value first time TR1 = 1; To start timer 1 P1.0 = 0; Initially to make it low while (1) { while (TF1 == 0); To check whether timer 1 ;has completed the 100 count P1.0 = ~P1.0; Complement P1.0 TF1 = 0; To clear the timer 1 flag

} Note in the above program the role of the statement P3.5 =1;. We make P3.5 an input pin by making it high to accept the external signal as an input.
Saturday, February 23, 2013 Prof. Ankit D. shah 32

Significance of Timer/Counter Interrupt:


The counters have been included on the chip to relieve the microcontroller of timing and counting chores. When the program whishes to count a certain numbers of internal pulses or external events, a number is placed in one of the counters. The number represents the maximum count less the desired count, plus 1. The counter increments form the initial number to the maximum and then rolls over 0 on the final pulse and also sets a timer flag. The flag condition may be tested by an instruction to tell the program that the count has been accomplished, or the flag may be used to interrupt the program.
Saturday, February 23, 2013 Prof. Ankit D. shah 33

Timer/Counter as a Timer:
If a counter is programmed to be a timer, it will count the internal clock frequency of the 8051 oscillator divide by 12. As an example, if the crystal frequency is 6 megahertz, then the timer clock will have a frequency of 500 kilohertz. The resultant timer clock is gate to the timer by means of the circuit shown in timer / counter control logic. In order for oscillator clock pulses to reach the timer, the C/T bit in the TMOD register must be set to 0 (timer operation). Bit TRX in the TCON register must be set to 1 (timer run), and the gate bit in the TMOD register must be 0, or external pin INTX must be a 1. In other words, the counter is configured as a timer, then the timer pulses are gated to the counter by the run bit and the gat bit or external input bits INTX.
Saturday, February 23, 2013 Prof. Ankit D. shah 34

Timer/Counter as a Counter:
The only difference between counting and timing is the source of the clock pulses to the counters. When used as a timer, the clock pulses are sourced from the oscillator through the divide by 12 circuit. When used as a counter, pin T0 (P3.4) supplies pulses to counter 0 and pin T1 (P3.5) to counter 1. The C/T bit in TMOD must be set to 1 to enable pulses from the TX pin to reach the control circuit shown timer / counter control logic. The input pulse on TX is sampled during P2 of state 5 every machine cycle. A change on the input from high to low between samples will increment the counter. Each high and low state of the input pulse must thus be held constant for at least one machine cycle to ensure reliable counting. Since this takes 24 pulses, the maximum input frequency that can be accurately counted is the oscillator frequency divide by 24. For our 6 megahertz crystal, the calculation yields a maximum external frequency of 250 kilohertz.
Saturday, February 23, 2013 Prof. Ankit D. shah 35

Timing Functions:
Functions are used by main programs in what is known as a transparent manner that is, the main program can use the functions without being bothered by the details of what is actually going on in the subroutine. Usually, the main program preloads certain locations with data (argument), calls the function, then gets the results back in the preload locations (return value). The function must take great care to save the values of all memory locations in the system that the function uses to perform internal functions and restore these values before returning to the call program. Failure to save values results in occasional bugs in the main program. The main program assumes that everything is the same both before and after a subroutine is called. Finally, good documentation is essential so that the user of the function knows precisely how to use it.
Saturday, February 23, 2013 Prof. Ankit D. shah 36

Time Delay:
Perhaps the most-used subroutine is one that generates a programmable time delay. Time delays may be done by using software loops that essentially do nothing for some period, or by using hardware timers that count internal clock pulses. The hardware timers may be operated in either a software or a hardware mode. In the software mode, the program inspects the timer Overflow flag and jumps when it is set. The hardware mode uses the interrupt structure of the 8051 to generate an interrupt to the program when the timer overflows. The interrupt method is preferred whenever microcontroller time is very precious. The interrupt mode allows the microcontroller to continue to execute useful code while the time delay is taking place. Both the pure software and timer-software modes tie up the microcontroller while the delay is taking place. If the interrupt mode is used, then the program must be have an interrupt handling function at the dedicated interrupt program vector location specified by the engineer who design the microcontroller. The programmer must be also have programmed the various interrupt control registers. This degree of non-transparency generally means that interrupt driven functions are normally written by the user as needed and not used from a purchased library of functions.
Saturday, February 23, 2013 Prof. Ankit D. shah 37

Pure Software Time Delay:


In this we generate time delay without the use of on chip timer / counter. To develop pure software time delay program, we must know the machine cycle required by each instruction used in the program. So, this is one of the limitations of pure software time delay program. Second limitation of it is that it will not generate accurate time delay and third limitation is the controller busy or tie up in the execution of time delay program so, it can not give attention to important task while execution of pure software time delay. Normally in pure software time delay program, we will find count value as per time delay required to be generated. It the count value is not integer then we will truncate it and then load that count value in one register or more than one register as a down counter. To generate accurate time delay we have to insert NOP instruction in the program as per the requirement and it kept outside the loop. In short the pure software time delay program is one kind of down counter program in which we load any one register or more than register by count value and then decrement it until count becomes 0.
Saturday, February 23, 2013 Prof. Ankit D. shah 38

Software Polled and Pure Hardware Time Delay:


A delay that uses the timers to generate the delay and a continuous software flag test (the flag is polled to see whether it is set) to determine when the timer have finished the delay. If delay must be done in a program, but the program can not wait while the delay is computed, then the delay will have to be done using a timer enable to interrupt the main program. A timer interrupt allows a timer to run and generate a time delay at the same time as the main program loop is executing.

Saturday, February 23, 2013

Prof. Ankit D. shah

39

Significance of Pure Software Time Delay:


Very short intervals can not be generated using timers because of the overhead needed to start and stop the timers. Such short timing intervals are usually generated using tight software loops. The following code segment generates a 250 KHz wave with 25% duty cycle on P1.0. Assume XTAL = 12 MHz. LOOP: SETB CLR SJMP P1.0 P1.0 LOOP ;1 machine cycle ;1 machine cycle ;2 machine cycles

CLR SETB 1 s 1 s SJMP 1 s 4 s 1 s SETB 1 s

Saturday, February 23, 2013

Prof. Ankit D. shah

40

Cont.

The following code segment generates a 250 KHz square wave on P1.0. Assume XTAL = 12 MHz.
NOP SETB P1.0 JBC P1.0,LOOP ;1 machine cycle ;1 machine cycle ;2 machine cycles

LOOP:

NOP 1 s

SETB 1 s 4 s

JBC P1.0 1 s 1 s

NOP 1 s

Saturday, February 23, 2013

Prof. Ankit D. shah

41

Constraint of Generating Time Delay by different technique:


Time Interval No limit No limit 65536 machine cycles 256 machine cycles Technique Software loops 16-bit plus software loops 16-bit timer Auto-reload (8-bit)

Saturday, February 23, 2013

Prof. Ankit D. shah

42

Steps to be consider while developing time delay program by Software-Polled Timer:


Find out the actual count value from the value of time delay to be generated and the time period of crystal frequency /12 or machine cycle time. Decide the timer mode from the value of actual count. Find out the load count value from the actual count value. The equation of load count value is load count value = maximum count value actual count value + 1. Select the mode and make the gate bit 0 by loading appropriate value in TMOD SFR for a timer / counter to be used. Copied the load count value in lower and higher byte of respective timer / counter. Set the run bit of respective timer / counter in TCON SFR by using TRx = 1; statement. Saturday, February 23, 2013 Prof. Ankit D. shah 43

Cont
Check the status of timer overflow flag of respective timer / counter in TCON SFR by using while syntax (while (TFx == 0);). If timers overflow flag is set then first cleared (set to 0) timer run bit of respective timer / counter in TCON SFR by using TRx = 0; statement. Perform the task which you have to perform after the generation of particular time delay by software polled timer. Cleared the timer overflow flag of respective timer / counter in TCON SFR by using TFx = 0; statement.
Saturday, February 23, 2013 Prof. Ankit D. shah 44

Steps to be consider while developing time delay program by Pure Hardware Delay:
Find out the actual count value from the value of time delay to be generated and the time period of crystal frequency /12 or machine cycle time. Decide the timer mode from the value of actual count. Find out the load count value from the actual count value. The equation of load count value is load count value = maximum count value actual count value + 1. Select the mode and make the gate bit 0 by loading appropriate value in TMOD SFR for a timer / counter to be used in main program. Copied the load count value in lower and higher byte of respective timer / counter in main program. Set the run bit of respective timer / counter in TCON SFR by using TRx = 1; statement in main program. Enable the interrupt bit of respective timer / counter and EA (D7) bit in interrupt enable register by using EA = 1; statement in main program.
Saturday, February 23, 2013 Prof. Ankit D. shah 45

Cont.
Also decide the priority of respective timer / counter interrupt in interrupt register by making it set or cleared using statement in main program. Also develop interrupt subroutine of respective timer / counter and stored it at predefined memory locations. For example for timer / counter 0 it must be stored at 000BH and for timer / counter 1 it must be stored at 001BH memory location. Then, continue the development of main program. Now, when you execute the main program and whenever respective timer / counter interrupt overflow is set due to rolls of all bits from 1s to 0s of respective timer / counter. Automatic program sequence change from main program to the respective timer / counter interrupt function is executed and respective timer / counter overflow flag is cleared. Now, if you want to execute timer / counter interrupt function only one time then you must cleared respective timer / counter run bit in TCON SFR by using TRx = 0; statement. After the execution of timer / counter interrupt function, the program sequence come back to an instruction in main program where the interrupt occurred.
Saturday, February 23, 2013 Prof. Ankit D. shah 46

Question Answer Session


Saturday, February 23, 2013 Prof. Ankit D. shah 47

Thank you

Вам также может понравиться