久久久久久久999_99精品久久精品一区二区爱城_成人欧美一区二区三区在线播放_国产精品日本一区二区不卡视频_国产午夜视频_欧美精品在线观看免费

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 2997|回復: 0
上一主題 下一主題
收起左側

STM32步進電機加減速曲線程序

[復制鏈接]
跳轉到指定樓層
樓主
ID:168655 發表于 2019-2-19 17:56 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
基于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. #define StepEnPin GPIO_Pin_4
  180. extern int S;
  181. extern int t1;
  182. extern int r2;
  183. extern int pulse1;
  184. extern int pulse2;
  185. extern int StepCount;
  186. extern TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
  187. void Delay(u32 nCount);
  188. void f(int Vt,int a,int d,int S);


  189. TIM2中斷函數程序:
  190. void TIM2_IRQHandler(void)
  191. {   
  192.   if (TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
  193.   {
  194.     TIM_ClearFlag(TIM2, TIM_FLAG_Update);
  195.     GPIO_WriteBit(GPIOA,GPIO_Pin_3,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_3)));
  196.     StepCount=StepCount+1;
  197.     f(300,10,10,4000);
  198.    
  199.   }
  200. }
復制代碼

所有資料51hei提供下載:
STM32_motor.rar (2.12 KB, 下載次數: 67)


評分

參與人數 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎勵!

查看全部評分

分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏 分享淘帖 頂 踩
回復

使用道具 舉報

沙發
ID:58925 發表于 2019-5-9 20:04
下了打不開,解壓失敗

您需要登錄后才可以回帖 登錄 | 立即注冊

本版積分規則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術交流QQ群281945664

Powered by 單片機教程網

快速回復 返回頂部 返回列表
主站蜘蛛池模板: 欧美精品一区二区在线观看 | 欧美性大战久久久久久久蜜臀 | 天天操天天怕 | 天天夜夜操 | 久久久一区二区三区 | 免费在线观看一区二区 | 精品久久一区二区三区 | 亚洲精品一区二区三区在线 | 一区二区不卡高清 | 99伊人 | 国产成人福利在线观看 | 日本亚洲一区 | 国产黄色av网站 | 国产色99 | 91亚洲欧美 | 亚洲一区二区三区四区五区中文 | 日韩中文字幕在线观看 | 中文字幕日韩一区 | 亚洲综合字幕 | 亚洲视频在线观看 | 亚洲精品一区二区在线观看 | 午夜免费观看网站 | 91国内外精品自在线播放 | av免费看片| 国产亚洲精品精品国产亚洲综合 | 一区二区三区中文字幕 | 精品久久久久香蕉网 | 欧洲一区二区三区 | 金莲网| 欧美一级特黄aaa大片在线观看 | 国产精品久久久久久久久久久新郎 | 欧美国产一区二区 | 在线观看国产视频 | 国家aaa的一级看片 h片在线看 | 久久99久久久久 | 在线免费观看毛片 | 无码一区二区三区视频 | 久久99成人 | 成人h视频在线观看 | 颜色网站在线观看 | 正在播放国产精品 |