;; filename:	cylon-20090830.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 configured for regular use (the firmware generates 30 uS (at
;;  60 Hz) pulses to drive the SSRs, synchronized to the zero-crossing input
;;  pin).  Here are the DEFINE statements to change for PWM use (generates a
;;  variable width pulse sync'd to the zero-crossing signal) or DC use
;;  generates a variable-width pulse, not sync'd to anything).

;; PWM_build can be either 0 or 1
#define PWM_build 1
;; DC_build can be either 0 or 1 (it should be 0 if PWM_build is 0)
#define DC_build 1

 #define CLOCKRATE 8000000

;; For positive_true outputs, remove the following define (normally, this
;; should be left in place).
 #define OUTPUT_NEGATIVE

 #define DECAY_FACTOR 1
		
;;  --------------------------------------------------------------------------
;;
;;  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.
;;
;;  Hardware mappings
;;  PIN 13 (RA0) - triac driver 1 (output)
;;  PIN 12 (RA1) - triac driver 2 (output)
;;  PIN 11 (RA2) - triac driver 3 (output)
;;  PIN 10 (RC0) - triac driver 4 (output)
;;  PIN 9  (RC1) - triac driver 5 (output)
;;  PIN 8  (RC2) - triac driver 6 (output)
;;  PIN 7  (RC3) - triac driver 7 (output)
;;  PIN 6  (RC4/TX) - NC
;;  PIN 5  (RC5/RX) - NC
;;  PIN 4  (RA3) - NC
;;  PIN 3  (RA4) - triac driver 0 (output)
;;  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, disable_map
	
  second_det_ctr
  direction_flag
  current_map

  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	; decrement 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 outputs off (set to ground)

  #IFDEF OUTPUT_NEGATIVE
	movlw	0xFF
	movwf	PORTA
	movwf	PORTC
  #ELSE
	swapf	disable_map,w	; disable_map tells which outputs to turn off
	movwf	PORTC		; at the zero-crossing time
	
	rrf	disable_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

	movlw	slot_id_ckt2
	movwf	FSR

	movlw	1
	movwf	direction_flag
	
;; configure the I/O ports

	bcf	STATUS,RP0	; select bank 0
	movlw	7
	movwf	CMCON0		; disable the comparators
	clrf	out_map
	
	movlw	0x00
	movwf	disable_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	disable_map
	
loop1:
	btfss	event_flag,0
	goto	loop1

	bcf	event_flag,0
				; now count 150 of these events.

	movlw	0xFF		; reset the current channel back to 255
	movwf	INDF
	
	call	fade		; executed 150 times per second.

	decfsz	second_det_ctr,f	; check if a new LED is just coming on
	goto	loop1		; jump out of the loop if not.
	
	movlw	75
	movwf	second_det_ctr

				; arrive here once per second.
	movf	direction_flag,w; direction_flag == 1 or 255
	addwf	FSR,f

	movlw	0xFE
	btfsc	FSR,3
	xorwf	direction_flag,f

	rlf	direction_flag,w
	andlw	0xFE
	
	btfsc	FSR,3
	addwf	FSR,f
	
	goto	loop1

fade:	
	movlw	0xFF		;
	movwf	disable_map	; start out with all channels enabled
	
	movlw	DECAY_FACTOR
	
	movf	slot_id_ckt0,f
	btfss	STATUS,Z
	subwf	slot_id_ckt0,f
	btfsc	STATUS,Z
	bcf	disable_map,0
	
	movf	slot_id_ckt1,f
	btfss	STATUS,Z
	subwf	slot_id_ckt1,f
	btfsc	STATUS,Z
	bcf	disable_map,1
	
	movf	slot_id_ckt2,f
	btfss	STATUS,Z
	subwf	slot_id_ckt2,f
	btfsc	STATUS,Z
	bcf	disable_map,2
	
	movf	slot_id_ckt3,f
	btfss	STATUS,Z
	subwf	slot_id_ckt3,f
	btfsc	STATUS,Z
	bcf	disable_map,3
	
	movf	slot_id_ckt4,f
	btfss	STATUS,Z
	subwf	slot_id_ckt4,f
	btfsc	STATUS,Z
	bcf	disable_map,4
	
	movf	slot_id_ckt5,f
	btfss	STATUS,Z
	subwf	slot_id_ckt5,f
	btfsc	STATUS,Z
	bcf	disable_map,5
	
	movf	slot_id_ckt6,f
	btfss	STATUS,Z
	subwf	slot_id_ckt6,f
	btfsc	STATUS,Z
	bcf	disable_map,6
	
	movf	slot_id_ckt7,f
	btfss	STATUS,Z
	subwf	slot_id_ckt7,f
	btfsc	STATUS,Z
	bcf	disable_map,7

	return
			
  END
