通过霍尔元件对电机的转速进行监测并用数码管显示转速
同时使用定时器,计数器
其中用定时器 0.1s的定时 满10个0.1s 也就是1s一共计数的电机在转速显示
单片机程序代码如下:
- #include<reg52.h>
- xdata unsigned char OUTBIT _at_ 0x8002;
- xdata unsigned char OUTSEG _at_ 0x8004;
- unsigned char LEDBuf[6],a,b,tmp;
- unsigned char tap3,tap4,tap5;
- code unsigned char LEDMAP[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71};
- void Delay (unsigned char CNT) //延时函数
- {
- unsigned char i;
- while(CNT--!=0)
- for(i=100;i!=0;i--);
- }
- void DisplayLED() //数码管显示函数
- {
- unsigned char i;
- unsigned char Pos;
- Pos=0x20;
- for(i=0;i<6;i++)
- {
- OUTBIT=0;
- OUTBIT=Pos;
- OUTSEG=LEDBuf[i];
- Pos>>=1;
- Delay(10);
- }
- }
- void main()
- {
- TMOD=0X15; //定时器和计数器设置
- TH1=0x3c;
- TL1=0XB0;
- ET0=1;
- EA=1;
- TH0=0;
- TL0=0;
- TR0=1;
- IT0=1;
- EX0=1;
- ET1=1;
- TR1=1;
- while(1)
- {
- tap3=tmp/100; //转BCD码
- tap4=(tmp%100)/10;
- tap5= tmp%10;
- LEDBuf[0]=LEDMAP[tap3]; //数码管显示
- LEDBuf[1]=LEDMAP[tap4];
- LEDBuf[2]=LEDMAP[tap5];
- LEDBuf[3]=LEDMAP[0];
- LEDBuf[4]=LEDMAP[0];
- LEDBuf[5]=LEDMAP[0];
- DisplayLED();
- }
- }
- void T0init()interrupt 3 //定时器中断
- { a++;
- if(a==10)
- {
- tmp=TH0*256+TL0;
- TH1=0x3c;
- TL1=0XB0;
- TH0=0;
- TL0=0;
- a=0;
- }
- }
复制代码 |