完善资料让更多小伙伴认识你,还能领取20积分哦, 立即完善>
|
|
相关推荐
1个回答
|
|
新增电位器模拟量输入调节步进电机转速,起保停,正反转依旧采用前面文章所使用的外部按键触发中断方法来实现,
/** *************************************************************************************************外部按键中断精准控制步进电机起保停,正反转,加减速Arduino UNO + TB6600 * 步进电机基础例程 * 利用模拟输出口调速 * A0引脚接电位器,GND-A0-3.3V */#include "PinChangeInt.h"const unsigned int EN_PIN = 8; //使能引脚const unsigned int DIR_PIN = 10; //方向引脚const unsigned int STEP_PIN = 9; //脉冲引脚const unsigned int speedup_PIN = 11; //速度引脚const unsigned int speeddown_PIN = 12; //速度引脚const unsigned int for_PIN = 7;//正转const unsigned int ror_PIN = 6;//反转const unsigned int stop_pin = 5;//停止static unsigned int speeds= 500;//初始速度static unsigned int value;void Motorstop(){ Serial.println("停转"); digitalWrite(EN_PIN, HIGH); digitalWrite(DIR_PIN, HIGH); }void for_key(){ digitalWrite(EN_PIN, LOW); digitalWrite(DIR_PIN, LOW); Serial.println("正转"); }void ror_key(){ digitalWrite(EN_PIN, LOW); digitalWrite(DIR_PIN, HIGH); Serial.println("反转"); }//void speed_down(){// // switch(speeds)// { // case 400: speeds +=50;delay(200);Serial.println("speed up:+50 速度:" + String(speeds)); break;// case 450: speeds +=50;delay(200);Serial.println("speed up:+50 速度:" + String(speeds)); break;// case 500: speeds +=50;delay(200);Serial.println("speed up:+50 速度:" + String(speeds)); break;// case 550: speeds +=50;delay(200); Serial.println("speed up:+50 速度:" + String(speeds));break;// case 600: speeds +=50;delay(200);Serial.println("speed up:+50 速度:" + String(speeds)); break;// case 650: speeds +=50;delay(200);Serial.println("speed up:+50 速度:" + String(speeds)); break;// case 700: speeds +=50;delay(200);Serial.println("speed up:+50 速度:" +String(speeds)); break;// case 750: speeds +=50;delay(200);Serial.println("speed up:+50 速度:" + String(speeds)); break;// case 800: speeds +=50;delay(200);Serial.println("speed up:+50 速度:" + String(speeds)); break;// default: Serial.println("Speed to a minimum!"); break;// // }//}//void speed_up(){//// switch(speeds)// { // case 450: speeds -=50;delay(200);Serial.println("speed up:-50 速度:"+String(speeds)); break;// case 500: speeds -=50;delay(200);Serial.println("speed up:-50 速度:"+String(speeds));break;// case 550: speeds -=50;delay(200);Serial.println("speed up:-50 速度:"+String(speeds)); break;// case 600: speeds -=50;delay(200);Serial.println("speed up:-50 速度:"+String(speeds)); break;// case 650: speeds -=50;delay(200);Serial.println("speed up:-50 速度:"+String(speeds));break;// case 700: speeds -=50;delay(200);Serial.println("speed up:-50 速度:"+String(speeds));break;// case 750: speeds -=50;delay(200);Serial.println("speed up:-50 速度:"+String(speeds)); break;// case 800: speeds -=50;delay(200);Serial.println("speed up:-50 速度:"+String(speeds)); break; // default: Serial.println("Reach maximum speed!"); break;// }//}void setup() { sei(); Serial.begin(115200); pinMode( EN_PIN, OUTPUT ); pinMode( DIR_PIN, OUTPUT ); pinMode( STEP_PIN, OUTPUT );// pinMode(speedup_PIN,INPUT_PULLUP);// pinMode(speeddown_PIN,INPUT_PULLUP); digitalWrite(EN_PIN, HIGH); //使能步进电机 digitalWrite(DIR_PIN, LOW); //方向初始化 digitalWrite(STEP_PIN, LOW); pinMode(for_PIN,INPUT_PULLUP); pinMode(ror_PIN,INPUT_PULLUP); pinMode(stop_pin,INPUT_PULLUP); attachPinChangeInterrupt(stop_pin , Motorstop, FALLING); attachPinChangeInterrupt( for_PIN , for_key, FALLING ); attachPinChangeInterrupt( ror_PIN , ror_key, FALLING );// attachPinChangeInterrupt( speedup_PIN , speed_up, FALLING );// attachPinChangeInterrupt( speeddown_PIN , speed_down, FALLING );// LOW 当引脚为低电平时,触发中断//CHANGE 当引脚电平发生改变时,触发中断//RISING 当引脚由低电平变为高电平时,触发中断//FALLING 当引脚由高电平变为低电平时,触发中断. Serial.println("Starting......");}void loop() { Motor_run();}void Motor_run(){ value = analogRead(A0); if(value > 50){ speeds = map(value, 0, 1023, 400, 900);} digitalWrite(STEP_PIN, HIGH); delayMicroseconds(speeds); digitalWrite(STEP_PIN, LOW); delayMicroseconds(speeds); Serial.println("Now Speed:"+String(speeds)); } |
|
|
|
只有小组成员才能发言,加入小组>>
2435 浏览 0 评论
9100 浏览 4 评论
36773 浏览 19 评论
5026 浏览 0 评论
24745 浏览 34 评论
1528浏览 2评论
1743浏览 1评论
2193浏览 1评论
1553浏览 0评论
524浏览 0评论
小黑屋| 手机版| Archiver| 电子发烧友 ( 湘ICP备2023018690号 )
GMT+8, 2024-12-22 21:09 , Processed in 1.233748 second(s), Total 77, Slave 60 queries .
Powered by 电子发烧友网
© 2015 bbs.elecfans.com
关注我们的微信
下载发烧友APP
电子发烧友观察
版权所有 © 湖南华秋数字科技有限公司
电子发烧友 (电路图) 湘公网安备 43011202000918 号 电信与信息服务业务经营许可证:合字B2-20210191 工商网监 湘ICP备2023018690号