跷跷板小车仿真原理图如下(proteus仿真工程文件可到本帖附件中下载)
单片机源程序如下:
- #include <AT89X51.H>
- unsigned char code dispcode[]={0x3f,0x06,0x5b,0x4f,
- 0x66,0x6d,0x7d,0x07,
- 0x7f,0x6f,0x77,0x7c,
- 0x39,0x5e,0x79,0x71,0x00};
- unsigned char second;
- unsigned char tcount,a,temp,b,c,zongshijian;
- void motor11(void)
- {
- P1_4=1;
- P1_5=0;
- P1_6=1;
- P1_7=0;
- }
- void motor01(void)
- {
- P1_4=0;
- P1_5=0;
- P1_6=1;
- P1_7=0;
- }
- void motor10(void)
- {
- P1_4=1;
- P1_5=0;
- P1_6=0;
- P1_7=0;
- }
- void motor00(void)
- {
- P1_4=0;
- P1_5=0;
- P1_6=0;
- P1_7=0;
- }
- void motor22(void)
- {
- P1_4=0;
- P1_5=1;
- P1_6=0;
- P1_7=1;
- }
- void motor02(void)
- {
- P1_4=0;
- P1_5=0;
- P1_6=0;
- P1_7=1;
- }
- void motor20(void)
- {
- P1_4=0;
- P1_5=1;
- P1_6=0;
- P1_7=0;
- }
- void delay1s(void)
- {
- unsigned char i,j,k;
- for(k=100;k>0;k--)
- for(i=20;i>0;i--)
- for(j=248;j>0;j--);
- }
- //------------------------------------------------------------------------------------------------------------//
- delay(unsigned int dd)
- {
- while(--dd);
- }
- //-------------------------------------------------------------------------------------------------------------//
- void main(void)
- {
- // P1=0xff;
- TMOD=0x01;
- TH0=(65536-50000)/256;
- TL0=(65536-50000)%256;
- TR0=1;
- ET0=1;
- EX0=1;
- IT0=1;
- EX1=1;
- IT1=1;
- temp=0;
- a=0;
- b=0;
- c=0;
- zongshijian=0;
- tcount=0;
- second=0;
- delay1s();
- motor00();
- while(1)
- {
- if(P1_0==0)
- {
- EA=1;
- motor11();
- temp=1;
- }
- if(P1_1==0&&P1_2==0&&temp==1&&b==0)
- motor11();
- if(P1_1==1&&P1_2==0&&temp==1&&b==0)
- motor01();
- if(P1_1==0&&P1_2==1&&temp==1&&b==0)
- motor10();
- if(P1_1==1&&P1_2==1&&temp==1&&b==0)
- motor11();
- if(P1_1==0&&P1_2==0&&temp==1&&b==1)
- motor22();
- if(P1_1==1&&P1_2==0&&temp==1&&b==1)
- motor02();
- if(P1_1==0&&P1_2==1&&temp==1&&b==1)
- motor20();
- if(P1_1==1&&P1_2==1&&temp==1&&b==1)
- motor22();
- if(P1_3==1&&b==1&temp==1)
- {
- motor00();
- c=1;
- zongshijian=second;
- }
- if(c==1)
- {
- motor00();
- }
- }
- }
- //--------------------------------------------------------------------------------------------------------//
- void t0(void) interrupt 1 using 0
- {
- tcount++;a++;
-
- if(a>=2)
- {P2=0xff;
- P0=dispcode[second/10];
- P2=0xfe;}
- if(a<2)
- {P2=0xff;
- P2=0xfd;
- P0=dispcode[second%10];
- }
- if(a>=4)
- {a=0;}
- if(tcount==200)
- {
- tcount=0;
- if(c==0)
- {
- second++;
- }
- if(c==1)
- {
- second=zongshijian;
- }
- if(second==100)
- {
- second=0;
- }
- }
- TH0=(65536-5000)/256;
- TL0=(65536-50000)%256;
- }
- ///////////////-------------------------------------------------------------------------//
- int0()interrupt 0
- {EX0=0;
- motor00();
- P2=0xfd;P0=0x3f;
- delay1s();P0=0x06;
- delay1s();P0=0x5b;
- delay1s();P0=0x4f;
- delay1s();P0=0x66;
- delay1s();P0=0x6d;
- delay1s();P0=0x7d;
- second+=6;
- EX0=1;
- motor00();
- }
- //--------------------------------------------------------------------------------------------//
- int1()interrupt 2
- {EX1=0;
- EX0=0;
- motor00();
- P2=0xfd;P0=0x3f;
- delay1s();P0=0x06;
- delay1s();P0=0x5b;
- delay1s();P0=0x4f;
- delay1s();P0=0x66;
- delay1s();P0=0x6d;
- delay1s();P0=0x7d;
- second+=6;
- b=1;
- // EX1=1;
- }
- //--------------------------------------------------------------------------------------------//
复制代码
所有资料51hei提供下载:
跷跷板小车.rar
(56.75 KB, 下载次数: 11)
|