/*****************************************************
This program was produced by the
CodeWizardAVR V2.05.0 Professional
Automatic Program Generator
© Copyright 1998-2010 Pavel Haiduc, HP InfoTech s.r.l.
http://www.hpinfotech.com

Project : 
Version : 
Date    : 2018-01-31
Author  : 
Company : 
Comments: 


Chip type               : ATmega128
Program type            : Application
AVR Core Clock frequency: 16.000000 MHz
Memory model            : Small
External RAM size       : 0
Data Stack size         : 1024
*****************************************************/
#include <mega128.h>
#include <delay.h>

#define PWM_HIGH PORTB.0 = 1;
#define PWM_LOW PORTB.0 = 0;

#define DIR_HIGH PORTB.1 = 1;
#define DIR_LOW PORTB.1 = 0;

#define MODE_HIGH PORTB.2 = 1;
#define MODE_LOW PORTB.2 = 0;

#define duty 50 //0 ~ 100

interrupt [TIM0_OVF] void timer0_ovf_isr(void)  // 100us
{
    static unsigned int cnt = 0;
    cnt++;
    if(cnt < duty)
    {
        PWM_HIGH;
    }
    else if(cnt<100)
    {
        PWM_LOW;
    }
    else
    {
        cnt = 0;
    }
    TCNT0 |= 0x38;               
}
void main(void)
{
    DDRB = 0x07;
              
    DIR_HIGH;
    MODE_HIGH;    
    
    TCCR0 = 0x02;
    TCNT0 = 0x38;
    TIMSK = 0x01;
    #asm("sei") 
while (1)
      {
      // Place your code here
           DIR_HIGH;
           delay_ms(500);
           DIR_LOW;
           delay_ms(500);
      }
}
