#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);
}
}
|