單片機紅外遙控+步進電機+1602液晶顯示c語言源程序
}
void int0() interrupt 0 using 1
{
uint i;
for(i=0;i4;i++)
{
delay1000();
if(IR_RE==1){k=~k;} //剛開始為4.5ms的引導(dǎo)碼,如果4ms內(nèi)出現(xiàn)高電平則退出解碼程序
}
if(k==0)
{
EX0=0; //檢測到有效信號關(guān)中斷,防止干擾
IR_decode(); //如果接收到的是有效信號,則調(diào)用解碼程序
//解碼成功,調(diào)用顯示程序,顯示該鍵值
}
EX0=1; //開外部中斷,允許新的遙控按鍵
}
void main(void)
{
InitLcd();//
DelayMs(15);
sprintf(Test1,"STEP MOTOR SPEED");//the first line
ShowString(0,Test1);
ShowChar(0x19,'r');
ShowChar(0x1a,'p');
ShowChar(0x1b,'m');
count = 0;
step_index = 0;
spcount = 0;
stop_flag = 0;
P1_0 = 0;
P1_1 = 0;
P1_2 = 0;
P1_3 = 0;
SP=0x60;
EA = 1; //允許CPU中斷
TMOD = 0x11; //設(shè)定時器0和1為16位模式1
ET0 = 1; //定時器0中斷允許
EX0=1;
TH0 = 0xFE;
TL0 = 0x0C; //設(shè)定時每隔0.5ms中斷一次
TR0 = 1; //開始計數(shù)
turn=0;
speedlevel=1;
while(1)
{
if(turn==0)
ShowChar(0x14,'+');
else
ShowChar(0x14,'-');
if(TR0 ==0)
{
ShowChar(0x15,'0');
ShowChar(0x16,'0');
ShowChar(0x17,'0');
ShowChar(0x18,'0');
}
else
{
if(speedlevel==1)
{
ShowChar(0x15,'3');
ShowChar(0x16,'0');
ShowChar(0x17,'0');
ShowChar(0x18,'0');
}
else if(speedlevel==2)
{
ShowChar(0x15,'1');
ShowChar(0x16,'5');
ShowChar(0x17,'0');
ShowChar(0x18,'0');
}
else if(speedlevel==3)
{
ShowChar(0x15,'1');
ShowChar(0x16,'0');
ShowChar(0x17,'0');
ShowChar(0x18,'0');
}
else if(speedlevel==4)
{
ShowChar(0x15,'0');
ShowChar(0x16,'7');
ShowChar(0x17,'5');
ShowChar(0x18,'0');
}
else if(speedlevel==5)
{
ShowChar(0x15,'0');
ShowChar(0x16,'6');
ShowChar(0x17,'0');
ShowChar(0x18,'0');
}
}
}
}
//定時器0中斷處理
void timeint(void) interrupt 1 using 0
{
TH0=0xFE;
TL0=0x0C; //設(shè)定時每隔0.5ms中斷一次
count++;
spcount--;
if(spcount=0)
{
spcount = speedlevel;
gorun();
}
}
void delay(unsigned int endcount)
{
count=0;
do{}while(countendcount);
}
void gorun()
{
if (stop_flag==1)
{
P1_0 = 0;
P1_1 = 0;
P1_2 = 0;
P1_3 = 0;
return;
}
switch(step_index)
{
case 0: //0
P1_0 = 1;
P1_1 = 0;
P1_2 = 0;
P1_3 = 0;
break;
case 1: //0、1
P1_0 = 1;
P1_1 = 1;
P1_2 = 0;
P1_3 = 0;
break;
case 2: //1
P1_0 = 0;
P1_1 = 1;
P1_2 = 0;
P1_3 = 0;
break;
case 3: //1、2
P1_0 = 0;
P1_1 = 1;
P1_2 = 1;
P1_3 = 0;
break;
case 4: //2
P1_0 = 0;
P1_1 = 0;
P1_2 = 1;
P1_3 = 0;
break;
case 5: //2、3
P1_0 = 0;
P1_1 = 0;
P1_2 = 1;
P1_3 = 1;
break;
case 6: //3
P1_0 = 0;
P1_1 = 0;
P1_2 = 0;
P1_3 = 1;
break;
case 7: //3、0
P1_0 = 1;
P1_1 = 0;
P1_2 = 0;
P1_3 = 1;
}
if (turn==0)
{
step_index++;
if (step_index>7)
step_index=0;
}
else
{
step_index--;
if (step_index0)
step_index=7;
}
}
51單片機相關(guān)文章:51單片機教程
c語言相關(guān)文章:c語言教程
評論