stc12c5a60s2控制L298N驱动步进电机,我想使步进电机转45度停止。如何解决啊
#include <stc12C5A60S2.h>
#include <intrins.h>
typedef unsigned char u8;
typedef unsigned int u16;
sbit TCRT1 = P2^2; //颜色识别
sbit TCRT2 = P2^3; //避障识别
sbit buzzer = P1^5; //蜂鸣器
u16 count = 0;
float targetAngle = 45.000; // 目标转动角度为90度
float currentAngle = 0.000; // 当前已转动角度为0
u8 code CCW[8]={0x08,0x0c,0x04,0x06,0x02,0x03,0x01,0x09}; //逆时针旋转相序表
u8 code CW[8]={0x09,0x01,0x03,0x02,0x06,0x04,0x0c,0x08}; //正时针旋转相序表
void IO_Init()
{
P0M1 = 0; P0M0 = 0;
P1M1 = 0; P1M0 = 0;
P2M1 = 0; P2M0 = 0;
}
void delay(unsigned int time) {
unsigned int i, j;
for (i = 0; i < time; i++) {
for (j = 0; j < 10; j++);
}
}
void buzzer_Init()
{
buzzer = 1;
}
void forward()
{
u16 i;
for(i=0;i<8;i++)
{
P1=(P1 & 0xF0) | CW[i];
delay(200);
}
}
void back(){
u16 i;
for(i=0;i<8;i++)
{
P1=(P1 & 0xF0) | CCW[i];
delay(200);
}
}
void stop(){
P1=P1&0xF0;
}
void Motor_Ctrl_Angle(int direction, int angle)
{
u16 j;
if(direction == 1)
{
for(j=0;j<64*angle/45;j++)
{
forward();//正转
}
stop();//停止
}
else
{
for(j=0;j<64*angle/45;j++)
{
back();//反转
}
stop();//停止
}
}
void main()
{
//IO_Init();
buzzer_Init();
/*if(TCRT1 == 1 && TCRT2 == 1) //避障识别到棋子且该棋子为黑子
{
Motor_Ctrl_Angle(0,45);
//Motor_Ctrl_Angle(0,0);
}
else if(TCRT1 == 1 && TCRT2 == 0)
{
Motor_Ctrl_Angle(1,45);
delay(20);
}
else{
buzzer = 0;
delay(2000);
buzzer = ~buzzer;
}*/
Motor_Ctrl_Angle(0,45);
}