基本信息
源码名称:四足蜘蛛走路程序(djb.c)
源码大小:7.44KB
文件格式:.c
开发语言:C/C++
更新时间:2021-02-28
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍
#include "reg51.h"
#define uint8 unsigned char    
#define uint16 unsigned short int
#define uint32 unsigned long 
 
sbit SERVO0=P2^0; //控制舵机的PWM输出口
sbit SERVO1=P2^1;
sbit SERVO2=P3^2;
sbit SERVO3=P3^3;
sbit SERVO4=P3^4;
sbit SERVO5=P3^5;
sbit SERVO6=P2^6;
sbit SERVO7=P2^7;
 
uint16 Servo0PwmDuty=1500; //PWM脉冲宽度
uint16 Servo1PwmDuty=1500; 
uint16 Servo2PwmDuty=1500; 
uint16 Servo3PwmDuty=1500; 
uint16 Servo4PwmDuty=1500; 
uint16 Servo5PwmDuty=1500; 
uint16 Servo6PwmDuty=1500; 
uint16 Servo7PwmDuty=1500; 

void DelayMs(uint16 ms)     //延时ms毫秒
{
uint16 i,j;
for(i=0;i<85;i ) //89单片机用85,12系列单片机用800
for(j=0;j<ms;j );
}

void InitTimer0(void)   //时钟0初始化
{
TMOD &= 0xF0; //设置定时器模式
TMOD |= 0x01; //设置定时器模式
TL0 = 0x00; //设置定时初值
TH0 = 0x00; //设置定时初值
TF0 = 0; //清除TF0标志
TR0 = 1; //定时器0开始计时
ET0 = 1; //开定时器0中断
}
void Timer0Value(uint16 pwm)
{
uint16 value;
value=0xffff-pwm;
TR0 = 0;
TL0=value; //16位数据给8位数据赋值默认将16位数据的低八位直接赋给八位数据
    TH0=value>>8; //将16位数据右移8位,也就是将高8位移到低八位,再赋值给8位数据
TR0 = 1;
}

void djb()
{    
Servo1PwmDuty = 2000;     
DelayMs(500);

Servo4PwmDuty = 1000;     
DelayMs(500);
Servo5PwmDuty = 1000;
DelayMs(500);
Servo4PwmDuty = 1500;
DelayMs(500);


        Servo1PwmDuty = 1000; 
Servo5PwmDuty = 2000;
DelayMs(500);



Servo3PwmDuty = 1000;  
DelayMs(500);

Servo6PwmDuty = 1000;    
DelayMs(500);
Servo7PwmDuty = 2000;
DelayMs(500);
Servo6PwmDuty = 1500;
DelayMs(500);

Servo3PwmDuty = 2000;    
Servo7PwmDuty = 1000;
DelayMs(500);

// Servo0PwmDuty = 1200;     // 脉冲宽度在1000微秒,对应-45°
// Servo2PwmDuty = 1200;     // 脉冲宽度在1000微秒,对应-45°
// Servo4PwmDuty = 1500;     // 脉冲宽度在1000微秒,对应-45°
// DelayMs(1000);
// Servo0PwmDuty = 1500;     // 脉冲宽度在1000微秒,对应-45°
// DelayMs(1000);
// Servo2PwmDuty = 1500;     // 脉冲宽度在1000微秒,对应-45°
// DelayMs(1000);
//
// Servo6PwmDuty = 1200;     // 脉冲宽度在1000微秒,对应-45°
// DelayMs(1000);
// Servo7PwmDuty = 2000;
// DelayMs(1000);
//
// Servo0PwmDuty = 1200;     // 脉冲宽度在1000微秒,对应-45°
// Servo2PwmDuty = 1200;     // 脉冲宽度在1000微秒,对应-45°
// Servo6PwmDuty = 1500;     // 脉冲宽度在1000微秒,对应-45°
// DelayMs(1000);
// Servo0PwmDuty = 1500;     // 脉冲宽度在1000微秒,对应-45°
// DelayMs(1000);
// Servo2PwmDuty = 1500;     // 脉冲宽度在1000微秒,对应-45°
// DelayMs(1000);
}


