Stopwatch Digital menggunakan fasilitas Timer 1 ATMEga 8535

stopwatch

Dalam projek ini saya menggunakan ATmega 8535, fitur yang saya pakai yaitu ekternal interupt 0, eksternal interupt 1 dan timer 1. Sedangkan pada hardware yaitu menggunakan 2 push button dan LCD 2×16.

 

 

 

 

 

 

Berikut Schematic :

 

 

 

 

 

 

 

 

 

 

Setting Codevision AVR :

TIMER AVR4 TIMER AVR1

TIMER AVR2 TIMER AVR3

Berikut programnya :

/*****************************************************
This program was produced by the
CodeWizardAVR V1.25.8 Professional
Automatic Program Generator
© Copyright 1998-2007 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project :
Version :
Date    : 3/15/2011
Author  : F4CG
Company : F4CG
Comments:

Chip type           : ATmega8535
Program type        : Application
Clock frequency     : 12.000000 MHz
Memory model        : Small
External SRAM size  : 0
Data Stack size     : 128
*****************************************************/

#include <mega8535.h>
#include <stdio.h>
// Alphanumeric LCD Module functions
#asm
.equ __lcd_port=0x1B ;PORTA
#endasm
#include <lcd.h>
unsigned char lcd_buffer[33];
unsigned int mulai;
// External Interrupt 0 service routine
interrupt [EXT_INT0] void ext_int0_isr(void)
{
// Place your code here
TCCR1A=0×00;
TCCR1B=0×02;
lcd_clear();
lcd_gotoxy(0,0);
sprintf(lcd_buffer,”Time: %d ms”,mulai);
lcd_puts(lcd_buffer);

}

// External Interrupt 1 service routine
interrupt [EXT_INT1] void ext_int1_isr(void)
{
// Place your code here
mulai=0;
TCCR1A=0×00;
TCCR1B=0×00;
TCNT1H=0×00;
TCNT1L=0×00;
}

// Timer 1 output compare A interrupt service routine
interrupt [TIM1_COMPA] void timer1_compa_isr(void)
{
// Place your code here

TCNT1H=0×00;
TCNT1L=0×00;
mulai++;

}

// Declare your global variables here

void main(void)
{
// Declare your local variables here

// Input/Output Ports initialization
// Port A 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=T State2=T State1=T State0=T
PORTA=0×00;
DDRA=0×00;

// Port B 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=T State2=T State1=T State0=T
PORTB=0×00;
DDRB=0×00;

// Port C 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=T State2=T State1=T State0=T
PORTC=0×00;
DDRC=0×00;

// 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=T State2=T State1=T State0=T
PORTD=0×00;
DDRD=0×00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
// Mode: Normal top=FFh
// OC0 output: Disconnected
TCCR0=0×00;
TCNT0=0×00;
OCR0=0×00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 1500.000 kHz
// Mode: Normal top=FFFFh
// OC1A output: Discon.
// OC1B output: Discon.
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer 1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: On
// Compare B Match Interrupt: Off
TCCR1A=0×00;
TCCR1B=0×02;
TCNT1H=0×00;
TCNT1L=0×00;
ICR1H=0×00;
ICR1L=0×00;
OCR1AH=0×05;
OCR1AL=0xDC;
OCR1BH=0×00;
OCR1BL=0×00;

// Timer/Counter 2 initialization
// Clock source: System Clock
// Clock value: Timer 2 Stopped
// Mode: Normal top=FFh
// OC2 output: Disconnected
ASSR=0×00;
TCCR2=0×00;
TCNT2=0×00;
OCR2=0×00;

// External Interrupt(s) initialization
// INT0: On
// INT0 Mode: Low level
// INT1: On
// INT1 Mode: Low level
// INT2: Off
GICR|=0xC0;
MCUCR=0×00;
MCUCSR=0×00;
GIFR=0xC0;

// Timer(s)/Counter(s) Interrupt(s) initialization
TIMSK=0×10;

// 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)
{
// Place your code here

};
}

 

 

 

Tampilan videonya saat dirunning :

Cara mengeset agar counter akan bertambah tiap 1 ms adalah sebagai berikut : Disini menggunakan timer1 mode CTC dengan prescalar 8, Crystal yang saya pakai yaitu 12MHz. Kuncinya yaitu pada registry:

TCCR1B=0×02;//Setting prescalar 8
OCR1AH=0×05;OCR1AL=0xDC;// Mensetting nilai 0CR pada timer 1 yaitu 0x05DC atau (=1500 desimal)

 

Pada mode CTC Timer 1, nilai timer yang ada  pada TCNTn akan dinolkan lagi jika TCNTn sudah sama dengan nilai yang ada pada register OCRn, sebelumnya OCR harus diset terlebih dahulu pada timer 0 dan 0 range dari OCR = 0-255 sedangkan pada timer 1 range dari OCR = 0- 65535.

TCCR1B=0×02;Crystal/8= 12000000MHz/8= 1500000 Hz =Waktu tiap 1 clock 1/1500000= 0,666 us.

OCR1AH=0×05;OCR1AL=0xDC ; Setting nilai dari OCR = 1500(10)

TIMSK=0×10;

TIMSK=0×10; saat nilai TCNT1 = OCR maka progam akan mengaktifkan fungsi interupt Timer 1 output compare A match.

Sehingga perhitungannya = waktu tiap 1 clok x 1500= 0.666 us X 1500 = 999 us= mendekati 1 ms.

You can leave a response, or trackback from your own site.

5 Responses to “Stopwatch Digital menggunakan fasilitas Timer 1 ATMEga 8535”

  1. aozora mengatakan:

    TCCR1B=0×02;//Setting prescalar 8

    0×02 bukannya 4 yah klo di desimalkan?

    :bingung

  2. aozora mengatakan:

    TCCR1B=0×02;//Setting prescalar 8
    0×02 bukannya 4 klo di desimalkan?

  3. elwinta mengatakan:

    makasi bro atas infonya….hehehehehe

Leave a Reply

You must be logged in to post a comment.

Anda mungkin juga sukaclose