51单片机步进电机正反转实验程序
按键控制可以让电机正转或倒转,按键由p0.0-0.3控制
ORG 0000H
AJMP START
ORG 0050H
MAIN: MOV R0,#00H START: LCALL START0
LCALL DELAY0
JNB P0.0,UPS
JNB P0.1,DWNS
AJMP START
UPS:;LCALL DELAY3
;JNB P0.0,$
;LCALL DELAY3
LCALL START0
LCALL DELAY1
JNB P0.0,UPS
JNB P0.1,START ; AJMP START
AJMP UPS
DWNS:LCALL START0
LCALL DELAY2
JNB P0.0,START
JNB P0.1,DWNS
; AJMP START
AJMP DWNS
START1: MOV R0,#00H START0: MOV A,R0
MOV DPTR,#TAB
MOVC A,@A+DPTR
JZ START1
MOV P1,A
INC R0
RET
DELAY0: MOV R7,#10 D1: MOV R6,#248
DJNZ R6,$
DJNZ R7,D1
RET
DELAY1: MOV R7,#5 D2: MOV R6,#248
DJNZ R6,$
DJNZ R7,D2
RET
DELAY2: MOV R7,#40 D3: MOV R6,#248
DJNZ R6,$
DJNZ R7,D3
RET
DELAY3: MOV R7,#60 D4: MOV R6,#248
DJNZ R6,$
DJNZ R7,D4
RET
TAB: DB 07H
DB 03H
DB 0BH
DB 09H
DB 0DH
DB 0CH
DB 0EH
DB 06H
DB 00H
END
#include
unsigned char i; unsigned char temp; unsigned char a,b; void up_speed(); void down_speed(); /********************************************************************
数 名:delay()
功 能:延时子程序
说 明:无
入口参数:无
返 回 值:无
设 计:yangm_cui 日 期:2008-03-19
修 改: 日 期: ***********************************************************************/
void delay(void) {
unsigned char m,n;
for(m=20;m>0;m--)
for(n=20;n>0;n--);
}
/********************************************************************
函 数 名:delay1()
功 能:延时子程序
说 明:无
入口参数:无
返 回 值:无
设 计:yangm_cui 日 期:2008-03-19
修 改: 日 期:
***********************************************************************/
void delay1(void)
{
unsigned char m,n,s;
for(m=20;m>0;m--)
for(n=20;n>0;n--)
for(s=248;s>0;s--);
}
/********************************************************************
函 数 名:up_speed()
功 能:加速程序
说 明:无
入口参数:无
返 回 值:无
设 计:yangm_cui 日 期:2008-03-19
修 改: 日 期:
***********************************************************************/
void up_speed()
{
while(1)
{
P1=0x03; //使电机正转的送数顺序03h 06h 0ch 09h
delay(); //转动速度
P1=0x06;
delay();
P1=0x0c;
delay();
P1=0x09;
delay();
if(P0_1==0)
{
delay1();
if(P0_1==0)
down_speed();
}
}
}
/********************************************************************
函 数 名:down_speed()
功 能:减速程序
说 明:无
入口参数:无
返 回 值:无
设 计:yangm_cui 日 期:2008-03-19
修 改: 日 期:
***********************************************************************/
void down_speed()
{
while(1)
{
P1=0x03;
delay1();
delay1();
P1=0x06;
delay1();
delay1();
P1=0x0c;
delay1();
delay1();
P1=0x09;
delay1();
delay1();
if(P0_0==0)
{
delay1();
if(P0_0==0)
up_speed();
}
}
}
/********************************************************************
函 数 名:main()
功 能:主程序
说 明:无
入口参数:无
返 回 值:无
设 计:yangm_cui 日 期:2008-03-19
修 改: 日 期: ***********************************************************************/
void main(void)
{
while(1)
{
P1=0x03;
delay1();
P1=0x06;
delay1();
P1=0x0c;
delay1();
P1=0x09;
delay1();
if(P0_0==0) //按键扫描
{
delay1(); //按键消抖
if(P0_0==0)
up_speed(); //加速
}
if(P0_1==0)
{
delay1(); //按键消抖
if(P0_1==0)
down_speed(); //减速
}
}
}