;HxBg8_T1.ASM - Updated 1/3/10 ; Basic Test & Personality-1 routines ; Subroutines for base function implemented & Tested: ; Movement ; Battery Voltage ; Light Level ; Sounds Library ; Power Down ; DeciDig - Output numerical value as beeps ;From: HxBg_p2.ASM Test/Base - T1 ; V ; V ; V ; V V V ; VVV ; V ; -*** New Bug PC Board ***_ ; ==>>> HXB27e <<<== ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Changes from HXB14c: ;; ;; Motor Driection WAS= GP2 // NEW= GP1 ;; ;; WAS= '1'= Forward, '0'= Reverse Turn ;; ;; NEW= '0'= Forward, '1'= Reverse Turn ;; ;; Vref WAS= AN3/GP5 // NEW= AN1/GP1 ;; ;; ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;-------------------------------- ; Target Proccessor is 12F675 - No PORTA,C ; Only GPIO & TRISIO ;--------- - ------------------ ; ******************************************************************* ; ;THIS SOFTWARE IS PROVIDED IN AN "AS IS" CONDITION. NO WARRANTIES, ;WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED ;TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A ;PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. THE COMPANY SHALL NOT, ;IN ANY CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL OR ;CONSEQUENTIAL DAMAGES, FOR ANY REASON WHATSOEVER. ;YOUR MILAGE MAY VARY. ; ; ******************************************************************* ; ; Filename: ;HxBg8_T1 ; Org.Date: 3/27/09, REV-1 ; Date: 1/4/10 ; File Version: REV-2 ; This S/W is for the HXB27e circuit board configuration- ; This program's purpose is to test the new configuration ; and test functional modules. ; ->>> Note Differences for using a PIC12F683 <<<- ; GP0 - Out, Always Motor ON/OFF ; GP1 - In/Out: Vref; Out- Motor DIRECETION; Also J4- I/O Expantion ; GP2 - In/Out: LED D1; Also J12- I/O Expantion ; GP3 - In, Always: Antena & Output from IR Rcvr Module ; GP4 - In/Out: Piezo; Out: LED D2; Also J3- I/O Expantion ; With *R1* added, activates D3 Vref as in original configuration ; GP5 - In/Out:Piezo; Power to IR Rcvr Module; Also J5- I/O Expantion- Power extra I/O ; = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ; Piezo- GP4,5 ; Motor ON- GP0 ; Motor DIR- GP1 '0'= Forward, '1'= Reverse Turn ; LED D1- GP2 AN2- Light level, Bug's right ; LED D2- GP4 AN3- Light Level, Bug's left ; Vref GP1, AN1 ; 0) Initialize uC ; Testing- ; 1) Blink LEDs; Loop till Antena touched (1 sec delay within loop) ; 2) Sound Piezo- / Wait 1 sec ; 3a) Run forward / Wait 1 sec ; 3b) Reverse turn / Wait 1 sec ; 3c) Run forward / Wait 1 sec ; 4) Test Battery voltage, and output via "DeciDig"/Piezo ; 5) Test LEDs as Light sensors; Output via "DeciDig"/Piezo ; Blink LEDs; Loop till Antena touched (1 sec delay within loop) ; Runing- ; 6) Atennae touch direction change ; 7) Go to Sleep upon Low Light Level ; 8) Nap after XX seconds of operation ; 9) Change Sound effect after first XX antenna bumps ; 10) Battery Test - If batteries are low, 'cry' and go to sleep ; 11) Stop-Go action - After XX second run for .5 sec, stop for 1 second ; ; ; Author: P.T.Miller * ; Company: Applied Inspirations,LLC * ; * ; * ;********************************************************************** ; ; FLASH SRAM EEPROM I/O 10-bit-A/D(ch) Comp Timers 8/16-bit ;PIC12F683 2048 128 256 6 4 1 2/1 ; ;********************************************************************** TITLE "PIC12F675_HexBugHack_8T1" list p=12f675 ; define processor #include ; processor specific variable definitions ; list p=12f683 ; define processor ; #include ; processor specific variable definitions errorlevel -302 ; suppress message 302 from list file ; __CONFIG _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_OFF & _WDT_ON & _PWRTE_ON & _INTRC_OSC_NOCLKOUT ;; __CONFIG _CP_OFF & _CPD_OFF & _BODEN_ON & _MCLRE_OFF & _WDT_ON & _PWRTE_ON & _INTRC_OSC_NOCLKOUT __CONFIG _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_OFF & _WDT_ON & _PWRTE_ON & _INTRC_OSC_NOCLKOUT ;NOTE: Brown-Out-Detection (BOD) is OFF(disabled) because of the very small supply voltage margins. ;Supply voltage is nominally 3 volts; BOD is 2.0 volts. The motor current for reverse is higher- ;Typ. 64ma Rev. / 57ma Fwd. Start-up current for the motor in reverse can lower the power supply voltage ;momentarily below 2.0 volt causing a reset. Typically, the processor will operate at 1.8 volts, though ;Microchip does not guaranty nor characterize it at this lower voltage. ;; USED BY 12F683-> ;xxxx; __CONFIG _CP_OFF & _CPD_OFF & _BOD_OFF & _MCLRE_OFF & _WDT_ON & _PWRTE_ON & _INTRC_OSC_NOCLKOUT ;From INC file- ;; __CONFIG _FCMEN_ON & _IESO_OFF & _CP_OFF & _CPD_OFF & _BOD_OFF & _MCLRE_ON & _WDT_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT ; '__CONFIG' directive lables are located in the respective .inc file. ;********************************** ;Storage/Working Registers, in sequence: ;[From 20h to 5Fh - 64 bytes] CBLOCK H'20' ;Interupt- Wtmp ;'W' Temp. Storage STATtmp ;'STATUS' Temp. Storage IOtmp ;Temp Stotage of GPIO TRStmp ;Temp Stotage of TRSIO ;Used by Delay functions- DLY_CNT ; low 8 bits of DeLaY CouNTer MID_CNT ; mid 8 bits of DeLaY CouNTer ; LNG_CNT ; mid 8 bits of DeLaY CouNTer <<=== Not Currently In Use XLNG_CNT ; high 8 bits of counter for 'X'tra LoNG CouNTs TEMP ;Temp High - USE ONLY WITHIN IMMIDIATE ROUTINE TEMPL ;Temp Low - USE ONLY WITHIN IMMIDIATE ROUTINE Glop ;General Loop counter ;Used by Sound generating functions- TONE ;Count for Tone TONEDUR ;Count for Duration of Tone SWPDUR ;Count for Duration of Sweep TonCnt ;Tone Counter DurCnt ;Duration Counter SwpCnt ;Sweep Counter TONE8 ;Cont of tone cycles for musical notes TrlRpt ;Trill Repeat Counter TrlSwp ;Trill Sweep RptTn ;Number of Repeats; If Zero, just once WavCnt ;Wave Count PTRNCT ;Pattern Count within table ;Misc- Ac100s Ac10s OutByt ;byte to output as sound SnoozCnt ;Snooze Cycles Counter TrisTmp ;Temp storage of TRISIO PhotoLv2 ;Temp Storage of Light value from D1, GP1, AN1 PhotoLv4 ;Temp Storage of Light value from D2, GP4, AN3 BmpCnt ;Count number of 'Bumps' ClrCnt ;Count Clear (No Bump) loops RndCnt ;Random Count OpCnt ;Opperation time count DncCnt ;Dance count BattCnt ;Number of opperation cycle until battery test(Saves Power) StatFlgs ;Status Flags - ;Bit-0 LastLvl, "Last Light Level" 0=Dark, 1=Light ;Bit-1 Motor Power Status- Within Read ADC, 0=OFF, 1=ON ;Bit-2 Motor Power Status- Within Test light level, 0=OFF, 1=ON ;Bit-4 Stop-Go state: 0= Normal travel, 1= Run .5 sec, Stop .5 sec ;Bit-5 xxxx ;Bit-6 xxxx ;Bit-7 xxxx ; ENDC ;********************************** ;Constants: ; ANSEL bit: AN0,1,2,3 on RA0,1,2,4 / AN4,5,6,7 on RC0,1,2,3 1=Analog, 0=Digital ANLSET EQU b'00000111' ;Set RC<2:0> as inputs and set RC<5:3> as outputs ANLIN EQU b'00001100' ;Set RA<3:2> as inputs,and set RA<5:4,1:0> as outputs CMPSET EQU b'00000111' ;Comp.OFF Setup- See Section 6.0 SWITCH EQU b'00001000' ;Momentary Pushbutton Switch MotPwr EQU 0 ;GPIO bit that controls Motor ON/OFF- 0=OFF, 1=ON MotDir EQU 2 ;GPIO bit that controls Motor Direction- 1=FORWARD, 0=REVERSe C EQU 0 ; Carry bit of STATUS Carry EQU 0 ; Carry bit of STATUS LastLvl EQU 0 ;Bit 0 of StatFlgs; "Last Light Level" 0=Dark, 1=Light ; ************************* <*> ****************************** ; **************************************************************** ;********** Data Values ************* ; **************************************************************** ; ************************* <*> ****************************** ;* * * * * * * * Implementation Specific Data Values * * * * * * * * * * SleepCnts EQU d'52';=2min ;Number of sleep cycles befor testing... ; Sleep time = Cnts * 2.3 sec; Max =255 * 2.3 sec = ~9.7 min ; 1 Sleep ; Total Sleep time cycle 4.6 sec ; 2 Sleep ; Total Sleep time cycle 6.9 sec ; 3 Sleep ; Total Sleep time cycle 9.2 sec ; 4 Sleep ; Total Sleep time cycle 11.5 sec ; 5 Sleep ; Total Sleep time cycle 13.8 sec ; 130 * 13.8 sec = 1794 sec = 29.9 min ;-------------------------------------------------- BattLow EQU d'135' ;Battery voltage at knee point- 2.307v ;BattLow EQU d'120' ;Battery voltage at knee point- 2.596v BattDly EQU d'21' ;.25Sec/Cnt -Number of opperation cycles until battSat is run ;-------------------------------------------------- ;Too High- LightLVL EQU d'10' ;Value below which light level is considered LOW ;With 8deg. LED, 500ms collection time, left justified ADC; LED reads 001 or 002 LightLVL EQU d'4' ;Value at which light level is considered LOW ;-------------------------------------------------- ; ************************* <*> ****************************** ; **************************************************************** ;********** CODE: ************* ; **************************************************************** ; ************************* <*> ****************************** ORG 0x000 ; Processor reset vector goto MAIN ; Go to beginning of program ORG 0x004 ; interrupt vector location MOVWF Wtmp ; save current W register contents MOVF STATUS,W ; move status register into W register MOVWF STATtmp ; save contents of STATUS register ; ISR code can go here or be located as a call subroutine elsewhere MOVF STATtmp,W ; retrieve copy of STATUS register MOVWF STATUS ; restore pre-isr STATUS register contents SWAPF Wtmp,F SWAPF Wtmp,W ; restore pre-isr W register contents RETFIE ; return from interrupt ;------------------------------------------------------------------------------ ORG 0x010 ; Past interrupt vector location ;------------------------------------------------------------------------------ MAIN: ;NOTE- 12F683 DOES NOT NEED TO STORE CALIBRATION- PRESET @ FACTORY ; FREQ. CAN BE ADJUSTED ON THE FLY VIA "OSCTUNE" ; ;These first 4 instructions are not required if the internal oscillator ;; is not used, but we'll use them for future compatability. ; CALL 0x3FF ; retrieve factory calibration value BSF STATUS,RP0 ; set file register to BANK 1 <<--+--### ; 12F675 does not have 'OSCCAL' ; MOVWF OSCCAL ; update register with factory cal value MOVLW b'10001111' ; Disable Pullups; Enable PreScaler for WDT Clk, & Set to 1:128 MOVWF OPTION_REG ; CLRF ANSEL ; Analog function turned OFF ; * * * * * * * CLRF IOC ;Disable interupt-on-change ; BSF IOCB,3 ;Enable interupt on GP3 bit- Antenna ;If using Interupts, Clearing the flag bit GPIF ends Interupt action. ; * * * * * * * CLRF VRCON ; Voltage Ref section turned OFF- reduce power consumption CLRF INTCON ; Turn OFF Interups ; * * * * * * * ; BSF INTCON,0 ; Enable interupts ; BSF INTCON,3 ; Enable Port Change interupt -Use with IOC ; * * * * * * * ;( TRISIO was set to all Inputs by RESET ) BCF STATUS,RP0 ; Back to BANK 0 <<--+--### CLRF ADCON0 ; A/D turned OFF - ;Note: The ANSEL (9Fh) and CMCON0 (19h)registers must be initialized to configure an ;analog channel as a digital input. Pins configured as analog inputs will read ‘0’ Digtal. MOVLW b'00000111' ; Turn Comparitor OFF, Lowest Power consumption. ;; USED BY 12F675-> MOVWF CMCON ; by setting CMCON<0:3> ;; USED BY 12F683-> MOVWF CMCON0 ; by setting CMCON0<0:3> ;; CLRF GPIO StartLp: ;**************** Start loop ****************************** ;** Delay use of MCLR, ICSPDAT, & ICSPCLK ports so Vpp can turn on ;** programing mode before ports get re-configured and can not reprogram ;** without being erased (5v seems to be needed for erasuer) MOVLW d'10' ;Wait 1 sec CALL DLY_X ;100ms/Cycle ;########################################## ;Initialize I/O- BSF StatFlgs,LastLvl ;Set last Light level ast Bright; 0=Dark, 1=Light CLRF RptTn ;Clear Repeat counts for sound routines CLRF GPIO ;All Port Bits OFF BSF STATUS,RP0 ; Set Bank 1 <<--+--### MOVLW b'11001000' ;Init. I/O- Motor, LED/Vref PWR, Piezo MOVWF TRISIO ; Set Port A I/O: 1= Input, 0= Output BCF STATUS,RP0 ; Set Bank 0 <<--+--### ;------------------------------------------------------------ ;------------------------------------------------------------ ;Jump from Here to Test individual Routines ........ ;------------------------------------------------------------ ZXZ: ; MOVLW .50 ;5 sec delay ; CALL DLY_X ;100ms/Cycle ;; CALL Tone72 ;; GOTO ZXZ ;>>> GOTO EndTsts ;Skip Systests ;------------------------------------------------------------ ;------------------------------------------------------------ ;Initialize Sleep Counter - MOVLW SleepCnts ;Load Snooze Cycles MOVWF SnoozCnt ;Initialize Battery test delay - MOVLW BattDly ;Load deley for battery test MOVWF BattCnt ;Initialize Last Light Level Status Flag BSF StatFlgs,LastLvl ;Set last Light level ast Bright; 0=Dark, 1=Light ; ************************** ; ******************************** ;****** uC Setup Completed ******** ; ******************************** ; ************************** ;########################################## ; Test LEDs & indicate system is funcioning ;>>> Test-1: BLINK LEDs & LOOP)----------------> ;------< Wait for Antena touch >-------- BSF GPIO,2 ;Left LED(D1) ON BSF GPIO,4 ;Right LED(D2) ON MOVLW d'2' CALL DLY_L ;@ 2.5mS per count = 5ms BCF GPIO,2 ;Left LED(D1) OFF BCF GPIO,4 ;Right LED(D2) OFF BTFSC GPIO,3 ;Skip next if Antena is touched.. GOTO StartLp ;Loop until Antena is touched... ;------------------------------------------------------------ ;########################################## ;>>> SOUND Tests ----------------> ; CLRF RptTn,F ;Set Repeat for 1 Trill Cycl ; CALL Trllup ;Up-Sweep Trill ; MOVLW d'10' ;Wait .5 sec ; CALL DLY_X ;100ms/Cycle ; ; CALL Trllwiz ;Long Trill-Up ; MOVLW d'10' ;Wait .5 sec ; CALL DLY_X ;100ms/Cycle ; CALL UhOh1 ;Uh-Oh ; MOVLW d'10' ;Wait .5 sec ; CALL DLY_X ;100ms/Cycle ; ; CALL OhUh1 ;OK? ; MOVLW d'10' ;Wait .5 sec ; CALL DLY_X ;100ms/Cycle ; ; CALL Grwl ;Growl ; MOVLW d'10' ;Wait .5 sec ; CALL DLY_X ;100ms/Cycle ; ; CALL CRY ;Cry ; MOVLW d'5' ;Wait .5 sec ; CALL DLY_X ;100ms/Cycle ; ; CALL Trlla ;Trill glad ; MOVLW d'10' ;Wait .5 sec ; CALL DLY_X ;100ms/Cycle ; CALL Trllb ;Trill sad ; MOVLW d'10' ;Wait .5 sec ; CALL DLY_X ;100ms/Cycle ; CALL Chips2 ;? ? ? ? ; MOVLW d'10' ;Wait .5 sec ; CALL DLY_X ;100ms/Cycle ; CALL Chips1 ;! ! ! ! ; MOVLW d'10' ;Wait .5 sec ; CALL DLY_X ;100ms/Cycle ;########################################## ;>>> Test-2: PIEZO )----------------> ;>>> Sound output: CLRF RptTn ;Set Repeat for 1 Trill Cycl CALL Trlldwn ;Down-Sweep Trill MOVLW d'10' ;Wait 1 sec CALL DLY_X ;100ms/Cycle ;------------------------------------------------------------------- ;########################################## ;>>> Test-3a: MOTOR FWD )----------------> ;-- Test GPIO-0, Motor ON, FWD (Pin 7) ;Fwrd: - Rvrs: CALL Fwrd1 ;Motor On, .5 sec, then STOP ;NOTE: Always turn motor OFF BEFORE CHANGING DIRECTION to prevent 'shoot-thru' MOVLW d'3' ;Wait .3 sec CALL DLY_X ;100ms/Cycle ;########################################## ;>>> Test-3b: MOTOR REVRS )----------------> ;-- Test GPIO-1, Direction-REV (Pin 6) CALL Rvrs1 ;Reverse for 1.5 sec,then STOP MOVLW d'3' ;Wait .3 sec CALL DLY_X ;100ms/Cycle ;########################################## ;>>> Test-3c: 2nd MOTOR FWD )--------------> ;-- Test GPIO-1, Direction-FWD (Pin 6) CALL Fwrd1 ;Motor On, .5 sec, then STOP MOVLW d'3' ;Wait .3 sec CALL DLY_X ;100ms/Cycle ;########################################## ;>>> Test-4: BATTERY Level )----------------> ;New for HXB27e ADC configuration (Done in Sub- BattLvl: ; Disable AN1 as Analog; Make GP1 Dig out,=1; ; Delay to charge cap. ; Change GP1 to Dig In & Enable AN1 as analog ; Read ADC BattLvl: ;Read Battery Voltage level- CALL BattStat MOVF ADRESH,W ;Get voltage level ;; MOVLW d'237' ; - USED FOR TESTING of DECIDIG Sub.- MOVWF OutByt ;Save for converstion... ;Convert value passed in OutByt to 3 Decimal Digits: CALL DeciDig ;Output value as 'Beeps' MOVLW d'10' ;Wait 1 sec CALL DLY_X ;100ms/Cycle CALL Tone72 ;- DONE - CALL Tone52 MOVLW d'10' ;Wait 1 sec CALL DLY_X ;100ms/Cycle ;########################################## ;>>> Test-5: Tets LED light sensors )----------------> ;New for HXB27e ADC configuration (Done in Sub- BattLvl: LightLvl: CALL LiteLvl ;Returns Light Level in 'W'; uses Vcc as Volt Ref. MOVWF OutByt ;Save 'W' for converstion... ;Convert value passed in OutByt to 3 Decimal Digits: CALL DeciDig ;Output value as 'Beeps' SkpPnt: ;TEMP.TEMP. MOVLW d'10' ;Wait 1 sec CALL DLY_X ;100ms/Cycle CALL Tone52 ;- DONE - CALL Tone72 MOVLW d'10' ;Wait 1 sec CALL DLY_X ;100ms/Cycle ;========================================== ;========================================== EndTsts: BSF STATUS,RP0 ; Set Bank 1 <<--+--### BCF TRISIO,2 ;LED D1 -Enable GP2,4 as outputs BCF TRISIO,4 ;LED D2 BCF STATUS,RP0 ; Set Bank 0 <<--+--### ;Blink both LEDs simultaniously until antena bump- BSF GPIO,2 ;Left LED(D1) ON BSF GPIO,4 ;Right LED(D2) ON MOVLW d'2' CALL DLY_L ;@ 2.5mS per count = 5ms BCF GPIO,2 ;Left LED(D1) OFF BCF GPIO,4 ;Right LED(D2) OFF MOVLW d'15' CALL DLY_X ;@ 100mS per count = 5ms BTFSC GPIO,3 ;Skip next if Antena is touched.. GOTO EndTsts ;Loop until Antena is touched... ;Announce beginning of "personality" program: CALL Trllwiz ;Long Up-Sweep Trill ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ;@@@@@@@@@@@@@@@@ START of Main personality Program @@@@@@@@@@@@@@@@@@@ ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ MainOpr: ;Init. Counters- CLRF OpCnt ;Clear Opperational time counter CLRF BmpCnt ;Clear bump Count CLRF ClrCnt ;Clear the 'No Bumps' Count BCF StatFlgs,4 ;Clear Stop-Go state: 0= Normal travel, 1=.5 sec.Ea Stop/Start MOVLW .1 ;Init battery test for 1st pass MOVWF BattCnt ;Don't test battery on all passes- Uses Motor-Direction bit; Motor must be Stopped ;-------------------------------------------------------------------------- ;Batter Power Good Test: ;DON'T Test Batt level every cycle- BattStat powers up Vref AND lights LED. ;DON't test Batt while motor is running- Load & Noise interfers with accuracy. BattTst: DECFSZ BattCnt,F ;Dec Batt test count &.. GOTO BattOK ;Skip test on this pass if not Zero... MOVLW BattDly ;Re-load deley for battery test MOVWF BattCnt TstBat: ;Test Battery... MOVF GPIO,W ;Get & save Motor On/Off state MOVWF IOtmp ;Save GPIO Value BCF GPIO,0 ;Turn Motor Off & Delay MOVLW .4 CALL DLY_L ;Delay 2.5ms/cnt; 10ms ;Now read ADC... CALL BattStat ;then Restore... MOVF IOtmp,W ;Get prior I/O state ANDLW b'00000011' ;Clear all but Motor bits IORWF GPIO,F ;OR with I/O to Restore Motor Dir. & On/Off state MOVLW BattLow ;Load OK Battery value => W SUBWF ADRESH,W ;Voltage level - OK value =>W, Z,C BTFSS STATUS,Carry ;If carry is set, Voltage leve is higher than trip point OK value GOTO BattOK ;Battery Good... ;===================== ; Battery is Low- CRY LOOP- ;If Battery is low, check on evey pass by... MOVLW .1 ;..Setting deley to repeat battery test MOVWF BattCnt ;.................. ;Use OpCnt to alternate Fwd/Rvs BTFSC OpCnt,0 ;0 bit will alternate CALL Rvrs0 ;Short Revese Step BTFSS OpCnt,0 CALL Fwrd1 ;Short Forward Step INCF OpCnt,F ;Inc count within 'Battery Low' routine ;.................. CALL CRY MOVLW d'1' ;Wait .1 sec CALL DLY_X ;100ms/Cycle CALL CRY SLEEP SLEEP ;Sleep for 6.9 sec, SLEEP GOTO BattTst ;& Loop till Batt.Volts Good ;===================== BattOK: CLRWDT ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ;-------------------------------------------------------------------------- StpGoTst: BTFSS StatFlgs,4 ;Skip next if Stop/Go flag is set GOTO NormRun ;-------------------------------------------------------------------------- ; Stop-Go function- BTFSC OpCnt,0 ;If .5sec, skip Stop CALL STOP BTFSS OpCnt,0 ;If 1sec, skip Run ;-------------------------------------------------------------------------- NormRun: CALL Fwrd ;TURN MOTOR ON & GO . . . INCF ClrCnt,F ;Inc No-Obsticles counter INCF OpCnt,F ;Opperational time counter, Counts .5 sec opp loops: ;-------------------------------------------------------------------------- ;Test light level: If DARK, TrillDwn, Sleep 2.8sec., then Check again. ; If light, TrillUp and exit Sub.; Else go back to sleep.. CALL PhotoTst ;Stays in subroutine if DARK; Exits on LIGHT. ;PhotoTst take 1/2 second to run, separate loop delay is NOT needed... ; - This routine contains a test block to output the leght level while running. - ;; 1/28/09 - PhotoStat updated: Delay now 1/4 sec ;------------------------------------------------------------------ ;-------------------------------------------------------------------------- ; Run for 1/4 sec, then check- MOVLW d'100' CALL DLY_L ;@ 2.5mS per count = .25 sec ;-------------------------------------------------------------------------- ;------------------------------------------------------------------ ;If OpCnt = 60, (30sec of opperation) Start Stop/Go- ; MOVLW .60 ;Set 'W' to actuation count @ 30sec ;;-SHORTER FOR DEMO--> MOVLW .30 ;Set 'W' to actuation count @ 15sec XORWF OpCnt,W BTFSC STATUS,Z ; skip next if not ZERO... BSF StatFlgs,4 ;Set Stop-Go state: 0= Normal travel, 1=.5 sec.Ea Stop/Start ;-------------------------------------------------------------------------- ;If OpCnt = 90, (45sec of opperation) End Stop/Go- ; MOVLW .90 ;Set 'W' to actuation count @ 45sec ;;-SHORTER FOR DEMO--> MOVLW .45 ;Set 'W' to actuation count @ 23sec XORWF OpCnt,W BTFSC STATUS,Z ; skip next if not ZERO... BCF StatFlgs,4 ;Clear Stop-Go state: 0= Normal travel, 1=.5 sec.Ea Stop/Start ;-------------------------------------------------------------------------- ;-------------------------------------------------------------------------- ; -ConstructionZone- Add new features HERE ; MOVLW .XX ;Load 'W' with actuation count... ; XORWF OpCnt,W ; BTFSC STATUS,Z ; skip next if not ZERO... ; GOTO (NEW Function) ;Jump to new function ; (End Function by Jumping Back to "MainOpr" to reset all, or "BattTst". ;-------------------------------------------------------------------------- ;-------------------------------------------------------------------------- ;If OpCnt = 120, (60sec of opperation) NapTime- ; MOVLW .120 ;Set 'W' to actuation count @ 60sec MOVLW .60 ;Set 'W' to actuation count @ 60sec ;;FOR DEMO- Shorter- XORWF OpCnt,W BTFSC STATUS,Z ; skip next if not ZERO... GOTO NapTime ;Take a Nap for 10 sec or until antenna is touched. ; NapTime returns to MainOpr, Resets counters ;-------------------------<<<<<<<<<+>>>>>>>>>---------------------------- BTFSC GPIO,3 ;Loop until Antena is touched.. GOTO BattTst ;NO BUMP- FORWARD/BattTst LOOP... ;...................................................................... ;........... Antenna Bumped ........................... ;...................................................................... INCF BmpCnt,F ;Inc BUMP count- CALL STOP ;ID bump count to select sound- MOVLW b'00000111' ;Mask for first 8 bumps ANDWF BmpCnt,F ;Now do tests- MOVLW .3 XORWF BmpCnt,W ;Set 'Z' if match BTFSC STATUS,Z ; skip next if not ZERO... CALL UhOh1 MOVLW .4 XORWF BmpCnt,W ;Set 'Z' if match BTFSC STATUS,Z ; skip next if not ZERO... CALL Grwl MOVLW .6 XORWF BmpCnt,W ;Set 'Z' if match BTFSC STATUS,Z ; skip next if not ZERO... CALL Chips1 ;! ! ! ! ;Reverse turn 90 Deg.- CALL Rvrs MOVLW .20 ;Reverse turn- ~110deg -TURN CALL DLY_X ;100ms/Cnt CALL STOP ;--------------------------- GOTO BattTst ;Continue Opperation Loop ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ;@@@@@@@@@@@@@@@@ END of Main personality Program @@@@@@@@@@@@@@@@@@@ ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ;@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ;@@@@@@@@@@@@@@@@ Sub Program @@@@@@@@@@@@@@@@@@@@@@@@ ; Length of 'Nap-Time' passed in 'W'; Save in 'SnoozCnt'. ;While in this Sub., monitor Antenna; If bumped, exit Nap-Time. NapTime: BCF GPIO,0 ;Motor Off ; MOVLW .40 ; 93sec Set deafult Nap time MOVLW .5 ; 11.5sec Nap time ShrtNap: ;Jump to here from end of 'Tantrum' with 'W' set for short nap. BSF STATUS,RP0 ; Set Bank 1 <<--+--### BCF TRISIO,2 ;LED D1 -Enable GP2,4 as outputs BCF STATUS,RP0 ; Set Bank 0 <<--+--### BSF GPIO,2 ;LED On, Left, D1 MOVWF SnoozCnt CALL Trllb MOVLW d'2' ;Wait .2sec CALL DLY_X ;100ms/Cycle CALL Trlldwn ;=======zzZzZZZZZZZZZZZZZZ sleep ZZZZZZZZZZZZZZzZzz======== SleepLoop: CALL PwrDwn ;Lowest power condition, modifies port config. to All Inputs- CLRWDT SnoozLoop: Sleep ;SLEEP clears WDT, full 2.3 sec WDT cycle begins. ;Check Switch after each SLEEP to reduce responce time... BTFSS GPIO,3 ; test SWITCH input (Skip if set(High), Button Not Pressed) GOTO WAKE ; Antenna Bump, Exit Sleep Cycles... DECFSZ SnoozCnt,F ;Dec Snooze counter, Skip GOTO if time to wake GOTO SnoozLoop ; Total Sleep time cycle: 130 * 13.8 sec = 1794 sec = 29.9 min ; CALL Tone72 ;<<< FOR TESTING - Beep at wake-up ; - - - - - - - - WAKE: ;------------------------------------------- ;Blink & Trill-up BSF STATUS,RP0 ; Set Bank 1 <<--+--### BCF TRISIO,2 ;Enable LED D1 as Output BCF STATUS,RP0 ; Set Bank 0 <<--+--### BSF GPIO,2 ;LED On MOVLW d'2' ;Wait .2 sec CALL DLY_X ;100ms/Cycle CLRF RptTn ;Set Repeat for 1 Trill Cycl CALL Trllwiz BCF GPIO,2 ;LED Off GOTO MainOpr ;Go back to Main Running Loop... ;======================================================================================= ;XXXXXXXXXXXXXXXXXXXXXXXX SUBROUTINES XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ;\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ;/////////////////////////////////////////////////////////////////////////////////////// ;\\\\\\\\\\\\\\\\\\\\\\\\\\ \\\\\\\\\\\\\\\\\\\\\\\\\\ ;//////////////////////// XXX X X XXXX XXX //////////////////////// ;\\\\\\\\\\\\\\\\\\\\\\\\ X X X X X X X X \\\\\\\\\\\\\\\\\\\\\\\\ ;//////////////////////// X X X X X X //////////////////////// ;\\\\\\\\\\\\\\\\\\\\\\\\ XXXX X X XXXX XXXX \\\\\\\\\\\\\\\\\\\\\\\\ ;//////////////////////// X X X X X X //////////////////////// ;\\\\\\\\\\\\\\\\\\\\\\\\ X X X X X X X X \\\\\\\\\\\\\\\\\\\\\\\\ ;//////////////////////// XXXX XXXX XXXXX XXXX //////////////////////// ;\\\\\\\\\\\\\\\\\\\\\\\\\\ \\\\\\\\\\\\\\\\\\\\\\\\\\ ;/////////////////////////////////////////////////////////////////////////////////////// ;\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ;------------------------------------------------------------------- ;--- Sound Effect Subs ---------------------------------------------- ;------------------------------------------------------------------- ;Uh-Oh- UhOh1: MOVLW .120 MOVWF TONE ;Set TONE count MOVLW .15 MOVWF TONEDUR ;Set TONE Duration ; MOVLW .20 MOVWF SWPDUR ;Set Sweep Duration CALL SweepU ;Handles I/O MOVLW d'1' ;Wait .1 sec CALL DLY_X ;100ms/Cycle MOVLW .180 MOVWF TONE ;Set TONE start count MOVLW .15 MOVWF TONEDUR ;Set TONE Duration MOVLW .24 MOVWF SWPDUR ;Set Sweep Duration CALL SweepD ;Handles I/O ;Hold end of sweep note- ; MOVLW .50 MOVLW .70 MOVWF TONEDUR ;Set TONE Duration MOVF TonCnt,W ;Get Tone count at end of sweep into 'W' to pass to Sub CALL MTone RETURN ;------------------------------------------------------------------- ;------------------------------------------------------------------- ;"OK?" OhUh1: MOVLW .180 MOVWF TONE ;Set TONE start count MOVLW .15 MOVWF TONEDUR ;Set TONE Duration MOVLW .24 MOVWF SWPDUR ;Set Sweep Duration CALL SweepD ;Handles I/O MOVLW d'1' ;Wait .1 sec CALL DLY_X ;100ms/Cycle ; MOVLW .120 MOVLW .100 MOVWF TONE ;Set TONE count MOVLW .25 ; MOVLW .15 MOVWF TONEDUR ;Set TONE Duration MOVLW .20 MOVWF SWPDUR ;Set Sweep Duration CALL SweepU ;Handles I/O ;Hold end of sweep note- MOVLW .80 MOVWF TONEDUR ;Set TONE Duration MOVF TonCnt,W ;Get Tone count at end of sweep into 'W' to pass to Sub CALL MTone RETURN ;------------------------------------------------------------------- ;------------------------------------------------------------------- Grwl: CALL StUpPzo ;Save prior TRIS0, and set GP4,5 for Piezo ;........................ CALL Grrr MOVLW d'7' ;Wait 7ms CALL DLY_L ;2.5ms/Cycle CALL Grrr MOVLW d'7' ;Wait 7ms CALL DLY_L ;2.5ms/Cycle CALL Grrr MOVLW d'7' ;Wait 7ms CALL DLY_L ;2.5ms/Cycle CALL Grrr ;........................ GOTO RelxPzo ;Go Relax Piezo and RETURN- ;=================================================================== ;------------------------------------------------------------------- Grrr: MOVLW .60 ;number of waves MOVWF WavCnt MOVLW .65 ;number of 1/2 waves MOVWF TONEDUR ;WavCnt GrrWv: MOVF TONEDUR,W ;WavCnt MOVWF TonCnt ;WavCnt ;------------------------------------------------------------------- Grr_LP: ;Create Pulse- MOVLW b'00110000' ; (1) XORWF GPIO,F ;Flip port bits to sound Piezo (1) ;---------------------------------------- MOVLW .11 ;1st Peak Res.of Kobitone Piezo (1) ; MOVLW .17 ;2nd Peak Res.of Kobitone Piezo (1) MOVWF DLY_CNT ; Store loop cnt (1) GR1_LP: ;2+(11*7)= 79 NOP ; (1) NOP ; (1) NOP ; (1) CLRWDT ;Clear WDT (1) DECFSZ DLY_CNT,F ; (1/2) GOTO GR1_LP ; 7uS/loop (2) ;---------------------------------------------------------------------------- DECFSZ TonCnt,F ; (1/2) 79+3+5=87 GOTO Grr_LP ; (2) ;Space between pulses- MOVLW d'6' ;Wait 80us CALL DLY_S ;10us/Cycle ; INCF TONEDUR,F ;Increase the tone duration DECF TONEDUR,F ;Reduce the tone duration DECFSZ WavCnt,F GOTO GrrWv RETURN ;------------------------------------------------------------------- ;------------------------------------------------------------------- CRY: MOVLW .12 MOVWF RptTn ;'Repeat'count ; ;------------------------------------ MOVLW .150 CRYrpt: MOVWF TONE ;Set TONE start count MOVLW .10 MOVWF TONEDUR ;Set TONE Duration MOVLW .20 MOVWF SWPDUR ;Set Sweep Duration CALL SweepU ;Handles I/O MOVLW d'10' ;Wait .1 sec ;++++++++++++++++++ MOVLW .10 MOVWF TONEDUR ;Set TONE Duration MOVLW .30 MOVWF SWPDUR ;Set Sweep Duration CALL SweepD ;Handles I/O MOVF TonCnt,W ;Get Prior tone count DECFSZ RptTn,F GOTO CRYrpt MOVLW .100 MOVWF TONEDUR ;Set TONE Duration MOVF TonCnt,W ;Get Tone count at end of sweep into 'W' to pass to Sub CALL MTone MOVLW d'1' ;Wait .1 sec CALL DLY_X ;100ms/Cycle RETURN ;------------------------------------------------------------------- ;-------------------- Happy Trill -------------------------------- Trlla: MOVLW .8 MOVWF TrlRpt ;ToneRepeat count Trll: MOVLW .40 MOVWF TONE ;Set TONE start count MOVLW .15 MOVWF TONEDUR ;Set TONE Duration MOVLW .20 MOVWF SWPDUR ;Set Sweep Duration CALL SweepU ;Handles I/O MOVLW d'20' ;Wait 50ms ; CALL DLY_L ;2.5ms/Cycle DECFSZ TrlRpt,F GOTO Trll MOVLW .16 MOVWF TrlRpt ;ToneRepeat count Trll2: MOVLW .70 MOVWF TONE ;Set TONE start count MOVLW .12 MOVWF TONEDUR ;Set TONE Duration ;(Use same Sweep Duration) CALL SweepU ;Handles I/O ; MOVLW d'20' ;Wait 50ms ; CALL DLY_L ;2.5ms/Cycle DECFSZ TrlRpt,F GOTO Trll2 RETURN ;------------------------------------------------------------------- ;--------------------- Sad Trill --------------------------------- Trllb: MOVLW .8 MOVWF TrlRpt ;ToneRepeat count Trll3: MOVLW .50 MOVWF TONE ;Set TONE start count MOVLW .15 MOVWF TONEDUR ;Set TONE Duration MOVLW .20 MOVWF SWPDUR ;Set Sweep Duration CALL SweepD ;Handles I/O MOVLW d'20' ;Wait 50ms ; CALL DLY_L ;2.5ms/Cycle DECFSZ TrlRpt,F GOTO Trll3 MOVLW .16 MOVWF TrlRpt ;ToneRepeat count Trll4: MOVLW .80 MOVWF TONE ;Set TONE start count MOVLW .12 MOVWF TONEDUR ;Set TONE Duration CALL SweepD ;Handles I/O MOVLW d'20' ;Wait 50ms ; CALL DLY_L ;2.5ms/Cycle DECFSZ TrlRpt,F GOTO Trll4 RETURN ;------------------------------------------------------------------- Trllwiz: MOVLW .40 MOVWF TrlRpt ;ToneRepeat count MOVLW .40 MOVWF TONE ;Set TONE start count Trllxd: ; MOVLW .12 MOVLW .30 MOVWF TONEDUR ;Set TONE Duration ;( Sweep Durration was set befor ) MOVLW .30 MOVWF SWPDUR ;Set Sweep Duration CALL SweepD ;Handles I/O DECF TONE,F ; MOVLW d'20' ;Wait 50ms ; CALL DLY_L ;2.5ms/Cycle DECFSZ TrlRpt,F GOTO Trllxd RETURN ;-------------------------------------------------------------------------------- ;-------------------------------------------------------------------------------- ;Sweep trill Up- ; RptS - Contains number time to repeat. If Zero, just once. Exits with RptS set to Zero Trllup: INCF RptTn,F ;If 'Repeat' is zero, its now 1. After DECSZ it will be zero again ;------------------------------------ TrUrpt: MOVLW .30 MOVWF TrlSwp ;Trill Repeat count for sweep MOVLW .30 MOVWF TONE ;Set TONE start count MOVLW .15 MOVWF TONEDUR ;Set TONE Duration MOVLW .15 MOVWF SWPDUR ;Set Duration of sweep Trll6: MOVLW .2 MOVWF TrlRpt ;ToneRepeat count Trll5: CALL SweepD ;Handles I/O CLRWDT ; MOVLW d'20' ;Wait 50ms ; CALL DLY_L ;2.5ms/Cycle DECFSZ TrlRpt,F GOTO Trll5 ;................... DECF TONE,F DECFSZ TrlSwp,F GOTO Trll6 ;------------------------------------ DECFSZ RptTn,F ;Dec & test for Zero GOTO TrUrpt ;Repeat... ;------------------------------------ RETURN ;-------------------------------------------------------------------------------- ;-------------------------------------------------------------------------------- ;Sweep trill Down- ; RptS - Contains number time to repeat. If Zero, just once. Exits with RptS set to Zero Trlldwn: INCF RptTn,F ;If 'Repeat' is zero, its now 1. After DECSZ it will be zero again ;------------------------------------ TrDrpt: MOVLW .30 MOVWF TrlSwp ;Trill Repeat count for sweep MOVLW .10 MOVWF TONE ;Set TONE start count MOVLW .12 MOVWF TONEDUR ;Set TONE Duration MOVLW .15 MOVWF SWPDUR ;Set Duration of sweep Trll8: MOVLW .2 ; MOVLW .1 MOVWF TrlRpt ;ToneRepeat count Trll7: CALL SweepD ;Handles I/O CLRWDT ; MOVLW d'20' ;Wait 50ms ; CALL DLY_L ;2.5ms/Cycle DECFSZ TrlRpt,F GOTO Trll7 ;................... INCF TONE,F INCF TONE,F DECFSZ TrlSwp,F GOTO Trll8 ;------------------------------------ DECFSZ RptTn,F ;Dec & test for Zero GOTO TrDrpt ;Repeat... ;------------------------------------ RETURN ;-------------------------------------------------------------------------------- ;-------------------------------------------------------------------------------- Chips2: MOVLW .7 MOVWF TrlRpt ;ToneRepeat count Chp1LP: MOVLW .50 MOVWF TONE ;Set TONE start count MOVLW .55 MOVWF TONEDUR ;Set TONE Duration MOVLW .5 MOVWF SWPDUR ;Set Sweep Duration CALL SweepU ;Handles I/O MOVLW d'100' ;Wait 25ms CALL DLY_L ;2.5ms/Cycle DECFSZ TrlRpt,F GOTO Chp1LP RETURN ;-------------------------------------------------------------------------------- ;-------------------------------------------------------------------------------- Chips1: MOVLW .20 MOVWF TrlRpt ;ToneRepeat count Chp2LP: MOVLW .25 MOVWF TONE ;Set TONE start count MOVLW .15 MOVWF TONEDUR ;Set TONE Duration MOVLW .10 MOVWF SWPDUR ;Set Sweep Duration CALL SweepU ;Handles I/O MOVLW d'10' ;Wait 25ms CALL DLY_L ;2.5ms/Cycle DECFSZ TrlRpt,F GOTO Chp2LP RETURN ;-------------------------------------------------------------------------------- ;================================================================================ ; SWEEPS- ;Tone/TonDur: 50/25- Drip sound, 30/25- Higher Drip ;If tone value is to short, tone 'Wraps-around' ;-------------------------------------------------------------------- ;-------------------------------------------------------------------- ;Sweep-Up Enter with TONE, TONEDUR, & SWPDUR set. ;These values will NOT be CHANGED, but used to set: ; DLY_CNT, TonCnt, DurCnt, & SwpCnt SweepU: ; Init. I/O - CALL StUpPzo ;Save prior TRIS0, and set GP4,5 for Piezo ;------------------------------------ MOVF TONE,W MOVWF TonCnt MOVF TONEDUR,W MOVWF DurCnt SwpLP2: ; MOVLW d'8' ; 1 MOVF SWPDUR,W ;Get # of cycles of tone MOVWF SwpCnt ;Init Tone Cycles 1 ;------------------------------------ SwpLP: MOVF TonCnt,W ;Get Value for Tone Delay: 1 MOVWF DLY_CNT ; Store loop cnt passed in 'W' 1 ;------------------------------------ SWP_LP: DECFSZ DLY_CNT,F ; Dec TONE count & Skp if Zero 1 (2) GOTO SWP_LP ; Not zero, so loop 2 V MOVLW b'00110000' ; 1 XORWF GPIO,F ;Flip port bits to sound Piezo 1 ;------------------------------------ DECFSZ SwpCnt,F ;Dec Cycle Counter & Skip if Z 1 ((2))Exit GOTO SwpLP ; 2 CLRWDT ;------------------------------------ DECF TonCnt,F ;Increase Pitch DECFSZ DurCnt,F ;Dec DurCnt & Skip if Zero 1 (2) GOTO SwpLP2 ; 2 V ;------------------------------------ GOTO RelxPzo ;Go Relax Piezo and RETURN- ;-------------------------------------------------------------------- ;-------------------------------------------------------------------- ;Sweep-Down Enter with TONE, TONEDUR, & SWPDUR set. ;These values will NOT be CHANGED, but used to set: ; DLY_CNT, TonCnt, DurCnt, & SwpCnt SweepD: ; Init. I/O - CALL StUpPzo ;Save prior TRIS0, and set GP4,5 for Piezo ;------------------------------------ SwDrpt: MOVF TONE,W MOVWF TonCnt MOVF TONEDUR,W MOVWF DurCnt SwpDLP2: ; MOVLW d'8' ;Use for testing- 1 MOVF SWPDUR,W ;Get # of cycles of tone MOVWF SwpCnt ;Init Tone Cycles 1 ;------------------------------------ SwpDLP: MOVF TonCnt,W ;Get Value for Tone Delay: 1 MOVWF DLY_CNT ; Store loop cnt passed in 'W' 1 ;------------------------------------ SWPD_LP: DECFSZ DLY_CNT,F ; Dec TONE count & Skp if Zero 1 (2) GOTO SWPD_LP ; Not zero, so loop 2 V MOVLW b'00110000' ; 1 XORWF GPIO,F ;Flip port bits to sound Piezo 1 ;------------------------------------ DECFSZ SwpCnt,F ;Dec Cycle Counter & Skip if Z 1 ((2))Exit GOTO SwpDLP ; 2 CLRWDT ;------------------------------------ ; INCF TonCnt,F ;Decrease Pitch INCFSZ TonCnt,F ;Decrease Pitch GOTO PssCnt DECF TonCnt,F ;Dec back to FFh PssCnt: DECFSZ DurCnt,F ;Dec DurCnt & Skip if Zero 1 (2) GOTO SwpDLP2 ; 2 V ;-----V now RelxPzo V------------------- ;= = = = Exit Point for sound routines = = = = = = = = = = = = = = = = = = = = = = = = = = = ;Relax Piezo, Turn of GP5- Aux Power- IR Rcvr Modl & RETURN- RelxPzo: BCF GPIO, 4 ;Ground Both Piezo terminals BCF GPIO, 5 ;...and allow piezo to discharge MOVLW d'20' CALL DLY_S ;@ 10uS per count = .2ms MOVF TrisTmp,W ;Get Prior I/O state- BSF STATUS,RP0 ; Set Bank 1 <<--+--### MOVWF TRISIO ; & restore it... BCF TRISIO,5 ;Aux Pwer off- IR Rcvr Modl BCF STATUS,RP0 ; Set Bank 0 <<--+--### BCF GPIO,5 ;IR Rvcr Mod OFF ;--------------- ;;;RetDly: Return ;= = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ;= = = = Setup for sound routines = = = = = = = = = = = = = = = = = = = = = = = = = = = = = ; Configure I/O to drive Piezo buzzer; Save state of TRISO and ;restore it when Piezo routine ends. ;NOTE::: For added volume, Piezo is driven differentialy. This can cause a problem for the ;PIC's I/O port- the sudden reversal of port states creates a 2x Vcc spike that can change ;the I/O state. If Both ports become matched, XORing the port with '11' will not create the ;differential change to sound the Piezo. The Remidy to this problem is to insert 100ohms ;minumum in series with the Piezo to limit the current. Too small a valure and the port ;will latch; Too big a value and the Piezo sound will be faint. StUpPzo: BSF STATUS,RP0 ; Set Bank 1 <<--+--### MOVF TRISIO,W ; Get I/O state- BCF TRISIO,4 ;Set GP4 as Output BCF TRISIO,5 ;Set GP5 as Output BCF STATUS,RP0 ; Set Bank 0 <<--+--### MOVWF TrisTmp ; Save I/O state- BSF GPIO, 4 ;Set complementary Piezo output bits BCF GPIO, 5 ;--------------- Return ;------------------------------------------------------- ;------------------------------------------------------- Tone0x: MOVLW d'50' MOVWF TONEDUR ;Save Duration passed in W MOVLW d'237' ;Tone - Low GOTO MTone ;(sub will store TONE) Tone1x: MOVLW d'101' MOVWF TONEDUR ;Save Duration passed in W MOVLW d'117' ;Tone - Mid GOTO MTone ;(sub will store TONE) Tone52: MOVLW d'190' ;* 8 cycles of tone = .288 sec of tone MOVWF TONEDUR ;Save Duration passed in W MOVLW d'29' ;Tone GOTO MTone ;(sub will store TONE) Tone72: MOVLW d'226' ;* 8 cycles of tone = .288 sec of tone MOVWF TONEDUR ;Save Duration passed in W MOVLW d'24' ;Tone - Hi GOTO MTone ;(sub will store TONE) ;------------------------------------------------------- ;------------------------------------------------------- ; Tone Subroutine- Recieves TONE value in 'W' ; Duration already set in TONEDUR. ; Counts out bit transitions to set duration for each note - ; Not dependent on hardware- WDT or Interups MTone: CLRWDT MOVWF TONE ;Save TONE passed in W ; Init. I/O - CALL StUpPzo ;Save prior TRIS0, and set GP4,5 for Piezo ;------------------------------------ MToneLP2: MOVLW d'8' ; 1 MOVWF TONE8 ;Init for 8 Cycles of Tone 1 MToneLP: MOVF TONE,W ;Get Value for Tone Delay: 1 MOVWF DLY_CNT ; Store loop cnt passed in 'W' 1 NM_LOOP: DECFSZ DLY_CNT,F ; Dec TONE count & Skp if Zero 1 (2) GOTO NM_LOOP ; Not zero, so loop 2 V MOVLW b'00110000' ; 1 XORWF GPIO,F ;Flip port bits to sound Piezo 1 DECFSZ TONE8,F ;Dec 8 Cycle Counter & Skip if Z 1 ((2))Exit GOTO MToneLP ; 2 DECFSZ TONEDUR,F ;Dec TONDUR & Skip if Zero 1 (2) GOTO MToneLP2 ; 2 V GOTO RelxPzo ;Go Relax Piezo and RETURN- ;--------------- ;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ;XXXXXXXXXXXXXXXXXXXXXX MOVES XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ;------------------------------------------------------- ;Stop Motor & leave in Fwd state- Same as Enable Motor to save code space- ; and assures I/O is configured for motor control. STOP: ;Enable Motor & prevent 'Shoot Through'- EnMot: BSF STATUS,RP0 ; Set file register to BANK 1 <<--+--### BCF TRISIO,0 ;Enable GP0 as Output- Motor On/Off ;For HXB14c: BCF TRISIO,2 ;Enable GP2 as Output- Motor Direction ;For HXB27e- BCF TRISIO,1 ;Enable GP1 as Output- Motor Direction BCF STATUS,RP0 ; Re-set file register to BANK 0 <<--+--### BCF GPIO,0 ;Assure Motor is OFF to prevent 'Shoot-Thru' ;-- NOTE: ----------- ;Provide delay for inductive kick from motor to dissipate. MOVLW .4 CALL DLY_L ;@ 2.5mS per count = 10ms ;---------------------- ;For HXB14c: BSF GPIO,2 ;LeaveMotor in FWD Direction ;For HXB27e- BCF GPIO,1 ;LeaveMotor in FWD Direction RETURN ;------------------------------------------------------- ;------------------------------------------------------- ; Motor ON- GP0 ;WAS for HXB14c- ; Motor DIR- GP2 '1'= Forward, '0'= Reverse Turn ;NEW For HXB27e- Motor DIR- GP1 '0'= Forward, '1'= Reverse Turn ;Motor ON, FWD (Pin 7) Fwrd: CALL EnMot ;Set I/O to enable Motor Control BSF GPIO,0 ;Motor ON RETURN ;-------------------------------------------------- ;-------------------------------------------------- ;Motor ON, REV (Pin 7) Rvrs: CALL EnMot ;Set I/O to enable Motor Control ;For HXB14c: BCF GPIO,2 ;Set REVERSE Direction ;For HXB27e- BSF GPIO,1 ;Set REVERSE Direction BSF GPIO,0 ;Motor ON RETURN ;================================================== ;-------------------------------------------------- ;Reverse X steps Rvrs0: CALL Rvrs MOVLW .5 ;Reverse turn- 45 deg -TURN CALL DLY_X ;100ms/Cnt CALL STOP RETURN ;-------------------------------------------------- ;-------------------------------------------------- ;Reverse X steps (.5 sec) - Use for Strat-up test & Dance routine Rvrs1: CALL Rvrs MOVLW .15 ;Reverse turn- 90deg -TURN CALL DLY_X ;100ms/Cnt CALL STOP RETURN ;-------------------------------------------------- ;-------------------------------------------------- ;Reverse X steps Rvrs2: CALL Rvrs MOVLW .28 ;Reverse turn- 180deg -TURN AROUND CALL DLY_X ;100ms/Cnt CALL STOP RETURN ;-------------------------------------------------- ;-------------------------------------------------- ;Reverse X steps Rvrs3: CALL Rvrs MOVLW .50 ;Reverse turn- 270deg -LEFT CALL DLY_X ;100ms/Cnt CALL STOP RETURN ;-------------------------------------------------- ;-------------------------------------------------- ;Forward X steps (.5 sec) - Use for Strat-up test & Dance routine Fwrd1: CALL Fwrd MOVLW .5 ;X steps Forward- Use for Dance routine CALL DLY_X ;100ms/Cnt CALL STOP RETURN ;-------------------------------------------------- ;-------------------------------------------------- ;Forward X steps- Use for Dance routine Fwrd2: CALL Fwrd MOVLW .15 ;X steps Forward- Use for Dance routine CALL DLY_X ;100ms/Cnt CALL STOP RETURN ;-------------------------------------------------- ;-------------------------------------------------- ;Forward X steps- Use for Dance routine Fwrd3: CALL Fwrd MOVLW .25 ;X steps Forward- Use for Dance routine CALL DLY_X ;100ms/Cnt CALL STOP RETURN ;------------------------------------------------------- ;--------------------------------------------------------------------------- ;============================================================================= ;========= Sub. =========================================================== ;Get Battery status: ; NOTE: RECALCULATE REF.TABLE FOR TYPE OF BATTERY ; 2x 1.5 volt Alkaline batteries ; 2x 1.25 volt NiMh batteries ; 1x 3.75 volt LiIon batteries ;;;;;;; ;New for HXB27e===>> ;;;;;;; ;Set up ADC using Vcc as ref. Turn on Vref (GP1), and charge Vref Cap. ; Set GP1 as DigInput, Enaable Analog on AN-1 and measure AN-1. ;If Vcc is 2.5VDC, then 1.22 Vref will read as 47.84% of the ADC range. ;With a Max count of 1024, 490 = Vcc = 2.5vdc. ;At a min battery voltage of 2.2, the ADC would read Vref as 568. ; At Vbat of 4.9vdc, Vref would read 255. ;Since the ADC output is 10bits, and the buss is 8bits, both ADRESH, & ADRESL need ;to be read if the data is right justified. ; If it is left justified, only ADRESh needs to be read, and the value considerd to be ;divided by 4. [Note: ADRESH is accessed in Bank-0, while ADRESL is in Bank-1.] ; For the selected voltages: 2.5vdc => 122; 2.2vdc => 142; ; . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ;Additional Notes- ;After power is applied to the ZXRE4041, 300uSec are needed for the Vref voltage ; across the .1uf cap to stabilize and settle. ;When Power is removed from the ZXRE4041, it takes 50uSec for the voltage to stabilize, ; and it will have dropped by 20mv. It will then decay at about 30mv per millisecond. ; Reading this voltage by loading the sample and hold capacitor does not seem to measurably lower it. ;If the ZXRE4041 capacitor voltage is used to drive the ADC's Vref input, ; The voltage will drop by 70mv durring the conversion. This is because a current of ~5.84uA ; will be drawn by the converter through the 12k resistor: I*R=V; 5.84ua * 12K = 70mv ;THEREFORE- If Vref is 1.225, the AD Converter ref will actual be: ; 1.225v - .020v - .070v = 1.135v This will drop an additional 40mv for every 1000usec ;after the initial 50us setteling from when the supply is turned off. ; . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . ; ADC registers for config: ADCON0, ANSEL ;The ANS7:ANS0 bits (ANSEL<7:0>) and the TRISIO bits control the operation of the A/D ;port pins. Set the corresponding TRISIO bits to set the pin output driver to its high ;impedance state (Digital In). Likewise, set the corresponding ANS bit to disable the digital ;input buffer. [Note: Analog voltages on any pin that is defined as a digital input ;(and not enabled as an ADC channel) may cause the input buffer to conduct excess current.] ;[Note: The GO/DONE bit should not be set in the same instruction that turns on the A/D.] ;------------------------------------------------------------ BattStat: BSF ADCON0,0 ;Turn on ADC ;Power-up Vref Chip- BSF STATUS,RP0 ; Set Bank 1 <<--+--### BCF TRISIO,1 ;==>> GP1 as Digital OUTPUT CLRF ANSEL ;All Analog Channels Disabled BCF STATUS,RP0 ; Set Bank 0 <<--+--### BSF GPIO,1 ;Turn Vref ON CLRWDT ;Clear WatchDogTimer to prevent interuptions CLRF ADRESH ;Clear High ADC data ;Time to allow charging & stabilization of Vref on C5, .1uf cap- ;XX MOVLW d'30' ;300uS ;XX CALL DLY_S ;@ 10uS per count MOVLW d'2' ;2 Cycle CALL DLY_L ;@ 2.5ms per cycle = 5ms ;Change GP1 to Digital IN, and ANALOG- BSF STATUS,RP0 ; Set Bank 1 <<--+--### BSF TRISIO,1 ;==>> GP1 as Digital INPUT ; bit: 76543210 (Low 4 bits Enabled channel/s 1=AnalogEnabled,Dig Disabled, 0=Digital) MOVLW b'01010010' ;AN1 enabled as Analog for ADC ; Set ADC Clock - Osc/16 = 4uS (1.6uS is minimum, but 4uS assures opperation at less than 3 Vdc.) ; Select/Enable Analog Channels (AN-1) MOVWF ANSEL ;==>> GP1 as ANALOG IN BCF STATUS,RP0 ; Set Bank 0 <<--+--### BCF GPIO,1 ;Turn Vref OFF for latter ; bit: 76543210 (bit 2,3 select channel to convert from) MOVLW b'00000101' ; Set: LEFT-JUSTIFIED, Vref to Vdd, Select Chanel AN-1, turn on ADC MOVWF ADCON0 ;------------------------------------------------------------ ; The minimum acquisition time is 20uS for the Sample & Hold capacitor to settle- ; Adjust settling time to allow for small inductive spike ;that momntariy supresses Vref @ Input pin- Scope showed ~50usec ; Vref will be 20mv less that spec. value due to initial decay. ;Vref will then decay at about 60mv/1ms. alowing a generous 100us/ setup-conversion-store, ;10 values could be read before the reference was down by 5%. MOVLW d'20' ;200usec Call DLY_S ;delay for 10uS/loop ;------------------------------------------------------------ ;Start ADC Conversion by turning GO/Done, bit-1 ON. BSF ADCON0,1 ;Conversion takes Max. 11 x ADC Clock = 11x4uS = 44uS: ADCDLY: MOVLW d'10' Call DLY_S ;10uS per count- 100uS ;Check for conversion complete: BTFSC ADCON0,1 ;Skip next if 'DONE'bit = 0 ; Conversion Complete. GOTO ADCDLY ;Delay again till conversion is complete, or WDT resets... ; Conversion Complete- Battery Status Value is now in 'ADRESH'; Turn off ADC. ;------------------------------------------------------------ BSF STATUS,RP0 ; Set Bank 1 <<--+--### CLRF ANSEL ;Deselect All analog BCF STATUS,RP0 ; Back to Bank 0 <<--+--### CLRF ADCON0 ;Turn Off ADC, Lowest Power Return ;/ / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / / ;*************************************************************************** ;--------------------------------------------------------------------------- ;============================================================================= ;========= Sub. =========================================================== ;Read Light Level from LED D1, GP2, AN-2; Uses Vcc as Vref- ;Does not use Vref, so motor can run while testing light level. ; LiteLvl: BSF ADCON0,0 ;Turn on ADC BSF STATUS,RP0 ; Set Bank 1 <<--+--### ;----------- Clear ADC ------------ CLRF ANSEL ;(In Bank 1) CLRF ADRESL ;Clear old ADC data- Low in Bank1 ;----------- Setup I/O to Digital Out, Discharge LED Cap. ------------ BCF TRISIO,2 BCF STATUS,RP0 ; Back to Bank 0 <<--+--### ;----------- Clear I/O ------------ BCF GPIO, 2 ;Ground LED & fully discharge MOVLW d'40' Call DLY_L ;delay for 2.5mS x 8 =20ms ;----------- Setup I/O Hi-Z/Dig Input ------------ BSF STATUS,RP0 ; Set Bank 1 <<--+--### BSF TRISIO,2 ; Set Port GP1(LED-D1) as Hi-Impedence MOVLW b'01010100' ;Clk=FOSC/16; Enable Analog, Set ADC Clock=FOSC/16, An2, An3 =ON MOVWF ANSEL ;(In Bank 1) BCF STATUS,RP0 ; Back to Bank 0 <<--+--### ;--------------------------------------------------------------------------------- ;--- SENSITIVITY -------------------------- ;---------------------------- ;Let .01uf caps accumulate charge from LEDs; Vref cap also charges- ; MOVLW d'50' ;125ms MOVLW d'100' ;250ms Works well for normal light levels ;; MOVLW d'150' ;375ms >> Best for .01uf cap << ;; MOVLW d'200' ;500ms - Higher sensitivity Call DLY_L ;delay for 2.5mS/cnt ;--------------------------------------------------------------------------------- ;--------------------------------------------------------------------------------- ;--------------------------------------------------------------------------------- ; Set ADC Clock - Osc/16 = 4uS (1.6uS is minimum, but 4uS assures opperation at less than 3 Vdc.) ; bit: 76543210 (Low 4 bits selcet enabled channel/s) ;Read voltage on .01uf LED Caps- ;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ;;;Save Motor Power status, and Turn OFF during ADC read- ;; BTFSC GPIO,MotPwr ;Test GPIO Motor PWR bit; Skip next if "OFF" ;; BSF StatFlgs,1 ;Set Prior Motor state flag to "ON" ;; BTFSS GPIO,MotPwr ;Test GPIO Motor PWR bit; Skip next if "ON" ;; BCF StatFlgs,1 ;Set Prior Motor state flag to "OFF" ;; BCF GPIO,MotPwr ;Turn Motor OFF ;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ; bit: 76543210 (bit 2,3 select channel to convert from) MOVLW b'10001001' ; Set: RIGHT-JUSTIFIED-Low 8 of 10, Vref to Vdd, Select Chanel AN-2, turn ON ADC [ADRESL] MOVWF ADCON0 ; The minimum acquisition time is 20uS for the Sample & Hold capacitor to settle, ; MOVLW d'7' ;70uS MOVLW d'20' ;200uS Let Motor settle Also Call DLY_S ;delay for 10uS/loop ;------------------------------------- ;Start ADC Conversion by turning GO/Done, bit-1 ON. BSF ADCON0,1 ;Conversion takes Min. 11 x ADC Clock = 11x4uS = 44uS: ADCDLY2: MOVLW d'10' ; 100us Call DLY_S ;delay for 10uS/loop ;Check for conversion complete: BTFSC ADCON0,1 ;Skip next if 'DONE'bit = 0 ; Conversion Complete. GOTO ADCDLY2 ;Delay again till conversion is complete, or WDT resets... ; Conversion Complete- Battery Status Value is now in 'ADRESH'; Turn off ADC. ;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ;Return Motor prior state- (Motor is current OFF; Only have to check for prior ON state ;; BTFSC StatFlgs,1 ;Test prior Motor PWR statebit; Skip next if "OFF" ;; BSF GPIO,MotPwr ;Turn Motor back "ON" ;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ; ------------------------------------------------------- ;Get ADC Data - ; MOVF ADRESH,W ;Get voltage (Left Justified-) level into 'W'(Bank 0) BSF STATUS,RP0 ; Set Bank 1 <<--+--### CLRF ANSEL ;Deselect All analog inputs MOVF ADRESL,W ;Get voltage (Right Justified-) level into 'W'(Bank 1) BCF STATUS,RP0 ; Back to Bank 0 <<--+--### CLRF ADCON0 ;Disable ADC; Zero power condition, I/O can now be Digital RETURN ;With 'W' set to ADC value ;--------------------------------------------------------------------------- ;============================================================================= ;========= Sub. =========================================================== ; (See 'BattStat' for details) ;Red LEDs are used as "solar cells" to charge .01uf Cap; 100ms in modest light, sat. = 1v ;(First version used a .1uf Cap; smaller .01uf gives faster responce.) ;Because the Piezo is connected through the 200ohm resistors to the LED D2 port, Discharge ;the Piezo first, and set GP5 to Hi-Z. ;Turn Motor OFF during conversion to reduce electrical noise. ; Return to prior state when done ~300uS ; Returns Value in PhotoLv2 and PhotoLv4- ;------------- New procedure for HXB27e ==> PhotoStat: ;Clear Charge on LEDs & Piezo- BSF STATUS,RP0 ; Set Bank 1 <<--+--### ;----------- Setup ADC ------------ ; Set ADC Clock - Osc/16 = 4uS (1.6uS is minimum, but 4uS assures opperation at less than 3 Vdc.) ; bit: 76543210 (Low 4 bits selcet enabled channel/s) CLRF ANSEL ;(In Bank 1) CLRF ADRESL ;Clear old ADC data- Low in Bank1 ;----------- Setup I/O to Digital Out ------------ BCF TRISIO,1 ;Vref as Outpus BCF TRISIO,2 ;Set LEDs & Piezo as Outpus BCF TRISIO,4 BCF TRISIO,5 BCF STATUS,RP0 ; Back to Bank 0 <<--+--### ;----------- Clear I/O ------------ BSF GPIO, 1 ;Charge Vref Cap. BCF GPIO, 2 ;Ground Piezo terminals & LEDs BCF GPIO, 4 ;to allow piezo BCF GPIO, 5 ;& LEDs Cap.to discharge ;; MOVLW d'8' MOVLW d'40' Call DLY_L ;delay for 2.5mS x 8 =20ms ;----------- Setup I/O Hi-Z/Dig Input ------------ BSF STATUS,RP0 ; Set Bank 1 <<--+--### BSF TRISIO,1 ; Set Vref Hi-Impedence BSF TRISIO,2 ; Set Port GP1(LED-D1) as Hi-Impedence BSF TRISIO,4 ; Set Port GP1(LED-D2) as Hi-Impedence BSF TRISIO,5 ; Set Piezo as Hi-Impedence MOVLW b'01011100' ;Clk=FOSC/16; Enable Analog, Set ADC Clock=FOSC/16, An2, An3 =ON MOVWF ANSEL ;(In Bank 1) BCF STATUS,RP0 ; Back to Bank 0 <<--+--### ;--------------------------------------------------------------------------------- ;--- SENSITIVITY -------------------------- ;---------------------------- ;Let .01uf caps accumulate charge from LEDs; Vref cap also charges- ; MOVLW d'50' ;125ms MOVLW d'100' ;250ms Works well for normal light levels ;; MOVLW d'150' ;375ms >> Best for .01uf cap << ;?; MOVLW d'200' ;500ms - Higher sensitivity Call DLY_L ;delay for 2.5mS/cnt ;;For .1uf cap- ;; MOVLW .4 ;400ms - Higher sensitivity ;; Call DLY_X ;delay for 100mS/cnt ;--------------------------------------------------------------------------------- BSF STATUS,RP0 ; Set Bank 1 <<--+--### BSF TRISIO,1 ; Set Vref Hi-Impedence MOVLW b'01011110' ;Now end charging of Vref Cap, An1 =ON MOVWF ANSEL ;(In Bank 1) BCF STATUS,RP0 ; Back to Bank 0 <<--+--### ;--------------------------------------------------------------------------------- ;Read voltage on .01uf LED Caps- ;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ;Save Motor Power status, and Turn OFF during ADC read- BTFSC GPIO,MotPwr ;Test GPIO Motor PWR bit; Skip next if "OFF" BSF StatFlgs,1 ;Set Prior Motor state flag to "ON" BTFSS GPIO,MotPwr ;Test GPIO Motor PWR bit; Skip next if "ON" BCF StatFlgs,1 ;Set Prior Motor state flag to "OFF" BCF GPIO,MotPwr ;Turn Motor OFF ;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ; bit: 76543210 (bit 2,3 select channel to convert from) MOVLW b'10000101' ; Set: RIGHT-JUSTIFIED-Low 8 of 10, Vref to Vdd, Select Chanel AN-1, turn ON ADC [ADRESL] ;; MOVLW b'00000101' ;;; Set: LEFT-JUSTIFIED-Hi 8 of 10, Vref to Vdd, Select Chanel AN-1, turn on ADC [ADRESH] MOVWF ADCON0 ; The minimum acquisition time is 20uS for the Sample & Hold capacitor to settle, ; MOVLW d'7' ;70uS MOVLW d'20' ;200uS Let Motor settle Also Call DLY_S ;delay for 10uS/loop ;Start ADC Conversion by turning GO/Done, bit-1 ON. BSF ADCON0,1 ;------------------------------------- ;Conversion takes Min. 11 x ADC Clock = 11x4uS = 44uS: ADCDLY3: MOVLW d'10' ; 100us Call DLY_S ;delay for 10uS/loop ;Check for conversion complete: BTFSC ADCON0,1 ;Skip next if 'DONE'bit = 0 ; Conversion Complete. GOTO ADCDLY3 ;Delay again till conversion is complete, or WDT resets... ; Conversion Complete- Battery Status Value is now in 'ADRESH'; Turn off ADC. ;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ;Return Motor prior state- (Motor is current OFF; Only have to check for prior ON state BTFSC StatFlgs,1 ;Test prior Motor PWR statebit; Skip next if "OFF" BSF GPIO,MotPwr ;Turn Motor back "ON" ;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ; ------------------------------------------------------- ;Get ADC Data - ; MOVF ADRESH,W ;Get voltage (Left Justified-) level into 'W'(Bank 0) BSF STATUS,RP0 ; Set Bank 1 <<--+--### CLRF ANSEL ;Deselect All analog inputs MOVF ADRESL,W ;Get voltage (Right Justified-) level into 'W'(Bank 1) BCF STATUS,RP0 ; Back to Bank 0 <<--+--### CLRF ADCON0 ;Disable ADC; Zero power condition, I/O can now be Digital Return ;With 'W' set to ADC value ;\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ PhotoOut: CALL PhotoStat ;Return light level in 'W'; Low 8 bits of 10 ; MOVF ADRESL,W ;Get Low voltage level- IN BANK 1 ; MOVF ADRESH,W ;Get High voltage level- IN BANK 0 ; MOVLW d'237' ; FOR TESTING 'OutByt'-- Wout: MOVWF OutByt ;Save for converstion... CALL STOP MOVLW d'5' ;Wait .5 sec CALL DLY_X ;100ms/Cycle ;Convert value passed in OutByt to 3 Decimal Digits: CALL DeciDig ;Output value as 'Beeps' MOVLW d'5' ;Wait .5 sec CALL DLY_X ;100ms/Cycle CALL Tone72 CALL Tone52 MOVLW d'5' ;Wait .5 sec CALL DLY_X ;100ms/Cycle RETURN ;--------------------------------------------------------------------------- ;============================================================================= ;========= Sub. =========================================================== ; Test light level and Sleep if low. Stays in Sub until light level rises above ; the choosen trigger point. Trills down upon ging to sleep, Trill-Up upon ligh rise. ;Saves Light level in 'PhotoLv'x''; Compares to 'LightLVL' for trip point. Call LiteLvl[WAS= PhotoStat]. ;;=> Test twice to be sure ADC didn't just see a Vcc glitch PhotoTst: CALL LiteLvl ;Get light level, returned in 'W' ;;WAS ==>> CALL PhotoStat ;Get light level, returned in 'W' MOVWF PhotoLv2 ;Save value ;;======================================================== ;;======================================================== ;;ENABLE THIS BLOCK OF CODE TO TEST LIGHT LEVELS- ; MOVWF OutByt ;Save for converstion... ; CALL DeciDig ;Output value as 'Beeps' ; MOVLW d'5' ;Wait .5 sec ; CALL DLY_X ;100ms/Cycle ; CALL Tone72 ; CALL Tone52 ;;======================================================== ;;======================================================== MOVF PhotoLv2,W ;Get Saved value MOVLW LightLVL ;Get set trigger level light value ==> DEFINED AT START OF CODE <== SUBWF PhotoLv2,W ;PhotoLv2 - LightLVL => W, z,c BTFSS STATUS,Carry ;If carry is set, PhotoLv2 is higher than trip point LightLVL GOTO ItsDark ;Low Light Level, ;;test previous Light Level... ;Bright Light Level... BTFSC StatFlgs,LastLvl ;Skip nxt if last was Dark; 0=Dark, 1=Light RETURN ;Last level was bright; No change, no trill-up ItsLight: BSF StatFlgs,LastLvl ;Set flag to Bright CLRF RptTn ;Set Repeat for 1 Trill Cycl CALL Trllup ;Up-Sweep Trill ;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ;Return Motor prior state- (Motor is currently OFF; Only have to check for prior ON state BTFSC StatFlgs,2 ;Test prior Motor PWR statebit; Skip next if "OFF" BSF GPIO,MotPwr ;Turn Motor back "ON" ;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX RETURN ;Exit Routine-- ;--------------------------------------- ;ItsDark2: ItsDark: BTFSS StatFlgs,LastLvl ;Skip nxt if last was Light; 0=Dark, 1=Light GOTO SlpLoop ;Last level was Dark; No change, no trill-up WasLight: ;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ;Save Motor Power status, and Turn OFF during ADC read- BTFSC GPIO,MotPwr ;Test GPIO Motor PWR bit; Skip next if "OFF" BSF StatFlgs,2 ;Set Prior Motor state flag to "ON" BTFSS GPIO,MotPwr ;Test GPIO Motor PWR bit; Skip next if "ON" BCF StatFlgs,2 ;Set Prior Motor state flag to "OFF" ;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX BCF GPIO,0 ;Motor OFF -If dark, STOP Immedialtly BCF StatFlgs,LastLvl ;Clear flag to Dark CLRF RptTn ;Set Repeat for 1 Trill Cycl CALL Trlldwn ;Dwn-Sweep Trill SlpLoop: BSF STATUS,RP0 ; Set Bank 1 <<--+--### MOVF TRISIO,W ;Get current I/O state MOVWF TRStmp ;temp Save TRSIO BCF STATUS,RP0 ; Back to Bank 0 <<--+--### MOVF GPIO,W ;Get current I/O MOVWF IOtmp ;temp Save IO CALL PwrDwn ;Power down all I/O SLEEP SLEEP ;4.8 sec sleep time... BSF STATUS,RP0 ; Set Bank 1 <<--+--### MOVF TRStmp,W ;Get Saved TRSIO MOVWF TRISIO ;Restore I/O state BCF STATUS,RP0 ; Back to Bank 0 <<--+--### MOVF IOtmp,W ;Get saved I/O MOVWF GPIO ;Restore IO GOTO PhotoTst ;Test for LIGHT... ;============================================================================= ;============================================================================= ;========= Sub. =========================================================== ;Put processor in lowest possible current consuption state- PwrDwn: MOVLW b'00000111' ; Turn Comparitor OFF 9Lowest Power consumption) MOVWF CMCON ; by setting CMCON<0:3> ;; USED BY 12F683-> MOVWF CMCON0 ; by setting CMCON0<0:3> CLRF GPIO BSF STATUS,RP0 ; Set Bank 1 <<--+--### CLRF ANSEL ;De-select all analog input channels to save power. MOVLW b'11111111' ; Set All bits to High Imp.input state- MOVWF TRISIO BCF STATUS,RP0 ; Back to Bank 0 <<--+--### CLRF GPIO ;All Ports= 0, for start-up Return ;NOTE: ; All ports should be in a High Impedence state. ;(TRIS bit=1, Port bit is High Impedence) ;NOTE: ; Both the comparator and voltage reference, if enabled ; before entering SLEEP mode, remain active during ; SLEEP. This results in higher SLEEP currents than ; shown in the power-down specifications. The ; additional current consumed by the comparator and the ; voltage reference is shown separately in the specifications. ; To minimize power consumption while in SLEEP ; mode, turn off the comparator, CM2:CM0 = 111, and ; voltage reference, VRCON<7> = 0. ;------------------------------------------ ;Extra-Long Delay - 'W' passes number of loops, 100ms ea ;Delays: 100ms to 25.5sec DLY_X: MOVWF XLNG_CNT ;Store number of 100ms loops- Max delay: 25.5sec MOVLW D'40' ;Set 40 loops at.. CALL DLY_L ; 2.5ms per loop DECFSZ XLNG_CNT,f ;If not 0, Execute next; If =0, Skip Next GOTO $-3 ;Run DELAY again RETURN ;------------------------------------------ ;Long Delay - 'W' passes number of loops, 2.5ms ea ;Delays: 2.5ms to .637ms DLY_L: MOVWF MID_CNT ;Store number of 2.5ms loops- Max delay: 637.5ms MOVLW D'250' ;Set 250 loops at.. CALL DLY_S ; 10us per loop DECFSZ MID_CNT,f ;If not 0, Execute next; If =0, Skip Next GOTO $-3 ;Run DELAY again RETURN ;------------------------------------------ ;Delay - 'W' passes number of loops, 10us ea ;Delays: 10 us to 2.55 ms ; Number of Delay loop passed in W & stored in DLY_CNT. Delay is ; ( 3 + (DLY_CNT * 10)) * 1us/Cycle = Delay ;With the 4mhz internal clock, 1 cycle is 1us ; Max delay is 3 + 255*10 = 2553 Cycles = 2.553ms Cycles DLY_S: MOVWF DLY_CNT ; Store loop cnt passed in 'W' (1) D1_LOOP: ; CALL RetDLY ;4 Cycles (4) GOTO $+1 ;2 Cycles (2) GOTO $+1 ;2 Cycles (2) ; NOP ; (1) CLRWDT ;Clear WDT (1) D2_LOOP: DECF DLY_CNT,F ; Subtract 1 (1) INCFSZ DLY_CNT,W ; Check for underflow (2) GOTO D1_LOOP ; Not zero so loop (2) RetDLY: ;Call this Return 'SUB' to delay 4 clock cycles: RETURN ; (2) ;---------------------------------------------------------------------------------------- ;--- Utillity Subroutines -------------------------- ;------------------------------------------- ;------------------------------------------------------------------ ; Get sensor value & output the decimal number between 0 & 255 as 'beeps': ;Low beep= Zero, One high beep for digit count ; Pause between digits ;End with decending tones ;********************************************************* ;XXXXXXXXXXXXXXXXX>>>- ;; FOR TESTING-- ;;FORTEST: ; MOVLW d'237' ; FOR TESTING-- ; MOVWF OutByt ;Save for converstion... ;------------------------------------------------------------------ ;Convert value passed in OutByt to 3 Decimal Digits: DeciDig: CLRF Ac100s ;clear the 100's counter CLRF Ac10s ;clear the 10's counter Sub100: MOVLW d'100' ;Check 100's (X) SUBWF OutByt,W ; (Y)-(X)=>'W' BTFSS STATUS,Carry ;If Carry is set, Number is > 100 GOTO Sub10 ;try 10's MOVWF OutByt ;Save reduced value, INCF Ac100s,F GOTO Sub100 Sub10: ;OutByt still has 10s,1s..... MOVLW d'10' ;Check 10's (X) SUBWF OutByt,W ; (Y)-(X)=>'W' BTFSS STATUS,Carry ;If Carry is set, Number is > 10 GOTO DigDone MOVWF OutByt ;Save 1'0s INCF Ac10s,F GOTO Sub10 DigDone: ;OutByt is now 1's ;------------------------------------------------------------------ ; Now, sound Beeps for Digits- CALL DigBeep ;Convert 100's in Ac100s, ; MOVLW d'10' ;~1 Sec Delay Between Digits MOVLW d'7' ;~1/2 Sec Delay Between Digits CALL DLY_X ;100ms/Cycle MOVF Ac10s,W MOVWF Ac100s Call DigBeep ;Convert 10s in Ac10s, ; MOVLW d'10' ;~1 Sec Delay Between Digits MOVLW d'7' ;~/21 Sec Delay Between Digits CALL DLY_X ;100ms/Cycle MOVF OutByt,W MOVWF Ac100s Call DigBeep ;,and 1s in OutByt. MOVLW d'7' ;~1/2 Sec Delay Between Digits CALL DLY_X ;100ms/Cycle ; Call HalfSecx ;~.5 Sec Delay Between Digits ; GOTO RptTests ;Repeat tests... RETURN ;------------------------------------------------------------------ ; Convert number passed in Ac100s to Beeps; If Zero, sound 1 low tone DigBeep: MOVF Ac100s,F ;Move to self- Set Zero bit in status... BTFSS STATUS,Z ;If Zero is set, skip and send Zero Beep GOTO ToneBit ;If Carry is Clear, not zero, sound Beeps... CALL Tone0x ;Sound Low note -Zero RETLW 0 ToneBit: MOVLW d'1' SUBWF Ac100s,F BTFSS STATUS,Carry ;If Carry is set, all 100's sent RETLW 0 ;No digits, return... Call Tone1x ; MOVLW d'5' ;~1/2 Sec Delay Between Digits MOVLW d'2' ;~1/2 Sec Delay Between Digits CALL DLY_X ;100ms/Cycle ; CALL HalfSecx GOTO ToneBit ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - ;******************************************************************************************** ;-------------------------------------------------- ;; ;Text is compressed- 7-bits ASCII char.s, 2 Char/14bit word ORG 0x3F0 DA "(c)2008 Applied Inspirations,LLC" ;******************************************************************************************** END ;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ;\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ;/////////////////////////////////////////////////////////////////////////////////////// ;\\\\\\\\\\\\\\\\\\\\\\\\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ;//////////////////////// XXXXXXXX X X XXXXX //////////////////////////////// ;\\\\\\\\\\\\\\\\\\\\\\\\ X X X X X X \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ;//////////////////////// X X X X X X ////////////////////////////// ;\\\\\\\\\\\\\\\\\\\\\\\\ XXXX X X X X X \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ;//////////////////////// X X X X X X ////////////////////////////// ;\\\\\\\\\\\\\\\\\\\\\\\\ X X X X X X \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ;//////////////////////// XXXXXXXX X X XXXXX //////////////////////////////// ;\\\\\\\\\\\\\\\\\\\\\\\\ \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ ;/////////////////////////////////////////////////////////////////////////////////////// ;\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\ . . .