c语言的位运算.dox
位运算#include sbit k1 = P2^0; sbit k2 = P2^1; sbit k3 = P2^2; sbit k4 = P2^3; sbit left1 = P0^0; sbit left2 = P0^1; sbit right1 = P0^2; sbit right2 = P0^3; sbit left3=P0^4; sbit left4=P0^5; sbit right3=P0^6; sbit right4=P0^7; sbit led=P2^4; sbit led1=P2^5; typedef unsigned char u8; typedef unsi gned int u16; void delay(u16 x) //@11.0592MHz { unsigned char i, j, k; while(x--) { i = 5; j = 52; k = 10; do { do { while (--k); } while (--j); } while (--i); } } void init() { left1=0; right1=0; left2=0; right2=0; left3=0; right3=0; left4=0; right4=0; } void move() { left1=1; right1=1; left2=0; right2=0; left3=1; right3=1; left4=0; right4=0; } void back() { left1=0; right1=0; left2=1; right2=1; left3=0; right3=0; left4=1; right4=1; } void right() { left1=0; right1=1; left2=1; right2=0; left3=0; right3=1; left4=1; right4=0; } void left() { left1=1; right1=0; left2=0; right2=1; left3=1; right3=0; left4=0; right4=1; } void stop() { left1=0; right1=0; left2=0; right2=0; left3=0; right3=0; left4=0; right4=0; } void xunji() { unsigned char flag; if((k1 == 0)&&(k2 == 0)&&(k3==0)&&(k4==0)) //1代表检测到黑线 { flag =0; } if((k1 == 0)&&(k2== 1)&&(k3==1)&&(k4==0)) { flag = 0; } if((k1== 1)&&(k2== 0)&&(k3==0)&&(k4==0)) { flag = 1; } if((k1== 1)&&(k2== 1)&&(k3==0)&&(k4==0)) { flag = 1; } if((k1 == 0)&&(k2== 0)&&(k3==1)&&(k4==1)) { flag = 2; } if((k1== 0)&&(k2== 1)&&(k3==1)&&(k4==0)) { flag = 2; } switch(flag) { case 0:move();break; case 1:right();break; case 2:left();break; case 3:back();break; case 4:stop();break; default:stop();break; } } void main() { init(); // k1 = 1; // k2= 1; // k3 = 1; // k4 = 1; while(1) { xunji(); } } --------------------- gned int u16; void delay(u16 x) //@11.0592MHz { unsigned char i, j, k; while(x--) { i = 5; j = 52; k = 10; do { do { while (--k); } while (--j); } while (--i); } } void init() { left1=0; right1=0; left2=0; right2=0; left3=0; right3=0; left4=0; right4=0; } void move() { left1=1; right1=1; left2=0; right2=0; left3=1; right3=1; left4=0; right4=0; } void back() { left1=0; right1=0; left2=1; right2=1; left3=0; right3=0; left4=1; right4=1; } void right() { left1=0; right1=1; left2=1; right2=0; left3=0; right3=1; left4=1; right4=0; } void left() { left1=1; right1=0; left2=0; right2=1; left3=1; right3=0; left4=0; right4=1; } void stop() { left1=0; right1=0; left2=0; right2=0; left3=0; right3=0; left4=0; right4=0; } void xunji() { unsigned char flag; if((k1 == 0)&&(k2 == 0)&&(k3==0)&&(k4==0)) //1代表检测到黑线 { flag =0; } if((k1 == 0)&&(k2== 1)&&(k3==1)&&(k4==0)) { flag = 0; } if((k1== 1)&&(k2== 0)&&(k3==0)&&(k4==0)) { flag = 1; } if((k1== 1)&&(k2== 1)&&(k3==0)&&(k4==0)) { flag = 1; } if((k1 == 0)&&(k2== 0)&&(k3==1)&&(k4==1)) { flag = 2; } if((k1== 0)&&(k2== 1)&&(k3==1)&&(k4==0)) { flag = 2; } switch(flag) { case 0:move();break; case 1:right();break; case 2:left();break; case 3:back();break; case 4:stop();break; default:stop();break; } } void main() { init(); // k1 = 1; // k2= 1; // k3 = 1; // k4 = 1; while(1) { xunji(); } } ---------------------
暂无评论