Minggu, 28 September 2014

Membuat Timer Atmega8



gambar 1. Simulasi timer 1 dengan proteus


Perhitungan timer :

Time value = (Required delay / clock value)-1

Ø  Required delay : waktu yang diinginkan
Ø  Clock value : clock timer1 periode

example :
- waktu yang di inginkan adalh 1 detik
- ext clock 8 Mhz
- clock value 7,813 Khz = 7813 Hz

Time value = (1s/(1/7813Hz))-1 diketahui periode adalah T = 1/F

Time value = (1s x 7813) - 1

Time value = 7812 = 0x1e84


Berikut contoh pengaturan timer1 :



gambar 2. konfigurasi chip mikrokontroller


gambar 3. konfigurasi timer 1 dengan ctc mode

berikut contoh program yang telah dibuat :
/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.3 Standard
Automatic Program Generator
© Copyright 1998-2011 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project :
Version         :
Date             : 9/22/2014
Author          : fahmibnu
Company     : 
Comments    :

Chip type                               : ATmega8
Program type                        : Application
AVR Core Clock frequency   : 8.000000 MHz
Memory model                      : Small
External RAM size                : 0
Data Stack size                     : 256
*****************************************************/

#include <mega8.h>
#include <stdlib.h>
#include <delay.h>

#define t_enter                 PINC.3
#define t_down                PINC.2
#define t_up                      PINC.1
#define t_pause               PINB.5

int count_time,temp,menit,temp_menit,up,down;
char counter_time[5],temperature[5],buf_menit[5];
void menu();
// Alphanumeric LCD functions
#include <alcd.h>

// Timer1 output compare A interrupt service routine
interrupt [TIM1_COMPA] void timer1_compa_isr(void)
{
// Place your code here
    count_time--;                //counter down per 1 detik
    if(count_time < 0 && menit != 0)
    {
        menit--;
        count_time = 59;
    }                             
    else if(count_time < 0 && menit == 0)
    {
        menit = 0;
        count_time = 0;
        TCCR1B = 0;
    }
}

// Declare your global variables here

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

// Input/Output Ports initialization
// Port B initialization
// Func7=In Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State7=T State6=T State5=P State4=T State3=T State2=T State1=T State0=T
PORTB=0x20;
DDRB=0x00;

// Port C initialization
// Func6=In Func5=In Func4=In Func3=In Func2=In Func1=In Func0=In
// State6=T State5=T State4=T State3=P State2=P State1=P State0=T
PORTC=0x0E;
DDRC=0x00;

// 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=0x00;
DDRD=0x00;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: Timer 0 Stopped
TCCR0=0x00;
TCNT0=0x00;

// Timer/Counter 1 initialization
// Clock source: System Clock
// Clock value: 7.813 kHz
// Mode: CTC top=OCR1A
// OC1A output: Discon.
// OC1B output: Discon.`
// Noise Canceler: Off
// Input Capture on Falling Edge
// Timer1 Overflow Interrupt: Off
// Input Capture Interrupt: Off
// Compare A Match Interrupt: On
// Compare B Match Interrupt: Off
 









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

// External Interrupt(s) initialization
// INT0: Off
// INT1: Off
MCUCR=0x00;

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

// USART initialization
// USART disabled
UCSRB=0x00;

// Analog Comparator initialization
// Analog Comparator: Off
// Analog Comparator Input Capture by Timer/Counter 1: Off
ACSR=0x80;
SFIOR=0x00;

// ADC initialization
// ADC disabled
ADCSRA=0x00;

// SPI initialization
// SPI disabled
SPCR=0x00;

// TWI initialization
// TWI disabled
TWCR=0x00;

// Alphanumeric LCD initialization
// Connections are specified in the
// Project|Configure|C Compiler|Libraries|Alphanumeric LCD menu:
// RS - PORTD Bit 2
// RD - PORTD Bit 3
// EN - PORTD Bit 4
// D4 - PORTD Bit 1
// D5 - PORTD Bit 5
// D6 - PORTD Bit 6
// D7 - PORTD Bit 7
// Characters/line: 16
lcd_init(16);

// Global enable interrupts
#asm("sei")
count_time = 0;
menit = 0;
lcd_gotoxy(0,0);
lcd_putsf("  :: Welcome :: ");
lcd_gotoxy(0,1);
lcd_putsf("  Fahmibnu-ELK   ");
delay_ms(1000);
lcd_clear();

while (1)
      {
      // Place your code here   
        itoa(menit,buf_menit);
        if(menit < 10)
        {    
            lcd_gotoxy(5,0);
            lcd_putsf("0");
            lcd_gotoxy(6,0);
            lcd_puts(buf_menit);
        }  
        else if(count_time > 9)
        {
            lcd_gotoxy(5,0);
            lcd_puts(buf_menit);
        }
       
        lcd_gotoxy(7,0);
        lcd_putsf(":");                
       
        itoa(count_time,counter_time);
        if(count_time < 10)
        {
            lcd_gotoxy(8,0);
            lcd_putsf("0");  
            lcd_gotoxy(9,0);
            lcd_puts(counter_time);
        }  
        else if(count_time > 9)
        {
            lcd_gotoxy(8,0);
            lcd_puts(counter_time);
        }
        lcd_gotoxy(0,1);
        lcd_putsf("  Membuat Timer");
        menu();
        delay_ms(50);
      }
}

void menu()
{
    if (t_up == 0)
    {
        up = 1;
        if(up == 1)
        {       
            delay_ms(50);
            count_time++;
            if(count_time > 59)
            {  
                menit++;
                count_time = 0;
            }
            up = 0;
        } 
        t_up = ~t_up;
        lcd_clear();
    }
    else if(t_down == 0)
    {
        down = 1;
        if(down == 1)
        {
            delay_ms(50);
            count_time--;
            if(count_time < 0)
            {
                menit--;
                count_time = 59;
            } 
            down = 0;
        }
        t_down = ~t_down;
        lcd_clear();
    }
    else if(t_enter == 0)
    {
        while(t_enter == 0);
        TCCR1B = 0x0D;         //timer1 is on
        t_enter = ~t_enter;
    }
    else if(t_pause == 0)
    {
        while(t_pause == 0);
        TCCR1B = 0x00;         //timer1 is off
        t_c = ~t_c;
    }
} 



Tidak ada komentar:

Posting Komentar