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

標題: stm32步進電機控制程序 [打印本頁]

作者: huge    時間: 2015-2-19 00:48
標題: stm32步進電機控制程序
步進電機控制程序:

main.c文件內容:
#include"stm32f10x_lib.h"
#include"main.h"
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
GPIO_InitTypeDef GPIO_InitStructure;
ErrorStatus HSEStartUpStatus;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
int pulse;
int StepCount;
int pulse1;
int pulse2;
int t1;
int t2;
int r1;
int r2;
void RCC_Configuration(void);
void NVIC_Configuration(void);
void GPIO_Configuration(void);
void TIM2_Configuration(void);
void f(int Vt,int a,int d,int S);
#define VECT_TAB_RAM
int main(void)
{  
  #ifdef DEBUG
    debug();/*[初始化外圍設備指針]*/
  #endif
  RCC_Configuration(); //初始化時鐘與復位  
  NVIC_Configuration();//初始化中斷嵌套
  TIM2_Configuration();//初始化定時器
  GPIO_Configuration();
  
  GPIO_WriteBit(GPIOD, GPIO_Pin_7, (BitAction)(0));
  GPIO_WriteBit(GPIOD, GPIO_Pin_6, (BitAction)(0));  //DCY1 DCY2為00,即Normal %0 DECAY
  
  GPIO_WriteBit(GPIOE, GPIO_Pin_7, (BitAction)(1));
  GPIO_WriteBit(GPIOB, GPIO_Pin_1, (BitAction)(0));  //M1M2為10,即1-2-phase
  //GPIO_WriteBit(GPIOA, GPIO_Pin_4, (BitAction)(1));  //正向旋轉
  //GPIO_WriteBit(GPIOA,GPIO_Pin_4,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_4)));正、反向旋轉控制
  
  GPIO_WriteBit(GPIOB, GPIO_Pin_0, (BitAction)(0));
  GPIO_WriteBit(GPIOC, GPIO_Pin_5, (BitAction)(1));  //TQ1 TQ2為01,即Current Ratio為50%
  
  GPIO_WriteBit(GPIOA, GPIO_Pin_7, (BitAction)(1));  //StepReset位
  GPIO_WriteBit(GPIOC, GPIO_Pin_4, (BitAction)(1));  //StepEn 使能位
  
while(1)
  {
  r1=0;
  r2=10;
  StepCount=0;
  GPIO_WriteBit(GPIOA,GPIO_Pin_4,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_4)));
  TIM2_Configuration();
  do
  {
  }while(r2);
  
  TIM_Cmd(TIM2, DISABLE);
  Delay(7000000);
  }
}
void GPIO_Configuration(void)
{
  GPIO_InitTypeDef GPIO_InitStructure;
  GPIO_InitStructure.GPIO_Pin=GPIO_Pin_3 | GPIO_Pin_4 | GPIO_Pin_7;   //PA的3.4.7接CLK,CW/CCW,StepReset
  GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  GPIO_Init(GPIOA,&GPIO_InitStructure);
  GPIO_InitStructure.GPIO_Pin= GPIO_Pin_5 | GPIO_Pin_6;   //PA的6.7接Protect和Mo
  GPIO_InitStructure.GPIO_Mode=GPIO_Mode_IPU;
  GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  GPIO_Init(GPIOA,&GPIO_InitStructure);
  GPIO_InitStructure.GPIO_Pin=GPIO_Pin_4 | GPIO_Pin_5;   //PC的4.5接StepEn和TQ2
  GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  GPIO_Init(GPIOC,&GPIO_InitStructure);
  GPIO_InitStructure.GPIO_Pin=GPIO_Pin_0 | GPIO_Pin_1;   //PB的0.1接TQ1和M2
  GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  GPIO_Init(GPIOB,&GPIO_InitStructure);
  GPIO_InitStructure.GPIO_Pin=GPIO_Pin_7;   //PE7接M1
  GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  GPIO_Init(GPIOE,&GPIO_InitStructure);
  GPIO_InitStructure.GPIO_Pin=GPIO_Pin_6 | GPIO_Pin_7;   //PD的67接DCY2和DCY1
  GPIO_InitStructure.GPIO_Mode=GPIO_Mode_Out_PP;
  GPIO_InitStructure.GPIO_Speed=GPIO_Speed_50MHz;
  GPIO_Init(GPIOD,&GPIO_InitStructure);
}
void NVIC_Configuration(void)
{
  NVIC_InitTypeDef NVIC_InitStructure;
#ifdef  VECT_TAB_RAM   
  NVIC_SetVectorTable(NVIC_VectTab_RAM, 0x0);
#else
  NVIC_SetVectorTable(NVIC_VectTab_FLASH, 0x0);   
#endif                                                  
  NVIC_InitStructure.NVIC_IRQChannel = TIM2_IRQChannel;  //設置TIM2通道輸入中斷
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; /*配置優先級組*/
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;      /*允許TIM2全局中斷*/
  NVIC_Init(&NVIC_InitStructure);
}
void TIM2_Configuration(void)
{
  TIM_SetCounter( TIM2, 0x0000);
  TIM_ClearFlag(TIM2, TIM_FLAG_Update);        /*清除更新標志位*/
  TIM_ClearITPendingBit(TIM2, TIM_FLAG_Update); //清除TIM2等待中斷更新中斷標志位
  TIM_ARRPreloadConfig(TIM2, ENABLE);        /*預裝載寄存器的內容被立即傳送到影子寄存器 */
  TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); //使能TIM2的更新   
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  TIM_TimeBaseStructure.TIM_Period = 2000;      //設定的最大計數值2000,最大計數值是0xffff
  TIM_TimeBaseStructure.TIM_Prescaler = 72;     //分頻72
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;     // 時鐘分割
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //計數方向向上計數
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
  TIM_Cmd(TIM2, ENABLE);       //TIM2 enable counter
}
void RCC_Configuration(void)
{      
  RCC_DeInit();
  RCC_HSEConfig(RCC_HSE_ON);         
  HSEStartUpStatus = RCC_WaitForHSEStartUp();
  if(HSEStartUpStatus == SUCCESS)
  {
    FLASH_PrefetchBufferCmd(FLASH_PrefetchBuffer_Enable);
    FLASH_SetLatency(FLASH_Latency_2);
    RCC_HCLKConfig(RCC_SYSCLK_Div1);
    RCC_PCLK2Config(RCC_HCLK_Div1);
    RCC_PCLK1Config(RCC_HCLK_Div2);
    RCC_PLLConfig(RCC_PLLSource_HSE_Div1, RCC_PLLMul_9);
    RCC_PLLCmd(ENABLE);
    while(RCC_GetFlagStatus(RCC_FLAG_PLLRDY) == RESET)
    {
    }
    RCC_SYSCLKConfig(RCC_SYSCLKSource_PLLCLK);
    while(RCC_GetSYSCLKSource() != 0x08)
    {
    }
  }  
  RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB | RCC_APB2Periph_GPIOC
                                | RCC_APB2Periph_GPIOD | RCC_APB2Periph_GPIOE,ENABLE);
  RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE); //打開TIM2的時鐘
}   
void Delay(u32 nCount)
{
  do{
    }
  while(nCount--);
}
void f(int Vt,int a,int d,int S)
{
  int pulse1;
  int pulse2;
  t1=Vt/a;
  
if(StepCount<t1)                 //加速階段,分t1級加速
  {
  r1++;
  pulse1=(150000*t1)/(r1*Vt);
  TIM_SetAutoreload(TIM2,pulse1);
  }
                          
if(t1<=StepCount<=4*S)          //勻速階段運行要求的步數或者距離
{
  pulse=150000/Vt;
  TIM_SetAutoreload(TIM2,pulse);
}
if(StepCount>4*S)                            //減速階段,走完S步后開始減速,分t2-1級減速
  {
    r2--;
   
  if(t2>=1)
  {
  pulse2=(150000*t2)/(r2*Vt);
  TIM_SetAutoreload(TIM2,pulse2);
  }

  }
}
main.h文件內容:
#define StepEnPin GPIO_Pin_4
extern int S;
extern int t1;
extern int r2;
extern int pulse1;
extern int pulse2;
extern int StepCount;
extern TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
void Delay(u32 nCount);
void f(int Vt,int a,int d,int S);

