|
发表于 2010-8-10 21:21:00
|
显示全部楼层
#include <stc11f02.h> //2010年4月20 先点亮LED,然后在驱动步进电机
#define uchar unsigned char
#define uint unsigned int
//sfr WDT_CONTR =0xc1;
sbit led_red = P1^6; //sbit KAI = P3^2; 1为亮,0为不亮
sbit led_blue = P1^7; //sbit TING = P3^3; 1为亮,0为不亮
sbit led_green = P1^5; //1为亮,0为不亮
sbit S1 = P1^4;
sbit S2 = P1^3;
sbit S3 = P1^2;
sbit S4 = P1^1;
sbit vin = P3^7; //声控0为LM358音频输入,1为无音频输入
uchar motor_speed;
uchar motor_step;
uchar flag;
void init(void);
void delay500ms(void)
{
unsigned char j,k;
for(j=40;j>0;j--)
for(k=42;k>0;k--);
}
static void time0(void) interrupt 1 using 1 //T0中断函数
{
TH0 = 0x50; /*计数初值重装*/
TL0 = 0x00;
WDT_CONTR = 0x3c;
motor_speed++; //重要点注意了,此参数决定电机的速度和步进
led_red = !led_red;
}
static void time1(void) interrupt 3 using 2 //T0中断函数
{
TH1 = 0x00; /*计数初值重装*/
TL1 = 0x00;
WDT_CONTR = 0x3c;
if(vin == 0)
{
flag = !flag;
}
led_blue = !led_blue;
}
void motor_stop(void)
{
S1 = 0;
S2 = 0;
S3 = 0;
S4 = 0;
WDT_CONTR = 0x3c;
}
void phasicA(void)
{
S1 = 1;
S2 = 0;
S3 = 0;
S4 = 0;
}
void phasicB(void)
{
S1 = 0;
S2 = 1;
S3 = 0;
S4 = 0;
}
void phasicC(void)
{
S1 = 0;
S2 = 0;
S3 = 1;
S4 = 0;
}
void phasicD(void)
{
S1 = 0;
S2 = 0;
S3 = 0;
S4 = 1;
}
void motor_right(uchar speed)
{
if(motor_speed >= speed)
{
motor_speed = 0;
motor_step++;
}
switch(motor_step)
{
case 0:
phasicA();
break;
case 1:
phasicB();
break;
case 2:
phasicC();
break;
case 3:
phasicD();
break;
default:
motor_step = 0;
break;
}
}
void motor_left(uchar speed)
{
if(motor_speed >= speed)
{
motor_speed = 0;
motor_step++;
}
switch(motor_step)
{
case 0:
phasicD();
break;
case 1:
phasicC();
break;
case 2:
phasicB();
break;
case 3:
phasicA();
break;
default:
motor_step = 0;
break;
}
}
main()
{
init();
led_green = 1;
//led_red = 0;
motor_stop();
WDT_CONTR = 0x3c;
while(1)
{
if(flag)
{
motor_left(1);
}
else
{
motor_right(1);
}
WDT_CONTR = 0x3c;
}
}
void init(void)
{
P1M0 = 0x00;
P1M1 = 0x00;
P2M0 = 0x00;
P2M1 = 0x00;
P3M0 = 0x00;
P3M1 = 0x00;
P1 = 0x00;
delay500ms();
TMOD = 0x11; /*T0工作在定时器方式1*/
TH0 = 0xcb; /*预置计数初值*/
TL0 = 0xeb;
TH1 = 0xcF;
TL1 = 0xcA;
PCON = 0x00;
SCON = 0x9C;
EA = 1; //CPU开中断
ET0 = 1; /*T0开中断*/
ET1 = 1;
TR0 = 1; /*启动T0*/
TR1 = 1;
} |
|