打印

怎么用MSP430F149和两组L298N控制四个电机呢?

[复制链接]
2527|2
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
xidianxuqi|  楼主 | 2014-5-7 20:09 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
怎么用MSP430F149和两组L298N控制四个电机呢?控制前进、后退、左转、右转,求程序,最好有注释。我现在能用一个L298N驱动两个电机。
#include "msp430x14x.h"
#define   P1_0    0x01      //  左电机控制  IN1    00000001
#define   P1_1    0x02      //  左电机控制  IN2    00000010
#define   P1_2    0x04      //  左电机控制  PWM1   00000100

#define   P1_3    0x08      //  右电机控制  PWM2   00001000
#define   P1_4    0x10      //  右电机控制  IN3    00001010
#define   P1_5    0x20      //  右电机控制  IN4    00010100

#define   PWM_TIMES         200            //PWM周期为20ms


/*******************************************************
PWM设置函数  motor:设置的电机编号  0左电机,1右电机;  
percent: PWM占空比; times:PWM周期 0.1ms为单位;
**********************************************************/
void Pwm_Set(unsigned char motor, unsigned int  percent,unsigned int times)
{
  unsigned int per;
  unsigned long count=0;
  if(percent==0)
    TACTL=0x00;
  if(percent<100)
  percent=100-percent;
  else
   percent=percent-100;
  count=80*times;                       //计时脉冲个数=80*times;
  per=count*percent/100;                //正脉冲计时个数      
  P1DIR |= 0x0C;                        // IO1.2 and IO1.3 output
  P1SEL |= 0x0C;                        // IO1.2 and IO1.3 TA1/2 otions
  CCR0 = count;                         // PWM Period
  if(motor==0)      
  {
    CCTL1 = OUTMOD_7;                   // CCR1 reset/set
    CCR1 = per;                         // CCR1 PWM duty cycle
  }
  else if(motor==1)
  {
     CCTL2 = OUTMOD_7;                  // CCR2 reset/set
     CCR2 = per;                        // CCR2 PWM duty cycle

  }
  TACTL = TASSEL_2 + MC_1;              // SMCLK, up mode
  
// _BIS_SR(LPM0_bits);                 // Enter LPM0
}




/****************************************************************  
   功能:电机执行动作
   参数:
         motor_num: 电机的号码,1 右电机 ; 0 左电机
         event:      电机的动作, 0停止,1前进,2后退,3刹车  
         speed:       电机的速度,既PWM占空比
*****************************************************************/
void  Motor(unsigned char motor_num,unsigned char event,unsigned char speed)
{
  if(motor_num==0)   
  {
    switch(event)
    {
    case  0:  P1OUT&=~(P1_0+P1_1);       //停止
      break;
    case  1:  P1OUT|=P1_0;P1OUT&=~P1_1; //前进
      break;
    case  2:  P1OUT|=P1_1;P1OUT&=~P1_0; //后退
      break;
    case  3:  P1OUT|=(P1_0+P1_1);        //刹车
      break;
    default:
      break;
    }
    Pwm_Set(0,speed,PWM_TIMES);
   
  }
   else if(motor_num==1)
  {
    switch(event)
    {
    case  0:  P1OUT&=~(P1_4+P1_5);       //停止
      break;
    case  1:  P1OUT|=P1_5;P1OUT&=~P1_4; //前进
      break;
    case  2:  P1OUT|=P1_4;P1OUT&=~P1_5; //后退
      break;
    case  3:  P1OUT|=(P1_4+P1_5);        //刹车
      break;
    default:
      break;
    }
    Pwm_Set(1,speed,PWM_TIMES);
   
  }
}

void delay(void)
{
  unsigned int count=0;
  while(count<50000)
    count++;
   
}
void  delay_long(void)
{
  int i=0;
  while(i++<10)
    delay();
}

int main( void )
{
  WDTCTL=WDTPW  + WDTHOLD;
  
  P2DIR=0xff;
  P2SEL=0x00;
  P2OUT=0xff;
  
  P1DIR=0xff;
  P1SEL=0x00;
  P1OUT=0x00;
  Motor(0,1,50);   
  Motor(1,1,50);    //小车前进
  delay_long();

  Motor(0,0,0);
  Motor(1,1,50);    //小车左拐弯
  delay_long();
  
  Motor(0,1,50);
  Motor(1,0,0);    //小车右拐弯
  delay_long();
  
  Motor(0,1,100);
  Motor(1,1,100);  //小车加速前进
  delay_long();
  
  Motor(0,3,50);
  Motor(1,3,50);   //小车刹车
  delay_long();
  
  Motor(0,2,10);
  Motor(1,2,10);   //小车后退
  delay_long();
  
  
  Motor(0,0,0);
  Motor(1,0,0);    //小车停止
  delay_long();
  
  return 0;
}

相关帖子

沙发
dirtwillfly| | 2014-5-9 09:13 | 只看该作者
能用一个L298N驱动两个电机,不也能用两个L298N驱动四个电机?

使用特权

评论回复
板凳
xidianxuqi|  楼主 | 2014-5-20 23:37 | 只看该作者
dirtwillfly 发表于 2014-5-9 09:13
能用一个L298N驱动两个电机,不也能用两个L298N驱动四个电机?

四个轮子。搞不定了。。。有点迷糊

使用特权

评论回复
发新帖 我要提问
您需要登录后才可以回帖 登录 | 注册

本版积分规则

1

主题

2

帖子

0

粉丝