;=======CCPPWM.ASM=================================11/11/98==
        list    p=16f628
	#include	<p16f628.inc>
	errorlevel	-302
	__CONFIG   _CP_OFF & _DATA_CP_OFF & _LVP_OFF & _BODEN_OFF & _MCLRE_OFF & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT 
 	
        radix   hex
;------------------------------------------------------------
;       cpu equates (memory map)
duty	equ	0x20
mcount  equ	0x21
ncount	equ	0x22
e_addr	equ	0x70
e_value	equ	0x71

;------------------------------------------------------------
;	bit equates
ccp1	equ	3	;ccp1 bit 3, port B
;------------------------------------------------------------
	equ	0
	de	0x7f

        org     0x000
;
start	bsf	STATUS, RP0  ;switch to bank 1
	movlw	b'00000111' ;inputs/outputs
        movwf   TRISA
	bcf	TRISB,ccp1  ;ccp1 pin output
	bcf	STATUS, RP0  ;switch back to bank 0
	movlw	0x7
	movwf	CMCON
	bcf	PORTB,ccp1  ;ccp1 pin low
	clrf	INTCON    ;disable global interrupts
	bsf	STATUS, RP0  ;bank 1
	clrf	PIE1;disable timer 2 interrupts
	bcf	STATUS, RP0   ;bank 0
	clrf	CCP1CON	    ;ccp1 module off
	bsf	STATUS, RP0    ;bank 1
	movlw	0xff	;decimal 255
	movwf	PR2	;load period register
	bcf	STATUS, RP0    ;bank 0
	movlw	0
	movwf	e_addr
	call	_ee_rd
	movwf	duty
	movwf	CCPR1L	;bits 9-2 of duty cycle
	movlw	b'00000001'   ;prescaler = 1:4,
	movwf	T2CON	      ;   tmr2 off
	clrf	TMR2	;clear tmr2
	movlw	b'00001100' ;ccp1 pwm mode, ccp1 module on
	movwf	CCP1CON
	bsf	T2CON,2	;tmr2 on	    
;------------------------------------------------------------
repeat	call	pressck	;look for incr/decr button press
	movlw	0x1	;delay 200 milliseconds
	movwf	mcount
loadn	movlw	0xff
	movwf	ncount
decn	decfsz	ncount,f  ;decrement N
	goto	decn	;again
	decfsz	mcount,f  ;decrement M
	goto	loadn	;again
	goto	repeat
;------------------------------------------------------------
pressck	btfss	PORTA,2 ;decrease button pressed?
	goto	decduty	;yes, decrement duty cycle
	btfss	PORTA,0	;increase button pressed?
	goto	incduty	;yes, increment duty cycle
	return
incduty movf	duty,w	;get duty cycle into W
	sublw	0xf3	;duty cycle=0xff?
	btfsc	STATUS, Z
	return
	incf	duty,f	;increment duty cycle
	movf	duty,w	;get duty cycle into W
	movwf	CCPR1L	;duty cycle into ccp register	
	return		;press check done
decduty	movf	duty,w	;get duty cycle into W
	sublw	0x0c	;duty cycle=0?
	btfsc	STATUS, Z
	return
	decf	duty,f	;decrement duty cycle
	movf	duty,w	;get duty cycle into W
	movwf	CCPR1L	;duty cycle into ccp register
	return		;press check done

	#include	<eeprom.inc>
;
        end
;------------------------------------------------------------
;at blast time, select:
;       memory unprotected
;       watchdog timer disabled (default is enabled)
;       standard crystal (using 4 MHz osc for test) XT
;       power-up timer on
;	brown-out detect on
;============================================================
