;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ; SpectraBloom Code, Version 0 ; ; Written by Will Schorre ; ; Oct-Nov 2019 Baltimore, MD startup code and buttons ; Feb 2020 Animation mode begun ; July-Aug 2020 added Conrad mode instead of animation mode ; Sept 2020 merged modes, in Conrad mode the buttons speed up the fading ; ; Code for the 12f1571 to be used in the Spectrabloom project. ; compile with mpasmx 2.10 ; ; Chromaccord.net Lorre-mill.com/spectrabloom ; ; ; ; pin 1 VDD ; pin 2 Button ; pin 3 Button ; pin 4 Button ; pin 5 PWM Output ; pin 6 PWM Output ; pin 7 PWM Output ; pin 8 VSS ; ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; ; ::::::::::NOTICE::::::::::: ; ; This code may be used freely for personal use, enjoy! ; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; #include "p12f1571.inc" ; CONFIG1 ; __config 0xFFA4 __CONFIG _CONFIG1, _FOSC_INTOSC & _WDTE_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOREN_ON & _CLKOUTEN_OFF ; CONFIG2 ; __config 0xDFFF __CONFIG _CONFIG2, _WRT_OFF & _PLLEN_OFF & _STVREN_ON & _BORV_LO & _LPBOREN_OFF & _LVP_OFF ;EACH COLOR REGISTER (redF, grnF, bluF) HAS THESE BITS ASSOCIATED WITH THESE FUNCTIONS ; BIT7 ; BIT6 ; BIT5 ; BIT4 ; BIT3 ; BIT2 ; BIT1 ; BIT0 ; FADE DIRECTION ; BUTTON RESULT ; SKIP ; FADE REACHED ; ;GLOBAL FLAGS ; BIT7 ; BIT6 ; BIT5 ; BIT4 ; BIT3 ; BIT2 ; BIT1 BIT0 ; ; BOOT ; NONE ; RED done ; GRN done ; BLU done ; cblock 0x20 ;Color variables redval:2 ;16bit brightness value reddest:2 ;16bit animation desitination... only 8 bits are used... redF ;flags associated with this color grnval:2 grndest:2 grnF bluval:2 bludest:2 bluF flags ;global flags ;lfsr and delay variables new_reg0 random:4 temp:4 rndred ;individual random delay registers... unused rndgrn rndblu delay ;8bit delay reg endc ;reset vector org 0x0000 ; processor reset vector nop goto main ; go to beginning of program org 0x0004 goto interrupt ;;;;;;;;;;;;;;;;;;;;;;;;;;SUBROUTINES & MACROS;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; conrad macro color, val, dest, button, del, plus movlb 0x00 btfsc PORTA, button ;button action goto $+9 movfw random movwf del lslf del lslf del lslf del lslf del decfsz del goto $-1 movlb 0x00 movfw val+1 subwf dest+1,w ;subtract value from destination btfss STATUS, C ;check carry bit goto $+4 btfss STATUS, Z ;check zero bit goto $+8 goto $+0x11 ;checkless: movfw dest+1 subwf val+1,w ;subtract destination from value btfss STATUS, C ;check carry bit btfss STATUS, Z goto $+7 goto $+11 ;countup: movlw 0x01 addwf val,f btfsc STATUS,C incf val+1 return ;countdn: decfsz val return decf val+1 nop return ;newval: call lfsr movfw random+plus movwf dest+1 endm fade macro color,value movlb 0x00 btfss color, 7 goto $+0x0A movlw 0x01 addwf value btfsc STATUS,C incf value+1 movfw value+1 sublw 0xFF btfsc STATUS,Z bcf color, 7 return movlb 0x00 bsf color,4 decfsz value return decf value+1 movfw value+1 sublw 0x01 btfsc STATUS,Z bsf color, 7 return endm colorload macro value, pwm, load movlb 0x00 movfw value movlb 0x1B movwf pwm movlb 0x00 movfw value+1 movlb 0x1B movwf pwm+1 bsf load,7 movlb 0x00 return endm conradred: conrad redF, redval, reddest, 3 , rndred, 0 return bred: movlb 0x00 movlw 0x11 movwf delay decfsz delay goto $-1 btfsc PORTA, 3 ;test red button call red ;fade red return red: fade redF,redval return setred: colorload redval,PWM1DCL,PWM1LDCON return conradgrn: conrad grnF,grnval, grndest, 4 , rndgrn, 2 return bgrn: movlb 0x00 movlw 0x11 movwf delay decfsz delay goto $-1 btfsc PORTA, 4 call green return green: fade grnF,grnval return setgrn: colorload grnval,PWM2DCL,PWM2LDCON return conradblue: conrad bluF, bluval, bludest, 5, rndblu, 3 return bblue: movlb 0x00 movlw 0x11 movwf delay decfsz delay goto $-1 btfsc PORTA, 5 call blue return blue: fade bluF,bluval return setblue: colorload bluval,PWM3DCL,PWM3LDCON return ;LFSR using byte movements from Tom Wiltshire's code in "Practical LFSR random number generators" lfsr: movf random+3, w movwf temp movwf temp+1 movwf new_reg0 movf random+2, w movwf temp+2 movwf temp+3 rlf temp+2, f rlf temp, f rlf temp+2, f rlf temp, w xorwf new_reg0, f rrf temp+1, f rrf temp+3, f movf temp+3, w xorwf new_reg0, f rrf temp+1, f rrf temp+3, w xorwf new_reg0, f movf random+2, w movwf random+3 movf random+1, w movwf random+2 movf random, w movwf random+1 movf new_reg0, w movwf random return ;;;;;;;;;;;;;;;;;;;;;;;;;;;ISR;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; interrupt: bcf INTCON, GIE ;turn off global interrupt flag bcf INTCON, TMR0IE bcf INTCON, TMR0IF ;interrupt is unused bsf INTCON, GIE ;turn on global interrupt flag retfie ;program main: ;;;;;;;;;;;;;;;;;;;;;;;SETUP CLOCK & PERIPHERALS;;;;;;;;;;;;;;;;;;;; movlw B'01111000' ; movlb 0x1 ;bank 1 movwf OSCCON ;Oscillator movlw 0x38 ;RA 0,1,2 are outputs movwf TRISA movlb 0x3 clrf ANSELA ;No ADC ;set up intcon and timer0 banksel INTCON movlw B'10000000' movwf INTCON banksel OPTION_REG movlw B'00000111' movwf OPTION_REG movlb 0x1B clrf PWM1PHL ;PWM Phase registers clear clrf PWM1PHH clrf PWM2PHL clrf PWM2PHH clrf PWM3PHL clrf PWM3PHH clrf PWM1DCL ;PWM dutycycle registers clear clrf PWM1DCH clrf PWM2DCL clrf PWM2DCH clrf PWM3DCL clrf PWM3DCH clrf PWM1CLKCON ;PWM CLock setups clrf PWM2CLKCON clrf PWM3CLKCON ;Clear Clkcon for highest speed and fosc clrf PWM1OFCON ;Clear Offset con clrf PWM2OFCON clrf PWM3OFCON movlw 0xC0 ;PWM enable movwf PWM1CON movwf PWM2CON movwf PWM3CON bsf PWM1LDCON,LDA ;LOAD REGS bsf PWM2LDCON,LDA bsf PWM3LDCON,LDA movlw 0xFF ;Period Registers LOW movwf PWM1PRL movwf PWM2PRL movwf PWM3PRL movlw 0xFF ;Period Registers HIGH movwf PWM1PRH movwf PWM2PRH movwf PWM3PRH MOVLB 0x00 movlw B'10000000' movwf redF movwf grnF movwf bluF clrf flags clrf redval clrf redval+1 clrf grnval clrf grnval+1 clrf bluval clrf bluval+1 bcf flags,7 movlw 0x9F movwf random movlw 0x9F movwf random+1 ;test buttons to set color modes btfsc PORTA, 3 bsf redF, 6 btfsc PORTA, 4 bsf grnF, 6 btfsc PORTA, 5 bsf bluF, 6 ;leds fade up fadered: movlb 0x00 movlw 0x11 movwf delay decfsz delay goto $-1 call red call setred movlb 0x00 btfss redF,4 goto fadered fadegrn: movlb 0x00 movlw 0x11 movwf delay decfsz delay goto $-1 call green call setgrn movlb 0x00 btfss grnF,4 goto fadegrn fadeblu: movlb 0x00 movlw 0x11 movwf delay decfsz delay goto $-1 call blue call setblue movlb 0x00 btfss bluF,4 goto fadeblu goto mainloop mainloop: ;if button is pressed at startup, remove it from animation mode. ;this way if no buttons are pressed it is in full conradmode. ;button pressed during conradmode will speed up the movements of that color. btfss redF, 6 call conradred btfsc redF, 6 call bred btfss grnF, 6 call conradgrn btfsc grnF, 6 call bgrn btfss bluF, 6 call conradblue btfsc bluF, 6 call bblue call setred ;set pwm for all colors call setgrn call setblue goto mainloop END