找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
查看: 7007|回复: 8
收起左侧

STM32步进电机加减速曲线源程序

  [复制链接]
ID:245515 发表于 2017-11-2 22:23 | 显示全部楼层 |阅读模式
STM32的步进电机控制的加减速曲 ..很好的程序。值得学习.

单片机源程序如下:
  1. #include"stm32f10x_lib.h"
  2. #include"main.h"
  3. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  4. GPIO_InitTypeDef GPIO_InitStructure;
  5. ErrorStatus HSEStartUpStatus;
  6. TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  7. int pulse;
  8. int StepCount;
  9. int pulse1;
  10. int pulse2;
  11. int t1;
  12. int t2;
  13. int r1;
  14. int r2;
  15. void RCC_Configuration(void);
  16. void NVIC_Configuration(void);
  17. void GPIO_Configuration(void);
  18. void TIM2_Configuration(void);
  19. void f(int Vt,int a,int d,int S);
  20. #define VECT_TAB_RAM
  21. int main(void)
  22. {  
  23.   #ifdef DEBUG
  24.     debug();/*[初始化外围设备指针]*/
  25.   #endif
  26.   RCC_Configuration(); //初始化时钟与复位  
  27.   NVIC_Configuration();//初始化中断嵌套
  28.   TIM2_Configuration();//初始化定时器
  29.   GPIO_Configuration();
  30.   
  31.   GPIO_WriteBit(GPIOD, GPIO_Pin_7, (BitAction)(0));
  32.   GPIO_WriteBit(GPIOD, GPIO_Pin_6, (BitAction)(0));  //DCY1 DCY2为00,即Normal %0 DECAY
  33.   
  34.   GPIO_WriteBit(GPIOE, GPIO_Pin_7, (BitAction)(1));
  35.   GPIO_WriteBit(GPIOB, GPIO_Pin_1, (BitAction)(0));  //M1M2为10,即1-2-phase
  36.   //GPIO_WriteBit(GPIOA, GPIO_Pin_4, (BitAction)(1));  //正向旋转
  37.   //GPIO_WriteBit(GPIOA,GPIO_Pin_4,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_4)));正、反向旋转控制
  38.   
  39.   GPIO_WriteBit(GPIOB, GPIO_Pin_0, (BitAction)(0));
  40.   GPIO_WriteBit(GPIOC, GPIO_Pin_5, (BitAction)(1));  //TQ1 TQ2为01,即Current Ratio为50%
  41.   
  42.   GPIO_WriteBit(GPIOA, GPIO_Pin_7, (BitAction)(1));  //StepReset位
  43.   GPIO_WriteBit(GPIOC, GPIO_Pin_4, (BitAction)(1));  //StepEn 使能位
  44.   
  45. while(1)
  46.   {
  47.   r1=0;
  48.   r2=10;
  49.   StepCount=0;
  50.   GPIO_WriteBit(GPIOA,GPIO_Pin_4,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_4)));
  51.   TIM2_Configuration();
  52.   do
  53.   {
  54.   }while(r2);
  55.   
  56.   TIM_Cmd(TIM2, DISABLE);
  57.   Delay(7000000);
  58.   }
  59. }

  60. void GPIO_Configuration(void)
  61. {
  62.   GPIO_InitTypeDef GPIO_InitStructure;
  63.   GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_7;   //PA的3.4.7接CLK,CW/CCW,StepReset
  64.   GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  65.   GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  66.   GPIO_Init(GPIOA,&GPIO_InitStructure);
  67.   GPIO_InitStructure.GPIO_Pin= GPIO_Pin_5 | GPIO_Pin_6;   //PA的6.7接Protect和Mo
  68.   GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
  69.   GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  70.   GPIO_Init(GPIOA,&GPIO_InitStructure);
  71.   GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4 | GPIO_Pin_5;   //PC的4.5接StepEn和TQ2
  72.   GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  73.   GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  74.   GPIO_Init(GPIOC,&GPIO_InitStructure);
  75.   GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0 | GPIO_Pin_1;   //PB的0.1接TQ1和M2
  76.   GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  77.   GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  78.   GPIO_Init(GPIOB,&GPIO_InitStructure);
  79.   GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7;   //PE7接M1
  80.   GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  81.   GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  82.   GPIO_Init(GPIOE,&GPIO_InitStructure);
  83.   GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6 | GPIO_Pin_7;   //PD的67接DCY2和DCY1
  84.   GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  85.   GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  86.   GPIO_Init(GPIOD,&GPIO_InitStructure);
  87. }

  88. void NVIC_Configuration(void)
  89. {
  90.   NVIC_InitTypeDef NVIC_InitStructure;
  91. #ifdef  VECT_TAB_RAM   
  92.   NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
  93. #else
  94.   NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);   
  95. #endif                                                  
  96.   NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;  //设置TIM2通道输入中断
  97.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; /*配置优先级组*/
  98.   NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  
  99.   NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;      /*允许TIM2全局中断*/
  100.   NVIC_Init(&NVIC_InitStructure);
  101. }
  102. void TIM2_Configuration(void)
  103. {
  104.   TIM_SetCounter( TIM2, 0x0000);
  105.   TIM_ClearFlag(TIM2, TIM_FLAG_Update);        /*清除更新标志位*/
  106.   TIM_ClearITPendingBit(TIM2, TIM_FLAG_Update); //清除TIM2等待中断更新中断标志位
  107.   TIM_ARRPreloadConfig(TIM2, ENABLE);        /*预装载寄存器的内容被立即传送到影子寄存器 */
  108.   TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); //使能TIM2的更新   
  109.   TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  110.   TIM_TimeBaseStructure.TIM_Period = 2000;      //设定的最大计数值2000,最大计数值是0xffff
  111.   TIM_TimeBaseStructure.TIM_Prescaler = 72;     //分频72
  112.   TIM_TimeBaseStructure.TIM_ClockDivision = 0;     // 时钟分割
  113.   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //计数方向向上计数
  114.   TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
  115.   TIM_Cmd(TIM2, ENABLE);       //TIM2 enable counter
  116. }
  117. void RCC_Configuration(void)
  118. {      
  119.   RCC_DeInit();
  120.   RCC_HSEConfig(RCC_HSE_ON);         
  121.   HSEStartUpStatus = RCC_WaitForHSEStartUp();
  122.   if(HSEStartUpStatus == SUCCESS)
  123.   {
  124.     FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
  125.     FLASH_SetLatency(FLASH_Latency_2);
  126.     RCC_HCLKConfig(RCC_SYSCLK_Div1);
  127.     RCC_PCLK2Config(RCC_HCLK_Div1);
  128.     RCC_PCLK1Config(RCC_HCLK_Div2);
  129.     RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
  130.     RCC_PLLCmd(ENABLE);
  131.     while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
  132.     {
  133.     }
  134.     RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
  135.     while(RCC_GetSYSCLKSource() != 0x08)
  136.     {
  137.     }
  138.   }  
  139.   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC
  140.                                 | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE,ENABLE);
  141.   RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //打开TIM2的时钟
  142. }   
  143. void Delay(u32 nCount)
  144. {
  145.   do{
  146.     }
  147.   while(nCount--);
  148. }

  149. void f(int Vt,int a,int d,int S)
  150. {
  151.   int pulse1;
  152.   int pulse2;
  153.   t1=Vt/a;
  154.   
  155. if(StepCount<t1)                 //加速阶段,分t1级加速
  156.   {
  157.   r1++;
  158.   pulse1=(150000*t1)/(r1*Vt);
  159.   TIM_SetAutoreload(TIM2,pulse1);
  160.   }
  161.                           
  162. if(t1<=StepCount<=4*S)          //匀速阶段运行要求的步数或者距离
  163. {
  164.   pulse=150000/Vt;
  165.   TIM_SetAutoreload(TIM2,pulse);
  166. }

  167. if(StepCount>4*S)                            //减速阶段,走完S步后开始减速,分t2-1级减速
  168.   {
  169.     r2--;
  170.    
  171.   if(t2>=1)
  172.   {
  173.   pulse2=(150000*t2)/(r2*Vt);
  174.   TIM_SetAutoreload(TIM2,pulse2);
  175.   }

  176.   }
  177. }

  178. main.h文件内容:
  179. ……………………

  180. …………限于本文篇幅 余下代码请从51黑下载附件…………
