程序简单,清晰易懂供给大家研究。
单片机源程序如下:
- #include <reg52.h>
- #define uchar unsigned char
- #define uint unsigned int
- sbit in1=P1^0;
- sbit in2=P1^2;
- uint a,b;
- void ys(uint z) //延时
- {
- uint x,y;
- for(z=x;x>0;x--)
- for(y=110;y>0;y--);
- }
- void main()
- {
- P1=0xff; //赋初值
- P2=0x00; //赋初值
- while(1)// 循环检测
- {
-
-
- while(in1==0) ////车子右偏,让左边减速
- {
- P2=0x00;
- P2=0x00;
- P2=0x00;
- P2=0x00;
- P2=0x00;
- P2=0x00;
- P2=0x00;
- P2=0x00;
- P2=0x00;
- P2=0x00;
-
- P2=0x90; //发送的是C,右转指令
- ys(50);
- }
-
- while(in2==0) //车子左偏,让右边减速
- {
- P2=0x00;
- P2=0x00;
- P2=0x00;
- P2=0x00;
- P2=0x00;
- P2=0x00;
- P2=0x00;
- P2=0x00;
-
- P2=0x60; //发送的是D,左转指令
- ys(50);
- }
- P2=0xa0;
- ys(1000);
-
- }
- }
复制代码
所有资料51hei提供下载:
实验13:寻轨车.zip
(27.69 KB, 下载次数: 8)
|