下面是51
单片机的一个简单的电机程序,想问一下各位朋友,在中断函数void Interruped() interrupt 1里面为什么要将P1口的值暂存?看书上解释说,是为了使高四位不受到影响,但是如果我直接对P1操作也可以电机转起来的,即
if(beat != 0)
{
P1 = P1 & 0xF0;
P1 = P1 | Beat[index];
index ++;
index = index & 0x07;
beat--;
}
请问有什么区别吗?新手入门不是很理解啊,求解答
#include<reg52.h>
unsigned long beat = 0;
void StartMotor(unsigned long angle);
void mian()
{
EA = 1;
TMOD = 0x01;
TH0 = 0xF8;
TL0 = 0xCD;
ET0 = 1;
TR0 = 1;
StartMotor(360*2 + 180);
while(1);
}
void StartMotor(unsigned long angle)
{
EA = 0;
beat = (angle*4076)/360;
EA = 1;
}
void Interruped() interrupt 1
{
unsigned char tmp;
sta
tic unsigned char index = 0;
unsigned char code Beat[8]={
0x0E ,0x0C ,0x0D, 0x09, 0x0B, 0x03, 0x07, 0x06
};
TH0 = 0xF8;
TL0 = 0xCD;
if(beat != 0)
{
tmp = P1; //将P1口当前值暂存
tmp = tmp & 0xF0;
tmp = tmp | Beat[index];
P1 = tmp;
index ++;
index = index & 0x07;
beat--;
}
else
{
P1 = P1 | 0x0F;
}
}
谢谢解答
一周热门 更多>