Skip to content

Chronometer 16-bit with TMR0 and INT #27

Description

@richsahin

/*

  • File: main.c
  • Description: 16-Bit Timer, Bölmesiz, Çift Butonlu (INT0/INT1) Kronometre
    */

#include <xc.h>
#include <htc.h> // Hocanın kodunda bu da vardı, sınavda görmek isteyebilir

#define _XTAL_FREQ 8000000
#pragma config FOSC = HSHP
#pragma config WDTEN = OFF

// Ortak Katot (Common Cathode) Ekran Rakam Dizisi
unsigned char seg_char[10] = {0x3F, 0x06, 0x5B, 0x4F, 0x66, 0x6D, 0x7D, 0x07, 0x7F, 0x6F};

// Zaman Değişkenleri
unsigned char seconds = 0;
unsigned char minutes = 0;
unsigned char tick_count = 0;
unsigned char digit_counter = 0;
unsigned char cont = 0;

void main(void) {
// Port Ayarları
ANSELA = 0x00;
TRISA = 0xF0;
PORTA = 0x00;

ANSELD = 0x00; 
TRISD = 0x00; 
PORTD = 0x00; 

ANSELB = 0x00; 
TRISB = 0xFF; 
PORTB = 0x00; 

// --- HOCANIN İSTEDİĞİ KISA YAZIM TARZI ---
RBPU = 0;       // Port B Dahili Pull-Up AÇ
INTEDG0 = 0;    // INT0 (Reset) Düşen Kenar (Falling Edge)
INTEDG1 = 0;    // INT1 (Play/Pause) Düşen Kenar

T0CON = 0x82;   // 16-Bit, 1:8 Prescaler ayarı
TMR0H = 0xF6;   // 10ms gecikme için High byte
TMR0L = 0x3C;   // 10ms gecikme için Low byte

TMR0IE = 1;     // Timer0 Kesme İzni
INT0IE = 1;     // INT0 (Reset) Kesme İzni
INT1IE = 1;     // INT1 (Play/Pause) Kesme İzni

TMR0IP = 1;     // Timer0 VIP (Yüksek Öncelikli)
INT1IP = 1;     // INT1 VIP

GIE = 1;        // Genel Kesme İzni (Global Interrupt Enable)
PEIE = 1;       // Çevresel Kesme İzni (Peripheral Interrupt Enable)

// Ana Döngü (İşlemci burada sadece bekler)
while (1) {
    // value = counter / 50; saçmalığı kalktı!
}

}

// --- KESME (INTERRUPT) FONKSİYONU ---
void __interrupt(high_priority) isr() {

// 1. OLAY: TIMER0 KESMESİ (10ms'de bir çalışır)
if (TMR0IF) {
    TMR0IF = 0; 
    TMR0H = 0xF6; 
    TMR0L = 0x3C;

    PORTA = 0x00; // Ekranı temizle
    unsigned char secilecek_veri = 0;
    
    // Multiplexing ve Nokta (DP) Yakma İşlemi
    if (digit_counter == 0) secilecek_veri = seg_char[minutes / 10]; 
    else if (digit_counter == 1) secilecek_veri = seg_char[minutes % 10] | 0x80; // Nokta!
    else if (digit_counter == 2) secilecek_veri = seg_char[seconds / 10]; 
    else if (digit_counter == 3) secilecek_veri = seg_char[seconds % 10]; 

    PORTD = secilecek_veri;        
    PORTA = 0x01 << digit_counter; 

    digit_counter++; 
    if (digit_counter >= 4) digit_counter = 0; 

    // Bölmesiz Zamanlama (Matematik Yükü Yok)
    if (cont == 1) { 
        tick_count++; 
        if (tick_count >= 100) {  // 100 kere 10ms = 1 saniye
            tick_count = 0;
            seconds++; 
            if (seconds >= 60) { 
                seconds = 0;
                minutes++;
                if (minutes >= 60) {
                    minutes = 0;
                }
            }
        }
    }
}

// 2. OLAY: PLAY/PAUSE BUTONU (INT1)
if (INT1IF) {
    INT1IF = 0; 
    cont = 1 - cont;        
}

// 3. OLAY: RESET BUTONU (INT0)
if (INT0IF) {
    INT0IF = 0; 
    seconds = 0;           
    minutes = 0;           
    tick_count = 0;        
}

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions