;; filename:	simple_flasher-20111020.asm
;; Copyright (c) 2006,2007,2009 Phil Short
;;
;;  This program is provided free for you to use in any way that you wish,
;;  subject to the laws and regulations where you are using it.  Due diligence
;;  is strongly suggested before using this code.
;;
;;  The Author makes no warranty of any kind, express or implied, with regard
;;  to this program or the documentation contained in this document.  The
;;  Author shall not be liable in any event for incidental or consequential
;;  damages in connection with, or arising out of, the furnishing, performance
;;  or use of these programs.
;;
;;  --------------------------------------------------------------------------
;;  NOTE:
;;  This code is set up for DC operation only, driving LEDs either directly
;;    or through something like a ULN2803 (recommended).
	
 #define CLOCKRATE 8000000

;; For positive_true outputs, remove the following define (normally, this
;; should be left in place).
 #define OUTPUT_NEGATIVE
;; DECAY_FACTOR determines how long it takes the outputs to 'decay' to zero
;;   when they are turned off.  A value of zero means that it never decays
;;   (i.e. lights always stay on once they are turned on), a value of 255 means
;;   the outputs decay (turn off) immediately, values in between means that the
;;   outputs slowly turn off.  The values of DECAY_FACTOR must be a factor of
;;   255.  A larger number means that it decays faster...

 #define DECAY_FACTOR 5
		
;;  --------------------------------------------------------------------------
;;
;;  30 Aug, 2009  PJS Started from Renard-20071229.asm by stripping out all
;;  code related to AC operation and serial operation, and adding code for
;;  moving the eyes from left to right.
;;  20 Oct, 2011  PJS Modified to be a simple flasher.
;;
;;  Hardware mappings
;;  PIN 13 (RA0) - output driver 1
;;  PIN 12 (RA1) - output driver 2
;;  PIN 11 (RA2) - output driver 3
;;  PIN 10 (RC0) - output driver 4
;;  PIN 9  (RC1) - output driver 5
;;  PIN 8  (RC2) - output driver 6
;;  PIN 7  (RC3) - output driver 7
;;  PIN 6  (RC4/TX) - NC
;;  PIN 5  (RC5/RX) - NC
;;  PIN 4  (RA3) - NC
;;  PIN 3  (RA4) - output driver 0
;;  PIN 2  (RA5) - NC
;;

 LIST R=DEC
 INCLUDE "p16f688.inc"

  __CONFIG _FCMEN_OFF & _IESO_OFF & _BOD_OFF & _CPD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTOSCIO

 CBLOCK 0x020
;; variables used by the interrupt service routine (ISR)
;;
;; These eight slot_id_ckt registers must be at 0x20-0x27
;;
  slot_id_ckt0, slot_id_ckt1, slot_id_ckt2, slot_id_ckt3	
  slot_id_ckt4, slot_id_ckt5, slot_id_ckt6, slot_id_ckt7	

  _w, _status

  ctr_ckt0, ctr_ckt1, ctr_ckt2, ctr_ckt3
  ctr_ckt4, ctr_ckt5, ctr_ckt6, ctr_ckt7

  ctr_timeslot
  out_map, full_bright_map
	
  second_det_ctr
  set_level

  event_ctr
	
  event_flag
	
 ENDC	

;; assigned numbers:
;;
;; ISR triggers once every 52 clocks, or at 38.400 KHz (approx), and the
;; LED refresh rate is 150 Hz (38,400 / 256)
	
 #define PWM_SLOT_CONSTANT (2-52)

 #define TEST_STROBE PORTA,0
;; ---------------------------------------------------------------------------

 org 0
	nop
	goto initialize

 org 4
	;; interrupt routine
	;; Just the timer interrupt for now...
Int
	movwf	_w		; save 'w'
	movf	STATUS, w	; src, dst
	movwf	_status		; save f to _status

	bcf	INTCON,T0IF	; reset the interrupt
	
	movlw	PWM_SLOT_CONSTANT
	addwf	TMR0,f

	incfsz  ctr_timeslot,f	; update slot time counter.
	goto	do_regular