复制代码

所有资料51hei提供下载:
STM32的步进电机控制的加减速曲线编程.rar (2.15 KB, 下载次数: 218)


回复

使用道具 举报

ID:323291 发表于 2018-5-6 10:29 | 显示全部楼层
厉害厉害厉害
回复

使用道具 举报

ID:326606 发表于 2018-8-23 09:29 | 显示全部楼层
参考参考,谢谢分享
回复

使用道具 举报

ID:384759 发表于 2018-8-24 23:32 | 显示全部楼层
谢谢楼主分享
回复

使用道具 举报

ID:155719 发表于 2018-9-10 14:47 | 显示全部楼层
貌似我的积分不太够了
回复

使用道具 举报

ID:393077 发表于 2018-9-11 09:05 | 显示全部楼层
谢谢楼主分享!
回复

使用道具 举报

ID:524621 发表于 2020-4-29 14:24 | 显示全部楼层
参考一下,感谢分享
回复

使用道具 举报

ID:302325 发表于 2020-4-30 09:50 | 显示全部楼层
参考参考,谢谢分享
回复

使用道具 举报

ID:750975 发表于 2020-5-13 18:33 | 显示全部楼层
谢谢楼主分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

手机版|小黑屋|51黑电子论坛 |51黑电子论坛6群 QQ 管理员QQ:125739409;技术交流QQ群281945664

Powered by 单片机教程网

快速回复 返回顶部 返回列表