舵机驱动
时间:10-02
整理:3721RD
点击:
这是我刚刚调试好的舵机程序,用按键控制舵机角度变化,给大家分享一下!(P2^1为控制信号端,P2^2减少舵机角度,P2^4增加舵机角度)#include <reg51.h>
sbit P2_1=P2^1;
sbit jia=P2^4;
sbit jian=P2^2;
unsigned char a=0;
void InitTimer0(void) //设置0.1ms中断一次
{
TMOD = 0x01;
TH0=(65536-100)/256;
TL0=(65536-100)%256;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void main(void)
{
InitTimer0();
a=10;
while(1)
{
if(0==jia)
{a=a+1;
while(!jia);}
if(0==jian)
{a=a-1;
while(!jian);}
}
}
void PWM() interrupt 1
{
static unsigned char count;
TH0=(65536-100)/256;
TL0=(65536-100)%256;
if(count<=a)
{P2_1=1;}
else
{P2_1=0;}
count++;
if(count == 200)
{
count=0; //20ms一个周期
}
}
sbit P2_1=P2^1;
sbit jia=P2^4;
sbit jian=P2^2;
unsigned char a=0;
void InitTimer0(void) //设置0.1ms中断一次
{
TMOD = 0x01;
TH0=(65536-100)/256;
TL0=(65536-100)%256;
EA = 1;
ET0 = 1;
TR0 = 1;
}
void main(void)
{
InitTimer0();
a=10;
while(1)
{
if(0==jia)
{a=a+1;
while(!jia);}
if(0==jian)
{a=a-1;
while(!jian);}
}
}
void PWM() interrupt 1
{
static unsigned char count;
TH0=(65536-100)/256;
TL0=(65536-100)%256;
if(count<=a)
{P2_1=1;}
else
{P2_1=0;}
count++;
if(count == 200)
{
count=0; //20ms一个周期
}
}
是不是应该加一个按键防抖啊?