找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3712|回復(fù): 1
收起左側(cè)

用HSI時鐘使步進(jìn)電機(jī)轉(zhuǎn)動的stm32程序

[復(fù)制鏈接]
ID:73735 發(fā)表于 2015-2-19 00:45 | 顯示全部樓層 |閱讀模式
用HSI時鐘使步進(jìn)電機(jī)轉(zhuǎn)動:
#include"stm32f10x_lib.h"
GPIO_InitTypeDef GPIO_InitStructure;
ErrorStatus HSIStartUpStatus;
void NVIC_Configuration(void);
void RCC_Configuration(void);
void TIM2_Configuration(void);
void GPIO_Configuration(void);
#define VECT_TAB_RAM
int main(void)
{
  #ifdef DEBUG
    debug();/*[初始化外圍設(shè)備指針]*/
  #endif
  RCC_Configuration(); //初始化時鐘與復(fù)位  
  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));  //正向旋轉(zhuǎn)
  
  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)
  {
  }
}
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;  //設(shè)置TIM2通道輸入中斷
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; /*配置優(yōu)先級組*/
  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);        /*清除更新標(biāo)志位*/
  TIM_ClearITPendingBit(TIM2, TIM_FLAG_Update); //清除TIM2等待中斷更新中斷標(biāo)志位
  TIM_ARRPreloadConfig(TIM2, ENABLE);        /*預(yù)裝載寄存器的內(nèi)容被立即傳送到影子寄存器 */
  TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE); //使能TIM2的更新   
  
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  TIM_TimeBaseStructure.TIM_Period = 120;        //設(shè)定的最大計數(shù)值,計數(shù)值到則產(chǎn)生一次中斷
  TIM_TimeBaseStructure.TIM_Prescaler = 30;     //分頻
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;     // 時鐘分割
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //計數(shù)方向向上計數(shù)
  TIM_TimeBaseInit(TIM2, &TIM_TimeBaseStructure);
  TIM_Cmd(TIM2, ENABLE);       //TIM2 enable counter
}
void RCC_Configuration(void)
{
  
         
  RCC_DeInit();
  RCC_HSICmd(ENABLE);
  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 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)));
  }
}


回復(fù)

使用道具 舉報

ID:74706 發(fā)表于 2015-3-16 11:28 | 顯示全部樓層
謝謝分享
回復(fù)

使用道具 舉報

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

本版積分規(guī)則

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

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

快速回復(fù) 返回頂部 返回列表