欧美极品高清xxxxhd,国产日产欧美最新,无码AV国产东京热AV无码,国产精品人与动性XXX,国产传媒亚洲综合一区二区,四库影院永久国产精品,毛片免费免费高清视频,福利所导航夜趣136

 找回密碼
 立即注冊

QQ登錄

只需一步,快速開始

搜索
查看: 3673|回復: 1
打印 上一主題 下一主題
收起左側

用HSI時鐘使步進電機轉動的stm32程序

[復制鏈接]
跳轉到指定樓層
樓主
ID:73735 發表于 2015-2-19 00:45 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
用HSI時鐘使步進電機轉動:
#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();/*[初始化外圍設備指針]*/
  #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(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;  //設置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 = 120;        //設定的最大計數值,計數值到則產生一次中斷
  TIM_TimeBaseStructure.TIM_Prescaler = 30;     //分頻
  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_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)));
  }
}


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

使用道具 舉報

沙發
ID:74706 發表于 2015-3-16 11:28 | 只看該作者
謝謝分享
回復

使用道具 舉報

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

本版積分規則

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

Powered by 單片機教程網

快速回復 返回頂部 返回列表