- #include "stm32f10x.h" // Device header
- #include "Delay.h"
- #include "Motor.h"
- #include "Serial.h"
- #include "string.h"
- uint8_t KeyNum, moto_flag=0; //定义用于接收按键键码的变量
- int Speed=3000; //定义速度变量
- unsigned int dir=0,time=3;
- int main(void)
- {
- Motor_Init(); //直流电机初始化
- Serial_Init();
- Serial_SendString("welcome!!!\n\r");
- GPIO_SetBits(GPIOB, GPIO_Pin_5);
-
- while (1)
- {
-
-
- if (Serial_RxFlag == 1) //如果接收到数据包
- {
-
- /*将收到的数据包与预设的指令对比,以此决定将要执行的操作*/
- if (strcmp(Serial_RxPacket, "Clockwise") == 0) //如果收到LED_ON指令
- {
- Motor_Set(0,1*1000); //点亮LED
- Serial_SendString("Clockwise\r\n"); //串口回传一个字符串LED_ON_OK
-
- }
-
- else if (strcmp(Serial_RxPacket, "Counterclockwise") == 0) //如果收到LED_OFF指令
- {
- Motor_Set(1,1*1000); //点亮LED
- Serial_SendString("Clockwise_1\r\n"); //串口回传一个字符串LED_ON_OK
- }
- else if (strcmp(Serial_RxPacket, "OFF") == 0) //如果收到LED_OFF指令
- {
- GPIO_SetBits(GPIOB, GPIO_Pin_5); //熄灭LED
- Serial_SendString("MOTO OFF\r\n"); //串口回传一个字符串LED_OFF_OK
- }
- else if (strcmp(Serial_RxPacket, "SPEED ADD") == 0) //如果收到LED_OFF指令
- {
- Speed=Speed+10; //熄灭LED
- Serial_SendString("SPEED ADD\r\n"); //串口回传一个字符串LED_OFF_OK
- }
- else if (strcmp(Serial_RxPacket, "SPEED SUB") == 0) //如果收到LED_OFF指令
- {
- Speed=Speed-10; //熄灭LED
- Serial_SendString("SPEED SUB\r\n"); //串口回传一个字符串LED_OFF_OK
- }
- else //上述所有条件均不满足,即收到了未知指令
- {
- Serial_SendString("ERROR_COMMAND\r\n"); //串口回传一个字符串ERROR_COMMAND
-
- }
-
- Serial_RxFlag = 0; //处理完成后,需要将接收数据包标志位清零,否则将无法接收后续数据包
- }
-
-
-
-
- // dir=Serial_RxPacket[0];
- // time=Serial_RxPacket[2];
- // if(dir == 0)
- // {
- // Serial_SendString("dir is Clockwise!\r\n");
- // }
- // else if (dir == 1)
- // {
- // Serial_SendString("dir is Counterclockwise!\r\n");
- // }
- //
- // Motor_Set(dir,time*1000);
- //
- //// Motor_Set(0,3*1000);
- // Serial_SendByte(Serial_RxPacket[0]);
- // Serial_SendByte(Serial_RxPacket[1]);
- // Serial_SendByte(Serial_RxPacket[2]);
- //
- // Serial_SendByte(dir);
- // Serial_SendByte(time);
-
-
- }
- }
复制代码
https://live.csdn.net/v/424954
原理图: 无
仿真: 无
代码:
步进电机 V2.7z
(693.32 KB, 下载次数: 2)
|