/*****************************************************
Chip type : ATtiny13A
AVR Core Clock frequency: 9,600000 MHz
Memory model : Tiny
External RAM size : 0
Data Stack size : 16
*****************************************************/

#include <tiny13a.h>
#include <delay.h>

void ir_stop(){
TCCR0A=0x00;
TCCR0B=0x00;
PORTB.0=0;
DDRB.0=0;
}
void ir_start(){
DDRB.0=1;
TCCR0A=0x42;
TCCR0B=0x01;
}


void shoot(unsigned char byte1,unsigned char byte2,unsigned char byte3, unsigned char bits){
unsigned char i=0,j=0,k=0, bytes[3];
ir_stop();
bytes[0]=byte1;
bytes[1]=byte2;
bytes[2]=byte3;
delay_ms(10);
ir_start();
delay_us(2300);
ir_stop();
for (k=0;k<3;k++){
j=8;
while ((j>0) && (i<bits)){
ir_stop();delay_us(550);ir_start();
if ((bytes[k]>>(j-1)) & 1) delay_us(1150);
else delay_us(550);
ir_stop();
i++;j--;
}
}
ir_stop();
}

void main(void)
{
unsigned char key, tmpkey;
// Crystal Oscillator division factor: 1
#pragma optsize-
CLKPR=0x80;
CLKPR=0x00;
#ifdef _OPTIMIZE_SIZE_
#pragma optsize+
#endif

// Input/Output Ports initialization
// Port B initialization
// Func5=In Func4=In Func3=In Func2=In Func1=In Func0=Out 
// State5=T State4=P State3=P State2=P State1=P State0=0 
PORTB=0x1E;
DDRB=0x01;

// Timer/Counter 0 initialization
// Clock source: System Clock
// Clock value: 9600,000 kHz
// Mode: CTC top=OCR0A
// OC0A output: Toggle on compare match
// OC0B output: Disconnected
TCCR0A=0x42;
TCCR0B=0x01;
TCNT0=0x00;
OCR0A=0x54;
OCR0B=0x00;

GIMSK=0x00;
MCUCR=0x00;

TIMSK0=0x00;

ACSR=0x80;
ADCSRB=0x00;
DIDR0=0x00;
ADCSRA=0x00;

ir_stop();

while (1)
{
tmpkey = ((~PINB>>1) & 0x0F);
delay_ms(10);
key = ((~PINB>>1) & 0x0F);
if (key != tmpkey) key = 0;
if (key>0)
{ 
switch (key){
case 1:shoot(0x83,0x04,0xE8,24);break; //0001 
case 2:shoot(0x83,0x0D,0xE8,24);break; //0010  
case 3:shoot(0x83,0x00,0xE8,24);break; //0011   
case 4:shoot(0x83,0x06,0xE8,24);break; //0100  
case 5:shoot(0x83,0x15,0xE8,24);break; //0101  
case 6:shoot(0x83,0x05,0xE8,24);break; //0110   
case 7:shoot(0x83,0x0B,0xE8,24);break; //0111  
case 8:shoot(0x83,0x09,0xE8,24);break; //1000  
case 9:shoot(0x83,0x0E,0xE8,24);break; //1001   
case 10:shoot(0xA8,0x00,0xE8,24);break; //1010   
case 11:shoot(0xA8,0x01,0xE8,24);break; //1011   
case 12:shoot(0xA8,0x02,0xE8,24);break; //1100     
case 13:shoot(0xA8,0x03,0xE8,24);break; //1101    
case 14:shoot(0xA8,0x04,0xE8,24);break; //1110   
case 15:shoot(0xA0,0x01,0xE8,24);delay_ms(1850);break; //1111  -    1  
}
delay_ms(150);
}

}

}