哪位大虾告诉我一个单片机控制舵机的C程序啊。
2个回答
展开全部
单片机无法控制舵机旋转360度,只能是180度,给你两个程序,一个是单个按键控制的舵机旋转,一个是矩阵键盘控制的
#include<reg51.h>
#define uint unsigned int
#define uchar unsigned char
uint TH0_1,TL0_1;TH0_2,TL0_2,TH0_3,TL0_3;
sbit key1=P3^2;
sbit key2=P3^3;
sbit key3=P3^4;
sbit out=P1^0;
void dingshiqi_chushihua()
{
F0=0;
out=0;
TMOD=0x01;
TR0=1;
IE=0x82;
TH0_1=(65536-500)/256;
TL0_1=(65536-500)%256;
TH0_2=(65536-1000)/256;
TL0_2=(65536-1000)%256;
TH0_3=(65536-1500)/256;
TL0_3=(65536-1500)%256;
}
void dingshiqi_chuli() interrupt 1
{
F0=~F0;
out=~out;
if(key1==0)
{
TH0=TH0_1; //旋转到180°
TL0=TL0_1;
}
if(key2==0)
{
TH0=TH0_2; //旋转到0°
TL0=TL0_2;
}
if(key3==0)
{
TH0=TH0_3; //旋转到90°
TL0=TL0_3;
}
}
void main()
{
dingshiqi_chushihua();
while(1)
{
}
}
下面这个是矩阵键盘控制的,注意引脚,我的单片机上矩阵键盘由P1口控制,所以舵机的输入引脚不能加在P1口上,(舵机由PWM波控制,这样做防止对舵机不必要的干扰)
#include<reg51.h>
#define uint unsigned int
#define uchar unsigned char
sbit out=P0^0;
uint TH0_1 , TL0_1;
void Delay_1ms(uint i)//1ms延时
{
uchar x,j;
for(j=0;j<i;j++)
for(x=0;x<=148;x++);
}
uint Keyscan(void)
{
uint i,j, temp, Buffer[4] = {0xfe, 0xfd, 0xfb, 0xf7};
uint m;
for(j=0; j<4; j++)
{
P1 = Buffer[j];
temp = 0x10;
for(i=0; i<4; i++)
{
if(!(P1 & temp))
{
m=(i+j*4);
return m;
}
temp <<= 1;
}
}
}
uint anjian(void)
{
uint Key_Value;
while(1)
{
P1 = 0xf0;
if(P1 != 0xf0)
{
Delay_1ms(20);
if(P1 != 0xf0)
{
Delay_1ms(20);
if(P1 != 0xf0)
{
Key_Value = Keyscan();
Key_Value = (500+11*11*Key_Value);
return Key_Value;
}
}
}
}
}
void main()
{
while(1)
{
uint chushizhi;
chushizhi=anjian();
F0=0;
out=0;
TMOD=0x01;
IE=0x82;
TR0=1;
TH0_1=(65536-chushizhi)/256;
TL0_1=(65536-chushizhi)%256;
}
}
void dingshiqi_chuli() interrupt 1
{
F0=~F0;
out=~out;
TH0=TH0_1;
TL0=TL0_1;
}
#include<reg51.h>
#define uint unsigned int
#define uchar unsigned char
uint TH0_1,TL0_1;TH0_2,TL0_2,TH0_3,TL0_3;
sbit key1=P3^2;
sbit key2=P3^3;
sbit key3=P3^4;
sbit out=P1^0;
void dingshiqi_chushihua()
{
F0=0;
out=0;
TMOD=0x01;
TR0=1;
IE=0x82;
TH0_1=(65536-500)/256;
TL0_1=(65536-500)%256;
TH0_2=(65536-1000)/256;
TL0_2=(65536-1000)%256;
TH0_3=(65536-1500)/256;
TL0_3=(65536-1500)%256;
}
void dingshiqi_chuli() interrupt 1
{
F0=~F0;
out=~out;
if(key1==0)
{
TH0=TH0_1; //旋转到180°
TL0=TL0_1;
}
if(key2==0)
{
TH0=TH0_2; //旋转到0°
TL0=TL0_2;
}
if(key3==0)
{
TH0=TH0_3; //旋转到90°
TL0=TL0_3;
}
}
void main()
{
dingshiqi_chushihua();
while(1)
{
}
}
下面这个是矩阵键盘控制的,注意引脚,我的单片机上矩阵键盘由P1口控制,所以舵机的输入引脚不能加在P1口上,(舵机由PWM波控制,这样做防止对舵机不必要的干扰)
#include<reg51.h>
#define uint unsigned int
#define uchar unsigned char
sbit out=P0^0;
uint TH0_1 , TL0_1;
void Delay_1ms(uint i)//1ms延时
{
uchar x,j;
for(j=0;j<i;j++)
for(x=0;x<=148;x++);
}
uint Keyscan(void)
{
uint i,j, temp, Buffer[4] = {0xfe, 0xfd, 0xfb, 0xf7};
uint m;
for(j=0; j<4; j++)
{
P1 = Buffer[j];
temp = 0x10;
for(i=0; i<4; i++)
{
if(!(P1 & temp))
{
m=(i+j*4);
return m;
}
temp <<= 1;
}
}
}
uint anjian(void)
{
uint Key_Value;
while(1)
{
P1 = 0xf0;
if(P1 != 0xf0)
{
Delay_1ms(20);
if(P1 != 0xf0)
{
Delay_1ms(20);
if(P1 != 0xf0)
{
Key_Value = Keyscan();
Key_Value = (500+11*11*Key_Value);
return Key_Value;
}
}
}
}
}
void main()
{
while(1)
{
uint chushizhi;
chushizhi=anjian();
F0=0;
out=0;
TMOD=0x01;
IE=0x82;
TR0=1;
TH0_1=(65536-chushizhi)/256;
TL0_1=(65536-chushizhi)%256;
}
}
void dingshiqi_chuli() interrupt 1
{
F0=~F0;
out=~out;
TH0=TH0_1;
TL0=TL0_1;
}
展开全部
51单片机定时器的程序你在网上看看,一大片的 产生1ms在循环20次就有20ms了
更多追问追答
追问
要舵机连续旋转左右各60度。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询