Eet 303 M5
Eet 303 M5
SYLLABUS
8051 Timer/counter programming - Serial port programming - Interrupt programming in assembly
language and embedded C.
Interfacing –ADC - DAC and temperature sensor
5.18051 Timer/Counter
5.1.1Timer SFRs:
TCON
TCONSFRandits individualbits
- IT0/IT1:UsedfortimerInterrupts
- IE0/IE1:UsedforexternalInterrupts
- TR0/TR1:Timer 0/1 run control bit. Set to 1 to start the timer / counter.
- TF0/TF1:Timer0/1 overflow flag. It is set when timer rolls from all 1s to 0s
TMOD
TMODSFRanditsindividualbits
- M0/M1:setstheModeoftherespectivetimer
- C/T:ExternalCounter/InternalTimerselect
1=Counter,0= Timer
- Gate:Whenset(1),timerrunsonly whenrespectiveINTinputis high.
5.1.2 Timer O p e r a t i n g Modes:
Four operating modes are there:
- Mode 0:13 bittimer
- Mode 1:16-bittimer
- Mode 2:8-Bitautoreload
- Mode 3:Splittimermode
Timer Mode 0: (13 bit Mode)
In order to program 8051 timers, it is important to know the calculation of initial count value
to be stored in the timer register. The calculations are as follows.
Mode 1:
➢ Load the TMOD value register indicating which timer (0 or 1) is to be used and which timer
mode is selected.
➢ Load registers TL and TH with initial count values.
➢ Start the timer by the instruction “SETB TR0” for timer 0 and “SETB TR1” for timer 1.
➢ 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.
➢ Stop the timer with the instructions “CLR TR0” or “CLR TR1”, for timer 0 and timer 1,
respectively.
➢ Clear the TF flag for the next round with the instruction “CLR TF0” or “CLR TF1”, for timer
0 and timer 1, respectively.
➢ Go back to step 2 to load TH and TL again.
Mode 0:
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. (13 bits)
Mode 2:
➢ Load the TMOD value register indicating which timer (0 or 1) is to be used; select timer
mode 2.
➢ Load TH register with the initial count value. As it is an 8-bit timer, the valid range is from
00 to FFH.
➢ Start the timer.
➢ Keep monitoring the timer flag (TFx) with the “JNB TFx,target” instruction to see if it is
raised. Get out of the loop when TFx goes high.
➢ Clear the TFx flag.
➢ Go back to step 4, since mode 2 is auto-reload.
The period of the required square wave is T = 1/(2 kHz) = 500 µs.
Delay required for each half cycle = 250 µs.
Timer Clock frequency = Master Frequency/12
= 12MHz/12 =1 MHz
Timer Clock period = 1/ Timer Clock frequency = 1/1MHz = 1 µs.
Value to be loaded in decimal, n = 65536 – (Delay Required/Timer clock period)
Value to be loaded in decimal, n = 65536 – (250 µs /1 µs)
=65536 – (250) = 65286 = FF06h
Example#2. Write a 8051 C program to toggle all the bits of port P1 continuously with 1mS
delay in between. Use Timer 0, 16-bit mode to generate the delay.
Solution:
Example#3. Write an 8051 C program to toggle only bit P1.5 continuously every 50 mS. Use Timer 1,
mode 1 (16-bit) to create the delay.
1. SBUF Register: Serial Buffer (SBUF) register is an 8-bit register. It has separate
SBUF registers for data transmission and for data reception. For a byte of data to be
transferred via the TXD line, it must be placed in SBUF register. Similarly, SBUF holds
the 8-bit data received by the RXD pin and read to accept the received data.
2. SCON register: The contents of the Serial Control (SCON) register are shown below.
This register contains mode selection bits, serial port interrupt bit (TI and RI) and also
the ninth data bit for transmission and reception (TB8 and RB8).
3. PCON register:The SMOD bit (bit 7) of PCON register controls the baud rate in
asynchronousmode transmission.
1. Mode 0
In this mode serial port runs in synchronous mode. The data is transmitted and received
through RXD pin and TXD is used for clock output. In this mode the baud rate is 1/12 of clock
frequency.
2. Mode 1
In this mode SBUF becomes a 10 bit full duplex transceiver. The ten bits are 1 start bit, 8 data
bit and 1 stop bit. The interrupt flag TI/RI will be set once transmission or reception is over.
In this mode the baud rate is variable and is determined by the timer 1 overflow rate.
Baud rate = [2smod/32] x Timer 1 overflow Rate
= [2smod /32] x [Oscillator Clock Frequency] / [12 x [256 – [TH1]]]
3. Mode 2
This is similar to mode 1 except 11 bits are transmitted or received. The 11 bits are, 1 start
bit, 8 data bit, a programmable 9th data bit, 1 stop bit.
Baud rate = [2smod /64] x Oscillator Clock Frequency
4. Mode 3
This is similar to mode 2 except baud rate is calculated as in mode 1
5.3 INTERRUPTS IN 8051
➢ When an interrupt event occurs, the microcontroller pause its current program and
attend to the interrupt by executing an Interrupt Service Routine (ISR).
➢ At the end of the ISR, the microcontroller returns to the program it had pause and
continue its normal operations.
➢ 8051 microcontrollers consists of two external hardware interrupts: INT0 and INT1.
➢ These can be edge triggered or level triggered.
➢ In level triggering, the low at the interrupt pin enables the interrupt.
➢ In edge triggering, the high to low transition enables the edge triggered interrupt.
➢ This edge triggering or level triggering is decided by the TCON register.
➢ 8051timersalwayscountup.Whentheircountrollsoverfromthemaximum
countto0000,theysetthecorrespondingtimerflagTF1orTF0inTCON and
aninterruptoccurs.
stored.
5.3.3 SequenceofEventsafteraninterrupt
Whenanenabledinterruptoccurs,
1.ThePCissavedonthestack.
2.Otherinterruptsoflowerpriorityandsamepriorityaredisabled.
3.Exceptfortheserialinterrupt,thecorrespondinginterruptflagiscleared.
4.PCisloadedwiththevectoraddresscorrespondingtotheinterrupt.
1.PCisrestoredbypoppingthestack.
2.Interruptstatusisrestoredtoitsoriginalvalue.(Sameandlowerpriority
interruptsrestoredtooriginalstatus).
3. TCON Register:
➢ In addition to the above two registers, the TCON register specifies the type of
external interrupt to the 8051 microcontroller, as shown in the figure.
➢ The two external interrupts, whether edge or level triggered, specify by this
register by a set, or cleared by appropriate bits in it.
5.3.5 Interrupt Programming in 8051
While programming interrupts, first thing to do is to specify the microcontroller which interrupts
must be served. This is done by configuring the Interrupt Enable (IE) register which enables or
disables the various available interrupts.
To enable any of the interrupts, first the EA bit must be set to 1. After that the bits corresponding
to the desired interrupts are enabled. ET0 and ET1 bits are used to enable the Timer Interrupts 0
and 1 respectively. EX0 and EX1 are used to enable the external interrupts 0 and 1. ES is used
for serial interrupt.
EA bit acts as a lock bit. If any of the interrupt bits are enabled but EA bit is not set, the interrupt
will not function. By default all the interrupts are in disabled mode.
Note that the IE register is bit addressable and individual interrupt bits can also be accessed.
For example –
Setting the bits of IE register is necessary and sufficient to enable the interrupts. Next step is to
specify the controller what to do when an interrupt occurs. This is done by writing a subroutine or
function for the interrupt. This is the ISR and gets automatically called when an interrupt occurs. It
is not required to call the Interrupt Subroutine explicitly in the code.
An important thing is that the definition of a subroutine must have the keyword interrupt followed
by the interrupt number. A subroutine for a particular interrupt is identified by this number. These
subroutine numbers corresponding to different interrupts are tabulated below.
0 External0 EX0
1 Timer0 IT0
2 External1 EX1
3 Timer1 IT1
4 Serial ES
5 Timer2 ET2
For example : Interrupt routine for Timer1
The timer interrupts IT0 and IT1 are related to Timers 0 and 1, respectively. (Please refer 8051
Timers for details on Timer registers and modes.) The interrupt programming for timers involves
following steps .
2. Load initial values in THx and TLx for mode 0 and 1; or in THx only for mode 2.
5. Write subroutine for Timer Interrupt. The interrupt number is 1 for Timer0 and 3 for Timer1.
6. To stop the timer, clear TRx in the end of subroutine. Otherwise it will restart from 0000H in case
of modes 0 or 1 and from initial values in case of mode 2.
7. If the Timer has to run again and again, it is required to reload initial values within the routine itself
(in case of mode 0 and 1). Otherwise after one cycle timer will start counting from 0000H.
Example code in Assembly Language:
Write a program that continuously get 8-bit data from P0 and sends it
to P1 while simultaneously creating a square wave of 200 μs period
on pin P2.1. Use timer 0 to create the square wave. Assume that
XTAL=11.0592MHz.
Solution:
We will use timer 0 in mode 2 (auto reload). TH0 = 100 μs /1.085 μs = 92
ORG 0000H
LJMP MAIN
// Main Program
ORG 0030H
MAIN: MOV TMOD, #02H ; Timer0 in auto reload mode
MOV P0, FFH ; P0 as input port
MOV TH0, #92d ; initial value for timer
MOV IE, #82H ; enable interrupt
SETB TR0
BACK: MOV A, P0
MOV P1,A
SJMP BACK
END
Timer interrupt to blink an LED; Time delay in mode1 using interrupt method
The external interrupts are the interrupts received from the (external) devices interfaced with the
microcontroller. They are received at INTx pins of the controller. These can be level triggered or
edge triggered. In level triggered, interrupt is enabled for a low at INTx pin; while in case of edge
triggering, interrupt is enabled for a high to low transition at INTx pin. The edge or level trigger is
decided by the TCON register.
Setting the IT0 and IT1 bits make the external interrupt 0 and 1 edge triggered respectively. By
default these bits are cleared and so external interrupt is level triggered.
Note : For a level trigger interrupt, the INTx pin must remain low until the start of the ISR and
should return to high before the end of ISR. If the low at INTx pin goes high before the start of
ISR, interrupt will not be generated. Also if the INTx pin remains low even after the end of ISR, the
interrupt will be generated once again. This is the reason why level trigger interrupt (low) at INTx
pin must be four machine cycles long and not greater than or smaller than this.
2. Write routine for external interrupt. The interrupt number is 0 for EX0 and 2 for EX1
respectively.
Example code#1
Example code#2
Example code#3
sent or received, the serial interrupt is generated and the TI or RI flag goes high. Here, the TI or
RI flag needs to be cleared explicitly in the interrupt routine (written for the Serial Interrupt).
3. Write routine or function for the Serial Interrupt. The interrupt number is 4.
Example code#1
Example code#2
Example code#3
Example: ADC interfacing program to read the digital value output of ADC from Port 2 and send it
to Port3.
Solution:
➢ The analog input I/P2 is used and therefore address pins A,B,C should be 0,1,0 respectively
to select I/P2.
➢ Analog channel select lines are connected at P1.2,P1.3 and P1.4
➢ Start of conversion signal is connected at P1.0.
➢ End of conversion signal is connected at P1.1. Hence P1.1 pin is an input pin.
➢ ADC digital output is connected at Port2. Hence Port 2 is input port.
Program:
ORG 0000H
MOV A,#FFH
MOV P2,A ;Initialize P2 as input port
SETB P1.1 ;Initialize P1.1 as input port
unsignedcharadc_val;
voidmain()
{
P2 = 0xFF; //Initialize Port2 as input port
eoc = 1; //Initialize Port1.1 as input pin
where DO is the LSB, D7 is the MSB for the inputs, and I ref is the input current that must
be applied to pin 14
Programming Example:
Write an ALP to generate Triangular wave form on port P1 of 8051 microcontroller using DAC.
ORG 0000h
MOV P1, #00H
REPEAT: ACALL TRIWAVE
SJMP REPEAT