52单片机与l298n驱动两个直流电机,并且调整一个电机的速度的程序…

如题所述

第1个回答  2012-03-29

图中可以再接个电机,以下程序是我以前做的小项目用到的,是无线发送接收控制小车的里面有控制两个电机,用的是PWM控制电机的速度,可以参考下。肯定是可以用。

#include"reg52.h"

#include"Allhead.h"

typedef unsigned char uchar;

typedef unsigned char uint;

//***********************************数码管0-9编码*******************************************

uchar seg[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};         //0~~9段码

//************************************数码管位选*********************************************

#define V_TH0  0XFF                  

#define V_TL0  0XC0                

#define V_TMOD 0X01

sbit P0_6 = P0^6;

sbit P0_7 = P0^7;

   

sbit P2_7 = P2^7;

sbit P1_0 = P1^0;

//sbit P3_1 = P3^1; 

//sbit P3_2 = P3^2;

//sbit P3_3 = P3^3;

sbit led2=P2^2;

sbit led1=P2^1;

sbit led0=P2^0;

/*//////////////////////系统初始化函数////////////////////////////*/              

void init_sys(void);            

void Delay5Ms(void);

void Display(uchar m);

void Forward(void);//向前走

void Left_Turn(void);

void Right_Turn(void); //向右走?

void Left_Turnlittle(void);

void Right_Turnlittle(void); //向右走?

void Speed_Down(void);

void Speed_Up(void);

void Camare_Left(void);

void Camare_Right(void); //向右走?

void Camare_Up(void);

void Camare_Down(void);

/************************主函数********************/

unsigned char ZKB1,ZKB2,ZKB3,ZKB4,uchar_ctr;

unchar  Getsign=0x00; 

void main()

{

    //led0=0;led1=0;led2=0;

for(uchar_ctr=0;uchar_ctr<RX_DATA_WITDH;uchar_ctr++)

{

RevTempDate[uchar_ctr]=0x00;

}

   //count=0;

   NRF24L01Int();

   //Max232Int();//串口初始化

   init_sys(); 

 //  ZKB1=18;            /*占空比初始值设定15  18   22*/ 

 //  ZKB2=10;            /*占空比初始值设定*/

 //  ZKB3=18;            /*占空比初始值设定15  18   22*/

   P0_7=0;

   while(1)

    {

NRFSetRXMode();   //设置为接收模式

GetDate();        //开始接受数

Getsign=RevTempDate[0];

if (Getsign==0x01)          //传输图像数据回去 

        { 

        Delay5Ms(); 

//Right_Turn();

        }

if (Getsign==0x02)         //小车不动 

       { 

        Delay5Ms(); 

       TR0=0; 

    ET0=0; 

    EA=0;

       } 

      if (Getsign==0x03)         //小车前进

       { 

        Delay5Ms();

Forward();

      } 

       if (Getsign==0x04)          //小车左移 

       { 

        Delay5Ms();  

  Left_Turn();

       } 

     if (Getsign==0x05)          //小车右移 

       { 

        Delay5Ms(); 

Right_Turn();

       } 

   if (Getsign==0x06)            //小车加速 

       { 

        Delay5Ms(); 

Speed_Up();

       }

   if (Getsign==0x07)            //小车减速

       { 

        Delay5Ms(); 

Speed_Down();

       }

if (Getsign==0x08)            //小车左转一点

       { 

        Delay5Ms(); 

Left_Turnlittle();

       }

if (Getsign==0x09)            //小车子右转一点

       { 

        Delay5Ms(); 

Right_Turnlittle();

}

if (Getsign==0x10)            //让摄像头中间

      { 

        Delay5Ms(); 

   ZKB1=20;ZKB2=1;ZKB3=20;ZKB4=20;

       }

   if (Getsign==0x11)            //让摄像头向左转

       { 

        Delay5Ms(); 

Camare_Left();

       }

if (Getsign==0x12)            //让摄像头右转

       { 

        Delay5Ms(); 

            Camare_Right(); //向右走?

       }

if (Getsign==0x14)            //让摄像头向上转

       { 

        Delay5Ms(); 

Camare_Up();

       }

if (Getsign==0x13)            //让摄像头下转

       { 

        Delay5Ms(); 

            Camare_Down();             //向右走?

       }

if (Getsign==0x20)            //初始状态

       { 

        Delay5Ms(); 

            ZKB1=20;ZKB2=1;ZKB3=20;ZKB4=20;          //向右走?

       }

      

for(uchar_ctr=0;uchar_ctr<RX_DATA_WITDH;uchar_ctr++)  //清空接受的数据

{

RevTempDate[uchar_ctr]=0x00;

}       

}

}

