|
樓主 根據(jù)你的程序修改的 如有問(wèn)題還請(qǐng)見(jiàn)諒
不能達(dá)到你所說(shuō)的一度一度的 但是還是可以達(dá)到你所寫(xiě)的那三個(gè)固定值以外的角度
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit pwm=P1^0; //信號(hào)輸出
sbit k1=P3^1; //按鍵k1接P3^5,控制順時(shí)針旋轉(zhuǎn)
sbit k2=P3^2; //按鍵k2接P3^4,控制逆時(shí)針旋轉(zhuǎn)
sbit k3=P3^3;
uchar count=0;
uchar n=5; //初始位置-在0°附近 改變n值可以改變舵機(jī)的初始位置
//這個(gè)n值在不同的舵機(jī)可能存在一點(diǎn)誤差,可以改變他的值來(lái)試試具體位置
void delay5ms()
{
unsigned char a,b;
for(b=110;b>0;b--)
for(a=130;a>0;a--);
}
void delay1ms()
{
unsigned char a;
for(a=130;a>0;a--);
}
void key()
{
if(k1==0)
{
delay5ms();
if(k1==0)
{
delay5ms();
n++; // 0度
if(n >= 25)
n = 25;
}
}
if(k2==0)
{
delay5ms();
if(k2==0)
{
delay5ms();
n--; // 90度
if(n <= 5)
n = 5;
}
}
}
void InitTimer() // 0.1ms 11.0592Mhz
{
TMOD = 0x01;
TH0 = 0xFF;
TL0 = 0XA4;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void main()
{
InitTimer();
while(1)
{
key();
}
}
void Timer() interrupt 1 //特別注意此處,0--外部中斷0,1--定時(shí)器中斷0,2--外部中斷1,3--定時(shí)器中斷1,4--串行口中斷1
{
TH0 = 0xFF;
TL0 = 0xA4;
count++;
if(count<=200)
{
if(count<=n)
{
pwm=1;
}
else
{
pwm=0;
}
}
else
{
count=0;
pwm=0;
}
} |
|