#include <c8051f120.h>
sbit receive1 = P4^5;
sbit receive2 = P4^6;
sbit receive3 = P4^7;
sbit input1 = P5^1;
sbit input2 = P5^3;
sbit input3 = P5^5;
sbit input4 = P5^7;
void OSCILLATOR_Init (void);
void PORT_Init (void);
void delay_m(unsigned int m);
/**************************前进*************************/
void advance(void)
{
SFRPAGE =0X0F;
input1 = 1;
input2 = 0;
input3 = 1;
input4 = 0;
}
/***************************后退**********************/
void back(void)
{
SFRPAGE = 0X0F;
input1 = 0;
input2 = 1;
input3 = 0;
input4 = 1;
}
/**************************左转************************/
void left_swerve(void)
{
SFRPAGE = 0X0F;
input1 = 0;
input2 = 1;
input3 = 1;
input4 = 0;
/* delay_m(1000);
input1 = 0;
input2 = 0;
input3 = 0;
input4 = 0;
delay_m(1); */
}
/***************************右转************************/
void right_swerve(void)
{ SFRPAGE = 0X0F;
input1 = 1;
input2 = 0;
input3 = 0;
input4 = 1;
}
/***************************停止***********************/
void stop(void)
{ SFRPAGE = 0X0F;
input1 = 0;
input2 = 0;
input3 = 0;
input4 = 0;
}
/***************************主函数*********************/
void main(void)
{
receive1=1;
receive2=1;
receive3=1;
WDTCN = 0xde; // Disable watchdog timer
WDTCN = 0xad;
PORT_Init(); // Initialize Port I/O
OSCILLATOR_Init ();
while(1)
{
while((receive1==0)&&(receive2==0)&&(receive3==0))
back();
while((receive1==0)&&(receive2==0)&&(receive3==1))
left_swerve();
while((receive1==0)&&(receive2==1)&&(receive3==0))
advance();
while((receive1==0)&&(receive2==1)&&(receive3==1))
left_swerve();
while((receive1==1)&&(receive2==0)&&(receive3==0))
right_swerve();
while((receive1==1)&&(receive2==0)&&(receive3==1))
stop();
while((receive1==1)&&(receive2==1)&&(receive3==0))
right_swerve();
while((receive1==1)&&(receive2==1)&&(receive3==1))
stop();
}
}
/************************延时************************
void delay_1(void)
{
unsigned int i;
for(i=0;i<25;i++);
}
void delay_m(unsigned int m)
{
unsigned int i=0;
for (i=0;i<m;i++)
delay_1();
} */
/**********************************时钟初始化*******************************/
void OSCILLATOR_Init (void)
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = CONFIG_PAGE; // set SFR page
OSCICN |= 0x03; // Configure internal oscillator for
// its maximum frequency (24.5 Mhz)
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}
/***********************************I/P口初始化****************************/
void PORT_Init (void)
{
char SFRPAGE_SAVE = SFRPAGE; // Save Current SFR page
SFRPAGE = CONFIG_PAGE;
XBR0 = 0x10;
XBR1 = 0x00;
XBR2 = 0x40; //交叉开关使能,因为当禁止时端口0、1、2 和3 的所有引脚被强制为输入方式
P1MDIN |= 0xff; // P1.6 is digital 0:端口引脚被配置为模拟输入方式。1:端口引脚被配置为数字输入方式
P1MDOUT = 0xff;
P0MDOUT = 0xff;
SFRPAGE = SFRPAGE_SAVE; // Restore SFR page
}