|
#include <reg52.h>
#define uchar unsigned char
#define uint unsigned int
typedef unsigned int u16;
typedef unsigned char u8;
sbit IN1 = P1^2;
sbit IN2 = P1^3;
sbit IN3 = P1^6;
sbit IN4 = P1^7;
/*sbit EN1 = P1^4;
sbit EN2 = P1^5;
#define left_motor_go IN1 =1 , IN2 = 0
#define left_motor_back IN1 = 0, IN2 = 1
#define right_motor_go IN3 = 1, IN4 = 0
#define right_motor_back IN3 = 0, IN4 = 1
u16 receivedata;
void delay(uint z)
{
uint x,y;
for(x = z; x > 0; x--)
for(y = 114; y > 0 ; y--);
}
void UART_INIT()
{
PCON=0x00;
SCON=0x50;
EA = 1;
ES = 1;
TMOD = 0x20;
TH1 = 0xfd;
TL1 = 0xfd;
}
/************************************************************************/
/* void run(void)
{
push_val_left =pwm;
push_val_right =pwm;
if(pwm==10) pwm=0;
if(pwm==0&&pwm<0) pwm=0;
}*/
/************************************************************************/
/* PWMµ÷ÖÆµç»úתËÙ */
/************************************************************************/
/* ×ó²àµç»úµ÷ËÙ */
/*µ÷½Úpush_val_leftµÄÖµ¸Ä±äµç»úתËÙ,Õ¼¿Õ±È */
/* void pwm_out_left_moto(void)
{
if(Left_moto_stop)
{
if(pwm_val_left<=push_val_left)
{
Left_moto_pwm=1;
}
else
{
Left_moto_pwm=0;
}
if(pwm_val_left>=10)
pwm_val_left=0;
}
else
{
Left_moto_pwm=0;
}
}*/
/******************************************************************/
/* ÓÒ²àµç»úµ÷ËÙ */
/* void pwm_out_right_moto(void)
{
if(Right_moto_stop)
{
if(pwm_val_right<=push_val_right)
{
Right_moto_pwm=1;
}
else
{
Right_moto_pwm=0;
}
if(pwm_val_right>=10)
pwm_val_right=0;
}
else {Right_moto_pwm=0; }
}
/***************************************************/
/* void timer0()interrupt 1 using 2
{
TH0=0XF8;
TL0=0X30;
time++;
pwm_val_left++;
pwm_val_right++;
pwm_out_left_moto();
pwm_out_right_moto();
}*/
void forward()
{
left_motor_go;
right_motor_go;
}
void left_go()
{
left_motor_back;
right_motor_go;
delay(700);
forward();
}
void right_go()
{
delay(50);
right_motor_back;
left_motor_go;
delay(700);
forward();
}
void left()
{
delay(50);
right_motor_go;
left_motor_back;
}
void right()
{
left_motor_go;
right_motor_back;
}
void back()
{
left_motor_back;
right_motor_back;
}
void stop()
{
IN1=0;
IN2=0;
IN3=0;
IN4=0;
}
void main()
{
IN1=0;IN2=0;IN3=0;IN4=0;
UART_INIT();
switch(receivedata)
{
case 'g': forward(); break;
case 'b': back(); break;
case 'l': left(); break;
case 'r': right(); break;
case 's': stop(); break;
case 'z': left_go(); break;
case 'y': right_go(); break;
}
}
void UART_SER() interrupt 4
{
receivedata=SBUF;
RI=0;
SBUF=receivedata;
while(!TI);
TI=0;
}
|
|