TIM2中斷函數程序:
void TIM2_IRQHandler(void)
{   
  if (TIM_GetITStatus(TIM2, TIM_IT_Update) == SET)
  {
    TIM_ClearFlag(TIM2, TIM_FLAG_Update);
    GPIO_WriteBit(GPIOA,GPIO_Pin_3,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOA,
                                                                                                                                GPIO_Pin_3)));
    StepCount=StepCount+1;
    f(300,10,10,4000);
  }
}



作者: XjinX    時間: 2018-12-7 13:22
請問你的stm32f10x_lib.h是怎么定義的呢?

作者: 菜雞一只314159    時間: 2019-4-8 23:55
stm32f10x_lib.h記得好像有一個較老版本的庫




歡迎光臨 (http://www.zg4o1577.cn/bbs/) Powered by Discuz! X3.1
主站蜘蛛池模板: 成人黄页在线观看 | 欧美成人免费电影 | 亚洲精品永久免费 | 密桃av| 国产精品自拍视频 | 一级毛片免费 | 精品免费国产视频 | 毛片久久久 | 久久久爽爽爽美女图片 | 国产精品99999999 | 毛片一级片 | 欧美一区二区三区在线免费观看 | h视频在线观看免费 | 精品亚洲一区二区 | 国产精品久久久久久久久久久久久 | 午夜免费av | 国产在线观看 | 婷婷不卡| www.亚洲精品 | 在线观看特色大片免费网站 | 成人夜晚看av | 黄网站涩免费蜜桃网站 | 小川阿佐美pgd-606在线 | 天天插天天舔 | 欧美精品一二区 | 国产精品国产三级国产aⅴ中文 | 久久久久9999 | 在线免费观看黄网 | 成人区精品一区二区婷婷 | 久久香蕉精品视频 | 日韩一区二区三区在线观看 | 天天操操| 亚洲精品免费看 | 最新中文字幕一区 | 青青草一区二区三区 | 欧美1区2区| 韩国精品一区二区三区 | 亚洲h视频 | 高清亚洲 | 欧美精品一区二区三区四区 在线 | 日韩精品免费视频 |