|
51单片机测速系统仿真原理图如下:
源程序:
- #include "include.h" //包含头文件
- sbit BEEP = P2^4;
- sbit LED = P2^5;
- sbit OE = P3^3;
- sbit EOC = P3^2;
- sbit ST = P3^1;
- sbit CLK = P3^0;
- u16 globalADCValue = 0;//存放ADC的值
- void TimeInitialize(void);
- void main(void)
- {
- u16 MSpeed = 0;//显示速度的大小
- u8 MDisplayBuff[5];//用来显示的BUFF
- lcd1602_initialize();
- TimeInitialize();//初始化定时器
- lcd1602_writestring("SPEED system",0,0);
- lcd1602_writestring("speed:1000rpm",1,0);
- while(1)
- {
- ST = 0;
- ST = 1;
- ST = 0;
- while(EOC == 0);
- OE = 1;
- globalADCValue = P1;
- OE = 0;
-
- MSpeed = (u16)(globalADCValue * 1000.0 / 255);//换算成速度
-
- if(MSpeed > 800)//大于800rpm就蜂鸣器响
- {
- BEEP = 0;//蜂鸣器响
- LED = 0;//蜂鸣器响
- }
- else
- {
- BEEP = 1;//蜂鸣器不响
- LED = 1;//蜂鸣器不响
- }
-
- MDisplayBuff[0] = MSpeed / 1000 + 0x30;
- MDisplayBuff[1] = MSpeed % 1000 / 100 + 0x30;
- MDisplayBuff[2] = MSpeed % 100 /10 + 0x30;
- MDisplayBuff[3] = MSpeed % 10 + 0x30;
- MDisplayBuff[4] = 0;
- lcd1602_writestring(MDisplayBuff,1,6);
- //ADC读取
- }
- }
- void TimeInitialize(void)//??????
- {
- TMOD = 0x02;
- TH0 = 0x9c;
- TL0 = 0x9c;
- IE = 0x82;
- TR0 = 1;
- }
- void Timer0_INT() interrupt 1
- {
- CLK =! CLK;
- }
复制代码
|
|