程序簡單,清晰易懂供給大家研究。
單片機源程序如下:
- #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)
2018-7-13 20:01 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|