#define STEP_PIN 26
#define DIR_PIN 27
#define BUTTON_PIN 33
//#define CIRCLE 1600 //#一圈1600个脉冲
int i_FOREWARD_STEP=2;//正转多少脉冲
int i_REVERSE_STEP=1;//反转多少脉冲
// setting PWM properties
const uint8_t freq_PWM = 5000;//设置频率,控制速度
const uint8_t channel_PWM = 0;//通道号,取值0 ~ 15
const uint8_t resolution_PWM = 8;//PWM分辨率,取值0 ~ 20
/*
MS1 MS2 MS3 操作模式 度数 跬步/转
0 0 0 全步(默认) 1.8 200
1 0 0 半步 0.9 400
0 1 0 1/4 步* 0.45 800
1 1 0 1/8 步长* 0.225 1600
1 1 1 1/16 步长* 0.1125 3200
*/
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial.println("Hello, ESP32!");
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(DIR_PIN, OUTPUT);
ledcSetup(channel_PWM, freq_PWM, resolution_PWM); // 设置通道
ledcAttachPin(STEP_PIN, channel_PWM); //将 LEDC 通道绑定到指定 IO 口上以实现输出
}
void loop() {
// put your main code here, to run repeatedly:
if(digitalRead(BUTTON_PIN)==LOW){
delay(300);
if (digitalRead(BUTTON_PIN)==LOW){
Serial.println("S");
// 1/频率*1000=毫秒
float ms=1.0/freq_PWM*1000.0*CIRCLE*i_FOREWARD;
Serial.printf("%f",ms);
ledcWrite(channel_PWM, 50);
delay(ms);
ledcWrite(channel_PWM, 0);
}
}
delay(10);
}