资源描述:
《工业控制技术实验程序》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、试验一程序#definePWM6unsignedcharled[6]={*,*,*,*,*,*};voidsetup(){for(inti=0;i<6;i++)pinMode(led[i],OUTPUT);pinMode(PWM,OUTPUT);}voidloop(){for(inti=0;i<6;i++)//正循环点亮{digitalWrite(led[i],HIGH);delay(100);digitalWrite(led[i],LOW);}for(inti=5;i>-1;i--)//反循环点
2、亮{digitalWrite(led[i],HIGH);delay(100);digitalWrite(led[i],LOW);}for(intj=0;j<3;j++)//全部灯闪烁三次{for(inti=0;i<6;i++){digitalWrite(led[i],LOW);}delay(500);for(inti=0;i<6;i++){digitalWrite(led[i],HIGH);}delay(500);}for(intk=0;k<256;k++)//全部灯逐渐熄灭{analogWrit
3、e(PWM,k);delay(20);}for(intk=255;k>-1;k--)//全部灯慢慢点亮{10analogWrite(PWM,k);delay(20);}for(inti=0;i<6;i++)//全部灯熄灭{digitalWrite(led[i],LOW);}while(1);}样例2:#definePinA2//中断0unsignedcharled[8]={38,39,40,41,42,43,44,45};volatileunsignedcharj;voidsetup(){for(
4、inti=0;i<8;i++)pinMode(led[i],OUTPUT);pinMode(PinA,INPUT);//D2脚为输入attachInterrupt(0,count,RISING);}voidloop(){while(1){digitalWrite(led[j],HIGH);if(j>=8){j=0;for(inti=0;i<8;i++)digitalWrite(led[i],LOW);}}}voidcount(){j++;}10试验二程序constintencoderPinA=3;
5、constintencoderPinB=6;oleanencoderALast=LOW; //remembersthepreviouspinstate unsignedlongtime=0;longencoderPos=0; //holdstempvalsintval;intpinI1=42;//定义I1接口intpinI2=44;//定义I2接口intspeedpin=46;//定义EA(PWM调速)接口 intpinI3=31;//定义I1接口intpinI4=33;//定义I2接口int
6、speedpin2=35;//定义EA(PWM调速)接口 //Initializevoidsetup(){ pinMode(pinI1,OUTPUT);//定义该接口为输出接口 pinMode(pinI2,OUTPUT); pinMode(speedpin,OUTPUT); pinMode(pinI3,OUTPUT);//定义该接口为输出接口 pinMode(pinI4,OUTPUT); pinMode(speedpin2,OUTPUT); analogWrite(speedpin,1
7、00);//输入模拟值进行设定速度 digitalWrite(pinI2,LOW);//使直流电机顺时针转 digitalWrite(pinI1,HIGH); analogWrite(speedpin2,130);//输入模拟值进行设定速度 digitalWrite(pinI3,LOW);//使直流电机顺时针转 digitalWrite(pinI4,HIGH); pinMode(encoderPinA,INPUT); pinMode(encoderPinB,INPUT); digi
8、talWrite(encoderPinA,HIGH); digitalWrite(encoderPinB,HIGH);10 Serial.begin(9600); time=millis();//时间初值} voidloop(){ if((millis()-time)>500) { Serial.println(encoderPos); time=millis(); } booleanencoderA=digitalRead(encoderPinA);