?? main.c
字號(hào):
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
#define juli 1
//------------------- 定義1602液晶控制管腳-------------------------------------------------
sbit RS=P2^0;
sbit RIW=P2^1;
sbit E=P2^2;
sbit int0=P3^2;
sbit int1=P3^3;
uchar i;
uint timecount=0;
uchar biaozhi=0; //第一個(gè)開關(guān)
float jishi1=0;
float jishi2=0;
float speed;
float length;
//-------------------------定義數(shù)組----------------------------------------------------------
uchar tab[16]=" ";
void delay(uint xms) //按鍵掃描延時(shí)子程序
{
uint i,j;
for(i=xms;i>0;i--)
for(j=110;j>0;j--);
}
//---------------------1602寫指令時(shí)序開始------------------------------------------------------
void wi(uchar com)
{
uint i=200;
RS=1;
RIW=1;
RS=0;
RIW=0;
E=0;
E=1;
P0=com;
while(i--);
E=0;
}
//---------------------1602寫指令時(shí)序結(jié)束---------------------------------------------------
//---------------------1602寫數(shù)據(jù)時(shí)序開始--------------------------------------------------
void wd(uchar dat)
{
uint i=200;
RS=0;
RIW=1;
RS=1;
RIW=0;
E=0;
E=1;
P0=dat;
while(i--);
E=0;
}
//---------------------1602寫數(shù)據(jù)時(shí)序結(jié)束------------------------------------------------
void chushi() //1602初始化程序
{
wi(0x01); //清屏
wi(0x06); //設(shè)置輸入方式 AC自動(dòng)加1,顯示不發(fā)生移位
wi(0x0F); //顯示開關(guān)控制 開顯示 開光標(biāo) 閃爍
wi(0x38); //功能設(shè)置 雙行顯示 5*8點(diǎn)陣 八位數(shù)據(jù)
}
void xianshi_speed(void)
{
unsigned long zancun=speed/1000;
tab[6]=zancun%10+0x30;
zancun=zancun/10;
tab[5]=zancun%10+0x30;
zancun=zancun/10;
tab[3]='.';
tab[4]=zancun%10+0x30;
zancun=zancun/10;
tab[2]=zancun%10+0x30;
zancun=zancun/10;
tab[1]=zancun%10+0x30;
zancun=zancun/10;
tab[0]=zancun%10+0x30;
wi(0x89);
for(i=0;i<16;i++)
{wd(tab[i]);} //顯示數(shù)組
}
void xianshi_length(void)
{
unsigned long zancun=length/100;
tab[6]=zancun%10+0x30;
zancun=zancun/10;
tab[5]=zancun%10+0x30;
zancun=zancun/10;
tab[4]='.';
tab[3]=zancun%10+0x30;
zancun=zancun/10;
tab[2]=zancun%10+0x30;
zancun=zancun/10;
tab[1]=zancun%10+0x30;
zancun=zancun/10;
tab[0]=zancun%10+0x30;
wi(0xc9);
for(i=0;i<16;i++)
{wd(tab[i]);} //顯示數(shù)組
}
void main()
{
IT0=1;
EX0=1;
IT1=1;
EX1=1;
PT1=1;
TMOD=0x11; //T0工作為16位計(jì)數(shù)器(方式1),T1工作方式為16位定時(shí)器
TH0=0; //計(jì)數(shù)為65536
TL0=0; //
TH1=0; //定時(shí)器采用定時(shí)4ms. 初值為4000
TL1=0;
//TR1=1; //TR1=1,控制開關(guān)閉合,計(jì)數(shù)脈沖進(jìn)入T1計(jì)數(shù)器,啟動(dòng)定時(shí)
//TR0=1; //TR0=1,控制開關(guān)閉合,計(jì)數(shù)脈沖進(jìn)入T1計(jì)數(shù)器,啟動(dòng)計(jì)數(shù)
ET0=1; //定時(shí)器0開中斷
ET1=1; //定時(shí)器1開中斷
EA=1;
chushi(); //初始化
wi(0x80);
wd('s');
wd('p');
wd('d');
wd('(');
wd('m');
wd('/');
wd('s');
wd(')');
wd('=');
wi(0xc0);
wd('l');
wd('e');
wd('n');
wd('(');
wd('c');
wd('m');
wd(')');
wd(' ');
wd('=');
while(1)
{
speed=41000/jishi1*1000000;
length=speed*jishi2/1000000;
if(speed>41000000000&&biaozhi==0)
{
speed=0;
length=0;
biaozhi=0;
EX0=0;
EX1=1;
}
xianshi_speed();
xianshi_length();
}
}
void INT0_interrupt() interrupt 0 using 1
{
//delay(30);
//int0=1;
delay(10);
if(int0==0)
{
switch(biaozhi)
{
case 1:
{
jishi1=timecount*65536+TH1*256+TL1;
EX0=0;
EX1=1;
biaozhi=2;
}break;
case 3:
{
biaozhi=0;
EX0=0;
EX1=1;
}break;
default :break;
}
}
}
void INT1_interrupt() interrupt 2 using 1
{
//delay(30);
//int1=1;
delay(10);
if(int1==0)
{
if(biaozhi==0) //開始計(jì)數(shù)
{
biaozhi=1;
TR1=1;
EX1=0;
EX0=1;
}
else if(biaozhi==2)
{
jishi2=timecount*65536+TH1*256+TL1;
biaozhi=3;
TR1=0; //停止計(jì)時(shí)
TH1=0; //計(jì)數(shù)初值重裝載
TL1=0;
timecount=0;
EX1=0;
EX0=1;
}
}
}
void t1(void) interrupt 3 using 0 //T1中斷服務(wù)
{
TH1=0; //計(jì)數(shù)初值重裝載
TL1=0;
timecount++;
if(timecount==65535)
{
timecount=0;
}
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -