智能车制作

标题: 程序那里出错了??高手帮帮忙 [打印本页]

作者: xiaoyao8745    时间: 2009-5-13 11:16
标题: 程序那里出错了??高手帮帮忙
程序那里出错了??高手帮帮忙

#include <hidef.h>      /* common defines and macros */
#include <MC9S12XS128.h>     /* derivative information */
#pragma LINK_INFO DERIVATIVE "mc9s12xs128"

unsigned int counter;
unsigned char high_speed;
static unsigned  int Direction0 = 3750;
unsigned int Direction3;
#pragma CODE_SEG __NEAR_SEG NON_BANKED //段定义
void CRG_Init(void);    //时钟初始化例程
void PORT_Init(void);   //端口初始化例程
void PWM_Init(void);   //PWM初始化例程
void Motor_Init(void);// 电机函数

void PORT_Init(void){
  DDRA=0x00;    //PortA为输入口
  DDRB=0xFF;    //PortB为输出口
  PORTB=0xFF;    //PortB所接的开发板上的LED全灭
  DDRP=0xFF;    //PWM口输出
}
void CRG_Init(void){
  
  PLLCTL=0xE1;    //锁相环允许
  SYNR=0x09;     //频率合成=39
  REFDV=0x03;    //参考分频因子=3,总线频率=40MHz
  while(CRGFLG_LOCK!=1); //等待频率稳定
  CLKSEL=0x80;    //选择锁相环时钟
}
  void PWM_Init(void){   //舵机用PWM初始化
  /*舵机PWM模块初始 */
   PWMCTL_CON01=1;  //通道0和通道1合成一个16位通道
   PWMCLK_PCLK1=1;   //通道01用SB时钟源
   PWMPRCLK_PCKB2=0;  //通道时钟B=80MHz/8
   PWMPRCLK_PCKB1=1;
   PWMPRCLK_PCKB0=1;
   PWMSCLB=1;    //通道7时钟SB=B/(2*PWMSCLB)=40MHz/8/2/1=2.5MHz
    PWMPOL_PPOL1=1;     //设定极性为正脉冲
                     //对齐方式默认为左对齐         
   PWMPER01=50000;   //舵机的频率是50Hz,一个计数周期内计数50000次  
                    //极性= 1 ( PPOLx = 1 ) 占空比= [ PWMDTYx / PWMPERx ] * 100 %
   PWME_PWME1=1;  //舵机用脉冲信号从PWM1输出
   
   /*电机PWM模块初始 */
   PWMCLK_PCLK7=1;   //1通道选SA时钟
   PWMPRCLK_PCKA2=0;
   PWMPRCLK_PCKA1=1;
   PWMPRCLK_PCKA0=1;
   PWMCAE_CAE7=0;  //通道1为左对齐
   PWMPOL_PPOL7=1;   //开始输出高电平
    PWMSCLA=1;    //SA时钟频率80/8/2/1=2.5mHZ
   PWMPER67=500;   
   PWME_PWME7=1;       //电机用脉冲信号从PWM7输出
   
}
  void Motor_Init(int Velocity){ //电机
     if(Velocity>high_speed) Velocity=high_speed;
     PWMPER67 = 500;    // 5kHz ( <10kHz )
     PWMDTY67 = high_speed; // 设置电机速度
     }
     
void Servo_Init(int Direction0) {  //伺服舵机
//Direction0 += Direction1;
Direction0 = Direction0/3 + 3750;
if(Direction0<2500) Direction0=2500; //设置舵机的最大转角
if(Direction0>5000) Direction0=5000;
PWMDTY01 = Direction0;   // 设置舵机角度

}
void TimerOverflow(void) {
/* This function waits for th timer overflow.
Then it changes the LEDs bargraph display */
  while (TCNT != 0x0000);
  while (TCNT == 0x0000);
  counter++;   
  if (counter == 8) PORTB = 0x7f; /* LEDs' lightshow */
  if (counter == 7) PORTB = 0xbf;
  if (counter == 6) PORTB = 0xdf;
  if (counter == 5) PORTB = 0xef;
  if (counter == 4) PORTB = 0xf7;
  if (counter == 3) PORTB = 0xfb;
  if (counter == 2) PORTB = 0xfd;
  if (counter == 1) PORTB = 0xfe;
}
void main(void)
{
  //CRG_Init();
  PORT_Init();   //端口初始化例程
  PWM_Init();   //PWM初始化例程
  TSCR1 = 0x80; /* enable timer TCNT */
  TSCR2 = 0x03; /* TCNT prescaler setup */
  counter = 0;
  high_speed = 500;
  DDRB=0xff; /* PTB as output */
  PORTB=0xff;  /* LEDs off */
  asm{
  nop
  }
  for (;;)
  {  Direction3 = 2500;
    Motor_Init(high_speed);
    if(PWMDTY01 != 0){
      TimerOverflow();
    }
   
    Servo_Init(Direction3);
    if (counter >= 8)
    {
         counter = 0;
         TSCR2 = 0x05;  /* TCNT prescaler switch */
    }
  }
}
作者: yudongx    时间: 2009-5-13 13:20
什么问题呢?
作者: tonyray0101    时间: 2009-5-13 21:14
这是全套程序吗?
作者: tonyray0101    时间: 2009-5-13 21:14
这是全套程序吗?
作者: xiaoyao8745    时间: 2009-5-14 16:01
是,这是测试电机和舵机的,但是我们的舵机只往一个方向转
作者: 星空下的学生    时间: 2009-5-14 18:54
你舵机的初始位置是中间吗??别本来就是在最边上还想让它再转哦
作者: ufk119    时间: 2009-5-16 21:20
估计得先把中心值大概算算,看看自己的频率来调
作者: zhouhong    时间: 2010-5-14 20:08
我的也是一直往一个方向,但是你给一个变化的占空比的方波,就可以左右摆。你的电机跳好了没?我给一个固定的占空比的方波比如50%,怎么电机不转?请教




欢迎光临 智能车制作 (http://dns.znczz.com/) Powered by Discuz! X3.2