是这样写吗?- #include<reg52.h>
- unsigned char werbg = 0;
- unsigned char werbg2 = 0;
- unsigned int Num ;
- unsigned char shan,cutnum,addnum,startnum,setnum,time;
- sbit K2 = P2^0;
- sbit SMG3 = P3^6;
- sbit SMG4 = P3^7;
- unsigned int table[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x40,0x71};
- unsigned char code BeatCode[8] = { 0xE, 0xC, 0xD, 0x9, 0xB, 0x3, 0x7, 0x6};
- unsigned char code BeatCodf[8] = { 0x6, 0x7, 0x3, 0xB, 0x9, 0xD, 0xC, 0xE};
- unsigned char t0_ct,num2;
- void delayms(xms);
- void Display(void);
- void delay(unsigned int cnt)
- {
- while (cnt--);
- }
- void delayms(xms)
- {
- unsigned int x,y;
- for(x=xms;x>0;x--)
- for(y=110;y>0;y--);
- }
- void TrunMotor(unsigned long angle)
- {
- unsigned char buf;
- unsigned char step = 0;
- unsigned long beats = 0;
- beats = (angle * 1100) / 20;
- if(werbg == 1)
- {
- while (beats--)
- {
- buf = P1 & 0xF0;
- buf |= BeatCode[step];
- P1 = buf;
- step++;
- step &= 0x07;
- delay(70);
- }
- P1 = 0x00;
- werbg = 0;
- TR1 = 1;
- }
- if(werbg2==1)
- {
- while (beats--)
- {
- buf = P1 & 0xF0;
- buf |= BeatCodf[step];
- P1 = buf;
- step++;
- step &= 0x07;
- delay(70);
- }
- P1 = 0x00;
- werbg2 = 0;
- }
- }
- void KeyScan(void)
- {
- if(K2==0)
- {
- delay(10);
- if(K2==0)
- {
- while(!K2);
- werbg = 1;
-
- }
- }
- }
- void T0_Init()
- {
- TMOD = 0x11;
- TH0 = (65535-45872)/256;
- TL0 = (65535-45872)%256;
- TH1 = (65536-45872)/256;
- TL1 = (65536-45872)%256;
- EA = 1;
- ET0 = 1;
- ET1 = 1;
- TR0 = 1;
-
- }
- void T0_time() interrupt 1
- {
- TH0 = (65535-100)/256;
- TL0 = (65535-100)%256;
- num2++;
- if(num2 == 20)
- {
- num2 = 0;
- Display();
- }
- }
- void T1_time() interrupt 3
- {
- TH1 = (65536-45872)/256;
- TL1 = (65536-45872)%256;
-
- if(++t0_ct==2)
- {
- t0_ct = 0;
- if(Num==10)
- {
- Num=0;
- if(time==0)
- {
- time=25;
- }
- time--;
- if(time==0)
- {
- TR1 = 0;
- werbg2=1;
- }
- }
- else Num++;
- }
- }
- void Display(void)
- {
- unsigned char b3,b4;
- b3=time/10;
- b4=time%10;
- P0=table[b3];
- SMG3=0;
- delayms(1);
- SMG3=1;
- P0=table[b4];
- SMG4=0;
- delayms(1);
- SMG4=1;
- }
- void main()
- {
- T0_Init();
- while(1)
- {
- KeyScan();
-
- TrunMotor(20*1);
- }
- }
复制代码
|