|
#include <reg51.h>
#define uchar unsigned char
#define uint unsigned int
sbit YF = P1^0;
sbit YZ = P1^1;
sbit ZZ = P1^2;
sbit ZF = P1^3;
bit flag1,flag2,temp;
void right();
void left();
void speed();
void slow();
void start();
void stop();
//void back();
uchar dat,ZKB1,ZKB2;
void init()
{
TMOD = 0x21;
TH0 = 0xFF;
TL0 = 0xA0;
ET0 = 1;
TR0 = 1;
ES = 1;
// IP = 0x10;
SCON =0x50;
TH1 = 0XFD;
TL1 = 0XFD;
EA = 1;
TR1 = 1;
ET1 = 1;
flag1 = flag2 = 0;
ZZ = ZF = YZ = YF = 0;
}
void main()
{
init();
while(1)
{
}
}
void start()
{ZKB1 = 50;ZKB2 = 50;}
void stop()
{ZKB1 = 0; ZKB2 = 0; }
void speed()
{
ZKB1 += 10;
ZKB2 += 10;
if(ZKB1 >= 100){ZKB1 = 100;}
if(ZKB2 >= 100){ZKB2 = 100;}
}
void slow()
{
ZKB1 -= 10;
ZKB2 -= 10;
if(ZKB1 <= 0){ZKB1 = 0;}
if(ZKB2 <= 0){ZKB2 = 0;}
}
void left()
{
if(!flag1){ZKB1 = ZKB1 - 30;ZKB2 = ZKB2 + 20;flag1 = ~flag1;}
if(flag1) {ZKB1 = ZKB1 + 30;ZKB2 = ZKB2 - 20;flag1 = ~flag1;}
}
void right()
{
if(!flag2){ZKB2 = ZKB2 - 30;ZKB1 = ZKB1 + 20;flag2 = ~flag2;}
if(flag2) {ZKB2 = ZKB2 + 30;ZKB1 = ZKB1 - 20;flag2 = ~flag2;}
}
void uart_ISR() interrupt 4
{
RI = 0;
dat = SBUF;
switch(dat)
{
case 0xAA: speed();break;
case 0xBB: slow();break;
// case 0xCC: back();break;
case 0xDD: left();break;
case 0xEE: right();break;
case 0xFF: start();break;
default : stop();break;
}
}
void timer0() interrupt 1
{
static uchar count = 0;
TH0 = 0xFF;
TL0 = 0xA0;
++count;
if(count > 100){count = 0;}
if(count < ZKB1)
ZZ = 1;
else ZZ = 0;
if(count < ZKB2)
YZ = 1;
else YZ = 0;
}
我是用蓝牙串口通信控制51单片机小车的电机调速。
现在的问题是,先发送0XFF,执行Start()后,两个占空比都设置为50,再发送一个字节的0XAA后,ZKB1与ZKB2不是变为 60 60 小车效果是直接速度变为最大,无法实现调速。
Start() 后再按slow() 同样无法减速,只是占空比变高了一点。
感觉程序就这么点了,还是无法看出自己的问题,请大家帮我看下我到底哪里出了问题了 = =! |
阿莫论坛20周年了!感谢大家的支持与爱护!!
曾经有一段真挚的爱情摆在我的面前,我没有珍惜,现在想起来,还好我没有珍惜……
|