Project : Stopwatch on ATmega16 (AVR)
Atmega16 and atmega8535 have the same characteristic, so this explanation should work for both of them.
I was helping Mrs Sri, she need this for her Thesis. It is a simply program, she just need to make a stopwatch with microcontroller. I use INT0 as timer ON and INT1 as timer OFF. We use LCD to display the time.
crystal clock = 11.0592 MHz, Timer 0.
This is the program================================================
#include
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0×15 ;PORTC
#endasm
#include lcd.h
#include stdio.h
char lcd_buffer[33];
unsigned int count;
// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{
count=0;
}
// External Interrupt 1 service routine
interrupt [EXT_INT1] void ext_int1_isr(void)
{
// Place your code here
//TCCR0=0×00;
lcd_gotoxy(0,0);
sprintf(lcd_buffer,”waktu %u ms”,count);
lcd_puts(lcd_buffer);
}
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
TCNT0=0×53;
count++;
}
void main(void)
{
// Port D initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=T State4=T State3=P State2=P State1=T State0=T
PORTD=0x0C;
DDRD=0×00;
// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock scale: 64
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0×03;
TCNT0=0×53;
OCR0=0×00;
// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Rising Edge
// INT1: On
// INT1 Mode: Rising Edge
// INT2: Off
GICR|=0xC0;
MCUCR=0x0F;
MCUCSR=0×00;
GIFR=0xC0;
// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0×01;
// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0×80;
SFIOR=0×00;
// LCD module initialization
lcd_init(16);
// Global enable interrupts
#asm(“sei”)
while (1);
}
=================================================================

How to set up 1 millisecond ?
the key point are : TCCR0=0×03; and TCNT0=0×53;
TCCR0=0×03; that means clock crystal/64, we got 5.78 us for one clock cycle in TCNT0
TCNT0=0×53; that means timer0 will overflow start from 83 (=53 in hexa) to 256, this is the multiplier
timer 0 will count 5.78 us x 173 (256-83) = 999.94 us, it’s almost 1 ms right
after the timer 0 finish the counting, micro will perform the program in the interrupt timer (interrupt [TIM0_OVF] void timer0_ovf_isr(void))
IMPORTANT : don`t open up PIN INT0 & INT1 keep them in low state when there is no signal interrupt.
-
Archives
- July 2010 (1)
- May 2010 (1)
- March 2010 (4)
- February 2010 (4)
- July 2009 (1)
- March 2009 (4)
- February 2009 (2)
-
Categories
-
RSS
Entries RSS
Comments RSS

