標(biāo)題: 求控制舵機(jī)的程序 [打印本頁]

作者: admin    時(shí)間: 2015-10-26 07:11
舵機(jī)控制程序http://www.torrancerestoration.com/bbs/dpj-37116-1.html
作者: wangxiangyang    時(shí)間: 2017-10-13 20:10
#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint  unsigned int
//sbit KEY1 = P3^2;        //定義調(diào)速的按鍵
sbit DC1 = P0^0;                //定義舵機(jī)的控制端口
sbit DC2 = P0^1;
sbit DC3 = P0^2;
sbit DC4 = P0^3;
sbit DC5 = P0^4;
sbit DC6 = P0^5;
uchar CYCLE;                  //定義周期
uchar PWM1,PWM2,PWM3,PWM4,PWM5,PWM6;                    //定義低電平時(shí)間
/*********************************
函數(shù):Delay_1ms()
參數(shù):t
返回:無
功能:延時(shí)子程序,延時(shí)時(shí)間為 1ms * del。
          使用晶振是11.0592M。                  g
**********************************/
void Delay_1ms(uint t)
{
        uint i,j;
        for(i=0;i<t;i++)
                for(j=0;j<=100;j++);
               
}
/*********************************
函數(shù):Time0Init()
參數(shù):無
返回:無
功能:設(shè)置定時(shí)器0的工作方式2定時(shí)時(shí)長(zhǎng)為100微秒,
      并打開中斷和啟動(dòng)定時(shí)器。
          該例中使用的單片機(jī)晶振為11.0592M。
**********************************/
void Time0Init(void)
{
        TMOD = 0x02;        //設(shè)定定時(shí)器0工作在方式0模式
        TH0  = 0xA4;    //定時(shí)時(shí)長(zhǎng)為100微秒的初值自動(dòng)重載
        TL0  = 0xA4;    //定時(shí)時(shí)長(zhǎng)為100微秒的初值
        ET0 = 1;        //打開定時(shí)器0中斷
        EA = 1;                    //打開總中斷
        TR0 = 1;            //啟動(dòng)定時(shí)器
}
/*********************************
函數(shù):Time0Int()
參數(shù):無
返回:無
說明:定時(shí)器0中斷函數(shù)。用于處理PWM的信號(hào)。
**********************************/
void Time0Int() interrupt 1
{
        static        uchar Count;
        Count++;
        if(Count >= PWM1)
               
        {
                DC1= 1;                
               
        } else  DC1 = 0;
        if(Count >= PWM2)   
        {
                DC2 = 1;                
               
        }  else  DC2 = 0;
        if(Count >= PWM3)   
        {
                DC3 = 1;                
               
        }  else  DC3 = 0;
        if(Count >= PWM4)   
        {
                DC4 = 1;                
               
        } else  DC4 = 0;
                if(Count >= PWM5)   
        {
                DC5 = 1;                
               
        } else  DC6 = 0;
                        if(Count >= PWM6)   
        {
                DC6 = 1;                
               
        } else  DC6 = 0;
        Count++;
       
        if(Count == CYCLE)  
        {
               
                Count=0;
               
        }

       
}


//void PWMInit()
//{
//          PWM1=146;
//        Delay_1ms(5);
//        PWM2=186;
//             Delay_1ms(5);       
//        PWM3=189;
//         Delay_1ms(5);
//        PWM4=170;
//         Delay_1ms(5);
//        PWM5=146;
//         Delay_1ms(5);
//        PWM6=148;
//         Delay_1ms(5);
//}

// void PWMna()
// {
//          PWM1=158; Delay_1ms(5);
//         PWM2=158; Delay_1ms(5);
//         PWM3=182; Delay_1ms(5);
//         PWM4=189; Delay_1ms(5);
//         PWM5=174; Delay_1ms(5);
//         PWM6=185; Delay_1ms(5);
// }

//void PWMfang()
//{
//        PWM1=174; Delay_1ms(5);
//        PWM2=174; Delay_1ms(5);
//        PWM3=154; Delay_1ms(5);
//        PWM4=189; Delay_1ms(5);
//        PWM5=146; Delay_1ms(5);
//        PWM6=169; Delay_1ms(5);
//}

void main()
{
        Time0Init();    //定時(shí)器0初始化
        CYCLE = 200;        //定義PWM周期為20毫秒
/*傳統(tǒng)舵機(jī) 必須連續(xù)發(fā)信號(hào),,數(shù)字舵機(jī)去掉while(1)就ok了*/
        while(1)
        {
//                PWMInit();       
//                Delay_1ms(1000);
//                PWMna();
//                Delay_1ms(1000);
//                PWMfang();
//                Delay_1ms(1000);
                //Delay_1ms(500);
                PWM1=176; Delay_1ms(250);
  //        PWM2=174; Delay_1ms(500);
            PWM3=176; Delay_1ms(500);
          // PWM4=189; Delay_1ms(50);
  //        PWM5=179; Delay_1ms(50);
          //PWM6=169; Delay_1ms(5)
                        Delay_1ms(1000);
                        PWM1=190; Delay_1ms(250);
                //        PWM2=186; Delay_1ms(500);
                         PWM3=192; Delay_1ms(500);
                        Delay_1ms(1000);
               
        }
               
       
}


       
       
                                                          



       
       
