;***************************************************************************
;* File Name		:pwm0.asm
;* Title		:Pulse Width Modulation 
;* Date	 		:23/08/97
;* Version 		:1.0
;* Target MCU		:AT90S1200
;* 
;* DESCRIPTION
;* Pulse width modulation by Timer/Counter0
;***************************************************************************

;***** Directives

.device at90s1200		; Device is AT90S1200
.nolist
.include "1200def.inc"
.list
.listmac

.def	temp 	= r16
.def	byte 	= r17
.def	reload	= r18

;***** Interrupt vector table

	rjmp	RESET		; Reset handle
	reti			; External Interrupt0 handle
	rjmp	OVF0		; Overflow0 Interrupt handle
	reti			; Analog Comparator Interrupt handle

;***** Interrupt handlers

OVF0:	brts	high		; Test T Flag
low:	cbi	PORTB, PB0	; Clear PB0
	mov	reload, byte
	out	TCNT0, reload	; Reload Timer/Counter0
	tst	byte		; Test Reload Value for Zero
	breq	low1
	set			; Set T Flag if Reload # Zero
low1:	reti

high:	sbi	PORTB, PB0	; Set PB0
	neg	reload		
	out	TCNT0, reload	; Reload Timer/Counter0
	clt			; Clear T Flag
	reti

;***** Main

RESET:	ldi	byte, $80
	mov	reload,byte	; Initialize Reload Value
	
	sbi	DDRB,PB0	; PB0 = output

	ldi	temp, 0b00000010
	out	TCCR0, temp	; Prescaler CK/8 => 2 us
	
	ldi	temp, TOV0<<1
	out	TIMSK, temp 	; T/C0 Interrupt Enable

	sei			; Global Interrupt Enable
	
loop:	rjmp 	loop		; Repeat forever
	

