.NOLIST 
.INCLUDE "m8def.inc" 
.LIST 

.DEF tmp		=r25
.DEF pwm		=r26

.ORG 0 
RJMP init

.ORG 15
rjmp ADCD

Init: 

;****************** 
LDI tmp,LOW(RAMEND) 
OUT SPL,tmp 
LDI tmp,HIGH(RAMEND) 
OUT SPH,tmp 
;****************** 

clr tmp
;out DDRD,tmp
out DDRC,tmp
ldi tmp,255
out DDRB,tmp


ADC_Init:
ldi tmp,0b01100000
out admux,tmp
ldi tmp,0b11101101
out adcsra,tmp


PWM_Init:
clr tmp						
out OCR1AH, tmp
out OCR1BH, tmp
clr pwm				  	
out OCR1AL, pwm    	 	
ldi tmp, 0b10100001      	
out TCCR1A, tmp   			
ldi tmp, $1            		
out TCCR1B, tmp
sei

loop:
rcall delay
rjmp loop


adcd:
push tmp
in tmp,adcl
in tmp,adch
out ocr1AL,tmp
pop tmp
reti

delay:
; ============================= 
;    delay loop generator 
;     25000 cycles:
; ----------------------------- 
; delaying 24990 cycles:
          ldi  R30, $22
WGLOOP0:  ldi  R31, $F4
WGLOOP1:  dec  R31
          brne WGLOOP1
          dec  R30
          brne WGLOOP0
; ----------------------------- 
; delaying 9 cycles:
          ldi  R30, $03
WGLOOP2:  dec  R30
          brne WGLOOP2
; ----------------------------- 
; delaying 1 cycle:
          nop
; =============================
ret
