各位大侠。我用89C2051做的电机测速器,改了很多次千位总是显示不出来,贴出来麻烦指点一下。谢谢。
- #include <AT892051.H>
- #include <STDIO.H>
- #define BYTE unsigned char
- #define WORD unsigned int
- #define minplus 120000000 //2*1000*1000*60
- sbit KEY = P3^5; //切换开关
- sbit CLK = P3^4; //脉冲输入端
- sbit qian = P3^3;
- sbit bai = P3^1;
- sbit shi = P3^0;
- sbit ge = P3^7;
- sbit pluse = P3^2;
- BYTE led[4],min,sec,dms,showBit,cn,cn_l,cn_h,cn_n;
- WORD speed,Temp1,Temp2,Temp3,count;
- bit DmS5 = 0,dp1, dp2, dp3;
- unsigned long count32,cn_Temp;
- union un
- {
- unsigned long cn_Zu;
- BYTE cn_byte[4];
- }uCount;
- const BYTE DATA_7SEG[ ] =
- {
- 0xE7, // 0
- 0x84, // 1
- 0xD3, // 2
- 0xD6, // 3
- 0xB4, // 4
- 0x76, // 5
- 0x77, // 6
- 0xC4, // 7
- 0xF7, // 8
- 0xF6, // 9
- 0xFF,
- 0x00
- };
- //delay()________________________________________________
- void delay(BYTE t)
- {
- BYTE i;
- for (i=0;i<t;i++);
- }
- //display()______________________________________________________
- void display(void)
- {
- BYTE i;
- for (i=0;i<4;i++)
- {
- P3=~(1<<i); //低电平驱位选
- P1=DATA_7SEG[led[i]];
- delay(255); //about 1.5ms
- }
- // P1=0xff;
- // P3=0xff;
- }
- void disp_init(void)
- {
- BYTE i;
- led[0]=0x0a;
- led[1]=0x0a;
- led[2]=0x0a;
- led[3]=0x0a;
- for(i=0;i<200;i++)
- {
- display();
- }
- }
- //Int0()_________________________________________________________
- void Int0(void) interrupt 0
- {
- EA = 0;
- TR0 = 0;
- uCount.cn_byte[2] = TH0;
- uCount.cn_byte[3] = TL0;
- uCount.cn_byte[1] = cn;
- cn = 0;
- TH0 = 0x00;
- TL0 = 0x0D;
- TR0 = 1;
- EA = 1;
- }
- //Time0()_________________________________________________________
- void Time0(void) interrupt 1
- {
- cn ++;
- }
- //Time1()_________________________________________________________
- void Time1(void) interrupt 3
- {
- TF1 = 0;
- TH1 = 0xd8;
- TL1 = 0xf0;
- DmS5 =1;
- switch (showBit)
- {
- case 0:
- {
- ge = 0;
- shi = 1;
- bai = 1;
- qian = 1;
- P1 = DATA_7SEG[led[showBit]];
- showBit ++;
- break;
- }
- case 1:
- {
- ge = 1;
- shi = 0;
- bai = 1;
- qian = 1;
- P1 = DATA_7SEG[led[showBit]];
- showBit ++;
- break;
- }
- case 2:
- {
- ge = 1;
- shi = 1;
- bai = 0;
- qian = 1;
- if((led[showBit] == 0) && (led[showBit+1] == 0))
- {
- P1 = 0;
- }
- else
- {
- P1 = DATA_7SEG[led[showBit]];
- }
- showBit ++;
- break;
- }
- case 3:
- {
- ge = 1;
- shi = 1;
- bai = 1;
- qian = 0;
- if(led[showBit] == 0)
- {
- P1 = 0;
- }
- else
- {
- P1 = DATA_7SEG[led[showBit]];
- }
- showBit = 0;
- break;
- }
- default:
- showBit =0;
- break;
- }
- }
- //________________________________________________________________
- void main(void)
- {
- //BYTE j;
-
- P1 = 0xff;
- P3 = 0xff;
-
- TMOD = 0X11;
-
- TH1 = 0xd8;
- TL1 = 0xf0;
- TR1 = 1;
- ET1 = 1;
-
- EA = 1;
-
- disp_init(); //初始化显示
- //TR0 = 0;
- //TH0 = 0x0;
- //TL0 = 0x0;
-
- IT0 = 1;
- EX0 = 1;
- PX0 = 1;
- ET0 = 1;
- pluse = 1;
-
- cn = 0;
- speed = 0;
- //count32 = 250000;
-
- while(1)
- {
-
- if(DmS5 ==1)
- {
-
-
- dms++;
- if(dms > 99)
- {
- KEY = 1;
- dms = 0;
- EA = 0;
- uCount.cn_byte[0] = 0;
-
- //count32 = (unsigned long)(cn_n << 16) + (cn_h << 8) + cn_l;
- cn_Temp = uCount.cn_Zu;
- EA = 1;
- if(cn_Temp == 0)
- {
- speed = 0;
- }
- else
- {
- if(KEY == 1)
- {
- speed = (minplus/cn_Temp)>>4;
- }
- else
- {
- speed = minplus/cn_Temp;
- }
- }
- led[3]= speed/1000;
- Temp1 = speed%1000;
- led[2]= Temp1/100;
- Temp2 = Temp1%100;
- led[1]= Temp2/10;
- led[0]= Temp2%10;
- }
- DmS5 = 0;
- }
-
- }
- }
复制代码 |