搜索
bottom↓
回复: 6

步进电机加速程序进不了中断

[复制链接]

出0入0汤圆

发表于 2010-4-19 16:49:35 | 显示全部楼层 |阅读模式
俺用的是stm32f10xxE
编译通过,但是电机转不动,中断也进不去,i和pulse的值一直不变。

#include "stm32f10x.h"
#include"main.h"
int i;
int pulse=0;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
GPIO_InitTypeDef GPIO_InitStructure;
ErrorStatus HSEStartUpStatus;
TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
void NVIC_Configuration(void);
void RCC_Configuration(void);
void TIM4_Configuration(void);
void GPIO_Configuration(void);
#define VECT_TAB_RAM
int main(void)
{
   i=0;
  #ifdef DEBUG
    debug();/*[初始化外围设备指针]*/
  #endif
  RCC_Configuration(); //初始化时钟与复位  
  NVIC_Configuration();//初始化中断嵌套
  TIM4_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)   
  {
     if(i<4500)
      {
       pulse=4999-i;
       TIM_SetAutoreload(TIM4,pulse);
      }
   }
}
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 | GPIO_Pin_6;   //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 = TIM4_IRQn;  //设置TIM4通道输入中断
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = 0; /*配置优先级组*/
  NVIC_InitStructure.NVIC_IRQChannelSubPriority = 0;  
  NVIC_InitStructure.NVIC_IRQChannelCmd = ENABLE;      /*允许TIM4全局中断*/
  NVIC_Init(&NVIC_InitStructure);
}
void TIM4_Configuration(void)
{
  TIM_SetCounter( TIM4, 0x0000);
  TIM_ClearFlag(TIM4, TIM_FLAG_Update);        /*清除更新标志位*/
  TIM_ClearITPendingBit(TIM4, TIM_FLAG_Update); //清除TIM4等待中断更新中断标志位
  TIM_ARRPreloadConfig(TIM4, ENABLE);        /*预装载寄存器的内容被立即传送到影子寄存器 */
  TIM_ITConfig(TIM4, TIM_IT_Update, ENABLE); //使能TIM4的更新   
  
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  TIM_TimeBaseStructure.TIM_Period = 4999;        //设定的最大计数值5000,最大计数值是0xffff
  TIM_TimeBaseStructure.TIM_Prescaler = 71;     //分频72
  TIM_TimeBaseStructure.TIM_ClockDivision = 0;     // 时钟分割
  TIM_TimeBaseStructure.TIM_CounterMode = TIM_CounterMode_Up;  //计数方向向上计数
  TIM_TimeBaseInit(TIM4, &TIM_TimeBaseStructure);
  TIM_Cmd(TIM4, ENABLE);       //TIM4 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_TIM4, ENABLE); //在开启时钟里一定要打开TIM4的时钟
}   
void Delay(u32 nCount)
{
  do
  {
  }
  while(nCount--);
}

main.h内容
#define StepEnPin GPIO_Pin_4
extern int i;
extern int pulse;
extern TIM_TimeBaseInitTypeDef TIM_TimeBaseStructure;
void Delay(u32 nCount);

中断程序
void TIM4_IRQHandler(void)
{
  if (TIM_GetITStatus(TIM4, TIM_IT_Update) == SET)
  {
    i=i+1;
    TIM_ClearFlag(TIM4, TIM_FLAG_Update);
    GPIO_WriteBit(GPIOA,GPIO_Pin_3,(BitAction)(1-GPIO_ReadOutputDataBit(GPIOA,GPIO_Pin_3)));
     if(i>=4500)
     {
     i=4500;
     }
  }
}

点击此处下载 ourdev_547449.rar(文件大小:374K) (原文件名:speedup.rar)

阿莫论坛20周年了!感谢大家的支持与爱护!!

知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)

出0入0汤圆

 楼主| 发表于 2010-4-19 17:28:54 | 显示全部楼层
come on

出0入0汤圆

 楼主| 发表于 2010-4-20 15:30:25 | 显示全部楼层
why?is my question naive?

出0入0汤圆

发表于 2010-4-20 16:27:50 | 显示全部楼层
程序有点乱
看看TIM例程

出0入0汤圆

发表于 2011-3-17 18:53:04 | 显示全部楼层
回复【3楼】qwerttt
-----------------------------------------------------------------------

出0入0汤圆

发表于 2011-3-17 18:54:13 | 显示全部楼层
我在四个地方看到了,佩服你的精神

出0入8汤圆

发表于 2011-7-15 21:42:00 | 显示全部楼层
mark,留着研究
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-7-23 23:28

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表