Вы находитесь на странице: 1из 4

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=015 ;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=000;

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=053; 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=000; // Timer/Counter 0 initialization // Clock source: System Clock // Clock scale: 64 // Mode: Normal top=FFh // OC0 output: Disconnected TCCR0=003; TCNT0=053; OCR0=000; // External Interrupt(s) initialization // INT0: On // INT0 Mode: Rising Edge // INT1: On // INT1 Mode: Rising Edge // INT2: Off GICR|=0xC0; MCUCR=0x0F; MCUCSR=000; GIFR=0xC0; // Timer(s)/Counter(s) Interrupt(s) initialization TIMSK=001; // Analog Comparator initialization // Analog Comparator: Off

// Analog Comparator Input Capture by Timer/Counter 1: Off ACSR=080; SFIOR=000; // LCD module initialization lcd_init(16); // Global enable interrupts #asm(sei) while (1); } =================================================================

How to set up 1 millisecond ? the key point are : TCCR0=003; and TCNT0=053; TCCR0=003; that means clock crystal/64, we got 5.78 us for one clock cycle in TCNT0 TCNT0=053; 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, its 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.

Вам также может понравиться