MC - Module 3
MC - Module 3
TCNTn (timer/Counter)
InATmega32we have TCNTO, TCNTI, and TCNT2.
The TCNTn register is a counter.
Upon reset, the TCNTn contains zero.
It counts up with each pulse. You can load a value into the TCNTn register or read its value.
TOVn(Timer Overflow)
It is a flag register.
When a time rover flows, its TOVn flag will be set.
TCCRn (timer/counter C o n t r o l register)
This register is used for setting modes of operation.
OCRn(Output Compare Register)
The content of the OCRn is compared with the content of the TCNTn. When they are equal
the OCFn (Output Compare Flag)flag will be set.
The timer registers are located in the I/O register memory. Therefore, you can read or write
from timer registers using IN and OUT instructions, like the other I/O registers.
TIFR(Timer/counter Interrupt Flag Register) register
Timer0 programming
Timer0 is 8-bit in ATmega32.
TCCR0(Timer/Counter Control Register) register
TCCR0 is an 8-bit register used for control of Timer0..
These bits in the TCCR0 register are used to choose the clock source.
If CS02:CS00=000, then the counter is stopped.
If C S 0 2 -CS00 have values between001 and 101, the oscillator is used as clock source and the
timer/counter acts as a timer. In this case, the timers are often used for time delay
generation.
If CS02-CS00 are 110 or 111, the external clock source is used and it acts as a counter.
WGM01 :WGM00
TOV0(Timer0Overflow )
The flag is set when the counter overflows, going from $FF to $00.
When the timer rolls over from $FF to 00, the TOV0 flag is set to 1 and it remains set until
the software clears it.
For example, the following program clears TOVO:
LDI R20,0x01
OUT TIFR,R20 ;TIFR Ob00000001
In this mode, the content of the timer/counter increments with each clock.
Timer1 programming
Timer1 is a16-bit timer.
Its 16-bit register is split in to two bytes.
The TCNT1 is referred as TCNT1L (Timer1 low byte) and TCNT1H (Timer1 high byte).
Timer1 also has two control registers named TCCR1A(Timer/counter1 control register and
TCCR1B.
Timer2 Programming
Timer2 is an 8-bit timer.
Therefore it works the same way as Timer0. But there are two differences between
Timer0 and Timer2:
1. Timer2 can be used as a real time counter.
2. In Timer0, when CS02-CSOO have values 110 or 111,Timer0 counts the
external events. But inTimer2 ,the multiplexer selects between the different scales of the
clock.
Interrupts Polling
Whenever any device needs the The microcontroller continuously monitors the
microcontroller's service, the device notifies it status of a given device; when the status
by sending an interrupt signal. condition is met, it performs the service.
Each device is serviced based on the priority It checks all devices in a round-robin fashion.
as signed to it.
It can ignore a device request. It cannot ignore a device request.
It avoids the tying down the microcontroller. It wastes much of the time by polling devices
that do not need service.
1. It finishes the instruction i t is currently executing a n d saves the address of the next
instruction ( program co unter ) on the stack.
2. It jumps t o a fixed location i n memory c a l l e d the interrupt vector table. The
Interrupt vector table directs the microcontroller to the address of the interrupt service
routine (ISR).
3. The microcontroller starts to execute the interrupt service subroutine until it
reaches the last instruction of the subroutine, which is RETI(return from interrupt).
4. Upon executing the RETI instruction, the microcontroller returns to the place where it
was interrupted .First, it gets the program counter (PC) address from the stack by popping
the top bytes of the stack into the PC. Then it starts to execute from that address.
Sources of interrupts in the AVR
Depending on which peripheral is incorporated in to the chip.
The following are some of the most widely used sources of interrupts in the AYR:
1. There are at least two interrupts set aside for each of the timers, one for over flow and
another for compare match.
2. Three interrupts are set aside for external hardware interrupts. Pins PD2 (PORTD.2),
PD3(PORTD.3), and PB2(PORTB.2) are for the external hardware interrupts INT0 ,INT1, and
INT2, respectively.
3. Serial communication's USART has three interrupts, one for receive and two interrupts for
transmit.
4. The SPI interrupts.
5. The ADC (analog-to-digita lconverter).
Enabling and disabling an interrupt
Upon reset, all interrupts are disabled (masked), meaning that none will be responded to by
the microcontroller if they are activated.
The interrupts must be enabled (unmasked)by software in order for the microcontroller to
respond to them.
The 07 bit(I flag) of the SREG(Status Register)register is responsible for enabling and disabling
the interrupts globally.
Bits of status register is shown below: