MA Unit2 Part 2
MA Unit2 Part 2
• Reset sources
• Oscillator options
• Port structure
• Timers
• Timer programming
• Interrupt handler
• Power management modes
Timers in C8051F340 Microcontroller
Timers
• Timers are used for: interval timing(delays), event counting(counter) or baud rate generation.
Software delay: The dummy method to produce delay is by using No Operation (NOP) in loop & repeat it ex. 100 times called software
delay.
• Processor is busy in execution of it.
1) Interval timing applications: In this a timer is programmed to overflow at a regular interval and the following:
• Set the timer overflow flag or
• Generate an interrupt
• This can also be used to generate waveforms at set frequencies
2) Event counting: It is used to determine the number of occurrences of an event, rather than to measure the elapsed time between
events.
• In this case, the timer functions as a counter.
• An “event” is any external stimulus that provides a high-to-low transition at the selected input pin
• The timers can also function as the baud rate generators for the C8051F340’s internal serial ports (UART0 and UART1)
• “Baud rate” is the bit rate of the serial port (the time period of a bit).
• The baud rate is the rate at which information is transferred in a communication channel.
• Baud rate is commonly used in electronics that use serial communication.
• In the serial port context, "9600 baud" means that the serial port is capable of transferring a maximum of 9600 bits per second.
Difference between Timer and Counter
Counter: Uses External Clock
Timer: Uses Internal Oscillator Clock
T0 T1
T0 T1
T2 T3
T2 T3
Fosc
Timer: If we know the frequency of the clock which is fixed, then before counter finishes we
find the time in which it can count ie you are working as a timer . (Internal clk freq is fixed)
So, If the freq is fixed-------- it is a timer.
Counter: For variable freq (you are counting at different speed) then we don’t know when it
will finish. Then you are working as a counter.
So, If the freq is variable------- it is a Counter.
MCU includes four counter/timers:
• Total 4 timers in CIP-51.
• Two are 16-bit counter/timers compatible with those found in the
standard 8051, and
• Two are 16-bit auto-reload timer for use with the ADC, SMBus, USB
(frame measurements), Low-Frequency Oscillator (period
measurements), or for general purpose use.
• These timers can be used to measure time intervals, count external
events and generate periodic interrupt requests.
• Timer 0 and Timer 1 are nearly identical and have four primary modes
of operation.
• Timer 2 and Timer 3 offer 16-bit and split 8-bit timer functionality
with auto-reload.
Timers
• Software delay : Using NOP in a loop to produce a delay.
• Hardware delay: Using hardware/timers to produce a delay.
Bit addressable
means u can use
these bits
individually.
Ans: Draw the TCON dig, Explain about Timer 0 , Timer 1 and Interrupt 0 & Interrupt 1
0 1 1 0 1 0 0 1
SFR Definition 21.3. CKCON: Clock Control
SFR Definition 21.3. CKCON: Clock Control
TMOD : Timer mode register :
It has 4 modes, 8-bit, 4-bits for Timer 0 & 4-bits for Timer 1 (They are identical bits)
SFR TMOD: Timer Mode
1 1 0 1 0 0 0 0
• TCON & TMOD are summarized in one Logic Diagram (Timer/Counter Logic Diagram):
Timer 0 & 1: Mode 0 (M0) : 13 bit Timer / Counter
(5-Bit) (8-Bit)
CLK
(8-Bit) (8-Bit)
CLK
Therefore T1 mode is used to produce the biggest delay and most preferred.
Timer 0 & 1 Mode 1: 16bit Counter/Timer
• Mode 1 configures Timer 0 and Timer 1 to operate as 16-bit
counter/timers.
• TH0 and TL0 holds the count.
• When the count in TH0 and TL0 overflows from all ones to 0x00, the
timer overflow flag is set.
• If Timer 0 interrupts are enabled, an interrupt will occur when the TF0
flag is set.
• The count in TH0 and TL0 should be loaded for next iteration.
T0 Mode 1 Block Diagram
Note: The diagram & explanation for T0 Mode 0 & Mode 1 are same, only Mode 0 is 13 bit and Mode 1 is 16 bit register
Mode 1 : 16 bit Counter / Timer
Timer 0 & 1 Mode 2 (M2) : 8 bit Timer / Counter with “Auto-Reload”
(8-Bit)
CLK
THX
(8-Bit)
• This mode automatically reload the counter (with the original count & not the random count) after completing
previous count without intervention of programmer.
• The system has to keep the backup of count. So system has 2 count, 1 for counting & 1 for reloading. (8-bit)
• This is continuous mode and all other are single mode ie when they finish counting, they stop after overflow.
(8-Bit)
CLK
• T0 is divided into 2 independent timers of 8-bit,
From i/p stage
TL0 TF0 INT
TL0 & TH0,
• so total 3 timers T1, TL0 & TL0 .
(8-Bit)
Fosc÷12 • T1 is used in serial bit communication where
TH0 TF1 INT the data bit will be sent bit by bit & don’t
require the timer overflow flag TF1.
• Timer 1 in mode 1
• Timer 1 in mode 2
• Timer 0 in mode 1
• Timer 0 in mode 2
• Timer 1 in mode 2 and Timer 0 in mode 1
Solution:
0 0 0 1 0 0 0 0
0 0 1 0 0 0 0 0
0 0 1 0 0 0 0 1
Time Delay Calculations – For Mode 1(16-bit)
Procedure for Time to Count Calculation – For Mode 1
Delay Required=
3mSec
Delay Required= 2mSec
ANS:
COUNT=(17517)d
= (446D)hex
TH0=44h TL0=6Dh
Time Delay Calculations – For Mode 2(8-bit)
Procedure for Time to Count Calculation– For Mode 2
Delay Required=
20usec
Procedure for Count to Time Calculation – For Mode 1
Example for Count to time calculation – For Mode 1
Procedure for Count to Time Calculation – For Mode 2
Example for Count to time calculation – For Mode 2
Steps to program in mode 1
Mode 1 : 3msec
Write an embedded C program to flash the LED connected to Port P4 continuously at an interval of 3mSec. Use Timer 0 in Mode 1 for generating the delay.
void Delay(); {
TH0 = 0x73; //Load High Byte of Count in TH0
int main()
TL0 = 0x52; //Load Low Byte of Count in TL0
{
TR0 = 1; //Start the Timer
P4MDOUT = 0xFF; // Configure Port4 as output
while (TF0 ==0 )
TMOD = 0x01; // Select Timer 0 in Mode 1 {
CKCON = 0x04; // Select System Clock for Timer 0 ---------------------
while (1) } ; //Wait for overflow
{ TF0 = 0; //Clear overflow flag
P4 = ~P4; // Compliment Port4 for flashing TR0 = 0; //Stop the Timer
status of LED
}
return 0;
}
Write an embedded C program to flash the LED connected to Port P4 continuously at an interval of 20usec Use Timer 1 in Mode 2 for generating the delay.
20u
20
u
Mode 2 : 2ousec
Write an embedded C program to flash the LED connected to Port P4 continuously at an interval of 20usec Use Timer 1 in Mode 2 for generating the delay.
void Delay(); {
• ISR tells the processor or controller what to do when the interrupt occurs. The
interrupts can be either hardware interrupts or software interrupts.
Hardware Interrupt and Software Interrupt
• Hardware Interrupt
A hardware interrupt is an electronic alerting signal sent to the processor from an external device, like a
disk controller or an external peripheral.
For example, when we press a key on the keyboard or move the mouse, they trigger hardware
interrupts which cause the processor to read the keystroke or mouse position.
• Software Interrupt
A software interrupt is caused either by an exceptional condition or a special instruction in the
instruction set which causes an interrupt when it is executed by the processor.
For example, if the processor's arithmetic logic unit runs a command to divide a number by zero, to
cause a divide-by-zero exception, thus causing the computer to abandon the calculation or display an
error message.
Software interrupt instructions work similar to subroutine calls.
Introduction to Interrupts
• An interrupt is the occurrence of a condition that causes a temporary suspension
of a program while the condition is serviced by another (sub) program
• The (sub) program that deals with an interrupt is called an interrupt service
routine (ISR) or interrupt handler
Interrupts
• An interrupt is an external or internal event that interrupts the
microcontroller to inform it that a device needs its service.
Interrupts vs. Polling
• A single microcontroller can serve several devices.
• There are two ways to do that:
• interrupts
• polling.
• The program which is associated with the interrupt is called the interrupt
service routine (ISR) or interrupt handler.
Interrupts
• When an interrupt occurs, the main program temporarily suspends execution and branches
to the ISR
• The ISR executes, performs the desired operation, and terminates with a “return from
interrupt” (RETI) instruction
• Starts to execute the interrupt service routine until RETI (return from interrupt).
• Upon executing the RETI the microcontroller returns to the place where it was interrupted.
Get pop PC from stack
What is Polling?
• The state of continuous monitoring is known as polling.
• The microcontroller keeps checking the status of other devices;
and while doing so, it does no other operation and consumes all its
processing time for monitoring.
• This problem can be addressed by using interrupts.
• In the interrupt method, the controller responds only when an
interruption occurs.
• Thus, the controller is not required to regularly monitor the status
(flags, signals etc.) of interfaced and inbuilt devices.
Interrupt Service Routine
For every interrupt, there must be an interrupt service routine (ISR),
or interrupt handler.
When an interrupt occurs, the microcontroller runs the interrupt
service routine.
For every interrupt, there is a fixed location in memory that holds
the address of its interrupt service routine, ISR.
The table of memory locations set aside to hold the addresses of
ISRs is called as the Interrupt Vector Table.
Comparison of Polling and Interrupt Method
Execution Flow
Interrupt handler
• Interrupt handler provides 16 interrupt sources into the CIP-51 (as opposed to 7 for
the standard 8051), allowing numerous analog and digital peripherals to interrupt
the controller.
• An interrupt driven system requires less intervention by the MCU, giving it more
effective throughput.
• Each interrupt source can be individually enabled or disabled through the use of an
associated interrupt enable bit in an SFR (IE-EIE2).
• However, interrupts must first be globally enabled by setting the EA bit (IE.7) to
logic 1 before the individual interrupt enables are recognized.
• Setting the EA bit to logic 0 disables all interrupt sources regardless of the
individual interrupt-enable settings.
Interrupt Summary
Interrupt
Summary
Interrupt Registers
Sr. No Name of Register Function
1.
IE(Interrupt Enable) Enables and Disables interrupts for entire system and also enables interrupts from
SPI, Timer 2, UART0, Timer 1, Timer 0 and external interrupt 1 and 2
2.
IP (Interrupt Priority) Sets the priorities of enabled interrupts in IE register
3. …
• Upon Reset, all the interrupts are disabled even if they are activated.
• The interrupts must be enabled using software in order for the microcontroller to
respond to those interrupts.
• IE (interrupt enable) register is responsible for enabling and disabling the interrupt.
• IE is a bit addressable register.
IE: Interrupt Enable
IE: Interrupt Enable
IP: Interrupt Priority
IP: Interrupt Priority
Power Management Modes
1. Idle
2. Stop.
Power Control Register (PCON)
• Stop mode-
• CPU is halted
• All interrupts inactive
• Internal oscillator is stopped
• All digital & analog peripherals stopped
• External oscillator circuit is not affected
• Setting the Stop Mode Select bit (PCON.1) causes the CIP-51 to enter Stop mode as soon as
the instruction that sets the bit completes execution.
• Stop mode can only be terminated by an internal or external reset.
• If enabled, the Missing Clock Detector will cause an internal reset and there by terminate the
Stop mode.
• The Missing Clock Detector should be disabled if the CPU is to be put to in STOP mode for
longer than the MCD timeout of 100 μsec
Reference
Datasheet:
https://www.silabs.com/documents/public/datasheets/C8051F34x.pdf