;********************************************
;filename: IR_bumper                        *
;device: atmega 8                           *
;freequency: 8 MHz                          *
;author: Agruzov                            *
;version:1.00                               *
;data:13.08.07                              *
;********************************************

;functioms: flashes led when TSOP get LED`s signal


.nolist
.include "D:\program files\atmel\avr tools\avrassembler2\appnotes\tn2313def.inc"
.list

;********************************************
; declarations
;********************************************

.def temp=R16
.def num_ovf=R17
.def int_numb=R18
.def int_numb2=R19
.def situation=R20

;********************************************

;PROGRAM START

.org 0
.cseg

;interrupt vectors
   rjmp init
   rjmp int_0
   rjmp int_1
   rjmp tim1_capt
   rjmp timer1_comp
   rjmp timer1_ovf
   rjmp timer0_ovf
   rjmp usart0_rx
   rjmp usart0_udre
   rjmp usart0_tx
   rjmp analog_comp
   rjmp pin_ch_inp
   rjmp timer1_compB
   rjmp timer0_compA
   rjmp timer0_compB
   rjmp usi_start
   rjmp usi_ovf
   rjmp ee_rdy
   rjmp wd_ovf

    
    
    int_1:
    tim1_capt:
    timer1_comp:
    timer1_ovf:
    timer0_ovf:
    usart0_rx:
    usart0_udre:
    usart0_tx:
    analog_comp:
    pin_ch_inp:
    timer1_compB:
    timer0_compB:
    usi_start:
    usi_ovf:
    ee_rdy:
    wd_ovf:

    reti


   ;TSOP signal generation
      timer0_compA:
	  


      push temp
      in temp, sreg
	  push temp        
      
	  
      subi num_ovf, -1
      cpi num_ovf, 100
	  brne away
      
	  clr num_ovf
      
	  
      brts path1

 
      ldi temp, 0b00000010
      out TCCR0A, temp



	  pop temp
	  out sreg, temp
	  pop temp
	  set
	  rjmp GO
	  
       
    path1:

	  ldi temp, 0b01000010
      out TCCR0A, temp

	  pop temp
	  out sreg, temp
	  pop temp
	  clt
      reti

   away:
	  pop temp
	  out sreg, temp
	  pop temp

   GO:
      reti
	   
      


;tsop output signal
  int_0:

  ;!
  sei
  ;!

  push temp
  in temp, sreg 
  push temp
  cli
  ser int_numb
  ser int_numb2
  clr temp
  out gimsk, temp
  ldi situation, 0b00010000
  out portd, situation
  pop temp
  out sreg, temp
  pop temp
  reti





    
  init:

 cli

 clr num_ovf


 ldi temp, 0b00001100
 out DDRB, temp

;!!!!!!!
 ldi temp, 0b00001000
 out PortB, temp

;!!!!!!!


 ldi temp, low(ramend)
 out SPL, temp


 ldi temp, 0b01000010
 out TCCR0A, temp
 
 clr int_numb
 clr int_numb2


 ldi temp, 111
 out OCR0A, temp

 ldi temp, 0b00000001
 out timsk, temp

 ldi temp, 0b00010000
 out DDRD, temp

 clr temp
 out DDRA, temp
 out PORTD, temp

 in temp, MCUCR
 ori temp, 0b00000010
 out MCUCR, temp 

 ldi temp, 0b01000000
 out GIMSK, temp
 
 
 ldi temp, 0b00000001
 out TCCR0B, temp
 sei


 
 Main:

   cpi int_numb,0
   breq main
   mainloop:   
   ser int_numb
   loop:
   subi int_numb,1
   brne loop
   subi int_numb2,1
   brne mainloop
   
   ldi temp, 0b01000000
   out EIFR, temp

   ldi temp, 0b01000000
   out GIMSK, temp
   
   clr situation
   out portd, situation
   
   rjmp main