/****************************************************** 

*函数功能:对系统进行初始化,包括定时器初始化和变量初始化*/ 

void init_sys(void)            /*系统初始化函数*/ 

  /*定时器初始化*/ 

  TMOD=V_TMOD; 

  TH0=V_TH0; 

  TL0=V_TL0; 

  ZKB1=20;ZKB2=1;ZKB3=20;ZKB4=20;

 // TR0=0; 

 // ET0=0; 

 // EA=0;

  TR0=1; 

  ET0=1; 

  EA=1;

    

}

void Forward(void)//向前走

{

Delay5Ms();

ZKB1=20;           //占空比初始值设定16 20   24

    ZKB2=130;

ZKB3=20;

ZKB4=20;

  TR0=1; 

  ET0=1; 

  EA=1;

}

void Left_Turn(void) //向左走

{

Delay5Ms();

ZKB1=17;           //占空比初始值设定16  20   24

    ZKB2=135;

  TR0=1; 

  ET0=1; 

  EA=1;

}

void Right_Turn(void) //向右走

{

Delay5Ms();

ZKB1=24;           //占空比初始值设定22  27   34

    ZKB2=135;

  TR0=1; 

   ET0=1; 

  EA=1;

void Speed_Down(void)

{

Delay5Ms();

ZKB2-=10;

if (ZKB2>=251) ZKB2=250; 

if (ZKB2<=59) ZKB2=60;

  TR0=1; 

  ET0=1; 

  EA=1;

}

void Speed_Up(void)

{

Delay5Ms();

ZKB2+=10;

if (ZKB2>=251) ZKB2=250; 

if (ZKB2<=59) ZKB2=60;

  TR0=1; 

  ET0=1; 

  EA=1;

}

void Left_Turnlittle(void)

{

Delay5Ms();

ZKB1-=1;

if (ZKB1>=24) ZKB1=23; 

if (ZKB1<=16) ZKB1=17;

}

void Right_Turnlittle(void) //

{

Delay5Ms();

ZKB1+=1;

if (ZKB1>=25) ZKB1=24; 

if (ZKB1<=16) ZKB1=17;

}

void Camare_Left(void)

{

Delay5Ms();

ZKB3+=1;

if (ZKB3>=39) ZKB3=38; 

if (ZKB3<=6) ZKB3=7;

}

void Camare_Right(void) //向右走?

{

Delay5Ms();

ZKB3-=1;

if (ZKB3>=39) ZKB3=38; 

if (ZKB3<=3) ZKB3=4;

}

void Camare_Up(void)

{

ZKB4-=1;

if (ZKB4>=39) ZKB4=38; 

if (ZKB4<=3) ZKB4=4;

}

void Camare_Down(void)

{

Delay5Ms();

ZKB4+=1;

if (ZKB4>=39) ZKB4=38; 

if (ZKB4<=3) ZKB4=4;

}

//延时 

void Delay5Ms(void) 

unsigned int TempCyc = 1000; 

while(TempCyc--); 

}

void Display(uchar m)

{

P0=seg[m];

/*中断函数*/ 

void timer0(void) interrupt 1 using 2 

static uchar click=0;                  /*中断次数计数器变量*/ 

TH0=V_TH0;                                    /*恢复定时器初始值*/ 

TL0=V_TL0; 

++click; 

if (click>=1000) click=0; 

if (click<=ZKB1)      /*当小于占空比值时输出低电平,高于时是高电平,从而实现占空比的调整*/ 

   P0_6=1; 

else 

   P0_6=0; 

if (click<=ZKB2)                        

   P0_7=1; 

else 

   P0_7=0;

  if (click<=ZKB3)                        

   P2_7=1; 

else 

   P2_7=0;

  if (click<=ZKB4) 

       P1_0=1; 

else 

   P1_0=0;                   

}

本回答被网友采纳