/*===========================================================================================
Project : 스테핑 모터 1상 여자 구동 방식으로 구동 하기  
Date    : 2011-07-28
Author  : Kyung Nam Lee
Company : LKEMBEDDED 
Compiler: CodevisionAVR + AVRStudio
Comment : 스테핑 모터 정회전 및 역회전 예제소스 

※※주의사항※※
10P 플랫케이블 연결시 아래와 같이 연결해주세요!
ATmega128-C 확장형 개발보드 10Pin Box커넥터(PORTC) <-------------> 스테핑모터 모듈 - 10Pin Box커넥터(PORT)
===========================================================================================*/

#include <mega2560.h>
#include <delay.h>
char cw[4]={0x11,0x44,0x22,0x88};
char ccw[4]={0x88,0x22,0x44,0x11};
bit Flag;
int Dir;
int Pulse_cnt;
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void)
{

    static int cnt,cnt2=0;
    cnt++;cnt2++;
    if(cnt==2)//2ms마다 
    { 
        Pulse_cnt++; // Pulse Count!
        if(Pulse_cnt==4)
        {
        Pulse_cnt=0;
        }
        cnt=0;
    }     
    if(cnt2==3000)
    {
		Dir^=1;    
        cnt2=0;
    }
    TCNT0+=0x06;
}

// Declare your global variables here

void main(void)
{
	int i=0;  //사용자 변수 clear

	PORTC=0x00; // PORTC 0~7핀 출력으로 설정 
	DDRC=0xff;

    TCCR0A=0x00;//Normal Mode Operation(Overflow Interrupt Enable) 
    TCCR0B=0x03;//64Prescale Select
    TCNT0=0x06;// Timer/Count Register initialization

    TIMSK0=0x01;// Timer(s)/Counter(s) Interrupt(s) initialization
    #asm("sei")// Global enable interrupts

while (1) //endless Loop
      {
	 switch(Dir)
     {
     case 0:PORTC=ccw[Pulse_cnt];break; //forward direction
     case 1:PORTC=cw[Pulse_cnt];break; //reverse direction      
     }
  }
}
