#include <reg52.h>
#define DataPort P0
sbit LATCH1=P2^2;
sbit LATCH2=P2^3;
unsigned char code dofly_DuanMa[10]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f};
unsigned char code dofly_WeiMa[]={0xfe,0xfd,0xfb,0xf7,0xef,0xdf,0xbf,0x7f};
unsigned char TempData[8];
sbit A1=P1^0;
sbit B1=P1^1;
sbit C1=P1^2;
sbit D1=P1^3;
#define Coil_AB1 {A1=1;B1=1;C1=0;D1=0;}
#define Coil_BC1 {A1=0;B1=1;C1=1;D1=0;}
#define Coil_CD1 {A1=0;B1=0;C1=1;D1=1;}
#define Coil_DA1 {A1=1;B1=0;C1=0;D1=1;}
#define Coil_A1 {A1=1;B1=0;C1=0;D1=0;}
#define Coil_B1 {A1=0;B1=1;C1=0;D1=0;}
#define Coil_C1 {A1=0;B1=0;C1=1;D1=0;}
#define Coil_D1 {A1=0;B1=0;C1=0;D1=1;}
#define Coil_OFF {A1=0;B1=0;C1=0;D1=0;}
unsigned char Speed;
void Display(unsigned char FirstBit,unsigned char Num);
void Init_Timer0(void);
void DelayUs2x(unsigned char t)
{
while(--t);
}
void DelayMs(unsigned char t)
{
while(t--)
{
DelayUs2x(245);
DelayUs2x(245);
}
}
void main()
{
unsigned int i=512;
Init_Timer0();
EA=1;
EX0=1;
IT0=1;
Speed=3;
TempData[0]=dofly_DuanMa[Speed/10];
TempData[1]=dofly_DuanMa[Speed%10];
Coil_OFF;
while(i--)
{ Coil_A1
DelayMs(Speed);
Coil_AB1
DelayMs(Speed);
Coil_B1
DelayMs(Speed);
Coil_BC1
DelayMs(Speed);
Coil_C1
DelayMs(Speed);
Coil_CD1
DelayMs(Speed);
Coil_D1
DelayMs(Speed);
Coil_DA1
DelayMs(Speed);
}
}
void ISR_INT0(void) interrupt 0
{
if(!INT0)
{
DelayMs(10);
if(!INT0)
{
Speed++;
if(Speed==20)
Speed=3;
TempData[0]=dofly_DuanMa[Speed/10];
TempData[1]=dofly_DuanMa[Speed%10];
}
}
}
void Display(unsigned char FirstBit,unsigned char Num)
{
static unsigned char i=0;
DataPort=0;
LATCH1=1;
LATCH1=0;
DataPort=dofly_WeiMa[i+FirstBit];
LATCH2=1;
LATCH2=0;
DataPort=TempData[i];
LATCH1=1;
LATCH1=0;
i++;
if(i==Num)
i=0;
}
void Init_Timer0(void)
{
TMOD |= 0x01;
EA=1;
ET0=1;
TR0=1;
PT0=1;
}
void Timer0_isr(void) interrupt 1
{
TH0=(65536-2000)/256;
TL0=(65536-2000)%256;
Display(0,8);
}
每次按键调整Speed大小后过一段时间步进机就自动复位初始Speed,这是哪里出错了吗 |