/******************************************************************************
 Title:    interrupt driven serial tx for tiny26
 

 tiny26 running at 8Mhz,
 
 
 Author:   rue_mohr
 Date:     Jan 8-14 2006
 Software: AVR-GCC 3.3 
 Hardware: attiny26
 
 
 PB0    Start Button
 PB1    Relay Control
 PB2
 PB3
 PB4
 RESET
    
*******************************************************************************/
#include<avr/io.h>
#include<avr/interrupt.h>

#define OUTPUT                   1
#define INPUT                    0

// Bit positions

#define SetBit(BIT, PORT)        PORT |= (1<<BIT)
#define ClearBit(BIT, PORT)      PORT &= ~(1<<BIT)
#define DefBit(BIT, PORT, VALUE) PORT &= (~(1<<BIT))|((VALUE==0?0:1)<<BIT)
#define IsHigh(BIT, PORT)       (PORT & (1<<BIT)) != 0
#define IsLow(BIT, PORT)        (PORT & (1<<BIT)) == 0
#define NOP()                    asm volatile ("nop"::)

void timerInit() ;
volatile unsigned int timeCounter;

#define outputOn()     SetBit(1, PORTB)
#define outputOff()    ClearBit(1, PORTB)
#define button()       IsLow(0, PINB)


int main (void)  {
 
       
  DDRB = (INPUT << PB0 | OUTPUT << PB1 | INPUT << PB2 | INPUT << PB3 | INPUT << PB4 | INPUT << PB5 );
  
  SetBit(0, PORTB); // button pullup on.
  
  outputOff();
  timeCounter = 0;  
  timerInit() ;
  sei();
             	         
  while(1) {   
    if (button()) timeCounter = 8450;    
    if (timeCounter) { outputOn(); 
    } else {           outputOff();  
    }
  }
 
}
    
//------------------------| FUNCTIONS |------------------------


void timerInit() {

  //set up bit timer 
  TCCR0B = (5<<CS00);  // 1.2Mhz / 1024   about 4.6Hz, so count to 8240
  TIMSK0 |= (1 << TOIE0);  

}


ISR( TIM0_OVF_vect ) {    

  if (timeCounter) timeCounter--;
  
}
