Professional Documents
Culture Documents
Timer Using Atmega 16: Sagar Mundada
Timer Using Atmega 16: Sagar Mundada
DEEPAK GUPTA
Timer using
ATMEGA 16
EM TESTED
EM TESTED
TED
EM TES
Sagar Mundada
Timers are widely used in industrial and domestic
application for automating tasks. Microcontrollers
can be used to design versatile and accurate timers
with ease. A simple timer that can be used to turn
on/off a load after user specified time. Timers are
standard features of almost every microcontroller. A
timer in simplest term is a register. Timers generally
have a resolution of 8 or 16 Bits. So a 8 bit timer is
8Bits wide so capable of holding value withing 0255. Its value increases/decreases automatically at
a predefined rate.
ATmega16 is an 8-bit high performance
microcontroller of Atmels Mega AVR family with
low power consumption. Atmega16 is based on
enhanced RISC (Reduced Instruction Set
Computing) architecture with 131 powerful
Circuit Diagram
The circuit is built and simulated on Proteus. The
controller works on a 32.768 KHz crystal.LCD is
connected to the port A of the Atmega controller.
Timer maximum value is FF i.e. 256.When this
value is reached counter starts counting from 0
again and gives hardware interrupt. Since Timer
works independently of CPU it can be used to
measure time accurately. Timers upon certain
conditions take some action automatically or
LM016L
TIME = 0:0:1
D6
D7
D4
D5
D2
D3
D0
D1
RS
R/W
VEE
VSS
VDD
U1
9
RESET
PCO/SCL
14
13
11
12
10
PC1/SDA
13
12
XTAL1
PC2/TCK
XTAL2
PC3/TMS
PC4/TD0
40
39
38
37
36
35
34
33
1
2
3
4
5
6
PA0/ADC0
PC5/TDI
PA1/ADC1
PC6/TOSC1
PA2/ADC2
PC7/TOSC2
22
23
24
25
26
27
28
29
32.768K
PA3/ADC3
PA4/ADC4
PD0/RXD
PA5/ADC5
PD1/TXD
PA6/ADC6
PD2/INT0
PA7/ADC7
PD3/INT1
PD4/OC1B
PB0/XCK/TO
PD5/OC1A
PB1/T1
PD6/1CP
PB2/INT2/AIN0
PD7/OC2
14
15
16
17
18
19
20
21
PB3/OC0/AIN1
PB4/SS
PB5/MOSI
PB6/MOSI
AVCC
PB7/SCK
AREF
ATMEGA16
X1
30
32
CONSTRUCTION
inform CPU. One of the basic conditions is the
situation when timer OVERFLOWS i.e. its counted
upto its maximum value (255 for 8 BIT timers) and
rolled back to 0. In this situation timer can issue an
interrupt and you must write an Interrupt Service
Routine (ISR) to handle the event.ISR
(TIMER1_OVF_vect) {} is interrupt function, which
is called when timer1 overflow interrupt occurs. A
task is performed whenever the timer overflows.
ASSR is the Asynchronous Status Register. It controls
the asynchronous operation.TCCR is the
Timer/Counter control register. This controls the
counting sequence.TCNT is the timer/count
register, whose value increases/decreases with the
clock tick.OCR is the output compare register. The
double buffered Output Compare Register (OCR2)
is compared with the Timer/Counter value at all
times. The result of the compare can be used by the
waveform generator to generate a PWM or
variable frequency output on the Output Compare
Pin (OC2).
Component Used
16x2LCD
The 16x2 LCD is very popular because of it's built in
HD44780 interface module. This module makes it
extremely easy to add an LCD to any project with its
built in character set and easy command structure.
It requires 3 control lines as well as either 4 or 8 I/O
lines for the data bus. The user may select whether
the LCD is to operate with a 4-bit data bus or an 8bit data bus. If a 4-bit data bus is used the LCD will
require a total of 7 data lines (3 control lines plus
the 4 lines for the data bus). If an 8-bit data bus is
used the LCD will require a total of 11 data lines (3
control lines plus the 8 lines for the data bus).
The three control lines are referred to as EN, RS,
and RW.
The EN line is called "Enable." This control line is
D7
D5
D6
14
13
D4
12
D2
D3
11
10
D1
D0
RS
R/W
6
VSS
VEE
VDD
TIME = 0:0:1
Atmega 16
The AVR microcontrollers are based on the
advanced RISC architecture and consist of 32 x 8bit general purpose working registers. Within one
single clock cycle, AVR can take inputs from two
general purpose registers and put them to ALU for
carrying out the requested operation, and transfer
back the result to an arbitrary register. The ALU can
perform arithmetic as well as logical operations
over the inputs from the register or between the
register and a constant. Single register operations
like taking a complement can also be executed in
ALU. We can see that AVR does not have any
register like accumulator as in 8051 family of
microcontrollers; the operations can be performed
between any of the registers and can be stored in
either of them. AVR follows Harvard Architecture
format in which the processor is equipped with
separate memories and buses for Program and the
Data information. Here while an instruction is being
executed, the next instruction is pre-fetched from
the program memory. Since AVR can perform single
cycle execution, it means that AVR can execute 1
million instructions per second if cycle frequency is
1MHz. The higher is the operating frequency of the
controller, the higher will be its processing speed.
CONSTRUCTION
used to tell the LCD that you are sending it data. To
send data to the LCD, your program should make
sure this line is low (0) and then set the other two
control lines and/or put data on the data bus.
When the other lines are completely ready, bring
EN high (1) and wait for the minimum amount of
time required by the LCD datasheet (this varies from
LCD to LCD), and end by bringing it low (0) again.
The RS line is the "Register Select" line. When RS is
low (0), the data is to be treated as a command or
special instruction (such as clear screen, position
cursor, etc.). When RS is high (1), the data being
sent is text data which sould be displayed on the
screen. For example, to display the letter "T" on the
screen you would set RS high.
The RW line is the "Read/Write" control line. When
RW is low (0), the information on the data bus is
being written to the LCD. When RW is high (1), the
program is effectively querying (or reading) the
LCD. Only one instruction ("Get LCD status") is a
read command. All others are write commands--so
RW will almost always be low.
Finally, the data bus consists of 4 or 8 lines
(depending on the mode of operation selected by
the user). In the case of an 8-bit data bus, the lines
are referred to as DB0, DB1, DB2, DB3, DB4, DB5,
DB6, and DB7.LCD is initialized by sending the
following commands
0x38 Function set Interface is 8-bit long
0x0E Cursor ON
0x01 Clear LCD
0x06 cursor movement to right direction
0x80 Move cursor to home location
Pinout
Pin
Symbol Function
1
Vss
ground (0 V)
2
Vdd
5 V logic supply voltage
3
Vo
contrast adjustment
4
RS
H/L register select signal
5
R/W
H/L read/write signal
6
E
H/L enable signal
7-14
DB0 DB7
H/L data bus for 4- or 8bit mode
15
A (LED+)
backlight anode
16
K (LED-) backlight cathode
Code
#include <mega16.h>
#include <lcd.h>
#include <stdio.h>
#define xtal 8000000
#asm
.equ __lcd_port=0x1B ;PORTA
#endasm
unsigned char second, minute,hour;
unsigned char lcd_buff[10];
interrupt [TIM2_OVF] void timer2_ovf_isr(void)
{
if(second==59)
{
second=0;
if(minute==59)
{
minute=0;
if(hour==24)
hour=0;
else
hour++;
}
else
minute++;
}
else
second++;
sprintf(lcd_buff,"Time =
%d:%d:%d",hour, minute, second);
lcd_clear();
lcd_puts(lcd_buff);
}
// Declare your global variables here
void main(void)
{
// Timer/Counter 2 initialization
// Clock source: TOSC1 pin
// Clock value: PCK2/128
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0x08;
TCCR2=0x05;
TCNT2=0x00;
OCR2=0x00;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0x40;
lcd_init(16);
#asm("sei") // Global enable interrupts
while (1);
}