标题:
单片机舵机转动超声波测距程序 为啥我加入超声波测距舵机就不转了 求帮助
[打印本页]
作者:
人生这里
时间:
2019-5-7 20:24
标题:
单片机舵机转动超声波测距程序 为啥我加入超声波测距舵机就不转了 求帮助
#include<reg51.h>
#include<intrins.h>
#define uchar unsigned char
#define uint unsigned int
uchar num=0,sun;
uchar shi,ge,bai;
int jia=0;
unsigned long int s=0;
void delay(uint max)
{
int x,y;
for(x=0;x<max;x++)
for(y=0;y<500;y++);
}
int cade[]={1,8,18};
sbit RS=P2^6;
sbit RW=P2^5;
sbit E=P2^7;
sbit trig=P2^1;
sbit echo=P2^2;
sbit pwm=P2^0;
void start()
{
trig=1;
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
_nop_();
trig=0;
}
uchar Lcd1602_ReadBusy()
{
uchar temp;
RS=0;
RW=1;
_nop_();
P0=0xff;
_nop_();
E=1;
_nop_();
temp=P0;
_nop_();
E=0;
return (temp&0x80);
}
void Lcd1602_WriteCom(uchar com)
{
while(Lcd1602_ReadBusy());
RS=0;
RW=0;
_nop_();
P0=com;
_nop_();
E=1;
_nop_();
_nop_();
E=0;
_nop_();
_nop_();
}
void Lcd1602_WriteData(uchar dat)
{
while(Lcd1602_ReadBusy());
RS=1;
RW=0;
_nop_();
P0=dat;
_nop_();
E=1;
_nop_();
_nop_();
E=0;
_nop_();
_nop_();
}
void Lcd1602_init()
{
delay(15);
Lcd1602_WriteCom(0x38);
delay(5);
Lcd1602_WriteCom(0x38);
delay(5);
Lcd1602_WriteCom(0x38);
Lcd1602_WriteCom(0x0c);
Lcd1602_WriteCom(0x01);
Lcd1602_WriteCom(0x06);
}
void count()
{
TMOD=0x01;
ET0=1;
EA=1;
TL0=0;
TH0=0;
start();
while(!echo);
TR0=1;
while(echo);
TR0=0;
s=TH0*256+TL0+jia;
jia=0;
s=s*0.017*12/11;
Lcd1602_init();
bai=s/100;
shi=s/10%10;
ge=s%10%10;
Lcd1602_WriteCom(0x80+0x02);
Lcd1602_WriteData(0x30+bai);
Lcd1602_WriteData(0x30+shi);
Lcd1602_WriteData(0x30+ge);
}
void T1_time()interrupt 3
{
jia=jia+65536;
TH0=0;
TL0=0;
}
void duoji(uint i)
{
TMOD=0X11;
TH0=(65536-100)/256;
TR0=(65536-100)%256;
EA=1;
ET0=1;
TR0=1;
sun=cade[i];
}
void T0_time()interrupt 1
{
TH0=(65536-100)/256;
TL0=(65536-100)%256;
num++;
if(num==50) num=0;
if(num>sun) pwm=0;
else pwm=1;
}
void main()
{
duoji(0);
delay(100);
duoji(1);
delay(100);
count();
duoji(2);
delay(100);
}
复制代码
作者:
大克汐
时间:
2019-11-3 12:44
补下电路图
作者:
大克汐
时间:
2019-11-3 13:03
你舵机TMOD为0x11,超声波为0x01,先舵机设置,后超声波,TMOD数值改了
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1