#include <AT89X51.h>
static unsigned int count;
static int step_index;
void delay(unsigned int endcount);
void gorun(bit turn, unsigned int speedlevel);
void main(void)
{
count = 0;
step_index = 0;
P1_0 = 0;
P1_1 = 0;
P1_2 = 0;
P1_3 = 0;
EA = 1; //允许CPU中断
TMOD = 0x11; //设定时器0和1为16位模式1
ET0 = 1; //定时器0中断允许
TH0 = 0xFE;
TL0 = 0x0C; //设定时每隔0.5ms中断一次
TR0 = 1; //开始计数
do {
gorun(1,10);
}while(1);
}
//定时器0中断处理
void timeint(void) interrupt 1
{
TH0=0xFE;
TL0=0x0C; //设定时每隔0.5ms中断一次
count++;
}
void delay(unsigned int endcount)
{
count=0;
do{}while(count<endcount);
}
void gorun(bit turn,unsigned int speedlevel)
{
switch(step_index)
{
case 0:
P1_0 = 1;
P1_1 = 0;
P1_2 = 0;
P1_3 = 0;
break;
case 1:
P1_0 = 1;
P1_1 = 0;
P1_2 = 1;
P1_3 = 0;
break;
case 2:
P1_0 = 0;
P1_1 = 0;
P1_2 = 1;
P1_3 = 0;
break;
case 3:
P1_0 = 0;
P1_1 = 1;
P1_2 = 1;
P1_3 = 0;
break;
case 4:
P1_0 = 0;
P1_1 = 1;
P1_2 = 0;
P1_3 = 0;
break;
case 5:
P1_0 = 0;
P1_1 = 1;
P1_2 = 0;
P1_3 = 1;
break;
case 6:
P1_0 = 0;
P1_1 = 0;
P1_2 = 0;
P1_3 = 1;
break;
case 7:
P1_0 = 1;
P1_1 = 0;
P1_2 = 0;
P1_3 = 1;
}
delay(speedlevel);
if (turn==0)
{
step_index++;
if (step_index>7)
step_index=0;
}
else
{
step_index--;
if (step_index<0)
step_index=7;
}
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
-----------------------------------------------------------------------
要细分才行。
一周热门 更多>