求大神指教SG90舵机的使用
时间:10-02
整理:3721RD
点击:
我用一下程序调试舵机的使用,其中P1.0-P1.4是数码管的位选,P0口是数码管的段选,P2.0是舵机的控制信号,P3.7和P3.6分别是角度加减的按键,但现在使用一下程序时,虽然舵机可以正常的旋转,但是转速很慢,而且数码管不停地闪烁,按键也没有发挥应有的作用,请大神指教#include<reg52.h>
#define uint unsigned int
#define uchar unsigned char
uchar count,jd;
sbit pwm=P2^0;
sbit jia=P3^7;
sbit jian=P3^6;
uchar code table[]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x88,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e};
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=125;y>0;y--);
}
void timer0_init()
{
TMOD=0x01;
IE=0x82;
TH0=0xfe;
TL0=0x33;
TR0=1;
}
void timer0() interrupt 1
{
TH0=0xfe;
TL0=0x33;
if(count<jd)
{
pwm=1;
}
else
pwm=0;
count=(count+1);
count=count%40;
}
void keyscan()
{
if(jia==0)
{
delay(10);
if(jia==0)
{
jd++;
count=0;
if(jd==6)
{
jd=5;
while(jia==0);
}
}
}
if(jian==0)
{
delay(10);
if(jian==0)
{
jd--;
count=0;
if(jd==0)
{
jd=1;
while(jian==0);
}
}
}
}
void display() //数码管显示函数
{
uchar bai,shi,ge;
switch(jd) //根据角度标识显示相应的数值
{
case 1:
bai=0;
shi=0;
ge=0;
break;
case 2:
bai=0;
shi=4;
ge=5;
break;
case 3:
bai=0;
shi=9;
ge=0;
break;
case 4:
bai=1;
shi=3;
ge=5;
break;
case 5:
bai=1;
shi=8;
ge=0;
break;
}
P1=0xfe;
P0=table[bai];
delay(5);
P1=0xfd;
P0=table[shi];
delay(5);
P1=0xfb;
P0=table[ge];
delay(5);
}
void main()
{
jd=1;
count=0;
timer0_init();
while(1)
{
keyscan();
display();
}
}
#define uint unsigned int
#define uchar unsigned char
uchar count,jd;
sbit pwm=P2^0;
sbit jia=P3^7;
sbit jian=P3^6;
uchar code table[]={
0xc0,0xf9,0xa4,0xb0,
0x99,0x92,0x88,0xf8,
0x80,0x90,0x88,0x83,
0xc6,0xa1,0x86,0x8e};
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=125;y>0;y--);
}
void timer0_init()
{
TMOD=0x01;
IE=0x82;
TH0=0xfe;
TL0=0x33;
TR0=1;
}
void timer0() interrupt 1
{
TH0=0xfe;
TL0=0x33;
if(count<jd)
{
pwm=1;
}
else
pwm=0;
count=(count+1);
count=count%40;
}
void keyscan()
{
if(jia==0)
{
delay(10);
if(jia==0)
{
jd++;
count=0;
if(jd==6)
{
jd=5;
while(jia==0);
}
}
}
if(jian==0)
{
delay(10);
if(jian==0)
{
jd--;
count=0;
if(jd==0)
{
jd=1;
while(jian==0);
}
}
}
}
void display() //数码管显示函数
{
uchar bai,shi,ge;
switch(jd) //根据角度标识显示相应的数值
{
case 1:
bai=0;
shi=0;
ge=0;
break;
case 2:
bai=0;
shi=4;
ge=5;
break;
case 3:
bai=0;
shi=9;
ge=0;
break;
case 4:
bai=1;
shi=3;
ge=5;
break;
case 5:
bai=1;
shi=8;
ge=0;
break;
}
P1=0xfe;
P0=table[bai];
delay(5);
P1=0xfd;
P0=table[shi];
delay(5);
P1=0xfb;
P0=table[ge];
delay(5);
}
void main()
{
jd=1;
count=0;
timer0_init();
while(1)
{
keyscan();
display();
}
}
数码管是动态扫描的, 你的按键按下,等待按键放开这个时间比较长,导致数码管扫描频率过慢,造成闪烁