打印

请教用51单片机的一个定时器驱动是个电机

[复制链接]
1662|3
手机看帖
扫描二维码
随时随地手机跟帖
跳转到指定楼层
楼主
靠双手奋斗|  楼主 | 2011-8-18 11:19 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
请教高手能不能把用一个定时器驱动四个电机的的程序,就是可以写成一个函数
例如:void  motor(unsigned char  num,unsigned char speed,unsigned char direction)
请高手指教,谢谢了。
沙发
jrcsh| | 2011-8-18 11:22 | 只看该作者
看你写程序的水平而已~~~没什么能不能的

使用特权

评论回复
板凳
靠双手奋斗|  楼主 | 2011-8-18 18:27 | 只看该作者
#ifndef __main_H__
#define __main_H__
sbit P2_0=P2^0 ;
sbit P2_1=P2^1 ;
sbit P2_2=P2^2 ;
sbit P2_3=P2^3 ;

#define uInt unsigned int
#define uchar unsigned char

uchar    PWM_table[11][2]={0,100,10,90,20,80,30,70,40,60,50,50,60,40,70,30,80,20,90,10,100,0};


#define motor_Control0     0
#define motor_Control1     1

uchar   PWM_motor_Control0_0;  //低电平                   电机1
uchar   PWM_motor_Control0_1;  //高电平

uchar   PWM_motor_Control1_0;  //
uchar   PWM_motor_Control1_1; //                       电机2

uchar   PWM_motor_time_Control0=0;
uchar   PWM_motor_time_Control1=0;

uchar   time_count0=0;
uchar   time_count1=0;

void InitTimer0(void);
void motor_Control(uchar num,uchar direction,uchar speed);
void motor_Init(void);
#endif

uchar    PWM_table[11][2]={0,100,10,90,20,80,30,70,40,60,50,50,60,40,70,30,80,20,90,10,100,0};


void main(void)      
{
    InitTimer0();
        motor_Init();
         while(1)
         {
            motor_Control(0,1,9);                  
        motor_Control(1,1,1);                  
            TR0 = 1;
                delay();
                delay();
                delay();
                TR0 = 0;
            motor_Control(0,1,1);          //2号电机            
        motor_Control(1,1,9);      //1号电机
            TR0 = 1;
                delay();
                TR0 = 0;
                                   
        }
}
/**************************************************************************
【函 数 名】:void motor_Init()
【功    能】:电机控制停函数  
【输入参数】:无
【输出参数】:无
【返 回 值】:无
【时    间】:2011年8月8号
****************************************************************************/
void motor_Init(void)
{
    P2_0=0;
        P2_1=0;
        P2_2=0;
        P2_3=0;
        TR0=0;
}
/**************************************************************************
【函 数 名】:void motor_Control(uchar num,uchar direction,uchar speed)
【功    能】:电机控制函数  
【输入参数】:uchar num,uchar direction,uchar speed
【输出参数】:无
【返 回 值】:无
【时    间】:2011年8月7号
****************************************************************************/
void motor_Control(uchar num,uchar direction,uchar speed)
{
         switch(num)
         {
            case  motor_Control0:
                              P2_0=direction;
                                           PWM_motor_Control0_0=PWM_table[speed][0];
                                          PWM_motor_Control0_1=PWM_table[speed][1];
                                          break;
                case  motor_Control1:
                              P2_2=direction;
                                           PWM_motor_Control1_0=PWM_table[speed][0];
                                          PWM_motor_Control1_1=PWM_table[speed][1];
                                          break;
         }
}

/**************************************************************************
【函 数 名】:void InitTimer0(void)
【功    能】:定时器0的方式1函数  
【输入参数】:200微妙
【输出参数】:无
【返 回 值】:无
【时    间】:2011年8月7号
****************************************************************************/
void InitTimer0(void)
{
   TMOD = 0x01;
    TH0 = 0x0FF;
    TL0 = 0x0A4;
    EA = 1;
    ET0 = 1;
}
/**************************************************************************
【函 数 名】:void Timer0Interrupt(void) interrupt 1
【功    能】:付初值函数  
【输入参数】:200微妙
【输出参数】:
【返 回 值】:无
【时    间】:2011年8月7号
****************************************************************************/
void Timer0Interrupt(void) interrupt 1
{
    TH0 = 0x0FF;
    TL0 = 0x0A4;   
        /******************电机0************************************************/
        if(PWM_motor_time_Control0)
          {
              if( time_count0=PWM_motor_Control0_1)
                        {
                           P2_1=1;
                           PWM_motor_time_Control0=0;
                           //time_count0=-1;
                        }
          }
        else
           {
                  if( time_count0=PWM_motor_Control0_0)
                        {
                           P2_1=0;
                           PWM_motor_time_Control0=1;
                           //time_count0=-1;
                        }
           }
/******************电机1************************************************/
        if(PWM_motor_time_Control1)
          {
              if( time_count1=PWM_motor_Control1_1)
                        {
                           P2_3=1;
                           PWM_motor_time_Control1=0;
                           //time_count1=-1;
                        }
          }
        else
           {
                  if( time_count1=PWM_motor_Control1_0)
                        {
                           P2_3=0;
                           PWM_motor_time_Control1=1;
                          // time_count1=-1;
                        }
           }


    time_count0++;
        time_count1++;
}


本人的写程序的水平确实不是很高,希望高人指点,谢谢!!!!看看那里有毛病,就是pwm调的不是很明显!!

使用特权

评论回复
地板
靠双手奋斗|  楼主 | 2011-8-18 18:27 | 只看该作者
本人的写程序的水平确实不是很高,希望高人指点,谢谢!!!!看看那里有毛病,就是pwm调的不是很明显!!

使用特权

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

本版积分规则

0

主题

151

帖子

1

粉丝