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

 找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 2894|回復(fù): 0
上一主題 下一主題
收起左側(cè)

STM32步進(jìn)電機(jī)加減速曲線程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
ID:168655 發(fā)表于 2019-2-19 17:56 | 只看該作者 回帖獎(jiǎng)勵(lì) |倒序?yàn)g覽 |閱讀模式
基于STM32控制步進(jìn)電機(jī)加減速曲線程序

單片機(jī)源程序如下:
  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();/*[初始化外圍設(shè)備指針]*/
  25.   #endif
  26.   RCC_Configuration(); //初始化時(shí)鐘與復(fù)位  
  27.   NVIC_Configuration();//初始化中斷嵌套
  28.   TIM2_Configuration();//初始化定時(shí)器
  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));  //正向旋轉(zhuǎn)
  37.   //GPIO_WriteBit(GPIOA,GPIO_Pin_4,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_4)));正、反向旋轉(zhuǎn)控制
  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;  //設(shè)置TIM2通道輸入中斷
  97.   NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; /*配置優(yōu)先級(jí)組*/
  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);        /*清除更新標(biāo)志位*/
  106.   TIM_ClearITPendingBit(TIM2, TIM_FLAG_Update); //清除TIM2等待中斷更新中斷標(biāo)志位
  107.   TIM_ARRPreloadConfig(TIM2, ENABLE);        /*預(yù)裝載寄存器的內(nèi)容被立即傳送到影子寄存器 */
  108.   TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); //使能TIM2的更新   
  109.   TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  110.   TIM_TimeBaseStructure.TIM_Period = 2000;      //設(shè)定的最大計(jì)數(shù)值2000,最大計(jì)數(shù)值是0xffff
  111.   TIM_TimeBaseStructure.TIM_Prescaler = 72;     //分頻72
  112.   TIM_TimeBaseStructure.TIM_ClockDivision = 0;     // 時(shí)鐘分割
  113.   TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //計(jì)數(shù)方向向上計(jì)數(shù)
  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); //打開(kāi)TIM2的時(shí)鐘
  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級(jí)加速
  156.   {
  157.   r1++;
  158.   pulse1=(150000*t1)/(r1*Vt);
  159.   TIM_SetAutoreload(TIM2,pulse1);
  160.   }
  161.                           
  162. if(t1<=StepCount<=4*S)          //勻速階段運(yùn)行要求的步數(shù)或者距離
  163. {
  164.   pulse=150000/Vt;
  165.   TIM_SetAutoreload(TIM2,pulse);
  166. }

  167. if(StepCount>4*S)                            //減速階段,走完S步后開(kāi)始減速,分t2-1級(jí)減速
  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文件內(nèi)容:
  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中斷函數(shù)程序:
  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. }
復(fù)制代碼

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


評(píng)分

參與人數(shù) 1黑幣 +50 收起 理由
admin + 50 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

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

使用道具 舉報(bào)

沙發(fā)
ID:58925 發(fā)表于 2019-5-9 20:04
下了打不開(kāi),解壓失敗

本版積分規(guī)則

手機(jī)版|小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表
主站蜘蛛池模板: 18av在线播放| 久久99精品视频 | 国产精品一区二区不卡 | 午夜a v电影 | 亚洲高清在线观看 | 久久精品视频免费观看 | 久久精品91久久久久久再现 | 精品视频免费 | 久久尤物免费一区二区三区 | 男女污网站 | 欧美日韩中文在线观看 | 色片在线观看 | 欧美中文一区 | 97av视频 | 国产专区在线 | 国产色网站 | 日韩精品在线播放 | 做a视频在线观看 | aaaaaaa片毛片免费观看 | 亚洲高清视频在线 | 波多野结衣中文视频 | 亚洲精品福利视频 | 一区中文 | 精品日本中文字幕 | 国产剧情一区 | 国产成人精品免高潮在线观看 | 午夜一区二区三区视频 | av中文字幕在线播放 | 欧洲尺码日本国产精品 | 久久综合狠狠综合久久综合88 | 91精品免费 | 中文字幕91 | 欧美日韩久久精品 | 国产精品久久久久久久久久久新郎 | 玖玖色在线视频 | 国产在线www | 夜夜av| 免费成人国产 | 蜜桃视频在线观看免费视频网站www | www.日本在线观看 | 成人在线视频免费观看 |