#include "reg51.h"
//Motor
unsigned char code Motor_l[]={0xfe,0xfc,0xfd,0xf9,0xfb,0xf3,0xf7,0xf6};//四相八拍,逆時針
unsigned char code Motor_r[]={0xf7,0xf3,0xfb,0xf9,0xfd,0xfc,0xfe,0xf6};//四相八拍,順時針
unsigned char MotorStep=0;
unsigned int Speed=8,CT=0;
sbit SB1=P2^0;
sbit SB2=P2^1;
sbit SB3=P2^2;
bit a=1,b=1;
unsigned char code discode[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x77,0x7c,0x39,0x5e,0x79,0x71,0x76,0x38,0x73,0x31,0x3e,0x6e,0x40,0x80};
/* 共陰碼:0:0; 1:1; 2:2; 3:3; 4:4; 5:5; 6:6; 7:7; 8:8; 9:9;
10:A; 11:B; 12:C; 13:d; 14:E; 15:F; 16:H; 17:L; 18:P; 19:R;
20:U; 21:Y; 22:-;23:.;*/
//#define speed 8 // 調整速度 數值不要設的太低 低了會引起震動。
void delay(unsigned int i)
{
unsigned int k;
for(k=0;k<i;k++);
}
void system_Ini()
{
TMOD= 0x01;
TH0 =(65536-100)/256;
TL0 =(65536-100)%256;
IE = 0x8A;
TR0 = 1;
}
void main()
{ system_Ini();
P1=0xff;// 初始化馬達
while(1)
{
if(a==0)P0=~discode[Speed-7];
else P0=0xff;
if(SB1==0)
{
delay(1000);
if(SB1==0)
{
while(!SB1);
a=!a;
}}
if(SB2==0)
{
delay(1000);
if(SB2==0)
{
while(!SB2);
b=!b;
}
}
if(SB3==0)
{
delay(1000);
if(SB3==0)
{
while(!SB3);
Speed++;
if(Speed==14)Speed=8;
}
}
if(a==0&&b==1)
P1=Motor_l[MotorStep];
else if(a==0&&b==0)
P1=Motor_r[MotorStep];
else P1=0xff;
}}
void int_tim0(void) interrupt 1
{
TH0 =(65536-100)/256;
TL0 =(65536-100)%256;
if( CT++==Speed)
{MotorStep++;
if(MotorStep==8)MotorStep=0;
CT=0;
}
}
|