下面是我编写的用2407的捕获单元3来进行直流电机测速的程序,大侠帮我看一下有什么问题:
为什么不可以实时显示直流电机的速度,而是只能得到一个值,是不是我的捕获中断根本就用错了?
#include \"2407c.h\"
ioport unsigned int port000e;//七段输出的两个I/O端口
ioport unsigned int port000f;
void interrupt capint(void);
void cap_init(void);
void delay();
unsigned int f;
unsigned int temp,uWork;
unsigned int seg[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
main()
{
int i,j;
asm(\" setc INTM \"
;
*WDCR=0x6f;
*WDKEY=0x5555;
*WDKEY=0xaaaa;
*SCSR1=0x83fe;
(*MCRB)=0;
*MCRA=*MCRA|0xbf40; //以下8句是设置PWM波形输出驱动直流电机转动
*ACTRA=0x06;
*DBTCONA=0x00;
*CMPR1=0x1500;
*T1PR=0x6000;
*COMCONA=0x8200;
*T1CON=0x1000;
*T1CON=*T1CON|0x0040;
cap_init();
while(1)
{
i=f/10; //这里是将得到的转速值分成个位和十位送到七段显示
port000e=seg
;
j=f%10;
port000f=seg[j];
delay();
}
}
void interrupt capint(void) //捕获中断程序
{
unsigned int capnum1=0;
unsigned int capnum2=0;
uWork=(*PIVR);
switch(uWork)
{
case 0x35:
{
(*EVAIFRC)=0x04;
capnum1=*CAP3FIFO;
capnum2=*CAP3FIFO;
temp=0;
if(capnum2>capnum1)
temp=capnum2-capnum1;
else
temp=capnum2+(0xffff-capnum1);
f=1875000/temp; //30MHZ÷16分频因子÷从捕获FIFO栈中得到的差值
break;
}
}
}
void cap_init(void) //捕获单元3初始化,捕获上升沿
{
*T2PR=0x0ffff;
*T2CNT=0;
*T2CON=0x1400;
*WSGR=0x0000;
*CAPCONA=0x9204;
*CAPFIFOA=0x3000;
asm(\" clrc INTM \"
*IMR=0x08;
*EVAIMRC=*EVAIMRC|0x0004;
*EVAIFRC=*EVAIFRC|0x0ffff;
}
void interrupt nothing()
{
asm(\" clrc INTM \"
return;
}
void delay()
{
int i,j;
for(i=0;i<400;i++)
for(j=0;j<400;j++);
}