#include <c8051f020.h>
#include <stdio.h>
#include <intrins.h>
typedef unsigned char uchar;
typedef unsigned long ulong;
typedef unsigned int uint;
#define L_MOTO_BREAK L_DIR1A=L_DIR1B
#define L_MOTO_BACK L_DIR1A=0;L_DIR1B=1
#define L_MOTO_FORD L_DIR1A=1;L_DIR1B=0
#define R_MOTO_BREAK R_DIR1A=R_DIR1B
#define R_MOTO_BACK R_DIR1A=0;R_DIR1B=1
#define R_MOTO_FORD R_DIR1A=1;R_DIR1B=0
sbit L_DIR1A=P2^2;
sbit L_DIR1B=P2^1;
sbit R_DIR1A=P2^7;
sbit R_DIR1B=P2^6;
sbit P1_0=P1^0;
sbit P1_1=P1^1;
sbit P1_2=P1^2;
int fb=1;//控制前后方向
sbit lcdrw=P3^2;//5
sbit lcdcs=P3^7;//6
sbit lcdrs=P3^6;//4
void config (void)
{
WDTCN = 0x07;
WDTCN = 0xde; //看门狗禁止
WDTCN = 0xad; //看门狗禁止
XBR0 = 0x50; //配置交叉开关,
XBR1 = 0x00;
XBR2 = 0x40; //交叉开关允许
P0MDOUT=0x00; //p0.4~p0.5为推挽输出
P1MDOUT=0xff;
P2MDOUT=0xff;
P3MDOUT=0xFF;
P74OUT=0x00; //p4--p6
P7=0xff;
OSCXCN = 0x00;
OSCICN = 0x16; //采用内部晶振,频率为 8MHZ
}
void delay1ms(uint time){//延迟子程序0.5MS
uint ii;
uint jj;
for (ii=0;ii<time;ii++)
{ for(jj=0;jj<4000;jj++)
;
} }
void PCA_Init()
{
PCA0MD=0x08; //PCA采用系统时钟,且PCA溢出中断禁止
PCA0CN=0x40; //启动PCA计数器
}
void PCA0_Init()
{
PCA0CPM0=0x42;//CEX0为8位PWM输出模式
PCA0CPM1=0x42; //CEX1为8bit PWM输出模式
}
void PWM0_set(uchar val){ //高电平占空比为val/256
PCA0CPH0=~val+1;
}
void PWM1_set(uchar val){ //高电平占空比为val/256
PCA0CPH1=~val+1;
}
void L_FORd(uchar L_spe_ford)
{
L_MOTO_FORD;
PWM0_set(L_spe_ford);
}//电机速度
void R_FORd(uchar R_spe_ford)
{
R_MOTO_FORD;
PWM1_set(R_spe_ford);
}
void L_BACK(uint L_spe_back)
{
L_MOTO_BACK;
PWM0_set(L_spe_back);
}
void R_BACK(uint R_spe_back)
{
R_MOTO_BACK;
PWM1_set( R_spe_back);
}
void L_BREAK()
{
L_MOTO_BREAK;
}
void R_BREAK()
{
R_MOTO_BREAK;
}
void main()
{
config();
PCA_Init();
PCA0_Init();
EA=1;
while(1)
{
R_FORd(200);
L_FORd(200);
if(fb==1)
{
if(P7&0x04)//前左微偏
{
R_FORd(170);
delay1ms(3);
L_FORd(210);
}
if(P7&0x02)//前右微偏
{
R_FORd(200);
L_FORd(180);
}
if((P7&0x04)&&(P7&0x08))//前左中偏
{
R_FORd(200);
L_FORd(170);
}
if((P7&0x02)&&(P7&0x01))//前右中偏
{
R_FORd(170);
L_FORd(190);
}
if(P7&0x08)//前左大偏
{
R_FORd(200);
L_FORd(160);
}
if(P7&0x01)//前右大偏
{
R_FORd(160);
L_FORd(200);
}
}
}
}
帮我看下程序,为何小车还是不循迹