Code Vi Điều Khiển 8051 Keil C:
/******************************************************************************
Chip type : STC89C52
Program type : Application
Clock frequency : 12 MHz
Project : DO TOC DO DONG CO
Version : V1.0
Date : 17/02/2013
Author : DAO XUAN TRUONG
Mobile : 0979.20.90.10
My Company : JVN TECH NOLOGY., JSC
Comments: Chuong trinh viet do toc do dong co mot chieu
Dau ra Encorder duoc dua vao chan ngat INT0
Encoder su dung la Encoder 32 xung
Note : Cam bat cu hinh thuc sao chep nao khi khong duoc su dong y cua tac gia
******************************************************************************/
#include<regx52.h>
#define Outled P0
#define true 1
#define false 0
#define THx 15536/256
#define TLx 15536%256
sbit E0 = P1^0;
sbit E1 = P1^1;
sbit E2 = P1^2;
sbit E3 = P1^3;
sbit L0 = P2^0;
sbit L1 = P2^1;
sbit L2 = P2^2;
sbit L3 = P2^3;
sbit L4 = P2^1;
sbit L5 = P2^2;
sbit L6 = P2^3;
sbit L7 = P2^7;
sbit Sp = P1^5;
bit chuki=false;
unsigned char k;
unsigned int Soxung,Demxung,Vong,n;
unsigned char time=0;
unsigned char CharCode[10]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};//for number 0->9
void Sys_init();
void Display(unsigned int dem);
/******************************************************************************
Chuong trinh ngat INT0
******************************************************************************/
void NGAT0(void) interrupt 0
{
Demxung++;
//L7=!L7;
}
/******************************************************************************
Chuong trinh ngat timer1 thoi gian tran 0.05s
******************************************************************************/
void ngat_time1(void) interrupt 3
{
time++ ;//tang bien dem thoi gian
if(Demxung>0) L7=!L7;
else if(Demxung==0) {L7=1;chuki=true;}
if(time==80)//TIME = 4s chuan
{
TR1=0; //dung bo dinh thoi
chuki=true; //bao het thoi gian dem xung
time=0;
}
TH1=THx;TL1=TLx;//dat cho lan sau
}
/******************************************************************************
Chuong trinh tre
******************************************************************************/
void delay_us(unsigned long int d)
{
for(n=0;n<d;n++);
}
/******************************************************************************
Chuong trinh hien thi Led 7 doan
******************************************************************************/
void Display(unsigned int dem)
{
if(dem>=9999)//hien thi 4 so khi gia tri hien thi > 9999
{ dem=9999;
L0=L1=L2=L3=0;
E0=0;
Outled=CharCode[dem/1000];
delay_us(1);
E0=1;
P0=0XFF;
E1=0;
Outled=CharCode[(dem%1000)/100];
delay_us(1);
E1=1;
P0=0XFF;
E2=0;
Outled=CharCode[(dem%100)/10];
delay_us(1);
E2=1;
P0=0XFF;
E3=0;
Outled=CharCode[dem%10];
delay_us(1);
E3=1;
P0=0XFF;
}
else if((dem<=999)&&(dem>99)) //hien thi 3 so khi gia tri hien thi <999
{
L0=L1=L2=0;L3=1;
E0=1;
E1=0;
Outled=CharCode[(dem%1000)/100];
delay_us(1);
E1=1;
P0=0XFF;
E2=0;
Outled=CharCode[(dem%100)/10];
delay_us(1);
E2=1;
P0=0XFF;
E3=0;
Outled=CharCode[dem%10];
delay_us(1);
E3=1;
P0=0XFF;
}
else if((dem<=99)&&(dem>9)) //hien thi 2 so khi gia tri hien thi <99
{
L0=L1=0;L2=L3=1;
E1=E0=1;
E2=0;
Outled=CharCode[(dem%100)/10];
delay_us(1);
E2=1;
P0=0XFF;
E3=0;
Outled=CharCode[dem%10];
delay_us(1);
E3=1;
P0=0XFF;
}
else if((dem<=9))//hien thi 1 so khi gia tri hien thi <9
{
L0=0;L1=L2=L3=L4=L5=L6=1;
E2=E1=E0=1;
E3=0;
Outled=CharCode[dem%10];
delay_us(1);
E3=1;
P0=0XFF;
}
}
/******************************************************************************
Chuong trinh chinh
******************************************************************************/
void main(void)
{
Sys_init(); // goi chuong trinh khoi tao
P2=0X00;
Sp=0;
delay_us(8000);
Sp=1;
P2=0XFF;
while(true)
{
while(!chuki)
{
Vong=(Soxung/200)*15; // soxung(trong 4s)*15 = so vong trong 1phut
Display(Vong);
};
Soxung= Demxung;//luu gia tri so xung vua dem
Demxung=0; //dat lai so xung
TR1=1; //dat lai thoi gian
chuki=false;
};
}
/******************************************************************************
Chuong trinh khoi tao
******************************************************************************/
void Sys_init()
{
TMOD=0x10; //cho time1 chay
EX0=1; //cho phep ngat ngoai 1
IT0=1; //cho phep ngat suon am
TH1=THx;TL1=TLx;//dat gia tri cho time 1
TR1=1; //dat thoi gian bat dau chay
ET1=1; //cho phep ngat time 1
EA=1; //cho phep ngat toan cuc
}
( Mô Phỏng + Code)
Khi nhấn nút "Tải Về Máy", bạn sẽ vào trang quảng cáo, vui lòng chờ 5 giây. Nút hiện ra ở góc phải phía trên, nhấn vào để đến trang download
Nguồn: Codientu.org
Đăng nhận xét