

NumChannels	equ	8	
			
PWMCounter:	ds	1		;Continuous counter
PWMOutput:	ds	1		;Staging register for the output port
			
PWMArray:		ds	8		;the 8 PWM settings you want
			
timerisr:		push	acc		;Save registers about to be thrashed
		push	psw	
		push	r0	
		push	r2	
			
		inc	PWMCounter	;Bump the counter
		mov	a,PWMCounter	;Read it for later use
		mov	r0,#PWMArray	;First PWM setting
		mov	r2,#NumChannels	;8 channels
			
PWMLoop:		cjne	a,@r0,dummy	;See if setting is bigger than counter
dummy:		xch	a,PWMOutput	;Avoid pushing as that costs cycles
		rlc	a		;Rotate in the result
		xch	a,PWMOutput	;Restore accu and store result
		inc	r0		;Next setting
		djnz	r2,PWMLoop	;Do all channels
			
		mov	p1,PWMOutput	;Output to port
			
		pop	r2		;Restore registers
		pop	r0	
		pop	psw	
		pop	acc	
		reti			;Done
