/*=======================================================================================
Project : 서보모터 제어하기  
Version : LK-SVM 01
Date    : 2012-01-18
Author  : Kyung Nam Lee
Company : LKEMBEDDED 
Comment : 2초마다 서보모터가 180도 회전 하는 프로그램 예제소스이다.
Compiler: WINAVR + AVRStuio
Connection: ATmega2560확장형 개발보드 10Pin박스커넥터(PORTB) <--------------> LK-SVM V01 10Pin박스커넥터(PORT)
=======================================================================================*/

#include <mega2560.h>
#define max ((1.5+0.8)*10)
#define min ((1.5-0.9)*10)
#define mideum (1.5*10) //서보모터의 90도 위치의 On-Time dury 1.5ms
#define duty 200
bit direction_flag;
char Pulse=0;
// Timer 0 overflow interrupt service routine
interrupt [TIM0_OVF] void timer0_ovf_isr(void) //100uS Tick Time
{

    static unsigned int cnt,cnt2=0;    
    cnt2++;
    cnt++;      
    if(cnt2==20000)// 2초 ?
    {           
       Pulse=min;
       }  
    if(cnt2==40000)// 4초 ?
    {           
       Pulse=max; 
       cnt2=0;
       }
    if(cnt==Pulse)
    {
    PORTB.0=0; 
    PORTB.1=0;
    PORTB.2=0;
    PORTB.3=0;
    }             
    if(cnt==duty)
    {
    PORTB.0=1;
    PORTB.1=1;
    PORTB.2=1;
    PORTB.3=1;
    cnt=0;
    }       

 TCNT0+=0xe7;//100uS Tick Time
}
void main(void)
{

    // Input/Output Ports initialization
    // Port A initialization
    PORTB=0x00;
    DDRB=0xff;

    // Timer/Counter 0 initialization         
    TCCR0A=0x00;//Normal Mode Operation(Overflow Interrupt Enable) 
    TCCR0B=0x03;//64Prescale Select
    TCNT0=0xe7;// Timer/Count Register initialization 100uS Tick Time

    TIMSK0=0x01;// Timer(s)/Counter(s) Interrupt(s) initialization
    #asm("sei")// Global enable interrupts

while (1)
          {

          }
}