- #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)
2024-10-27 03:37 上傳
點擊文件名下載附件
下載積分: 黑幣 -5
|