/* ---------------------------------------------------------------------------
 * AVR Blink Demo Application
 * www.mlab.cz miho 2008
 * ---------------------------------------------------------------------------
 * Demo program for AVR processors 
 * Simple blinking application for ATtiny13/ATmega8 and similar
 * ---------------------------------------------------------------------------
 * 00.00 2008/04/18 First Version
 * ---------------------------------------------------------------------------
 */


// Configuration
// -------------


// Define LED port here
// Use port available on all devices with no conflict with ISP port
#define LED                     B
#define LED_BIT         3


// Define CPU clock frequency here
// Do not forget to set Clock Oscillator in Low Fuse
// Select just one
//#define F_CPU 1000000UL               // Frequency in Hz
#define F_CPU   F_CPU_DEFAULT   // Default internal RC clock frequency
//#define F_CPU F_CPU_MAX               // Maximum internal RC clock frequency


// Define blink period
#define PERIOD          5000            // in ms
#define DUTY            20                      // in %


// End of Configuration
// --------------------


// Helpers
#define GLUE(a,b)                       a##b
#define PORT(a)                     GLUE(PORT,a)
#define PIN(a)                          GLUE(PIN,a)
#define DDR(a)                          GLUE(DDR,a)


// Port definitions
#define LED_PORT                        PORT(LED)
#define LED_DDR                         DDR(LED)


// Processor definitions
#include <avr/io.h>


// Define clock values for selected processors

// ATtiny13
#if _AVR_IOTN13_H_
#define F_CPU_DEFAULT   1200000UL       // Default clock 9.6MHz/8
#define F_CPU_MAX               9600000UL       // Maximal internal clock
#endif

// ATmega8/16/32/64
#if _AVR_IOM8_H_ | _AVR_IOM16_H_ | _AVR_IOM32_H_ | _AVR_IOM64_H_ \
        | _AVR_IOM88_H_ 
#define F_CPU_DEFAULT   1000000UL       // Default clock 1MHz
#define F_CPU_MAX               8000000UL       // Maximal internal clock
#endif


// Use delay library
#include <util/delay.h>


// Define ON and OFF times on PERIOD and DUTY
#define TIME_ON         (1L*PERIOD*DUTY/100)
#define TIME_OFF        (1L*PERIOD*(100-DUTY)/100)


// Main program
int main()
{

        LED_DDR |=1<<LED_BIT;                   // Set LED port as output

        for(;;)                                                 // Never ending story
        {
                LED_PORT |= 1<<LED_BIT;         // Set LED port to 1
                _delay_ms(TIME_ON);                     // Wait
                LED_PORT &= ~(1<<LED_BIT);      // Clear LED port to 0
                _delay_ms(TIME_OFF);            // Wait
        }
        
        return 0;
}