; Copyright(c) 1999 Noriaki Mitsunaga
; Sample for PWM

MR0	equ	0	; PORTA bit 0: right 0
MR1	equ	1	; PORTA bit 1: right 1
ML1	equ	2	; PORTA bit 2: left 1
ML0	equ	3	; PORTA bit 3: left 0
SPDL	equ	0	; PORTB bit 0: SPD Front left
SPDR	equ	1	; PORTB bit 1: SPD Front right
LEDL	equ	2	; PORTB bit 2: IR LED left
LEDR	equ	3	; PORTB bit 3: IR LED right
FL	equ	4	; PORTB bit 4: front left(0:white, 1:black)
RL	equ	5	; PORTB bit 5: rear left
RR	equ	6	; PORTB bit 6: rear right
FR	equ	7	; PORTB bit 7: front right

	list	p=16f84
	#include p16f84.inc

PWM	equ	0x46
PWML	equ	0x47
PWMR	equ	0x48
MORTORL	equ	0x4c
MORTORR	equ	0x4d

TIMER	equ	0xf5	; near 1kHz
PWMLEV	equ	0xf0	; 15 levels

	org	0
	goto	START

	org 4
interrupt
	movlw	TIMER
	movwf	TMR0

	incf	PWM,f
	btfss	STATUS,Z
	goto	DO_PWM
			; start of PWM new phase
	movlw	PWMLEV
	movwf	PWM
	movfw	MORTORL
	movwf	PWML
	movfw	MORTORR
	movwf	PWMR	

DO_PWM
	incf	PWML,f
	incf	PWMR,f

	movfw	PWML
	btfss	STATUS,Z
	goto	MORTORL_ON
MORTORL_OFF
	movlw	0xff
	movwf	PWML
	bsf	PORTA,ML0
	bsf	PORTA,ML1
	goto	DO_PWM0
MORTORL_ON
	bcf	PORTA,ML0

DO_PWM0
	movfw	PWMR
	btfss	STATUS,Z
	goto	MORTORR_ON
MORTORR_OFF
	movlw	0xff
	movwf	PWMR
	bsf	PORTA,MR0
	bsf	PORTA,MR1
	goto	DO_PWM1
MORTORR_ON
	bcf	PORTA,MR0

DO_PWM1
	
intret
	bcf	INTCON,T0IF	; clear intterrupt
	retfie			; return from intterrupt

START
	clrf	STATUS
 	BSF	STATUS,RP0	; register bank 1
	MOVLW	B'11100000'	; 0が出力,1が入力を意味する
	MOVWF	TRISA	;TRISAにレジスタWのデータ-を移動/A ポートの設定
	MOVLW	B'11110011'	; 0が出力,1が入力を意味する
	MOVWF	TRISB		;TRISBにレジスタWのデータ-を移動/Bポートの設定
	movlw	0x7		; PORTB pull up
				; PSA = TMR0
				; PS=1/256
	movwf	OPTION_REG
	BCF	STATUS, RP0	; register bank 0
	
	MOVLW	B'00011111'
	MOVWF	PORTA		;　停止スタンバイ
	MOVLW	B'11110011'
	MOVWF	PORTB

	movlw	TIMER
	movwf	TMR0
	movlw	0xff
	movwf	PWML
	movwf	PWMR
	movwf	MORTORL
	movwf	MORTORR

	bsf	INTCON,T0IE	; timer 0 intterupt enable
	bsf	INTCON,GIE	; global intterupt enable

	movlw	0xfa
	movwf	MORTORL
	movwf	MORTORR

	goto	$

	end

