#include<reg52.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
//sbit KEY1 = P3^2; //定义调速的按键
sbit DC1 = P0^0; //定义舵机的控制端口
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; //定义低电平时间
/*********************************
函数:Delay_1ms()
参数:t
返回:无
功能:延时子程序,延时时间为 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++);
}
/*********************************
函数:Time0Init()
参数:无
返回:无
功能:设置定时器0的工作方式2定时时长为100微秒,
并打开中断和启动定时器。
该例中使用的单片机晶振为11.0592M。
**********************************/
void Time0Init(void)
{
TMOD = 0x02; //设定定时器0工作在方式0模式
TH0 = 0xA4; //定时时长为100微秒的初值自动重载
TL0 = 0xA4; //定时时长为100微秒的初值
ET0 = 1; //打开定时器0中断
EA = 1; //打开总中断
TR0 = 1; //启动定时器
}
/*********************************
函数:Time0Int()
参数:无
返回:无
说明:定时器0中断函数。用于处理PWM的信号。
**********************************/
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(); //定时器0初始化
CYCLE = 200; //定义PWM周期为20毫秒
/*传统舵机 必须连续发信号,,数字舵机去掉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);
}
}
|