;***************************************************************************
;* File Name		:pwm1.asm
;* Title		:Pulse Width Modulation
;* Author		:C.Kuehnel
;* Date	 		:10/15/97
;* Version 		:1.0
;* Target MCU		:AT90S8515
;* 
;* DESCRIPTION
;* Pulse width modulation by Timer/Counter1
;***************************************************************************	.

;***** Directives
.device	 at90s8515
.nolist
.include "8515def.inc"
.list

; Main Program Register variables
;---------------------------------------------------------------------------
.def 	temp 		= r16


;***** Interrupt vector table

	rjmp	RESET		; Reset handle
	reti			; External Interrupt0 handle
	reti			; External Interrupt1 handle
	reti			; T/C1 Capture Event Interrupt handle
	reti			; T/C1 CompareA Interrupt handle
	reti			; T/C1 CompareB Interrupt handle
	reti			; T/C1 Overflow Interrupt handle
	reti			; T/C0 Overflow Interrupt handle
	reti			; SPI Transfer Complete Interrupt handle
	reti			; UART Rx Complete Interrupt handle
	reti			; UART Data Register Empty Interrupt handle
	reti			; UART Tx Complete Interrupt handle
	reti			; Analog Comparator Interrupt handle


;***** Subroutines

pwm1_init:				; Initializes T/C1 as 10-bit PWM
	ldi	temp, 0b10110011	; Timer/Counter1 is a 10-bit PWM
	out	TCCR1A, temp
	ldi	temp, 0b00000010	; CK/8 => PWM frequency is 500 kHz 
	out	TCCR1B, temp
	ret

pwm1_disable:
	clr	temp			; Disable T/C1
	out	TCCR1A, temp
	ret

;***** Main


RESET:	ldi	temp, LOW(RAMEND)
	out	SPL, temp		; Initialize SPL
	ldi	temp, HIGH(RAMEND)
	out	SPH, temp		; Initialize SPH
	sbi	DDRD, PD5		; PD5 is output

	ldi	temp, $03		; Load the Hi Byte of OCR1X (00..3F)
	out	OCR1AH, temp
	out	OCR1BH, temp

	ldi	temp, $F0		; Load the Lo Byte of OCR1X (00..FF)
	out	OCR1AL, temp
	out	OCR1BL, temp

	rcall	pwm1_init

loop:	rjmp 	loop			; Repeat forever
	