Sunteți pe pagina 1din 3

CONSTRUCTION

DEEPAK GUPTA

Timer using ATMEGA 16


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

EM TESTED EM TESTED

TED EM TES

Sagar Mundada
instructions. Most of the instructions execute in one machine cycle. Atmega16 can work on a maximum frequency of 16MHz.The Timer uses a standard 16x2 LCD module for user interface (UI).The count value is displayed on the LCD module.

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

U1 VDD VSS VEE R/W RS D0 D1 D2 D3 D4 D5 D6 D7 9 E RESET XTAL1 XTAL2 40 39 38 37 36 35 34 33 1 2 3 4 5 6 7 8 PA0/ADC0 PA1/ADC1 PA2/ADC2 PA3/ADC3 PA4/ADC4 PA5/ADC5 PA6/ADC6 PA7/ADC7 PD0/RXD PD1/TXD PD2/INT0 PD3/INT1 PD4/OC1B PB0/XCK/TO PB1/T1 PB2/INT2/AIN0 PB3/OC0/AIN1 PB4/SS PB5/MOSI PB6/MOSI PB7/SCK ATMEGA16 AVCC AREF 30 32 PD5/OC1A PD6/1CP PD7/OC2 14 15 16 17 18 19 20 21 PCO/SCL PC1/SDA 10 12 13 14 13 12 11 7 8 1 2 3 4 5 6 9 PC2/TCK PC3/TMS PC4/TD0 PC5/TDI PC6/TOSC1 PC7/TOSC2 22 23 24 25 26 27 28 29 X1 32.768K

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). We need to optimize the power consumption with processing speed and hence need to select the operating frequency accordingly. There are two flavors for Atmega16 microcontroller: 1. Atmega16:- Operating frequency range is 0 16 MHz. 2. Atmega16L:- Operating frequency range is 0 8 MHz. If we are using a crystal of 8 MHz = 8 x 106 Hertz = 8 Million cycles, then AVR can execute 8 million instructions. ATmega16 is a 40 pin microcontroller. There are 32 I/O (input/output) lines which are divided into four 8-bit ports designated as PORTA, PORTB, PORTC and PORTD.ATmega16 has 16 KB programmable flash memory, static RAM of 1 KB and EEPROM of 512 Bytes. The endurance cycle of flash memory and EEPROM is 10,000 and 100,000, respectively.ATmega16 has various inbuilt peripherals like USART, ADC, Analog Comparator, SPI, JTAG etc. Each I/O pin has an alternative task related to in-built peripherals.

Component Used
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.

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

TIME = 0:0:1

VDD

VSS

VEE

R/W

RS

D0

D1

D2

D3

D4

D5

D6

10

11

12

13

14

D7

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 #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); }

Code
#include <mega16.h> #include <lcd.h>

S-ar putea să vă placă și