;;
;; ISR code executed if counter expires
;;
;;
;; Initialize the ctr_ckt registers for upcoming half-cycle
;;
	movf	slot_id_ckt0,w
	movwf	ctr_ckt0
	
	movf	slot_id_ckt1,w
	movwf	ctr_ckt1
	
	movf	slot_id_ckt2,w
	movwf	ctr_ckt2
	
	movf	slot_id_ckt3,w
	movwf	ctr_ckt3
	
	movf	slot_id_ckt4,w
	movwf	ctr_ckt4
	
	movf	slot_id_ckt5,w
	movwf	ctr_ckt5
	
	movf	slot_id_ckt6,w
	movwf	ctr_ckt6
	
	movf	slot_id_ckt7,w
	movwf	ctr_ckt7

;; turn on outputs that are at full brightness, all others off.

  #IFDEF OUTPUT_NEGATIVE
	swapf	full_bright_map,w
	xorlw	0xFF
	movwf	PORTC

	rrf	full_bright_map,w
	xorlw	0xFF
	andlw	0x07
	btfss	STATUS,C
	iorlw	0x10
	movwf	PORTA
  #ELSE
	swapf	full_bright_map,w	; full_bright_map tells which outputs to turn off
	movwf	PORTC		; at the zero-crossing time
	
	rrf	full_bright_map,w
	andlw	0x7
	btfsc	STATUS,C
	iorlw	0x10
	movwf	PORTA

  #ENDIF
	bsf	event_flag,0

	goto	isr_exit

;;
;; handle the normal case (next timeslot when no zero-crossing, no timeout).
;;
do_regular:

	clrf	out_map		; map of outputs
			
	incfsz	ctr_ckt0,f
	bsf	out_map,3	; turn off this SSR
	incfsz	ctr_ckt1,f
	bsf	out_map,0
	incfsz	ctr_ckt2,f
	bsf	out_map,1
	incfsz	ctr_ckt3,f
	bsf	out_map,2
	incfsz	ctr_ckt4,f
	bsf	out_map,4		;
	incfsz	ctr_ckt5,f
	bsf	out_map,5
	incfsz	ctr_ckt6,f
	bsf	out_map,6
	incfsz	ctr_ckt7,f
	bsf	out_map,7
  #IFDEF OUTPUT_NEGATIVE
	swapf	out_map,w	; a '1' indicates that an LED s/b on.
	andwf   PORTC,f
	movf	out_map,w
	andlw	0x0F
	addlw	8
	andwf	PORTA,f
  #ELSE
	swapf	out_map,w	; a '1' indicates that an LED s/b on.
	xorlw	0xFF
	iorwf   PORTC,f
	comf	out_map,w
	andlw	0x0F
	addlw	8
	iorwf	PORTA,f
  #ENDIF
isr_exit:	

	movf	_status, w
	movwf	STATUS
	swapf	_w,f
	swapf	_w,w
	retfie

;;
;; End of Interrupt Routine, start of Foreground code.
;;

initialize:
;;
;; initialize some registers
;;
	clrf	slot_id_ckt0	; set all 'lamps' to lowest intensity
	clrf	slot_id_ckt1
	clrf	slot_id_ckt2
	clrf	slot_id_ckt3
	clrf	slot_id_ckt4
	clrf	slot_id_ckt5
	clrf	slot_id_ckt6
	clrf	slot_id_ckt7

	clrf	ctr_timeslot	; used by ISR - rolls over 150 times per second

;; configure the I/O ports

	bcf	STATUS,RP0	; select bank 0
	movlw	7
	movwf	CMCON0		; disable the comparators
	clrf	out_map
	
	movlw	0x00
	movwf	full_bright_map	; value to turn off outputs at zc time

	movwf	PORTA
	movwf	PORTC

	bsf	STATUS,RP0

	movlw	0x74
	movwf	OSCCON ^ 0x80
	
	movlw	0x20		; bit 5 is the clock input bit...just in case
	movwf	TRISA ^ 0x80	; h/w ignores bit 3 of this register

	movlw	0xF0		; upper four bits are tri-stated for uart op.
	movwf	TRISC ^ 0x80		; 
	clrf	ANSEL ^ 0x80
	bcf	OPTION_REG ^ 0x80,7	; global weak-pullup enable
	movlw	0x20		; turn on weak pullup for unused input (RA5)
	movwf	WPUA ^ 0x80

