#define PORT_0 0x1e0 #define PORT_1 PORT_0+1 #define PORT_2 PORT_0+2 #define PORT_3 PORT_0+3 #define PORT_4 PORT_0+4 #define PORT_5 PORT_0+5 测转速的端口地址,其中D4位为转速光电 传感器信号 #define PORT_6 PORT_0+6 #define PORT_7 PORT_0+7 #define PORT_8 PORT_0+8 8253计数器0的地址 #define PORT_9 PORT_0+9 ... 1.... #define PORT_A PORT_0+0xa ... 2 ... #define PORT_B PORT_0+0xb .... 控制字.. 由于计数器的溢出不能用,故采用光读计数器的办法,记到一半时,就重装 计数器。实验结果是大部分时间是在误差范围内,但是过一段时间就会出现一两次误差很大的数据,这种情况频繁出现,不知哪里出了问题,清高手指点。 #include <stdio.h> #include <dos.h>
void init8253() { short iTemp8253; iTemp8253 = 0x30; outportb(PORT_B,iTemp8253); iTemp8253 = 0x70; outportb(PORT_B,iTemp8253); } unsigned int read8253(int l) { unsigned int i,j; unsigned char c; c=l; outportb(PORT_B,0x00+0x40*c); c=inportb(PORT_8+l); i=c;
c=inportb(PORT_8+l); j=c;
j=j*256+i; return(j); } main() { unsigned char k,count,status=0; unsigned int m,count1; float time=0,times=0; printf(" Interval Time
"); init8253(); read8253(0); while(1) { k=inportb(PORT_5); if(k&0x10) { if(status) { m=read8253(0); time=(65535-m)/1000.0*4.47+times; count1=(unsigned int) (60000/time); printf("Sudu: %u
",count1); 转速输出
} else status=1; outportb(PORT_8,0xff); outportb(PORT_8,0xff); times=0;
do { k=inportb(PORT_5); } while(k&0x10); } else { m=read8253(0); if(m<=32767) { outportb(PORT_8,0xff); outportb(PORT_8,0xff); times=times+(65535-m)/1000.0*4.47; do { m=read8253(0); } while(m>32767); }
} } } |