标题: STM32单片机驱动tb6600步进电机程序 [打印本页]

作者: abscond    时间: 2024-10-25 14:34
标题: STM32单片机驱动tb6600步进电机程序
  1. #include "stm32f10x.h"                  // Device header
  2. #include "Delay.h"
  3. #include "Motor.h"
  4. #include "Serial.h"
  5. #include "string.h"

  6. uint8_t KeyNum, moto_flag=0;                //定义用于接收按键键码的变量
  7. int Speed=3000;                //定义速度变量

  8. unsigned int dir=0,time=3;

  9. int main(void)
  10. {

  11.         Motor_Init();                //直流电机初始化
  12.         Serial_Init();
  13.         Serial_SendString("welcome!!!\n\r");


  14.         GPIO_SetBits(GPIOB, GPIO_Pin_5);
  15.         
  16.         while (1)
  17.         {
  18.                
  19.                
  20.                 if (Serial_RxFlag == 1)                //如果接收到数据包
  21.                 {

  22.                                 
  23.                         /*将收到的数据包与预设的指令对比,以此决定将要执行的操作*/
  24.                         if (strcmp(Serial_RxPacket, "Clockwise") == 0)                        //如果收到LED_ON指令
  25.                         {
  26.                                 Motor_Set(0,1*1000);                                                                                //点亮LED
  27.                                 Serial_SendString("Clockwise\r\n");                                //串口回传一个字符串LED_ON_OK
  28.                

  29.                         }
  30.                         
  31.                         else if (strcmp(Serial_RxPacket, "Counterclockwise") == 0)        //如果收到LED_OFF指令
  32.                         {
  33.                                 Motor_Set(1,1*1000);                                                                                        //点亮LED
  34.                                 Serial_SendString("Clockwise_1\r\n");                                //串口回传一个字符串LED_ON_OK

  35.                         }
  36.                         else if (strcmp(Serial_RxPacket, "OFF") == 0)        //如果收到LED_OFF指令
  37.                         {
  38.                                 GPIO_SetBits(GPIOB, GPIO_Pin_5);                                                                //熄灭LED
  39.                                 Serial_SendString("MOTO OFF\r\n");                        //串口回传一个字符串LED_OFF_OK

  40.                         }
  41.                         else if (strcmp(Serial_RxPacket, "SPEED ADD") == 0)        //如果收到LED_OFF指令
  42.                         {
  43.                                 Speed=Speed+10;        //熄灭LED
  44.                                 Serial_SendString("SPEED ADD\r\n");                        //串口回传一个字符串LED_OFF_OK

  45.                         }
  46.                         else if (strcmp(Serial_RxPacket, "SPEED SUB") == 0)        //如果收到LED_OFF指令
  47.                         {
  48.                                 Speed=Speed-10;        //熄灭LED
  49.                                 Serial_SendString("SPEED SUB\r\n");                        //串口回传一个字符串LED_OFF_OK

  50.                         }
  51.                         else                                                //上述所有条件均不满足,即收到了未知指令
  52.                         {
  53.                                 Serial_SendString("ERROR_COMMAND\r\n");                        //串口回传一个字符串ERROR_COMMAND
  54.                
  55.                         }
  56.                         
  57.                         Serial_RxFlag = 0;                        //处理完成后,需要将接收数据包标志位清零,否则将无法接收后续数据包
  58.                 }
  59.                
  60.                
  61.                
  62.                
  63. //                                dir=Serial_RxPacket[0];
  64. //                        time=Serial_RxPacket[2];
  65. //                        if(dir == 0)
  66. //                        {
  67. //                                Serial_SendString("dir is Clockwise!\r\n");
  68. //                        }
  69. //                        else if (dir == 1)
  70. //                        {
  71. //                                Serial_SendString("dir is Counterclockwise!\r\n");
  72. //                        }
  73. //                        
  74. //                        Motor_Set(dir,time*1000);
  75. //                        
  76. ////                        Motor_Set(0,3*1000);
  77. //                        Serial_SendByte(Serial_RxPacket[0]);
  78. //                        Serial_SendByte(Serial_RxPacket[1]);
  79. //                        Serial_SendByte(Serial_RxPacket[2]);
  80. //               
  81. //                        Serial_SendByte(dir);
  82. //                        Serial_SendByte(time);        
  83.                
  84.                


  85.         }
  86. }
复制代码


https://live.csdn.net/v/424954

原理图: 无
仿真: 无
代码: 步进电机 V2.7z (693.32 KB, 下载次数: 2)

作者: bb1237    时间: 2025-1-21 10:37
下载来学习一下!感谢分享!




欢迎光临 (http://www.51hei.com/bbs/) Powered by Discuz! X3.1