void djb2()
{
Servo4PwmDuty = 1000;     
        DelayMs(1000);
Servo5PwmDuty = 1000;
        DelayMs(1000);
Servo4PwmDuty = 1500;
        DelayMs(1000);
Servo5PwmDuty = 2000;
        DelayMs(1000);

        Servo6PwmDuty = 1000;     
        DelayMs(1000);
Servo7PwmDuty = 2000;
        DelayMs(1000);
Servo6PwmDuty = 1500;
        DelayMs(1000);
Servo7PwmDuty = 1000;
        DelayMs(1000);
}
void djb3()
{
Servo1PwmDuty = 1000;
DelayMs(1000);
Servo0PwmDuty = 1000;     
DelayMs(1000);
Servo1PwmDuty = 2000;
DelayMs(1000);
Servo0PwmDuty = 1500;
DelayMs(1000);

Servo5PwmDuty = 2000;
DelayMs(1000);
Servo4PwmDuty = 1000;     
DelayMs(1000);
Servo5PwmDuty = 1000;
DelayMs(1000);
Servo4PwmDuty = 1500;
DelayMs(1000);

Servo3PwmDuty = 2000;
DelayMs(1000);
Servo2PwmDuty = 1000;     
DelayMs(1000);
Servo3PwmDuty = 1000;
DelayMs(1000);
Servo2PwmDuty = 1500;
DelayMs(1000);

Servo7PwmDuty = 1000;
DelayMs(1000);
        Servo6PwmDuty = 1000;     
DelayMs(1000);
Servo7PwmDuty = 2000;
DelayMs(1000);
Servo6PwmDuty = 1500;
DelayMs(1000);
}
void djb4()
{
        Servo1PwmDuty = 1000; 
        Servo5PwmDuty = 2000; 
        Servo3PwmDuty = 2000; 
        Servo7PwmDuty = 1000; 
DelayMs(3000);


        Servo6PwmDuty = 1000;     
DelayMs(1000);
Servo7PwmDuty = 1900;
DelayMs(1000);
Servo6PwmDuty = 1500;
DelayMs(1000);

        Servo0PwmDuty = 1000;     
DelayMs(1000);
Servo1PwmDuty = 2000;
DelayMs(1000);
Servo0PwmDuty = 1500;
DelayMs(1000);


Servo4PwmDuty = 1000;     
DelayMs(1000);
Servo5PwmDuty = 1100;
DelayMs(1000);
Servo4PwmDuty = 1500;
DelayMs(1000);

Servo2PwmDuty = 1000;     
DelayMs(1000);
Servo3PwmDuty = 1000;
DelayMs(1000);
Servo2PwmDuty = 1500;
DelayMs(1000);
}
void main()
{
InitTimer0(); //定时器0初始化
EA = 1; //开总中断
        Servo0PwmDuty=1500;
        Servo1PwmDuty=1000; 
        Servo2PwmDuty=1500; 
        Servo3PwmDuty=2000; 
        Servo4PwmDuty=1500; 
        Servo5PwmDuty=2000; 
        Servo6PwmDuty=1500; 
        Servo7PwmDuty=1000;
        DelayMs(500);

//        Servo0PwmDuty=1500;
//        Servo1PwmDuty=2000; 
//        Servo2PwmDuty=1500; 
//        Servo3PwmDuty=1000; 
//        Servo4PwmDuty=1500; 
//        Servo5PwmDuty=1000; 
//        Servo6PwmDuty=1500; 
//        Servo7PwmDuty=2000;
//        DelayMs(500);

//        Servo0PwmDuty=1500;
//        Servo1PwmDuty=2000; 
//        Servo2PwmDuty=1500; 
//        Servo3PwmDuty=1000; 
//        Servo4PwmDuty=1500; 
//        Servo5PwmDuty=1100; 
//        Servo6PwmDuty=1500; 
//        Servo7PwmDuty=1900;
//        DelayMs(1000);
while(1) //大循环
{
        djb2();
    }
}

void Timer0_isr(void) interrupt 1 using 1     //时钟0中断处理
{
static uint16 i = 1; //静态变量:每次调用函数时  $$保持上一次所赋的值$$
//跟全局变量类似,不同是它只能用于此函数内部
switch(i)
{
case 1:
SERVO0 = 1; //PWM控制脚高电平
//给定时器0赋值,计数Pwm0Duty个脉冲后产生中断,下次中断会进入下一个case语句
Timer0Value(Servo0PwmDuty);
break;
case 2:
SERVO0 = 0; //PWM控制脚低电平
//高脉冲结束后剩下的时间(20000-Pwm0Duty)全是低电平了,Pwm0Duty (20000-Pwm0Duty) = 20000个脉冲正好为一个周期20毫秒 
Timer0Value(20000-Servo0PwmDuty);
break;

        case 3:
SERVO1 = 1; //PWM控制脚高电平
Timer0Value(Servo1PwmDuty);
break;
        case 4:
SERVO1 = 0; //PWM控制脚低电平
            Timer0Value(20000-Servo1PwmDuty);
break;  

        case 5:
SERVO2 = 1; //PWM控制脚高电平
Timer0Value(Servo2PwmDuty);
break;
        case 6:
SERVO2 = 0; //PWM控制脚低电平
            Timer0Value(20000-Servo2PwmDuty);
break;
        
        case 7:
SERVO3 = 1; //PWM控制脚高电平
Timer0Value(Servo3PwmDuty);
break;
        case 8:
SERVO3 = 0; //PWM控制脚低电平
            Timer0Value(20000-Servo3PwmDuty);
            
break; 
                  
        case 9:
SERVO4 = 1; //PWM控制脚高电平
Timer0Value(Servo4PwmDuty);
break;
        case 10:
SERVO4 = 0; //PWM控制脚低电平
            Timer0Value(20000-Servo4PwmDuty);
break;
        
        case 11:
SERVO5 = 1; //PWM控制脚高电平
Timer0Value(Servo5PwmDuty);
break;
        case 12:
SERVO5 = 0; //PWM控制脚低电平
            Timer0Value(20000-Servo5PwmDuty);
break;
        
        case 13:
SERVO6 = 1; //PWM控制脚高电平
Timer0Value(Servo6PwmDuty);
break;
        case 14:
SERVO6 = 0; //PWM控制脚低电平
            Timer0Value(20000-Servo6PwmDuty);
break;

        case 15:
SERVO7 = 1; //PWM控制脚高电平
Timer0Value(Servo7PwmDuty);
break;
        case 16:
SERVO7 = 0; //PWM控制脚低电平
            Timer0Value(20000-Servo7PwmDuty);
            i=0;
break;  
}
i ;
}