;; Set up timer0 - generate periodic interrupt at 512 times powerline freq

	movlw	0x08		; assign prescaler to WDT (i.e. not used)
	movwf	OPTION_REG ^ 0x80; clear all bits -> timer mode, clk/2
	bcf	STATUS,RP0

	movlw	PWM_SLOT_CONSTANT
	movwf	TMR0
	movlw	(1 <<GIE) | (1<<T0IE)
	movwf	INTCON		; enable interrupts

	movlw	0x00
	movwf	full_bright_map

	movlw	255
	movwf	set_level

	movlw	75
	movwf	second_det_ctr

loop1:
	btfss	event_flag,0	; ISR will set this flag 150 times/second.
	goto	loop1		; wait till bit 0 of event_flag is set.

	bcf	event_flag,0	; now count 150 of these events

;; ---------------------------------------------------------------------------
;;
;; The logic between the two dashed lines is what determines the light pattern
;;
;; This sample code turns all of the LEDs on for 1 second, then allows them to
;;   all decay to full OFF, waits 1.5 seconds and then restarts the process.

	call	fade

	movf	set_level,w

	btfsc	STATUS,Z
	goto	set_done

;; the numbers moved into the slot_id_ckt variables must be either 0 or 255.

	movwf	slot_id_ckt0	; otherwise, set brightness of each LED to full
	movwf	slot_id_ckt1
	movwf	slot_id_ckt2
	movwf	slot_id_ckt3
	movwf	slot_id_ckt4
	movwf	slot_id_ckt5
	movwf	slot_id_ckt6
	movwf	slot_id_ckt7

set_done:	     ; see if we're at the end of half-second interval

	decfsz	second_det_ctr,f ; 
	goto	end_loop	; jump out of the loop if not.

	movlw	75
	movwf	second_det_ctr

;; arrive here twice per second

	decfsz	event_ctr,f
	goto	end_loop

	comf	set_level,f	; go from full brightness to off.

	movlw	2		; 1 second on,
	movf	set_level,f
	btfsc	STATUS,Z	; skip an instruction if set_level is non-zero
	movlw	3		; set_level is zero, so make it 1.5 seconds off
	movwf	event_ctr
	
;; ----------------------------------------------------------------------------
end_loop:
	call	set_full_bright_map
	
	goto	loop1

;;
;; Fades all of the LEDs that are turned on, will normally be called 150 times
;;   per second (the PWM rate). The constant DELAY_FACTOR determines how fast
;;   the decay occurs, with 1 being the slowest decay.  Note that DELAY_FACTOR
;;   must be a factor of 255 (i.e. 1, 3, 5, 15, etc).  Set it to 255 for the
;;   fastest rate (i.e. decays instantaneously).
;;
fade:	
	movlw	DECAY_FACTOR
	
	movf	slot_id_ckt0,f
	btfss	STATUS,Z
	subwf	slot_id_ckt0,f
	
	movf	slot_id_ckt1,f
	btfss	STATUS,Z
	subwf	slot_id_ckt1,f
	
	movf	slot_id_ckt2,f
	btfss	STATUS,Z
	subwf	slot_id_ckt2,f
	
	movf	slot_id_ckt3,f
	btfss	STATUS,Z
	subwf	slot_id_ckt3,f
	
	movf	slot_id_ckt4,f
	btfss	STATUS,Z
	subwf	slot_id_ckt4,f
	
	movf	slot_id_ckt5,f
	btfss	STATUS,Z
	subwf	slot_id_ckt5,f
	
	movf	slot_id_ckt6,f
	btfss	STATUS,Z
	subwf	slot_id_ckt6,f
	
	movf	slot_id_ckt7,f
	btfss	STATUS,Z
	subwf	slot_id_ckt7,f

	return

set_full_bright_map:
	clrf	full_bright_map

	incfsz	slot_id_ckt0,w
	bsf	full_bright_map,0

	incfsz	slot_id_ckt1,w
	bsf	full_bright_map,1

	incfsz	slot_id_ckt2,w
	bsf	full_bright_map,2

	incfsz	slot_id_ckt3,w
	bsf	full_bright_map,3

	incfsz	slot_id_ckt4,w
	bsf	full_bright_map,4

	incfsz	slot_id_ckt5,w
	bsf	full_bright_map,5

	incfsz	slot_id_ckt6,w
	bsf	full_bright_map,6

	incfsz	slot_id_ckt7,w
	bsf	full_bright_map,7
	
	comf	full_bright_map,f
	return
	
  END
