; **  AT90S8535 Assembly Language File **

               
	.include"c:\avrtools\appnotes\8535def.inc"     

;*************************************************************************		   
;*  Author    : John Banks
;* Company : Technical Support
;*************************************************************************
;* PROGRAM 
;*	ADC3 
;* DESCRIPTION
;* 	Demostrates the use of the ADC in single conversion Mode
;*	using Timer 1 overflow to set when the conversion occurs.
;*	This program uses the STK200 board
;*	Portb 2 will flash when timer1 overflows
;*	Portb 0 will flash when the conversion is happening
;*	PortC  is the output from ADCL
;*	PortD is the output from ADCH
;************************************************************************		   
	
		   
		rjmp Init
		reti			; External 0 interrupt  Vector 
		reti			; External 1 interrupt  Vector 
		reti			; Timer 2 Compare Vector 
		reti			; Timer 2 Overflow Vector 
		reti			; Timer 1 Capture  Vector 
		reti			; Timer1 CompareA  Vector 
		reti			; Timer 1 CompareB  Vector 
		rjmp  TIM1_OVF		; Timer 1 Overflow interrupt  Vector 
		reti			;  Timer 0 Overflow  Vector 
		reti			; SPI  Vector 
		reti			; UART Receive  Vector 
		reti			; UDR Empty  Vector 
		reti			; UART Transmit  Vector 
		reti			; ADC Conversion Complete Vector 
		reti			; EEPROM Ready Vector 
		reti			; Analogue Comparator  Vector 


;*************************************************************************
;* FUNCTION
;*	TIM1_OVF
;* DESCRIPTION
;*	Variables used 
;	R22 to store SREG
;*	R23 as a counter 
;	When r23 = 5 then set bit 6 in ADCSR
;************************************************************************

TIM1_OVF:	nop
		rcall flash2
		in R22,SREG		;Store SREG in R22
		inc R23			;increment r23
		cpi R23,5		;does R23 = 5
		brne No_update		;No - go to no_update
		rcall flash0		;
		clr R23			;Clear R23
		sbi ADCSR,6		;Set Bit 6 in ADCSR

no_update:	nop	
		out SREG,R22
		reti			;
 
;*************************************************************************
;* FUNCTION
;*	INIT 
;* DESCRIPTION
;*	Intialises the :- 
;*	Stack pointer
;*	Comparator OFF to save power
;*	Timer 1 set to  CK/64
;*	Port A for ADC inputs
;*	Port B to ouputs for the LED's on the STK200
;*	Port C to outputs for ADCL	
;*	Port D to outputs for ADCH
;*	ADC set to CK/128 channel 0 used 
;************************************************************************               
INIT:                
		; **** Stack Pointer Setup Code ****
		ldi r16,high(RAMEND)	; Stack Pointer Setup 
		out SPH,r16		; Stack Pointer High Byte 
		ldi r16,low(RAMEND)	; Stack Pointer Setup 
		out SPL,r16		; Stack Pointer Low Byte 
                       
		; **** Timer1 Setup Code ****  
		ldi  r16,$ff		; Timer 1 Initial Value
		out TCNT1L,r16		; Low Byte
		ldi  r16,$ff		; Timer 1 Initial Value
		out TCNT1H,r16		; High Byte
		ldi r16,$00		; Default Value
		out TCCR1A,r16		; Timer1 Control Register A
		ldi  r16,$03		;Timer : CK/64 , Timer Not Cleared on Compare A Match 
		out TCCR1B,r16		; Input Capture Off 
		ldi r16,$04		; Timer Interrupt Enables 
		out TIMSK,r16		;  Timer 1: Overflow, 
                       
		; **** Comparator Setup Code ****  
		ldi r16,$80		; 
		out ACSR,r16		; Comparator Disabled to save power 
                       
		; **** ADC Setup Code ****  
		ldi r16,$87		; ADC Interrupt Disabled, ADC Enabled
		out ADCSR,r16		; ADC Single Conversion Mode, Prescaler:CK/128
		ldi r16,$00
		out ADMUX,r16		; Channel 0 Selected
                       
		; **** Port A Setup Code ****  
		ldi r16,$00		; A2D Inputs
		out DDRA,r16		; Port A Direction Register
		ldi r16,$00		; Init value 
		out PORTA,r16		; Port A value
                       
		; **** Port B Setup Code ****  
		ldi r16,$FF		; I/O Direction Values 
		out DDRB,r16		; Port B Direction Register
		ldi r16,$FF		; Init value 
		out PORTB,r16		; Port B value
                       
		; **** Port C Setup Code ****  
		ldi r16,$FF		; I/O direction values 
		out DDRC,r16		; Port C Direction Register
		ldi r16,$FF		; Init value 
		out PORTC,r16		; Port C value
                       
		; **** Port D Setup Code ****  
		ldi r16,$FF		; I/O Direction Values 
		out DDRD,r16		; Port D Direction Register
		ldi r16,$FF		; Init value 
		out PORTD,r16		; Port D value
		sei			; Enable All Interrupts 

;*************************************************************************
;* FUNCTION
;*	MAIN 
;* DESCRIPTION
;*	Skip if bit 6 set in ADCSR 
;*       If bit not set then jump to ADC_not
;*	 If bit set then :-
;*		Save MCUCR in R5
;*		Save SREG in R4
;*		Set Bit 6 in MCUCR  - Sleep enable bit
;*		Enable Interupts
;*		SLEEP
;*		Read ADCL into 	R20
;*		Read ADCH into R21
;*		out ADCL to Port C 	
;*		Out ADCH to Port D 
;*		Restore MCUCR and SREG from there saved values
;* 		Clear Bit 6 of ADCSR - switch off ADC conversion
;************************************************************************               		
MAIN:		nop
		nop
		sbis ADCSR,6
		rjmp ADC_NOT
		in R5,MCUCR
		in R4,SREG
		ldi R16,$40		
		out MCUCR,R16
		SEI
		SLEEP
		in R20,ADCL
		nop
		nop
		nop
		in R21,ADCH
		nop
		nop
		nop
		out PORTC,R20		
		out PORTD,R21
		out SREG,r4
		out MCUCR,r5
		cbi ADCSR,6
		rcall flash0
ADC_NOT:	nop
		rjmp MAIN




;*************************************************************************
;* FUNCTION
;*	FLASH0 / FLASH2
;* DESCRIPTION
;*	Flash2 will flash when timer0 overflows
;*	Flash0 will light at start off conversion and
;*	Off when finished.
;************************************************************************               

FLASH0:		sbis PINB,0
		sbi  PORTB,0
		sbic PINB,0
		cbi PORTB,0
		ret
		
FLASH2:		sbis PINB,2
		sbi PORTB,2
		sbic PINB,2
		cbi PORTB,2
		ret

