10/17/2008

AT89S5X STEPPER MOTOR

使用89S5X控制STEPPER MOTOR
MOTOR有點不是很精準 0.0
203步才轉1圈
此例是正轉1圈停0.5S
在反轉1圈



/*
First ver 1.0
for step motor test
motor 14pm-m011-g1st 0.2a 1.8deg 5v
user uln2003a
P1_0 = A
P1_1 = B
P1_2 = /A
P1_3 = /B

51 TO 2003a TO MOTO
P1_0---1 16---MOTO A(yellow)
P1_1---2 15---MOTO B(blue)
P1_2---3 14---MOTO /A(red)
P1_3---4 13---MOTO /B(orange)
8=-v 9=+5v MOTO black=+5v

Brian @Taiwan
*/

#include "AT89X52.H"

void sys_init(void);

int time_ms;
int time_1s;


void main(void)
{
unsigned char step;
char motor[4]={8,4,2,1};
char motor_index;
char dir;

sys_init();

time_ms=0;
time_1s=0;
P0_1=1;
TR0=1;
step=0;
dir=0;
motor_index=0;

while(1)
{
if(time_1s >3)
{
if(step<=203)
{
P1=motor[motor_index];
step++;
if(dir)
{
motor_index++;
if(motor_index>3)
motor_index=0;
}
else
{
motor_index--;
if(motor_index<0)
motor_index=3;
}
}
else
{
while(time_1s!=500);
step=0;
dir=~dir;
}
time_1s=0;

}
}
}

void sys_init(void)
{
//ALL SYSTEM REG SET
IE = 0x82; //USER INTERRUPT TIME0
TMOD = 0x02; //SET TIME 0 MODE
TH0 = 256-200; //SET RELOAD VALUE
TL0 = 256-200; // FAST VALUE
}

void Time0_int(void)interrupt 1
{
// TIME 0 MODE2 AUTORELOAD USER INTERRUPT
// 1000 = 0.1S(100ms), 10000 = 1S(1000ms), 100=0.01S(10ms) ,10=0.001S(1ms)
time_ms++;
if(time_ms==10)
{
time_ms=0;
time_1s++;
}
}

沒有留言: