找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 2280|回復(fù): 3
打印 上一主題 下一主題
收起左側(cè)

求控制舵機(jī)的程序

[復(fù)制鏈接]
樓主
ID:1 發(fā)表于 2015-10-26 07:11 來(lái)自觸屏版 | 只看該作者
舵機(jī)控制程序http://www.torrancerestoration.com/bbs/dpj-37116-1.html
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:227369 發(fā)表于 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
返回:無(wú)
功能:延時(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ù):無(wú)
返回:無(wú)
功能:設(shè)置定時(shí)器0的工作方式2定時(shí)時(shí)長(zhǎng)為100微秒,
      并打開(kāi)中斷和啟動(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;        //打開(kāi)定時(shí)器0中斷
        EA = 1;                    //打開(kāi)總中斷
        TR0 = 1;            //啟動(dòng)定時(shí)器
}
/*********************************
函數(shù):Time0Int()
參數(shù):無(wú)
返回:無(wú)
說(shuō)明:定時(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);
               
        }
               
       
}


       
       
                                                          



       
       
回復(fù)

使用道具 舉報(bào)

板凳
ID:227369 發(fā)表于 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
返回:無(wú)
功能:延時(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ù):無(wú)
返回:無(wú)
功能:設(shè)置定時(shí)器0的工作方式2定時(shí)時(shí)長(zhǎng)為100微秒,
      并打開(kāi)中斷和啟動(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;        //打開(kāi)定時(shí)器0中斷
        EA = 1;                    //打開(kāi)總中斷
        TR0 = 1;            //啟動(dòng)定時(shí)器
}
/*********************************
函數(shù):Time0Int()
參數(shù):無(wú)
返回:無(wú)
說(shuō)明:定時(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);
               
        }
               
       
}


       
       
                                                          



       
       
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表