;HxBg5_R1.ASM - HexBug Hack 5, Release 1 - 9/19/08 ; Personality 1 ;Modified from: HexBug Hack 5a - 1/2/08 ; ;-------------------------------- ; Target Proccessor is 12F675 - No PORTA,C ; Only GPIO & TRISIO ;--------- - ------------------ ; ;******************************************************************** ; * ; Filename: ;HxBg5.ASM * ; Date: 1/2/08 * ; File Version: REV-5, REL-1 * ; Author: P.T.Miller * ; Company: Applied Inspirations,LLC * ; * ;******************************************************************** ; This S/W is for the HXB14c circuit board configuration- * ;;; ,If using a PIC12F683- (2X Memory) >>>Note Differences<<< !! ;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; NOTE: ;; ;; This is just a starting place to get the Bug up & running. ;; ;; With these code elements in place, many more interesting ;; ;; behaviors/personalities can be created. ;; ;; Now, get busy, and be careful out there... ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; GP0 - Out, ALWAYS Motor ON/OFF ; GP1 - In/Out: Vref, LED; Also J12- I/O Expantion ; GP2 - Out Motor DIRECETION ; Also J15- I/O Expantion ; GP3 - In, Always Antena ; GP4 - In/Out: Activate/Read: Vref, Piezo; Also J14- I/O Expantion ; GP5 - Out: Piezo; Also J13- I/O Expantion- Power extra I/O ; Piezo- GP4,5 ; Motor ON- GP0 ; Motor DIR- GP2 '0'= Forward, '1'= Reverse Turn ;******************************************************************** ; 1) Initialize uC ; Testing- ; 2) Loop till Antena touched (1 sec delay within loop) ; 3) Sound Piezo- Trill up / Wait 1 sec ; 4) Light LED / Wait 1 sec ; 5a) Run forward / Wait 1 sec ; 5b) Reverse turn / Wait 1 sec ; 5c) Run forward / Wait 1 sec ; 6) Test Battery voltage, and output via "DeciDig"/Piezo ; WORKING- 12/27/07 ; ----- Now add 'Functions' ----- ; 7a) Atenae touch direction change ; 7b) 10 bumps, sounds 'complaint', restart prog. ; WORKING- 12/28/07 ; Update Sound routines- 12/29/07, 1/1/08 ; LED 'Eye' working (Sub: PhotoStat) 1/1/08 ; 'Radomize' collison sound 1/2/08 ; 8) 'Dance' pattern ; WORKING- 1/2/08 ; ;********************************************************************** ; ; FLASH SRAM EEPROM I/O 10-bit-A/D(ch) Comp Timers 8/16-bit ;PIC12F675 1024 64 128 6 4 1 2/1 ;PIC12F683 2048 128 256 6 4 1 2/1 ; ; ;********************************************************************** TITLE "PIC12F675_HexBugHack_5" list p=12f675 ; 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_OFF & _PWRTE_ON & _INTRC_OSC_NOCLKOUT __CONFIG _CP_OFF & _CPD_OFF & _BODEN_OFF & _MCLRE_OFF & _WDT_ON & _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 ;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 XLNG_CNT ; high 8 bits of counter for 'X'tra LoNG CouNTs ;Local temporary values- TEMP ;Temp High - USE ONLY WITHIN IMMIDIATE ROUTINE TEMPL ;Temp Low - USE ONLY WITHIN IMMIDIATE ROUTINE ;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 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 BmpCnt ;Count number of 'Bumps' ClrCnt ;Count Clear (No Bump) loops RndCnt ;Random Count StatFlgs ;Status Flags - Bit-0 ; Bit-1 ; Bit-4 ; 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 C Equ 0 ; Carry bit of STATUS Carry Equ 0 ; Carry bit of STATUS ; (((((((((((((((((((((( \ / )))))))))))))))))))))))) ; ((((((((((((((((((((((((((( V ))))))))))))))))))))))))))))) ;((((((((((((((((((((( <<<<<<< (*) >>>>>>> )))))))))))))))))))))))) ;* * * * * * * * Implementation Specific Data Values * * * * * * * * * * SleepCnts EQU d'10' ;Number of sleep cycles befor testing... ; Sleep ; Total Sleep time cycle 4.6 sec ; Sleep ; Total Sleep time cycle 6.9 sec ; Sleep ; Total Sleep time cycle 9.2 sec ; Sleep ; Total Sleep time cycle 11.5 sec ; 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.27-2.37v ;((((((((((((((((((((( <<<<<<< (*) >>>>>>> )))))))))))))))))))))))) ; ((((((((((((((((((((((((((( ^ ))))))))))))))))))))))))))))) ; (((((((((((((((((((((( / \ )))))))))))))))))))))))) ; ************************************************************ ; **************************************************************** ;********** CODE: ************* ; **************************************************************** ; ************************************************************ ;********************************************************************** ORG 0x000 ; processor reset vector GOTO MAIN ; go to beginning of program ;- - - - - - - - - - - - - - ORG 0x004 ; interrupt vector location MOVWF Wtmp ; save off current W register contents MOVF STATUS,w ; move status register into W register MOVWF STATtmp ; save off 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 ; interrupt vector location ;-------------------------------------- MAIN: ;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 <<--+--### 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 CLRF VRCON ; Voltage Ref section turned OFF- reduce power consumption CLRF INTCON ; Turn OFF Interups ;( 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 CMCON (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. MOVWF CMCON ; by setting CMCON<0:3> CLRF GPIO TstLp1: ;********************************************************************** ;** 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 erasure) MOVLW d'10' ;Wait 1 sec CALL DLY_X ;100ms/Cycle ;** ;********************************************************************** 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 <<--+--### ; ******************************** ;****** uC Setup Completed ******** ; ******************************** BSF GPIO,1 ;LED On MOVLW d'2' ;Wait .1 sec CALL DLY_L ;@ 2.5mS per count = 5ms BCF GPIO,1 ;LED Off BTFSC GPIO,3 ;Loop until Antena is touched.. GOTO TstLp1 ;------------------------------------------------------------ ;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ;=== SOUNDS Test ===================================== ;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX CALL Trllup ;Up-Sweep Trill MOVLW d'10' ;Wait 1 sec CALL DLY_X ;100ms/Cycle GOTO IOtst ;Now test I/O... ;;------------------------------------------------------------------- ;============================================================================= ; OR... Test All SOUNDS and FUNCTIONS- ;============================================================================= ;;------------------------------------------------------------------- CALL Chips1 MOVLW d'10' ;Wait 1 sec CALL DLY_X ;100ms/Cycle ;;------------------------------------------------------------------- CALL Grwl MOVLW d'10' ;Wait 1 sec CALL DLY_X ;100ms/Cycle ;;------------------------------------------------------------------- CALL OhUh1 ;"OK?" MOVLW d'10' ;Wait 1 sec CALL DLY_X ;100ms/Cycle ;;------------------------------------------------------------------- CALL OhOhOh MOVLW d'10' ;Wait 1 sec CALL DLY_X ;100ms/Cycle ;;------------------------------------------------------------------- CALL Chips2 MOVLW d'10' ;Wait 1 sec CALL DLY_X ;100ms/Cycle ;;------------------------------------------------------------------- CALL UhOh1 MOVLW d'10' ;Wait 1 sec CALL DLY_X ;100ms/Cycle ;;------------------------------------------------------------------- CALL Trlldwn ;Down-Sweep Trill MOVLW d'10' ;Wait 1 sec CALL DLY_X ;100ms/Cycle ;;------------------------------------------------------------------- CALL Trlla CALL Chips1 CALL Trlla CALL Trllb MOVLW d'10' ;Wait 1 sec CALL DLY_X ;100ms/Cycle ;------------------------------------------------------------------- CALL Trllup ;Down-Sweep Trill CALL Trlldwn ;Down-Sweep Trill CALL Trllb ;------------------------------------------------------------------- IOtst: BSF STATUS,RP0 ; Set Bank 1 <<--+--### MOVLW b'11001000' ; Define I/O MOVWF TRISIO ; Set Port A I/O: 1= Input, 0= Output BCF STATUS,RP0 ; Set Bank 0 <<--+--### ;-- Test GPIO-1, Test LED (Pin 6) BSF GPIO,1 MOVLW d'5' ;Wait .5 sec CALL DLY_X ;100ms/Cycle BCF GPIO,1 ;LED Off ;-- Test GPIO-0, Motor ON, FWD (Pin 7) ;Fwrd: - Rvrs: ; BSF GPIO,2 ;Direction ; BSF GPIO,0 ;Motor ON CALL Fwrd MOVLW d'10' ;Wait 1 sec CALL DLY_X ;100ms/Cycle BCF GPIO,0 ;Motor OFF MOVLW d'20' CALL DLY_L ;@ 2.5mS per count = 50ms ;NOTE: Always turn motor OFF BEFORE CHANGING DIRECTION to prevent 'shoot-thru' ;-- Test GPIO-2, Direction-REV (Pin 5) ; BCF GPIO,2 ; BSF GPIO,0 ;Motor ON CALL Rvrs MOVLW d'15' ;Wait 1.5 sec CALL DLY_X ;100ms/Cycle BCF GPIO,0 ;Motor OFF MOVLW d'20' CALL DLY_L ;@ 2.5mS per count = 50ms ;-- Test GPIO-2, Direction-FWD (Pin 5) ; BSF GPIO,2 ;Direction ; BSF GPIO,0 ;Motor ON CALL Fwrd MOVLW d'10' ;Wait 1 sec CALL DLY_X ;100ms/Cycle ;-Turn Motor OFF- BCF GPIO,0 ;Motor Off MOVLW d'5' ;Wait 1/2 sec CALL DLY_X ;100ms/Cycle ;;--------------------------------------------------------------------- ;PhotLvl: ;;Read Phot Voltage level- ; CLRWDT ;;Uses Right-Justified, Low Byte is in BANK-1 ; CALL PhotoStat ;Returns with light level in 'W' ; MOVWF OutByt ;Save for converstion... ;;Convert value passed in OutByt to 3 Decimal Digits: ; CALL DeciDig ;Output value a 'Beeps' ; ; MOVLW d'10' ;Wait 1 sec ; CALL DLY_X ;100ms/Cycle ; ; CALL Tone2 ; CALL Tone3 ; MOVLW d'10' ;Wait 1 sec ; CALL DLY_X ;100ms/Cycle ;;------------------------------------------------------------------- ;============================================================================= ;============================================================================= ;;------------------------------------------------------------------- BattLvl: BSF STATUS,RP0 ; Set Bank 1 <<--+--### MOVLW b'11001000' ; Define I/O MOVWF TRISIO ; Set Port A I/O: 1= Input, 0= Output BCF STATUS,RP0 ; Set Bank 0 <<--+--### ;Read Battery Voltage level- CLRWDT CLRF ADRESH CALL BattStat MOVF ADRESH,W ;Get voltage level ; MOVLW d'237' ; FOR TESTING-- ; MOVLW d'23' ; FOR TESTING-- 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 Tone3 CALL Tone2 ; CALL BEEP MOVLW d'10' ;Wait 1 sec CALL DLY_X ;100ms/Cycle ;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ;-------------- Wait for antenna touch to go into full opperation ------------- ;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX OprLP: MOVLW d'10' ;Wait 1 sec CALL DLY_X ;100ms/Cycle BSF GPIO,1 ;LED On MOVLW d'2' ;Wait .1 sec CALL DLY_L ;@ 2.5mS per count = 5ms BCF GPIO,1 ;LED Off BTFSC GPIO,3 ;Loop until Antena is touched.. GOTO OprLP ;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ;--------------------------------------------------------------------- RUNN: 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 <<--+--### CALL Trllup ;Up-Sweep Trill ;--------------------------------------------------------------------- ;Init. 'Bump' & 'Clear' Counters- MOVLW .10 ;Obstical Bumps till re-start- MOVWF BmpCnt CLRF ClrCnt ;Clear the 'No Bumps' Count FwrdLp: ;-------------------------------------------------------------------------- ;;;;; ADD BATT TEST ----------- ;-------------------------------------------------------------------------- INCF ClrCnt,F ;-- GPIO-0, Motor ON, FWD (Pin 7) CALL Fwrd ;MOTOR ON FORWARD <<--=========<<< ;-------------------------------------------------------------------------- ;For Testing- Remove to consrve Power - BSF GPIO,1 ;LED On MOVLW d'2' CALL DLY_L ;@ 2.5mS per count = 5ms BCF GPIO,1 ;LED Off MOVLW d'100' CALL DLY_L ;@ 2.5mS per count = .25 sec ;-------------------------------------------------------------------------- ;If ClrCnt = 120, (30sec) Celebrate- MOVLW .120 ;Set 'W' to actuate count XORWF ClrCnt,W BTFSC STATUS,Z ; skip next if not ZERO... GOTO Celbrt ;-------------------------<<<<<<<<<<<<<<<<<<<<<---------------------------- BTFSC GPIO,3 ;Loop until Antena is touched.. GOTO FwrdLp ;NO BUMP- FORWARD LOOP... ;............. BCF GPIO,0 ;Motor OFF MOVLW d'1' ;Wait .1 sec CALL DLY_X ;100ms/Cycle ;==================================SSSSSSSSSSSSSSSSSSSSSSSSSSSS============ CALL BmpSnd ;Semi-Random selection of bump sound DECF BmpCnt,F ;Dec Bump Count- CLRF ClrCnt ;Clear the 'No Bumps' Count (start over) ;Reverse turn 90 Deg.- ;--------------------------- ;;on 9th bump, throw tantrum- ; MOVLW .9 ;Set 'W' to actuate count ; XORWF BmpCnt,W ; BTFSC STATUS,Z ; skip next if not ZERO... ; GOTO TanTrm ;--------------------------- ;On 10th bump, throw tantrum- MOVF BmpCnt,W ANDWF BmpCnt,W ;Set Z BTFSC STATUS,Z ; skip next if not ZERO... GOTO TanTrm ;--------------------------- CALL Rvrs MOVLW d'15' ;Wait 1.5 sec CALL DLY_X ;100ms/Cycle BCF GPIO,0 ;Motor OFF MOVLW d'20' CALL DLY_L ;@ 2.5mS per count = 50ms GOTO FwrdLp ;================================================================ TanTrm: CALL Grwl BCF GPIO,0 ;Motor OFF MOVLW d'2' ;Wait .2sec CALL DLY_X ;100ms/Cycle CALL Grwl ;............ CALL Rvrs MOVLW d'3' ;Wait .3 sec CALL DLY_X ;100ms/Cycle BCF GPIO,0 ;Motor OFF CALL Chips2 CALL Fwrd MOVLW d'3' ;Wait .3 sec CALL DLY_X ;100ms/Cycle BCF GPIO,0 ;Motor OFF CALL Rvrs MOVLW d'4' ;Wait .4 sec CALL DLY_X ;100ms/Cycle BCF GPIO,0 ;Motor OFF CALL Chips2 CALL Fwrd MOVLW d'5' ;Wait .5 sec CALL DLY_X ;100ms/Cycle BCF GPIO,0 ;Motor OFF ;............ CALL Grwl CALL Rvrs MOVLW d'3' ;Wait .3 sec CALL DLY_X ;100ms/Cycle BCF GPIO,0 ;Motor OFF CALL Chips2 CALL Fwrd MOVLW d'3' ;Wait .3 sec CALL DLY_X ;100ms/Cycle ;............ CALL Rvrs MOVLW d'3' ;Wait .3 sec CALL DLY_X ;100ms/Cycle BCF GPIO,0 ;Motor OFF CALL Trlldwn MOVLW d'200' ;Wait 20 sec CALL DLY_X ;100ms/Cycle GOTO RUNN ;Start New ;================================================================ ;30 sec no bumps- Celbrt: BCF GPIO,0 ;Motor OFF MOVLW d'2' ;Wait .2sec CALL DLY_X ;100ms/Cycle CALL OhUh1 ;"OK?" MOVLW d'10' ;Wait 1sec CALL DLY_X ;100ms/Cycle ;===================================================== ;====== Turn in reverse while 'singing' ====== ;==== (Create more interseting SONG & DANCE here) ==== CALL Rvrs CALL Trllup ;Up-Sweep Trill MOVLW d'4' ;Wait .4 sec CALL DLY_X ;100ms/Cycle CALL Trlldwn MOVLW d'4' ;Wait .4 sec CALL DLY_X ;100ms/Cycle CALL Trlla CALL Trllb CALL Trlla CALL Trllup ;Up-Sweep Trill MOVLW d'4' ;Wait .4 sec CALL DLY_X ;100ms/Cycle CALL Trlldwn MOVLW d'4' ;Wait .4 sec CALL DLY_X ;100ms/Cycle ;====== ====== ;===================================================== BCF GPIO,0 ;Motor OFF MOVLW d'100' ;Wait 10sec CALL DLY_X ;100ms/Cycle CALL Trlla CALL Chips1 CALL Trlla CALL Trllb ;===== Now, Do it all over again ===== GOTO RUNN ;Start New ;* * * * * * * * * * * * * * * * * * * * * * * * * ;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ; Use this code block to setup 'Sleep' mode, very low power- ;==================================================================== ;=======SSSSSSSSSSSSSSSSSSS sleep SSSSSSSSSSSSSSSSSSSSSSSSS======== SleepLoop: ; Save power during SLEEP: MOVLW SleepCnts ;Set number of SLEEP Cycles MOVWF SnoozCnt CALL PwrDwn ;Lowet power condition, modifies port config. to All Inputs- 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 ; Button pushed Exit Sleep Cycles... DECFSZ SnoozCnt,F ;Dec Snooze counter, Skip GOTO if tim to wake GOTO SnoozLoop ; Total Sleep time cycle: 130 * 13.8 sec = 1794 sec = 29.9 min ; CLRF ANSEL ;Turned OFF Analog functions <<< DONE IN PwrDwn ; CALL Note_A0 ;<<< FOR TESTING - Beep at wake-up ; - - - - - - - - WAKE: ;------------------------------------------- ; Test Mom.Sw for activity- ; PortA, Bit-3 is Mom.SW , Normaly tied HIGH. Activation pulls it to GND ; PwrDwn has already put GPIO in the input state. ChkSW: BTFSC GPIO,3 ; test SWITCH input (Skip if clear(Low), Bit LOW, Button Pressed) GOTO TempSkp ; Button not pushed... ;XXXXXXXXXXXXXXXXX INPUT BUTTON PRESSED XXXXXXXXXXXXXXXXXXXXXXX ; No Debouncing of the input switch is needed; once dedected that it's been pressed, ; the TREE call will delay longer than the bounce time. TempSkp: NOP ;Additional Code Here.......... ;------------------------------------------------ ;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX ;======================================================================================= ;==================================================================== ;------------------- Sound Effect Sub.s ----------------------- ;==================================================================== ;======================================================================================= ;Bump Sound- Semi Random selection of sound. Depends upon ClrCnt & BmpCnt- ; Routine jumps to sound effect, and returns from there... BmpSnd: MOVLW .8 ;No complaints on 1st 3 bumps- SUBWF BmpCnt,W ;BmpCnt-7=>W, C,Z BTFSC STATUS,C ; skip next if Borrow (carry=0) occured... RETURN ;If it's the 1st 3 bumps, no sound ;............................. MOVF BmpCnt,W ADDWF ClrCnt,W MOVWF RndCnt ;Store selection MOVLW b'00001111' ANDWF RndCnt,F ;Limit count to 0-15 ;........................ SelctSnd: DECFSZ RndCnt,F GOTO $+2 ;Skip Next ;Jump is inside a CALL, the routine jumped to will provide the return. GOTO UhOh1 ;1 DECFSZ RndCnt,F GOTO $+2 ; GOTO Trllup ;2 GOTO Grwl ;2 DECFSZ RndCnt,F GOTO $+2 GOTO Trlldwn ;3 DECFSZ RndCnt,F GOTO $+2 GOTO Chips1 ;4 DECFSZ RndCnt,F GOTO $+2 GOTO Grwl ;5 DECFSZ RndCnt,F GOTO $+2 GOTO OhOhOh ;6 DECFSZ RndCnt,F GOTO $+2 GOTO OhUh1 ;7 DECFSZ RndCnt,F GOTO $+2 RETURN ;8 - No Sound DECFSZ RndCnt,F GOTO $+2 GOTO Trlla ;9 DECFSZ RndCnt,F GOTO SelctSnd ;If no match, loop around again GOTO Trllb ;======================================================================================= ;------------------------------------------------------------------- ;------------------------------------------------------------------- ; Growl- Grwl: CALL Grrr MOVLW d'7' ;Wait xms CALL DLY_L ;2.5ms/Cycle CALL Grrr MOVLW d'7' ;Wait xms CALL DLY_L ;2.5ms/Cycle CALL Grrr RETURN ;Done ;------------------------------------------------------------------- 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 ;........................ BSF STATUS,RP0 ;Switch to BANK 1 <<--+--### BCF TRISIO,4 ; Set Port A I/O: 1= Input, 0= Output BCF TRISIO,5 ; Set Port A I/O: 1= Input, 0= Output BCF STATUS,RP0 ; Switch Back to BANK 0 <<--+--### ;Set complementary Piezo output bits BSF GPIO, 4 BCF GPIO, 5 ;---------------------------------------- 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 ;*GR1_LP: ;2+(17*7)= 121 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 ;* DECFSZ TonCnt,F ; (1/2) 121+3+5=129 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 ;Relax Piezo- BCF GPIO, 4 BCF GPIO, 5 RETURN ;Done ;------------------------------------------------------------------- ;------------------------------------------------------------------- UhOh1: MOVLW .120 MOVWF TONE ;Set TONE count ; MOVLW .25 MOVLW .15 MOVWF TONEDUR ;Set TONE Duration MOVLW .20 MOVWF SWPDUR ;Set Sweep Duration CALL SweepU 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 ;Hold end of sweep note- MOVLW .50 MOVWF TONEDUR ;Set TONE Duration MOVF TonCnt,W ;Get Tone count at end of sweep into 'W' to pass to Sub CALL MkTone RETURN ;------------------------------------------------------------------- ;------------------------------------------------------------------- OhOhOh: ; MOVLW .180 MOVLW .80 MOVWF TONE ;Set TONE start count MOVLW .15 MOVWF TONEDUR ;Set TONE Duration MOVLW .34 MOVWF SWPDUR ;Set Sweep Duration CALL SweepD ;Hold end of sweep note- MOVLW .180 MOVWF TONEDUR ;Set TONE Duration MOVF TonCnt,W ;Get Tone count at end of sweep into 'W' to pass to Sub CALL MkTone MOVLW d'1' ;Wait .1 sec CALL DLY_X ;100ms/Cycle ;++++++++++++++++++ ; MOVLW .140 MOVLW .100 MOVWF TONE ;Set TONE start count MOVLW .15 MOVWF TONEDUR ;Set TONE Duration ; MOVLW .24 MOVLW .30 MOVWF SWPDUR ;Set Sweep Duration CALL SweepD ;Hold end of sweep note- MOVLW .120 MOVWF TONEDUR ;Set TONE Duration MOVF TonCnt,W ;Get Tone count at end of sweep into 'W' to pass to Sub CALL MkTone MOVLW d'1' ;Wait .1 sec CALL DLY_X ;100ms/Cycle ;++++++++++++++++++ MOVLW .200 MOVWF TONE ;Set TONE start count MOVLW .15 MOVWF TONEDUR ;Set TONE Duration ; MOVLW .24 MOVLW .30 MOVWF SWPDUR ;Set Sweep Duration CALL SweepD ;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 MkTone RETURN ;------------------------------------------------------------------- ;------------------------------------------------------------------- ;Sound like "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 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 ;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 MkTone RETURN ;------------------------------------------------------------------- ;------------------------------------------------------------------- Trlla: MOVLW .8 MOVWF TrlRpt ;ToneRepeat count Trll: MOVLW .50 MOVWF TONE ;Set TONE start count MOVLW .15 MOVWF TONEDUR ;Set TONE Duration MOVLW .20 MOVWF SWPDUR ;Set Sweep Duration CALL SweepU MOVLW d'20' ;Wait 50ms ; CALL DLY_L ;2.5ms/Cycle DECFSZ TrlRpt,F GOTO Trll MOVLW .8 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 MOVLW d'20' ;Wait 50ms ; CALL DLY_L ;2.5ms/Cycle DECFSZ TrlRpt,F GOTO Trll2 RETURN ;-------------------------------------------------------------------------------- ;-------------------------------------------------------------------------------- Trllb: MOVLW .8 MOVWF TrlRpt ;ToneRepeat count Trll3: MOVLW .50 MOVWF TONE ;Set TONE start count MOVLW .15 MOVWF TONEDUR ;Set TONE Duration CALL SweepD MOVLW d'20' ;Wait 50ms ; CALL DLY_L ;2.5ms/Cycle DECFSZ TrlRpt,F GOTO Trll3 MOVLW .11 MOVWF TrlRpt ;ToneRepeat count Trll4: MOVLW .70 MOVWF TONE ;Set TONE start count MOVLW .12 MOVWF TONEDUR ;Set TONE Duration CALL SweepD MOVLW d'20' ;Wait 50ms ; CALL DLY_L ;2.5ms/Cycle DECFSZ TrlRpt,F GOTO Trll4 RETURN ;-------------------------------------------------------------------------------- ;-------------------------------------------------------------------------------- ;Sweep trill Up- Trllup: 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 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 RETURN ;-------------------------------------------------------------------------------- ;-------------------------------------------------------------------------------- ;Sweep trill Down- Trlldwn: MOVLW .30 MOVWF TrlSwp ;Trill Repeat count for sweep MOVLW .10 MOVWF TONE ;Set TONE start count MOVLW .12 MOVWF TONEDUR ;Set TONE Duration Trll8: MOVLW .2 MOVWF TrlRpt ;ToneRepeat count Trll7: CALL SweepD CLRWDT ; MOVLW d'20' ;Wait 50ms ; CALL DLY_L ;2.5ms/Cycle DECFSZ TrlRpt,F GOTO Trll7 ;................... ; INCF TONE,F ; INCF TONE,F INCF TONE,F INCF TONE,F DECFSZ TrlSwp,F GOTO Trll8 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 MOVLW d'10' ;Wait 25ms CALL DLY_L ;2.5ms/Cycle DECFSZ TrlRpt,F GOTO Chp2LP 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 MOVLW d'100' ;Wait 25ms CALL DLY_L ;2.5ms/Cycle DECFSZ TrlRpt,F GOTO Chp1LP 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 - BSF STATUS,RP0 ; Set Bank 1 <<--+--### MOVF TRISIO,W ; Save I/O state- MOVWF TrisTmp BCF TRISIO,4 ; Set Port A I/O for Output on 4,5 BCF TRISIO,5 ; Set Port A I/O for Output on 4,5 BCF STATUS,RP0 ; Set Bank 0 <<--+--### BSF GPIO, 4 ;Set complementary Piezo output bits BCF GPIO, 5 ;------------------------------------ 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 ;------------------------------------ DECF TonCnt,F ;Increase Pitch DECFSZ DurCnt,F ;Dec DurCnt & Skip if Zero 1 (2) GOTO SwpLP2 ; 2 V ;------------------------------------ ;Relax Piezo- BCF GPIO, 4 BCF GPIO, 5 BSF STATUS,RP0 ; Set Bank 1 <<--+--### MOVF TrisTmp,W ;Get I/O state- MOVWF TRISIO ;& restore it... BCF STATUS,RP0 ; Set Bank 0 <<--+--### 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 - BSF STATUS,RP0 ; Set Bank 1 <<--+--### MOVF TRISIO,W ; Save I/O state- MOVWF TrisTmp BCF TRISIO,4 ; Set Port A I/O for Output on 4,5 BCF TRISIO,5 ; Set Port A I/O for Output on 4,5 BCF STATUS,RP0 ; Set Bank 0 <<--+--### BSF GPIO, 4 ;Set complementary Piezo output bits BCF GPIO, 5 ;------------------------------------ MOVF TONE,W MOVWF TonCnt MOVF TONEDUR,W MOVWF DurCnt SwpDLP2: ; MOVLW d'8' ; 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 ;------------------------------------ INCF TonCnt,F ;Decrease Pitch DECFSZ DurCnt,F ;Dec DurCnt & Skip if Zero 1 (2) GOTO SwpDLP2 ; 2 V ;------------------------------------ ;Relax Piezo- BCF GPIO, 4 BCF GPIO, 5 BSF STATUS,RP0 ; Set Bank 1 <<--+--### MOVF TrisTmp,W ;Get I/O state- MOVWF TRISIO ;& restore it... BCF STATUS,RP0 ; Set Bank 0 <<--+--### ;--------------- Return ;-------------------------------------------------- ;-------------------------------------------------- ;256 Cycles at Freq. set duration passed in 'W'. ; Base for all tones. BEEP turns ON needed I/O, Then turns it OFF when done. BEEP: MOVWF TONE ;Save TONE passed in W ; Enter HERE if TONE is set elsewhere.... BEEPTN: ;(Call here if tone is deifined externaly) MOVLW d'180' MOVWF TEMPL ;Set Length of Beep ; Enter HERE if TONE & TEMPL are set elsewhere.... BEEPTLN: ;(Call here if tone & length of beep deifined externaly) ; Piezo- GP4,5 ;; CLRF GPIO BSF STATUS,RP0 ;Switch to BANK 1 <<--+--### ; MOVLW b'11111010' ; Define I/O for Peizo..... ; MOVWF TRISIO ; Set Port A I/O: 1= Input, 0= Output BCF TRISIO,4 ; Set Port A I/O: 1= Input, 0= Output BCF TRISIO,5 ; Set Port A I/O: 1= Input, 0= Output BCF STATUS,RP0 ; Switch Back to BANK 0 <<--+--### ;Set complementary Piezo output bits BSF GPIO, 4 BCF GPIO, 5 BEEPLP: MOVF TONE,W ;Get Value for Tone Delay: CALL DLY_S ;Delay = 10uS * 'W' ;WAS MOVLW b'00010001' MOVLW b'00110000' XORWF GPIO,F ;Flip port bits to sound Piezo DECFSZ TEMPL,F ; Check for end of min durration GOTO BEEPLP Return ;-------------------------------------------------------------------- ;-------------------------------------------------------------------- ;****************************** Tone0: MOVLW d'50' MOVWF TONEDUR ;Save Duration passed in W MOVLW d'237' ;Tone - G (Fa) GOTO MkTone ;(sub will store TONE) ;---------------- Tone1: MOVLW d'101' MOVWF TONEDUR ;Save Duration passed in W MOVLW d'117' ;Tone - G (Fa) GOTO MkTone ;(sub will store TONE) ;---------------- Tone2: MOVLW d'190' ;* 8 cycles of tone = .288 sec of tone MOVWF TONEDUR ;Save Duration passed in W MOVLW d'29' ;Tone - E (Re) GOTO MkTone ;(sub will store TONE) ;---------------- Tone3: MOVLW d'226' ;* 8 cycles of tone = .288 sec of tone MOVWF TONEDUR ;Save Duration passed in W MOVLW d'24' ;Tone - G (Fa) GOTO MkTone ;(sub will store TONE) ;================================================================= ; Make 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 MkTone: CLRWDT MOVWF TONE ;Save TONE passed in W ; Init. I/O - BSF STATUS,RP0 ; Set Bank 1 <<--+--### ; MOVLW b'11001111' ; Define I/O ; MOVWF TRISIO ; Set Port A I/O: 1= Input, 0= Output MOVF TRISIO,W ; Save I/O state- MOVWF TrisTmp BCF TRISIO,4 ; Set Port A I/O for Output on 4,5 BCF TRISIO,5 ; Set Port A I/O for Output on 4,5 BCF STATUS,RP0 ; Set Bank 0 <<--+--### BSF GPIO, 4 ;Set complementary Piezo output bits BCF GPIO, 5 MkoteLP2: MOVLW d'8' ; 1 MOVWF TONE8 ;Init for 8 Cycles of Tone 1 MkoteLP: MOVF TONE,W ;Get Value for Tone Delay: 1 MOVWF DLY_CNT ; Store loop cnt passed in 'W' 1 Nk_LOOP: DECFSZ DLY_CNT,F ; Dec TONE count & Skp if Zero 1 (2) GOTO Nk_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 MkoteLP ; 2 DECFSZ TONEDUR,F ;Dec TONDUR & Skip if Zero 1 (2) GOTO MkoteLP2 ; 2 V ;--------------- ;Relax Piezo- BCF GPIO, 4 BCF GPIO, 5 BSF STATUS,RP0 ; Set Bank 1 <<--+--### MOVF TrisTmp,W ;Get I/O state- MOVWF TRISIO ;& restore it... BCF STATUS,RP0 ; Set Bank 0 <<--+--### ;--------------- Return ;==================================================================== ;============================================================================= ;---------------------- Motion Sub.s -------------------------- ;============================================================================= ;Motor ON, FWD (Pin 7) Fwrd: BCF GPIO,0 ;Assure Motor is OFF to prevent 'Shoot-Thru' MOVLW d'10' CALL DLY_L ;@ 2.5mS per count = 25ms BSF GPIO,2 ;Set FORWARD Direction BSF GPIO,0 ;Motor ON RETURN ;-------------------------------------------------- ;-------------------------------------------------- ;Motor ON, REV (Pin 7) Rvrs: BCF GPIO,0 ;Assure Motor is OFF to prevent 'Shoot-Thru' MOVLW d'10' CALL DLY_L ;@ 2.5mS per count = 25ms BCF GPIO,2 ;Set REVERSE Direction BSF GPIO,0 ;Motor ON RETURN ;-------------------------------------------------- ;-------------------------------------------------- ;Motor OFF, FWD (Pin 7) STOP: BSF GPIO,2 ;Set FWD Direction BCF GPIO,0 ;Motor OFF RETURN ;============================================================================= ;---------------------- Utillity Sub.s -------------------------- ;============================================================================= ;------------------------------------------------------- ;Get Battery status: ; NOTE: RECALCULATE FOR 2x 1.25 volt NiMh batteries ;Set up ADC using Vcc as ref. Turn on Vref (GP0), and measure AN-1. If Vcc is 2.5VDC, ; then 1.22 Vref will read as 47.84% of the ADC. 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; ; ADC registers for config: ADCON0, ADCON1, 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. 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 ; 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 STATUS,RP0 ; Set Bank 1 <<--+--### ;Set GP-1 as digital out (turn on Vref), All others a 3st via TRISIO : ; bit: 76543210 MOVLW b'11111101' MOVWF TRISIO ; bit: 76543210 (Low 4 bits selcet enabled channel/s) MOVLW b'01011000' ;AN3 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 BCF STATUS,RP0 ; Back to Bank 0 <<--+--### ; ------------------------------------------------------- BSF GPIO,1 ;Turn on External 'Vref' to be read on AN3 ; bit: 76543210 (bit 2,3 select channel to convert from) MOVLW b'00001101' ; Set: LEFT-JUSTIFIED, Vref to Vdd, Select Chanel AN-3, turn on ADC MOVWF ADCON0 ; The minimum acquisition time is 20uS for the Sample & Hold capacitor to settle, ; Allow 150ms for things to settle: ;xxxx;TEMP <<<<<<<<<<<<<<<<<<,, ; MOVLW d'250' ;.625 sec ; Call DLY_L ;delay for 2.5mS/loop MOVLW d'5' ; .5 sec Call DLY_X ;delay for 100mS/loop ;Start ADC Conversion by turning GO/Done, bit-1 ON. BSF ADCON0,1 ; The minimum acquisition time is 20uS for the Sample & Hold capacitor to settle, MOVLW d'60' Call DLY_L ;delay for 2.5mS x 60 = 150mS ;------------------------------------- ;Conversion takes Min. 11 x ADC Clock = 11x4uS = 44uS: ADCDLY: ; MOVLW d'6' MOVLW d'20' ;try 200us Call DLY_S ;delay for 10uS x 6 = 60uS ;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. ; ------------------------------------------------------- BCF GPIO,1 ;Turn OFF External 'Vref' to be read on AN3 ; Call PwrDwn ;[Also turns off all analog inputs.] Return ;============================================================================= ;============================================================================= ;------------------------------------------------------- ; (See 'BattStat' for details) ;Read LED used as solar cell, Return Vale in 'W'- PhotoStat: BSF STATUS,RP0 ; Set Bank 1 <<--+--### CLRF ADRESL ;Clear old data ;Clear Charge on LED- ; bit: 76543210 MOVLW b'11111101' MOVWF TRISIO ; bit: 76543210 (Low 4 bits selcet enabled channel/s) MOVLW b'01010000' ;Disable Analog MOVWF ANSEL BCF STATUS,RP0 ; Back to Bank 0 <<--+--### BCF GPIO,1 ;Pull LED LOW MOVLW d'60' Call DLY_L ;delay for 2.5mS x 60 = 150mS BSF STATUS,RP0 ; Set Bank 1 <<--+--### ;Set All as 3st via TRISIO, Letting LED acumilate charge- ; bit: 76543210 MOVLW b'11111111' MOVWF TRISIO BCF STATUS,RP0 ; Back to Bank 0 <<--+--### ;------------------------------------------ ;Charge LED Cap- MOVLW d'20' ; 2 sec delay to charge LED... Call DLY_X ;delay for 100mS/loop ;------------------------------------------ ;Now Start Reading LED- BSF STATUS,RP0 ; Set Bank 1 <<--+--### ; bit: 76543210 (Low 4 bits selcet enabled channel/s) 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 BCF STATUS,RP0 ; Back to Bank 0 <<--+--### ; ------------------------------------------------------- ; bit: 76543210 (bit 2,3 select channel to convert from) MOVLW b'10000101' ; Set: RIGHTT-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, MOVLW d'2' ;5ms Call DLY_L ;delay for 2.5mS/loop ;Start ADC Conversion by turning GO/Done, bit-1 ON. BSF ADCON0,1 MOVLW d'60' Call DLY_L ;delay for 2.5mS x 60 = 150mS ;------------------------------------- ;Conversion takes Min. 11 x ADC Clock = 11x4uS = 44uS: ADCDLY2: ; MOVLW d'6' MOVLW d'20' ;try 200us 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. ; ------------------------------------------------------- BSF STATUS,RP0 ; Set Bank 1 <<--+--### MOVF ADRESL,W ;Get voltage level into 'W' BCF STATUS,RP0 ; Back to Bank 0 <<--+--### Return ;============================================================================= ;============================================================================= ;------------------------------------------------------- PwrDwn: MOVLW b'00000111' ; Turn Comparitor OFF, Lowest Power consumption. MOVWF CMCON ; by setting CMCON<0:3> CLRF GPIO BSF STATUS,RP0 ; Set Bank 1 <<--+--### CLRF ANSEL ;Deselect 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 <<--+--### Return ;NOTE: ; All ports should be in 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) ;============================================================================= ;============================================================================= ;------------------------------------------------------- ; 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 ;********************************************************* 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 CALL DLY_X ;100ms/Cycle MOVF Ac10s,W MOVWF Ac100s Call DigBeep ;Convert 10s in Ac10s, MOVLW d'10' ;~1 Sec Delay Between Digits CALL DLY_X ;100ms/Cycle MOVF OutByt,W MOVWF Ac100s Call DigBeep ;,and 1s in OutByt. MOVLW d'5' ;~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 Tone0 ;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 Tone1 MOVLW d'5' ;~1/2 Sec Delay Between Digits CALL DLY_X ;100ms/Cycle ; CALL HalfSecx GOTO ToneBit ;- - - - - - - - - - - - - - - - - - - - - - - - - - - - - ;-------------------------------------------------- ; ; ORG 0x3c0 ; da "(c)2007 Applied Inspirations, LLC" ORG 0x3DA DT " " DT "(" DT "c" DT ")" DT "2" DT "0" DT "0" DT "7" DT " " DT "A" DT "p" DT "p" DT "l" DT "i" DT "e" DT "d" DT " " DT "I" DT "n" DT "s" DT "p" DT "i" DT "r" DT "a" DT "t" DT "i" DT "o" DT "n" DT "s" DT "," DT " " DT "L" DT "L" DT "C" DT " " ;******************************************************************************************** END ;XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX . . .