#include <pic.h>
#include <stdio.h>
#include "delay.c"
#include "lcd.c"
// Configuration Mask Definitions
#define CONFIG_ADDR 0x2007
// Protection of flash memory
#define PROTECT 0x1FFF
#define UNPROTECT 0x3FFF
// Low voltage programming enable
#define LVPEN 0x3FFF
#define LVPDIS 0x3F7F
// Power up timer enable
#define PWRTEN 0x3FF7
#define PWRTDIS 0x3FFF
// Watchdog timer enable
#define WDTEN 0x3FFF
#define WDTDIS 0x3FFB
#define XT 0x3FED
//Konfigurasyon ayarlari
__CONFIG(WDTDIS&LVPDIS&PWRTEN&XT);
// Global degiskenler
unsigned char kontrol=0;
unsigned char sonuc[]=" ";
unsigned int CX;
float sure=0;
float sure2=0;
int LCD_sayac=200;
void hesapla(void){
if(CX!=0){
sure=(float)250/CX;// hız hesaplamaları
sure2=(float)7.469*sure;//hız hesaplamaları
}else{
sure2=0;
}
}
//--------- LCD alt programi----------
void LCD_yaz(void){
lcd_clear(); // LCD'yi temizle
sprintf(sonuc,"Hiz: %d km/h ",(int)sure2);
lcd_goto(0x00); lcd_puts(sonuc);
}
//---- INT kesme alt programi -----
static void interrupt kesme(void){
if(INTF){
if(T0IE==0) T0IE=1;
else
{
T0IE=0;
kontrol=1;
}
INTF=0;
}
if(T0IF){
if(CX<900){
CX++;
TMR0=6;
LCD_sayac--;
if(LCD_sayac==1)
{LCD_yaz();LCD_sayac=200;}
}
else{
lcd_clear();
lcd_goto(0x00); lcd_puts("");
lcd_goto(0x40); lcd_puts(" HIZ --0--");//ilerleme yoksa 0 yazsın
DelayMs(500);
}
T0IF=0; // Bayragi temizle
TMR0=6; // TIMER'i tekrar yukle
}
}
//----------- ANA PROGRAM ------------
main(void)
{
TRISA=0x00; // Port A'nin ilk pini giris
TRISB=0x01; // PortB'nin hepsi cikis
PORTA=0; // Baslangic durumu ayarlari
PORTB=0;
// LCD islemleri
DelayMs(250);
lcd_init(FOURBIT_MODE);
lcd_clear();
lcd_goto(0x00); lcd_puts("HIZ");
lcd_goto(0x40); lcd_puts("GOSTERGESI");
// TIMER islemleri
INTCON=0X90; // Butun kesmeler etkin
kontrol=0;
// TIMER islemleri
T0CS=0; // Dahili clock
PSA=0; // Prescaler TMR0 icin ayarli
PS0=1; PS1=1; PS2=0; // Oran 1:16
T0IE=0; // Baslangicta TIMER pasif
T0IF=0; // Bayrak temizle
TMR0=6;
CX=0;
// Hiz olcum islemleri
for(;;){
if(kontrol==1){hesapla();CX=0;kontrol=0;}
} // islemleri tekrarla
} // Programin sonu