2013年7月7日 星期日

8051 step motor with LCD



#include "reg52.h"  
#define Fosc 11059200
#define LEDOff LEDPort = 0x00;
#define LEDPort P0
#define LEDigiCon P2
#define MotorPort P1
#define Step4Cycle 48
#define uchar unsigned char
#define uint unsigned int
bit Finish=0;
bit Move1Step=0;
bit TxEnd=1;
char StepControl=0;
static unsigned char DisplayLocation=0;
static unsigned char IntTimes = 0;
uchar Forward=1;
uchar Reverse=1;
uchar TurnsPerMinute=60;
uint ForwardSteps;
uint ReverseSteps;
unsigned char Ascii[8]={'2','7','3','0','6','7','7','6'};
unsigned char Digi[8]={2,7,3,0,6,7,7,6};
unsigned char LightLevel=3, DigiPoint = 0x90;
unsigned char TempValue;
unsigned char code Data1[][3]={{20,20,20},{30,30,30},{40,40,40},{45,45,60}};
unsigned char code Seg7[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7c,0x07,0x7f,0x67};
void Show01234567onLed(void);
void StepMotorExe(void) ;
void StepMotorRun(void);

void main() 
{
   IE=0x92;
   TMOD=0x21;
   SCON=0xC0;
   TCON=0x50;
   TH1=253;
   TH0=(65536-576)/256;
   TL0=(65536-576)%256;
   TL1=253;
   LEDOff ;


   while(1) {
Digi[2]=11;
Digi[5]=11;
MotorPort=0xf0;
ET2=1;
EA=1;
StepMotorRun();

   SBUF='T'; TxEnd=0;
   } //while
}//main



void ET0_int(void) interrupt 1
{
   TH0=(65536-576)/256;
   TL0=(65536-576)%256;
   Show01234567onLed();
}
void Show01234567onLed(void)
{
if(++IntTimes !=4)
{
switch(LightLevel) //light level control
{
case 0:
if (IntTimes == 1 ) LEDOff ;
case 1:
if (IntTimes == 2 ) LEDOff ;
case 2:
if (IntTimes == 3 ) LEDOff ;
}
return;
}
LEDOff;
IntTimes=0;
if (++DisplayLocation == 8) DisplayLocation = 0;
if (DigiPoint == DisplayLocation)
{
LEDPort = Digi[DisplayLocation];  // LEDPort is P0
LEDPort = Seg7[LEDPort];  // Display 1 - 7 Character
LEDPort |= 0x80 ; //add digi point
else
{
LEDPort = Digi[DisplayLocation];
LEDPort = Seg7[LEDPort];
}
TempValue = DisplayLocation <<2 ;
LEDigiCon &= 0xe3;  // clear p22-24 for DisplayLocation
LEDigiCon |= TempValue; // update p22-24 for DisplayLocation
}

void StepMotorRun(void)
{
int m ;
TR2=1;
for (m=0;m<sizeof(Data1)/3;m++){
SBUF='T';   
ForwardSteps=Data1[m][0];  ReverseSteps=Data1[m][1]; TurnsPerMinute=Data1[m][2]; StepMotorExe();}
MotorPort=0xf0;TR2=0;
}

void  StepMotorExe(void)  
{
float Timer4Step;
Timer4Step=Fosc/12/((float)TurnsPerMinute/60*Step4Cycle);
Digi[6]=TurnsPerMinute/10; 
Digi[7]=TurnsPerMinute%10;  // Display the speed on Led
Ascii[6]=TurnsPerMinute/10+0x30; 
Ascii[7]=TurnsPerMinute%10+0x30;  // Display the speed on Led on SCON
RCAP2H=(65536-Timer4Step/20)/256;
RCAP2L=(65536-(uint)Timer4Step/20)%256;
Finish=0;
while(!Finish)
{
if (Move1Step)
{
Move1Step=0;
if (ForwardSteps !=0){ForwardSteps--;
Digi[0]=ForwardSteps/10; 
Digi[1]=ForwardSteps%10;
Ascii[0]=ForwardSteps/10+0x30; 
Ascii[1]=ForwardSteps%10+0x30;
if(++StepControl>3) StepControl=0;}
else if (ReverseSteps !=0){ReverseSteps--;
Digi[3]=ReverseSteps/10; 
Digi[4]=ReverseSteps%10;
Ascii[3]=ReverseSteps/10+0x30; 
Ascii[4]=ReverseSteps%10+0x30;
if(--StepControl<0) StepControl=3;} else Finish=1; // finish =1 means this workingStep is Finish
switch (StepControl)  // The Step Motor Runing Pulse
{
case 0:MotorPort = 0xf3; break;
case 1:MotorPort = 0xf9; break;
case 2:MotorPort = 0xfc; break;
case 3:MotorPort = 0xf6; break;
}//switch
} //Move1Step
} //while(!Finish)
}

void Time2Int(void) interrupt 5
{static int Count = 0;TF2 = 0;if (Count++ == 19){Move1Step=1;Count=0; }}



void SCON_int(void) interrupt  4 {
if (TI)  // UART Transfer 
{
static char i = 0;
TI = 0; 
if (i > 8) {i= 0;TxEnd = 1; SBUF='T';}
else
{
switch (i)
{
case 0:SBUF = Ascii[0]; break;
case 1:SBUF = Ascii[1]; break;
case 2:SBUF = Ascii[2]; break;
case 3:SBUF = Ascii[3]; break;
case 4:SBUF = Ascii[4]; break;
case 5:SBUF = Ascii[5]; break;
case 6:SBUF = Ascii[6]; break;
case 7:SBUF = Ascii[7]; break;
}
i++;
}


}  //if(TI) UART Transfer



} //interrupt 4

沒有留言:

張貼留言