|
3#
樓主 |
發表於 2012-7-30 17:07:52
|
只看該作者
To Nichal:
謝謝您的回覆~
我有參考您的方式,修改了一下,但因為IC不同,所以有幾個地方
不知道改的對不對,可是基本上4個都有PWM訊號,有幾個問題如下
再麻煩您費心~程式如下~Xtal=24MHz
1.修改每一個P2_0,P2_1....的ANGLE,Dutycycle就會隨著改變
#include <AT89X51.H>
#define SERVO1_PWM P2_0=1
#define SERVO2_PWM P2_1=1
#define SERVO3_PWM P2_2=1
#define SERVO4_PWM P2_3=1
#define MAX_ANGLE 4400
#define MIN_ANGLE 1500
#define MID_ANGLE 3000
unsigned int dutytime=40000;
unsigned int ton=4400;
bit start_flag;
bit change_flag;
void Timer0_Init(void);
void Timer0_ISR(void);
void Timer1_Init(void);
void Switch(unsigned int counts);
void Timer1_ISR(void);
void main(void)
{
TMOD|=0x11;
Timer1_Init();
Timer0_Init();
start_flag=0;
change_flag=0;
while(1)
{
if(start_flag==0)
{
start_flag=0;
SERVO1_PWM;
Switch(MAX_ANGLE);
while (change_flag==0);
{
change_flag=0;
SERVO2_PWM;
Switch(MAX_ANGLE);
}
while (change_flag==0);
{
change_flag=0;
SERVO3_PWM;
Switch(MAX_ANGLE);
}
while (change_flag==0);
{
change_flag=0;
SERVO4_PWM;
Switch(MAX_ANGLE);
}
while (change_flag==0);
{
change_flag=0;
}
}
}
}
void Timer0_Init(void)
{
ET0=1;
TH0=(65536-dutytime)/256;
TL0=(65536-dutytime)%256;
EA=1;
TR0=1;
}
void Timer0_ISR(void) interrupt 1
{
TF0=0;
TH0=(65536-dutytime)/256;
TL0=(65536-dutytime)%256;
start_flag=0;
}
void Timer1_Init(void)
{
TH1=(65536-ton)/256;
TL1=(65536-ton)%256;
ET1=0;
TR1=0;
}
void Switch(unsigned int counts)
{
TH1=(65536-counts)/256;
TL1=(65536-counts)%256;
ET1=1;
TR1=1;
}
void Timer1_ISR(void) interrupt 3
{
TR1=0;
P2=0x00;
change_flag=1;
} |
|