这是我个人根据学到知识所编辑的一个源程序,仅供参考,有用的话,拿去不谢。
单片机源程序如下:
- #include
- #define uint unsigned int
- #define uchar unsigned char
- #define ulong unsigned long
- sbit you_2=P2^0; //红外探测端口定义
- sbit you_1=P2^1;
- sbit zhong=P2^2;
- sbit zuo_1=P2^3;
- sbit zuo_2=P2^4;
- sbit CG=P0^1;
- sbit DC=P0^0;
- uchar code L_F[8]= {0x10,0x30,0x20,0x60,0x40,0xc0,0x80,0x90}; //左电机正转 uchar code L_B[8]= {0x90,0x80,0xc0,0x40,0x60,0x20,0x30,0x10}; //左电机反转 uchar code R_F[8]= {0x01,0x03,0x02,0x06,0x04,0x0c,0x08,0x09}; //右电机正转 uchar code R_B[8]= {0x09,0x08,0x0c,0x04,0x06,0x02,0x03,0x01}; //右电机反转 uchar code B_F[8]= {0x91,0x83,0xc2,0x46,0x64,0x2c,0x38,0x19}; //左反右正 uchar code F_B[8]= {0x19,0x38,0x2c,0x64,0x46,0xc2,0x83,0x91}; //左正右反 uchar code duandian[8]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}; //断电
- unsigned char code qianjin[]={0x11,0x33,0x22,0x66,0x44,0xcc,0x88,0x99}; //前进 unsigned char h[]={0x11,0x33,0x22,0x66,0x44,0xcc,0x88,0x99}; //
- uchar i;
- void delay(uint z)
- {
- uint k ;
- uint j;
- for(k=0; k
- for(j=0; j
- }
- void QJ(unsigned int i)
- {
- for(i=0;i
- {
- P1=h[i]=qianjin[i];
- delay(13);
- }
- }
- void DD()
- {
- P1=0x00;
- delay(300);
- }
- void wtj()
- {
- while(1)
- {
- if(P2==0xff)
- {
- DD();
- delay(1000);
- break;
- }
- else
- {
- QJ(8);}
- }
- }
- void YG_1()
- {
- unsigned char i; for(i=0;i
- }
- }
- void ZG_1()
- { unsigned char i; for(i=0;i
- {P1=h[i]=B_F[i]; delay(10);
- }
- }
- void byg()
- {
- while(1)
- {
- if(P2==0xfb)
- break;
- else if(P2==0xf9) break;
- else if(P2==0xfd) break;
- /* if(P2^0==0)
- break;
- else if(P2^1==0) break;
- /* else if(P2^2==0) break;
- else if(P2^3==0) break;
- else if(P2^4==0) break;*/
- else
- YG_1();
- }
- }
- void bzg()
- {
- while(1)
- {
- if(P2==0xfb)
- break;
- else if(P2==0xf3) break;
- else if(P2==0xf7) break;
- /* if(P2^0==0)
- break;
- else if(P2^1==0) break;
- else if(P2^2==0) break;
- if(P2^3==0)
- break;
- else if(P2^4==0)
- break;*/
- else
- ZG_1();
- }
- }
- void YG_2()
- {
- // unsigned char i; // unsigned char g;
- if(P2==0xfa)
- {//delay(4000);
- wtj();
- byg();
- /*for(g=0;g
- {
- for(i=0;i
- { P1=F_B[i];
- delay(20);
- }
- } */
- }
- else if(P2==0xf2)
- {
- //delay(4000);
- wtj();
- byg();
- /*for(g=0;g
- {
- for(i=0;i
- { P1=F_B[i];
- delay(20);
- }
- }*/
- }
- else if(P2==0xf6)
- {
- //delay(4000);
- byg();
- /*for(g=0;g
- {
- for(i=0;i
- { P1=F_B[i];
- delay(20);
- }
- }*/
- }
- else if(P2==0xf4) {
- //delay(4000);
- wtj();
- byg();
- /*for(g=0;g
- {
- for(i=0;i
- { P1=F_B[i];
- delay(20);
- }
- }*/
- }
- else ;
- }
- void ZG_2()
- {
- //unsigned char i;
- //unsigned char g;
- if(P2==0xeb)
- {//delay(4000);
- wtj();
- bzg();
- /*for(g=0;g
- {
- for(i=0;i
- {P1=B_F[i];
- delay(20);
- }
- }*/
- else if(P2==0xe9)
- {
- //delay(4000);
- wtj();
- bzg();
- /* for(g=0;g
- for(i=0;i
- {P1=B_F[i];
- delay(20);
- }
- }*/
- }
- else if(P2==0xed)
- {
- //delay(4000);
- wtj();
- bzg();
- /* for(g=0;g
- for(i=0;i
- {P1=B_F[i];
- delay(20);
- }
- }*/
- }
- else if(P2==0xe5) {
- //delay(4000);
- wtj();
- bzg();
- /* for(g=0;g
- for(i=0;i
- {P1=B_F[i];
- delay(20);
- }
- }*/
- }
- else ;
- }
- void ZG() {
- unsigned char i; for(i=0;i
- {
- P1=h[i]=R_F[i]; delay(11);
- }
- }
- void YG() {
- unsigned char i; for(i=0;i
- {
- P1=h[i]=L_F[i]; delay(11);
- }
- }
- /*void HT()
- {
- unsigned char i; for(i=0;i
- {
- P1=houtui[i]; delay(55);
- }
- }*/
- void zd()
- {
- while(1)
- {if(P2==0xe0)
- QJ(8);
- else if(P2==0xff) {
- while(1)
- {
- DD();
- }
- }
- else
- {
- QJ(80);
- if(P2==0xff) {
- while(1)
- {
- DD();
- }
- }
- else
- {DC=0;
- break;
- break;}
- }
- }
- }
- void zd1()
- {
- while(1)
- {if(P2==0xe0)
- QJ(8);
- else if(P2==0xff) {
- while(1)
- {
- DD();
- }
- }
- else
- {
- QJ(80);
- if(P2==0xff)
- while(1)
- {
- DD();
- }
- }
- else
- {DC=0;
- break;
- }
- }
- }
- }
- void hy()
- {
- unsigned char i;
- for(i=0;i
- {
- P1=h[i];
- delay(10);
- }
- }
- void main()
- {
- DC=1;
- while(1)
- {
- QJ(8);
- if(P2!=0xff)
- {
- delay(9000); break;
- }
- }
- while(1)
- {
- DC=1;
- if(CG==1)
- {delay(9000);
- DC=1;
- while(1)
- {
- switch(P2)
- {
- case 0xfb:QJ(8);break;
- case 0xf1:QJ(8);break;
- case 0xf3:QJ(8);break; case 0xf7:ZG();break; case 0xe7:ZG_1();break; case 0xef:ZG_1();break; case 0xe3:ZG_1();break; case 0xe1:ZG_1();break;
- case 0xf9:QJ(8);break; case 0xfd:YG();break; case 0xfc:YG_1();break; case 0xfe:YG_1();break; case 0xf8:YG_1();break; case 0xf0:YG_1();break;
- case 0xfa:
- QJ(16);
- // delay(4000); YG_2();
- break;
- case 0xf2: //10010 //11011 //10001 //10011 //10111 //00111 //01111 //00011 //00001 //11001 //11101 //11100 //11110 //11000 //10000 //11010 //f7
- QJ(16);
- // delay(4000);
- YG_2();
- break;
- case 0xf6: //10110
- QJ(16);
- // delay(4000);
- YG_2();
- break;
- case 0xf4: //10100
- QJ(16);
- // delay(4000);
- YG_2();
- break;
- case 0xeb:
- QJ(16);
- // delay(4000);
- ZG_2();
- break;
- case 0xe9:
- QJ(16);
- // delay(4000);
- ZG_2();
- break;
- case 0xed:
- QJ(16);
- // delay(4000);
- ZG_2();
- break;
- case 0xe5:
- //01011 //fd //01001 //01101 //00101
- QJ(16);
- // delay(4000);
- ZG_2();
- break;
- case 0xe0:zd1();
- break; //00000
- case 0xff:hy();break;//11111
- default:QJ(8); break;
- }
- }
- }
- else
- {
- switch(P2)
- {
- case 0xfb:QJ(8);break; //11011
- case 0xf1:QJ(8);break; //10001
- case 0xf3:QJ(8);break; //10011 case 0xf7:ZG();break; //10111 case 0xe7:ZG_1();break; //00111 case 0xef:ZG_1();break; //01111 case 0xe3:ZG_1();break; //00011 case 0xe1:ZG_1();break; //00001
- case 0xf9:QJ(8);break; //11001 case 0xfd:YG();break; //11101 case 0xfc:YG_1();break; //11100 case 0xfe:YG_1();break; //11110 case 0xf8:YG_1();break; //11000
- case 0xf0:YG_1();break; //10000
- case 0xfa: //11010
- QJ(16);
- // delay(4000);
- YG_2();
- //f7
- break;
- case 0xf2: //10010
- QJ(16);
- // delay(4000);
- YG_2();
- break;
- case 0xf6: //10110
- QJ(16);
- // delay(4000);
- YG_2();
- break;
- case 0xf4: //10100
- QJ(16);
- // delay(4000);
- YG_2();
- break;
- case 0xeb:
- QJ(16);
- // delay(4000);
- ZG_2();
- break;
- case 0xe9:
- QJ(16);
- // delay(4000);
- ZG_2();
- break;
- case 0xed: //01011 //fd //01001 //01101
- QJ(16);
- // delay(4000);
- ZG_2();
- break;
- case 0xe5: //00101
- QJ(16);
- // delay(4000);
- ZG_2();
- break;
- case 0xe0:zd();
- break; //00000
- case 0xff:hy();break;//11111
- default:QJ(8); break;
- }
- }
- }
- }
复制代码
所有资料51hei提供下载:
2.docx
(18.28 KB, 下载次数: 4)
|