code vxl

#include<stdio.h> //khai bao thu vien viet chuong trinh bang C

#include<reg52.h> // khai bao thu vien cua vdk at89...

#include<math.h> //khai bao thu vien tinh toan

unsigned char M[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90}; //khai bao kieu mang hien thi len led 7 thanh tu 0-9

unsigned char dv,ch; //khai bao bien bien kieu ki tu

int j,f,dem ; //khai bao bien kieu nguyen

float n; //khai bao bien kieu thuc

void delay(unsigned long int t) //ham tao tre

{

unsigned long int i;

for(i=0;i<=t;++i);

}

void hienthi(void) //ham hien thi

{

ch=((f%1000)%100)/10;

dv=((f%1000)%100)%10;

for(j=1;j<=100;++j)

{

P1=0X08; //cap nguon cho led

P2=0xa4; //hien thi so 2

delay(100); //tao tre 100ms

P1=0X04; //cap nguon cho led

P2=0x89; //hien thi chu H

delay(100);

P1=0X02; //cap nguon cho led

P2=M[dv]; //hien thi tan so hang don vi

delay(100);

P1=0X01; //cap nguon cho led

P2=M[ch]; //hien thi tan so hang chuc

delay(100);

}

}

void main(void) //ham chinh

{

TMOD=0X25; //chon timer 0 lam bo dem

IE=0X88; // cho phepo timer 0

IP=0X08; //uu tien ngat do timer 0

TH0=TL0=0; //xoa timer 0

TH1=TL1=-100; //gia tri nap dau cho timer

f=0; //gia tri ban dau cua bo dem

while(1) //chuong trinh con phuc vu ngat

{

TR1=TR0=1; //khoi tao thanh ghi cho phep ngat va uu tien ngat

while(dem<10000); //cho het khoang thoi gian 0.01 giay

TR1=TR0=0;

n=(TH0*256+TL0); //doi gia tri dem duoc thanh so thap phan

f=n; //don vi la hz nhan 1 vi chi dem trong 0.01 giay;

if (f<45) // { neu nho hon 45 hz

{ //thi ket qua hien thi la so 0 }

f=0;

} //tuong tu neu tan so > 55 hz

if (f>55)

{

f=0;

}

hienthi(); //bat ham hien thi

}

}

void ngat(void) interrupt 3 //chuong trinh ngat

{

dem++; //dem tang

TF1=0; //xoa co ngat TF1 de timer tiep tuc hoat dong

TR1=1;

}

//neu bay gio dung timer 1 hien thi thi chung ta lam nhu sau

//dau tien noi mach cua chan clock vao chan P3.5 (tuc la chan T1)

//va sau do su chuong trinh code nhu sau

// TMOD=0X20;

//IE=0X8A;

//IP=0X08;

//void ngat(void) interrupt 1

Bạn đang đọc truyện trên: AzTruyen.Top

Tags: