📄 main.c
字号:
///////////////////////////////
//main()//
////////////////////////////////
#include "mydefine.h"
#include "intrins.h"
#define WaitTime 60
bdata unsigned char State;
//bit State_0,State_1,State_2,State_3;
sbit State_0 = State^0;
sbit State_1 = State^1;
sbit State_2 = State^2;
sbit State_3 = State^3;
unsigned int SpeedRight = 4,SpeedLeft = 4;
unsigned int OldSpeedRight ,OldSpeedLeft ;
//bdata unsigned int PWM_0,PWM_1;
data unsigned int pwm[16]=
{0x0000,// pwm[0] = 0x0000; //0000 0000 0000 0000
0x0001,// pwm[1] = 0x0001; //0000 0000 0000 0001
0x0101,// pwm[2] = 0x0101; //0000 0001 0000 0001
0x0821,// pwm[3] = 0x0821; //0000 1000 0010 0001
0x1111,// pwm[4] = 0x1111; //0001 0001 0001 0001
0x2245,// pwm[5] = 0x2245; //0010 0010 0100 1001
0x2525,// pwm[6] = 0x2525; //0010 0101 0010 0101
0x52aa,// pwm[7] = 0x52aa; //0101 0010 1010 1010
0x5555,// pwm[8] = 0x5555; //0101 0101 0101 0101
0xad55,// pwm[9] = 0xad55; //1010 1101 0101 0101
0xdada,// pwm[10] = 0xdada; //1101 1010 1101 1010
0xddb6,// pwm[11] = 0xddb6; //1101 1101 1011 0110
0xeeee,// pwm[12] = 0xeeee; //1110 1110 1110 1110
0xf7de,// pwm[13] = 0xf7de; //1111 0111 1101 1110
0xfefe,// pwm[14] = 0xfefe; //1111 1110 1111 1110
// 0xfffe,// pwm[15] = 0xfffe; //1111 1111 1111 1110
0xffff};// pwm[15] = 0xffff; //1111 1111 1111 1111
bdata unsigned char OldState,OtherState;
unsigned int flag;
void main(void)
{
Initialize();
State = 0x00;
OldState = State;
DirectionLeft = 0;
DirectionRight = 0;
OldSpeedLeft = SpeedLeft;
OldSpeedRight = SpeedRight;
SpeedLeft = 15;
SpeedRight = 15;
// Delay(5000);
Run();
while(1)
{
DirectionRight = 0;
DirectionLeft = 0;
Run();
Run_at(12,Right);
Run_at(12,Left);
Delay(5000);
Stop();
// Delay(500);
// DirectionRight = 0;
// DirectionLeft = 0;
// Run();
// Run_at(5,Right);
// Run_at(5,Left);
Delay(5000);
DirectionRight = 1;
DirectionLeft = 1;
Run();
Run_at(10,Right);
Run_at(10,Left);
Delay(5000);
Stop();
Delay(5000);
}
}//end of main()
void Initialize (void)
{
P3 = 0xff; /*speed-up pull-ups*/
P1_6 = 0;
P1 = 0xff;
/* Timer0 Initialize */
TMOD = 0x01;
TH0 = 0xfe;
TL0 = 0xd4;
ET0 = 0;
TR0 = 0;
/* Timer1 Initialize */
/* Timer2 Initialize */
/* Out0 Initialize */
/* Out1 Initialize */
/* ALL interrupt */
EA = 1;
}
void Delay(unsigned int time) //time /ms
{unsigned int i;
register unsigned char j;
time*=2;
for (i=0;i<time;i++)
{_nop_ ();
for (j=0;j<197;j++)
{_nop_ ();
_nop_ ();
}
}
}
void Run()
{
_nop_();
_nop_();
PWM_0 = pwm[SpeedLeft];
PWM_1 = pwm[SpeedRight];
DriveLeftMo1 = 0;
DriveLeftMo2 =0;
DriveRightMo1 =0;
DriveRightMo2 =0;
ET0 = 1; //Timer 0 open
TR0 = 1; //Timer 0 begins
_nop_();
_nop_();
}
void Run_at(unsigned int Speed,bit Motor)
{
if(Motor==1)
{ OldSpeedLeft = SpeedLeft;
SpeedLeft =Speed;
}
else
{ OldSpeedRight = SpeedRight;
SpeedRight =Speed;
}
Run_in();
}
void Run_in()
{ if(SpeedLeft!=OldSpeedLeft)
PWM_0 = pwm[SpeedLeft];
if(SpeedRight!=OldSpeedRight)
PWM_1 = pwm[SpeedRight];
_nop_();
_nop_();
}
void Stop()
{
DriveLeftMo1 = 0;
DriveLeftMo2 = 0;
DriveRightMo1 = 0;
DriveRightMo2 = 0;
TR0 = 0;
ET0 = 0;
TF0 = 0;
_nop_();
_nop_();
}
void SpeedUp(unsigned int Delta,bit Motor)
{ int x,y;
x = SpeedLeft + Delta;
y = SpeedRight + Delta;
if(Motor==1)
{ OldSpeedLeft = SpeedLeft;
if(x<=15)
{SpeedLeft +=Delta;}
}
else
{ OldSpeedRight = SpeedRight;
if(y<=15)
{SpeedRight +=Delta; }
}
Run_in();
_nop_();
_nop_();
}
void SpeedDown(unsigned int Delta,bit Motor)
{
int x,y;
x = SpeedLeft - Delta;
y = SpeedRight - Delta;
if(Motor==1)
{OldSpeedLeft = SpeedLeft;
if(x>=1)
{SpeedLeft -=Delta;}
}
else
{OldSpeedRight = SpeedRight;
if(y>=1)
{SpeedRight -=Delta;}
}
Run_in();
_nop_();
_nop_();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -