Jumat, 13 Mei 2016

Pengetahuan Dasar Aplikasi Timer Mikrokontroler AVR : Stop Watch

Pengetahuan Dasar Aplikasi Timer Mikrokontroler AVR  : Stop Watch

Teory Dasar
Contoh Aplikasi
Source code
Proteus
Video  Penjelasan

/*****************************************************
Date    : 5/16/2016
Chip type           : ATmega16
Clock frequency     : 11.059200 MHz
*****************************************************/

#include <mega16.h>
#include <stdio.h>
#include <stdlib.h>
#include <delay.h>
// Alphanumeric LCD Module functions
#asm
   .equ __lcd_port=0x15 ;PORTC
#endasm
#include <lcd.h>

char lcd[25];   //tempat menampung string display lcd
unsigned char counting,detik,menit,jam;

// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{
// Reinitialize Timer 0 value
TCNT0=0x94;
// Place your code here
counting++;        
if(counting==100)  // 100 counting = 1 detik
{
  counting=0;
  detik++;
  if(detik==60)    // 60 detik = 1 menit
   {
     detik=0  ;
     menit++;
      if(menit==60)
      { menit=0;
        jam++;
        if(jam==60)
        {
        jam=0;
        }
      }
    }
   
   
  }
       
}// end of isr

// Declare your global variables here

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

// Input/Output Ports initialization
// Port A initialization
PORTA=0xff;
DDRA=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 10.800 kHz
// Mode: Normal top=FFh
// OC0 output: Disconnected
//TCCR0=0x05;  
TCCR0=0x00;  // TIMER STOP
TCNT0=0x94;
OCR0=0x00;




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


// LCD module initialization
lcd_init(16);

// Global enable interrupts
#asm("sei")

while (1)
      {    
     
      if (!PINA.0) //JIKA TOMBOL START DITEKAN
        {
          lcd_gotoxy(0,3);
        lcd_putsf("timer run  ");  
          TCCR0=0x05;
         
        }
       
         if (!PINA.1) //JIKA TOMBOL stop DITEKAN
        {
          delay_ms(60);
           lcd_gotoxy(0,3);
        lcd_putsf("timer stop ");  
          TCCR0=0x0;
        }
       
        if (!PINA.2) //JIKA TOMBOL reset DITEKAN
        {
          delay_ms(60);
           lcd_gotoxy(0,3);
        lcd_putsf("timer reset");
          detik=0;
          menit=0;
          jam=0;  
          TCCR0=0x0; //timer stop
        }
     
         lcd_gotoxy(0,0);
        lcd_putsf("H:M:S:mS");
     
        lcd_gotoxy(0,1);
        itoa(jam , lcd);
        lcd_puts(lcd);
         lcd_putsf(":");
        itoa(menit , lcd);
        lcd_puts(lcd);
        lcd_putsf(":");
        itoa(detik , lcd);
        lcd_puts(lcd);
        lcd_putsf(":");
        itoa(counting , lcd);
        lcd_puts(lcd);
      };
}

Tidak ada komentar:

Posting Komentar