|
|
先說一下我想要的效果,是通過霍爾元件來計(jì)算電機(jī)的速度,我的想法是一個(gè)用2號(hào)中斷來計(jì)算霍爾元件的電平改變次數(shù),另一個(gè)用1號(hào)中斷進(jìn)行每0.5s中斷一次,并統(tǒng)計(jì)次數(shù)再打印到數(shù)碼管上顯示(要求實(shí)時(shí)),但是發(fā)現(xiàn)似乎不能達(dá)到這個(gè)要求的效果,想問一下是我的代碼哪里出錯(cuò)了?謝謝
單片機(jī)源程序如下:
#include<stc.h>
#define uch unsigned char
uch led[]={0xc0,0xf9,0xa4,0xb0,0x99,0x92,0x82,0xf8,0x80,0x90};
sbit k1=P1^0;
sbit k2=P1^1;
sbit k3=P1^2;
sbit k4=P1^3;
uch count=0,countt=0,rad=0,Radd=0,t=0;
void time0()
{
IT1=1;
EX1=1;
EA=1;
PX1=0; //設(shè)置優(yōu)先級(jí),使時(shí)間中斷優(yōu)先度高于次數(shù)中斷,防止統(tǒng)計(jì)沖突
}
void delay(uch time)
{
int j=0;
for(j=0;j<200;j++)
for(;time>0;time--) ;
}
void main()
{
while(1){
time0();
/*k1=0;
k2=1;
k3=1;
k4=1;
delay(50);
P2=led[count%10];
k1=1;
k2=0;
k3=1;
k4=1;
delay(50);
P2=led[count/10];
if(count>=100)
{
count=0;
countt++;
if(countt>=100)
{
count=0;
countt=0;
}
}
k1=1;
k2=1;
k3=0;
k4=1;
delay(50);
P2=led[countt%10];
k1=1;
k2=1;
k3=1;
k4=0;
delay(50);
P2=led[countt/10];*/ //以上注備通過P2接入led來判斷2號(hào)中斷的次數(shù)統(tǒng)計(jì)是否有正確進(jìn)行
Radd=rad*200; //每秒的rad //這里開始是轉(zhuǎn)速的顯示部分
k1=0;
k2=1;
k3=1;
k4=1;
delay(50);
P0=led[Radd/1000];
k1=1;
k2=0;
k3=1;
k4=1;
delay(50);
P0=led[Radd%1000/100];
k1=1;
k2=1;
k3=0;
k4=1;
delay(50);
P0=led[Radd%100/10];
k1=1;
k2=1;
k3=1;
k4=0;
delay(50);
P0=led[Radd%10];
}
}
writerdown_time1() interrupt 2
{
count++;
if(TR0==0)
{
TH0=0xfe; //5000us
TL0=0x0c; //5000us
TR0=1;
}
}
taker_time2() interrupt 1
{
t++;
if(t==20) //這里時(shí)間計(jì)算好像錯(cuò)誤,但是只是數(shù)值問題,影響不大,不是關(guān)鍵問題
{
rad=count;
count=0;
TH0=0xfc;
TL0=0x0c;
t=0;
}
}
|
|