智能车制作

 找回密码
 注册

扫一扫,访问微社区

查看: 1031|回复: 1
打印 上一主题 下一主题

求一个52259 的电机正反转简单程序

[复制链接]

1

主题

4

帖子

0

精华

注册会员

Rank: 2

积分
133
威望
76
贡献
33
兑换币
36
注册时间
2013-11-29
在线时间
13 小时
毕业学校
合院
跳转到指定楼层
1#
发表于 2013-12-30 14:06:33 | 只看该作者 回帖奖励 |正序浏览 |阅读模式
如题 求大神带下

0

主题

2

帖子

0

精华

注册会员

Rank: 2

积分
153
威望
83
贡献
30
兑换币
42
注册时间
2013-12-24
在线时间
20 小时
毕业学校
东南大学电子工程
2#
发表于 2014-2-3 19:02:40 | 只看该作者
#include <hidef.h>      /* common defines and macros */
#include "derivative.h"      /* derivative-specific definitions */

unsigned int  CurrentVelocity=0;
unsigned int  WholeVelocity=0;
unsigned int  vTmpPIT=0;
unsigned int  flag=0;


/***************************************************
** FunctionName: PLL_Init
** FunctionDiscription: Clock Initialization
** FunctionStatement:BusClock is 48MHz
****************************************************/
void PLL_Init(void)   //PLLCLK=2*OSCCLK *(SYNR+1)/(REFDV+1)
{                     
  
   CLKSEL=0x00;  
   PLLCTL_PLLON=1;
   SYNR=0XC0 | 0X05;
   REFDV=0X80 | 0X01;
   POSTDIV=0X00;
   _asm(nop);
   _asm(nop);
   while(0==CRGFLG_LOCK);
   CLKSEL_PLLSEL=1;
}


/***********************************************************
** FunctionNameIT_Init
** FunctionDiscription:PeriodicInterruptTimer Initialization
** FunctionStatement:Interrupt Time is 20ms
************************************************************/
void PIT_Init(void)//定时中断初始化函数 0.01s=10ms定时中断设置
{
    PITCFLMT_PITE=0;
    PITCE_PCE0=1;   
    PITMTLD0=240-1;
    PITLD0=2000-1;
    PITINTE_PINTE0=1;
    PITCFLMT_PITE=1;
}


/**********************************************************
** FunctionName:PWM_Init
** FunctionDiscription:PulseWidthModulation Initialization
** FunctionStatement: PWM1 is assigned to MotorIn1
                      PWM3 is assigned to MotorIn2
***********************************************************/
void PWM_Init(void)
{   
    //CH01  Motor In1
    //CH23  Motor In2
    //SB,B for ch2367
    //SA,A for ch0145   
    PWME  = 0x00;      
    PWMCTL = 0x70;                  
    PWMCAE = 0x00;               
    PWMPOL = 0xFF;               
    PWMPRCLK = 0x33;      
    PWMSCLA = 0x03;     
    PWMSCLB = 0x03;         
    PWMCLK  = 0xFF;     

    PWMPER01=12000;            
    PWMDTY01=0;  
    PWMPER23=12000;   
    PWMDTY23=0;
   
    PWMCNT01 = 0;       
    PWMCNT23 = 0;       
   
    PWME = 0x2A;     
}

/**********************************************************
** FunctionName:Main
** FunctionDiscription:Main Function
***********************************************************/
void main(void) {
  /* put your own code here */

  
  PLL_Init();
  PWM_Init();
  PIT_Init();

        EnableInterrupts;
  DDRB=0XFF;

for (;;) {
  
    _FEED_COP(); /* feeds the dog */
  /* loop forever */
  /* please make sure that you never leave main */
}
}

/**********************************************************
** FunctionName:Periodic Interrupt Timer
** FunctionDiscription:
**FunctionStatement:MotorCotrol Block
**                  
**********************************************************/
#pragma CODE_SEG __NEAR_SEG NON_BANKED
void interrupt 66 PIT0(void)
{
  int i;
       for(i=0;i<5;i++)
  {
      
  flag++;
  PITTF_PTF0=1;
  //------Motor is stoping for 1s----//
  if(flag <= 1000)   
  {
    PWME  = 0x00;     
    PWMDTY01=0;
    PWMDTY23=0;
  }
  //------Motor is turning  for 2s----//
  if(flag > 1000 && flag <=2000)
  {
    PWMDTY01=3000;
    PWMDTY23=0;
    PWME  = 0x2A;      
  }
  //------Motor is stoping for 1s----//
  if(flag > 2000 && flag <= 4000)   
  {
    PWME  = 0x00;      
    PWMDTY01=0;
    PWMDTY23=0;
  }
  //------Motor is turning  for 2s----//
  if(flag > 4000 && flag <= 5000)   
  {
    PWME  = 0x2A;      
    PWMDTY01=0;
    PWMDTY23=3000;
  }

  //------Motor is turning  for 2s----//
  if(flag > 5000 && flag <= 6000)
  {
   PWME  = 0x00;     
    PWMDTY01=0;
    PWMDTY23=0;   
  }
  //------Motor is stoping for 1s----//
  if(flag > 6000 && flag <= 7000)   
  {
    PWMDTY01=3000;
    PWMDTY23=0;
    PWME  = 0x2A;     
  }
  //------Motor is turning  for 2s----//
  if(flag > 7000 && flag <= 8000)   
  {
    PWME  = 0x00;      
    PWMDTY01=0;
    PWMDTY23=0;
  }
  if(flag > 8000 && flag <= 9000)   
  {
    PWME  = 0x00;      
    PWMDTY01=3000;
    PWMDTY23=0;
  }
  if(flag > 9000 )   
  {
    PWME  = 0x00;      
    PWMDTY01=0;
    PWMDTY23=0;
  }

  //--------LEDFlag------//
  vTmpPIT++;
  if(vTmpPIT==10)
  {
  PORTB=~PORTB;
  vTmpPIT=0;
  }

}  
}     
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

关于我们|联系我们|小黑屋|智能车制作 ( 黑ICP备2022002344号

GMT+8, 2024-9-21 17:26 , Processed in 0.044778 second(s), 33 queries , Gzip On.

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

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