作者: wangxiangyang    時(shí)間: 2017-10-13 20:11
admin 發(fā)表于 2015-10-26 07:11
舵機(jī)控制程序http://www.torrancerestoration.com/bbs/dpj-37116-1.html

#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint  unsigned int
//sbit KEY1 = P3^2;        //定義調(diào)速的按鍵
sbit DC1 = P0^0;                //定義舵機(jī)的控制端口
sbit DC2 = P0^1;
sbit DC3 = P0^2;
sbit DC4 = P0^3;
sbit DC5 = P0^4;
sbit DC6 = P0^5;
uchar CYCLE;                  //定義周期
uchar PWM1,PWM2,PWM3,PWM4,PWM5,PWM6;                    //定義低電平時(shí)間
/*********************************
函數(shù):Delay_1ms()
參數(shù):t
返回:無
功能:延時(shí)子程序,延時(shí)時(shí)間為 1ms * del。
          使用晶振是11.0592M。                  g
**********************************/
void Delay_1ms(uint t)
{
        uint i,j;
        for(i=0;i<t;i++)
                for(j=0;j<=100;j++);
               
}
/*********************************
函數(shù):Time0Init()
參數(shù):無
返回:無
功能:設(shè)置定時(shí)器0的工作方式2定時(shí)時(shí)長(zhǎng)為100微秒,
      并打開中斷和啟動(dòng)定時(shí)器。
          該例中使用的單片機(jī)晶振為11.0592M。
**********************************/
void Time0Init(void)
{
        TMOD = 0x02;        //設(shè)定定時(shí)器0工作在方式0模式
        TH0  = 0xA4;    //定時(shí)時(shí)長(zhǎng)為100微秒的初值自動(dòng)重載
        TL0  = 0xA4;    //定時(shí)時(shí)長(zhǎng)為100微秒的初值
        ET0 = 1;        //打開定時(shí)器0中斷
        EA = 1;                    //打開總中斷
        TR0 = 1;            //啟動(dòng)定時(shí)器
}
/*********************************
函數(shù):Time0Int()
參數(shù):無
返回:無
說明:定時(shí)器0中斷函數(shù)。用于處理PWM的信號(hào)。
**********************************/
void Time0Int() interrupt 1
{
        static        uchar Count;
        Count++;
        if(Count >= PWM1)
               
        {
                DC1= 1;                
               
        } else  DC1 = 0;
        if(Count >= PWM2)   
        {
                DC2 = 1;                
               
        }  else  DC2 = 0;
        if(Count >= PWM3)   
        {
                DC3 = 1;                
               
        }  else  DC3 = 0;
        if(Count >= PWM4)   
        {
                DC4 = 1;                
               
        } else  DC4 = 0;
                if(Count >= PWM5)   
        {
                DC5 = 1;                
               
        } else  DC6 = 0;
                        if(Count >= PWM6)   
        {
                DC6 = 1;                
               
        } else  DC6 = 0;
        Count++;
       
        if(Count == CYCLE)  
        {
               
                Count=0;
               
        }

       
}


//void PWMInit()
//{
//          PWM1=146;
//        Delay_1ms(5);
//        PWM2=186;
//             Delay_1ms(5);       
//        PWM3=189;
//         Delay_1ms(5);
//        PWM4=170;
//         Delay_1ms(5);
//        PWM5=146;
//         Delay_1ms(5);
//        PWM6=148;
//         Delay_1ms(5);
//}

// void PWMna()
// {
//          PWM1=158; Delay_1ms(5);
//         PWM2=158; Delay_1ms(5);
//         PWM3=182; Delay_1ms(5);
//         PWM4=189; Delay_1ms(5);
//         PWM5=174; Delay_1ms(5);
//         PWM6=185; Delay_1ms(5);
// }

//void PWMfang()
//{
//        PWM1=174; Delay_1ms(5);
//        PWM2=174; Delay_1ms(5);
//        PWM3=154; Delay_1ms(5);
//        PWM4=189; Delay_1ms(5);
//        PWM5=146; Delay_1ms(5);
//        PWM6=169; Delay_1ms(5);
//}

void main()
{
        Time0Init();    //定時(shí)器0初始化
        CYCLE = 200;        //定義PWM周期為20毫秒
/*傳統(tǒng)舵機(jī) 必須連續(xù)發(fā)信號(hào),,數(shù)字舵機(jī)去掉while(1)就ok了*/
        while(1)
        {
//                PWMInit();       
//                Delay_1ms(1000);
//                PWMna();
//                Delay_1ms(1000);
//                PWMfang();
//                Delay_1ms(1000);
                //Delay_1ms(500);
                PWM1=176; Delay_1ms(250);
  //        PWM2=174; Delay_1ms(500);
            PWM3=176; Delay_1ms(500);
          // PWM4=189; Delay_1ms(50);
  //        PWM5=179; Delay_1ms(50);
          //PWM6=169; Delay_1ms(5)
                        Delay_1ms(1000);
                        PWM1=190; Delay_1ms(250);
                //        PWM2=186; Delay_1ms(500);
                         PWM3=192; Delay_1ms(500);
                        Delay_1ms(1000);
               
        }
               
       
}


       
       
                                                          



       
       




歡迎光臨 (http://www.torrancerestoration.com/bbs/) Powered by Discuz! X3.1