#include "My_Arduino_GPIO_Lib_V1_8.h"
#define A 8 // 1
#define B 9 // 2
#define A_ 10 // 4
#define B_ 11 // 8
char mode =1; //1: 1상, 2: 2상, 3: 1-2상
char *p;
// 1상 여자
const char st_1ph_cw[] = {0x01, 0x02, 0x04, 0x08};
const char st_1ph_ccw[] = {0x08, 0x04, 0x02, 0x01};
// 2상 여자
const char st_2ph_cw[] = {0x03, 0x06, 0x0c, 0x09};
const char st_2ph_ccw[] = {0x09, 0x0c, 0x06, 0x03};
// 1-2상 여자 = 1상 + 2상 여자를 합친것
// 1상, 2상, 1상, 2상, 1상, 2상, 1상, 2상
const char st_1_2ph_cw[] = {0x01, 0x03, 0x02, 0x06, 0x04, 0x0c, 0x08, 0x09};
const char st_1_2ph_ccw[] = {0x09, 0x08, 0x0c, 0x04, 0x06, 0x02, 0x03, 0x01};
int delayTime = 2;
#define CW 1
#define CCW 2
void st_motor(char mode, char dir) // dir : 1 = cw, 2 = ccw
{
if(mode == 3) // 1-2상 여자방식
{
// 1-2상 여자방식
if(dir == CW) p = st_1_2ph_cw;
else p = st_1_2ph_ccw;
for(int k = 0; k<= 50; k++) // 1024
{
for(int i = 0; i<= 7; i++)
{
PORTB = p[i];
delay(delayTime);
}
}
}
else // 1상, 2상여자 방식
{
if((mode == 1) && (dir == CW)) p = st_1ph_cw;
else if((mode == 1) && (dir == CCW)) p = st_1ph_ccw;
else if((mode == 2) && (dir == CW)) p = st_2ph_cw;
else if((mode == 2) && (dir == CCW)) p = st_2ph_ccw;
for(int k = 0; k<= 50; k++) // 512
{
for(int i = 0; i<= 3; i++)
{
PORTB = p[i];
delay(delayTime);
}
}
}
}
void setup() {
// put your setup code here, to run once:
pinMode(A, OUTPUT);
pinMode(B, OUTPUT);
pinMode(A_, OUTPUT);
pinMode(B_, OUTPUT);
d_out(A, 0);
d_out(B, 0);
d_out(A_, 0);
d_out(B_, 0);
}
void loop() {
// put your main code here, to run repeatedly:
// 실습 2
st_motor(3, CW); // 1상여자 cw
delay(1000);
st_motor(3, CCW); // 1상여자 ccw
delay(1000);
// 실습 1
// st_motor(1, CW); // 1상여자 cw
// delay(500);
// st_motor(2, CCW); // 2상여자 ccw
// delay(500);
// st_motor(3, CW); // 1-2상여자 cw
// delay(500);
}