Mosaic
Well-Known Member
MPLAB SIM issues- PIC Hardware INIT solved.
Hi all:
Below is a commented, working proggie that counts the # of debounced switch clicks in the Pic Kit 2 LPC board.
Odd thing is, the LEds Start displaying the count as 1, when it should be zero.
The simulation says it IS zero.
If I Change the start value of the variable 'Clicks' from 0 to 255, it seems to work fine.
BUT I CAN"T SEE WHY that is necessary.
Thanks for any help. You can setup watch variables and see if you can shed any light on this one.
list p=16f690 ; list directive to define processor
#include <P16F690.inc> ; processor specific variable definitions
__config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF) ; allows PortA as dig. input..
;***** VARIABLE DEFINITIONS
VARIABLES UDATA_SHR
Delay1 RES 1
Delay2 RES 1
Switch RES 1
Latch RES 1
Clicks RES 1
;**********************************************************************
ORG 0x000 ; processor reset vector
goto Main ; go to beginning of program
Main
;Switch input setup (RA3)
Banksel TRISA
BSF TRISA,3 ; Set RA3 as an input
BANKSel ANSEL;
BCF ANSEL,3 ; RA3 is digital.
Banksel Switch
CLRF Switch
CLRF Latch
CLRF Clicks ; set switch clicks to 0
; PORTC setup
BankSel TRISC
movlw b'11110000'
movwf TRISC ; MAKE I/O PIN C0,c1,c2,c3 AN OUTPUT, BY SETTING PINS 0,1,2,3 LOW.
BankSel Delay1
Longloop
CLRF Delay1
CLRF Delay2
LOOP
DECFSZ Delay1,F ; 1 inst cycle, when delay1=0 => test = true skip next line.
GOTO LOOP ; 2 inst cycles, this is looped 256 times before skipping to next line => 768 microsec at 1 MIP
CALL GETSWITCH ; this is the core loop, use to monitor switch.
DECFSZ Delay2,F ; 1 inst cycle
GOTO LOOP ; 2 inst cyles, by adding the loops = 256 * 768 + (256*3) = 257* 768 = 197376 microsec = .2 sec.
MOVF Clicks,w ; prepare to display clicks on LEDs
MOVWF PORTC ;OUTPUT TO C.
GOTO Longloop ; CONTINUE
GETSWITCH ;debounce routine of Switch on RA3
BTFSS PORTA,3 ; test RA3, skip if HI (SW is OPEN)
CALL CLOSED
BTFSC PORTA,3 ; test RA3, skip if lo (SW is CLOSED)
CALL OPEN
RETURN
OPEN ; When Switch is Open
DECFSZ Switch,F; Debounce Countdown if switch is open.
Return ; return if switch value is non zero
Incf Switch ; make switch minimum 1
BCF Latch,0 ; clear onlatch as Switch has reached lowest count.
RETURN
CLOSED: ; When switch is closed
INCF Switch,F ; Debounce count up if switch is on/closed
BTFSC Switch,5
BSF Latch,1 ; set onlatch,1 to 1 if Switch =32
BTFSC Switch,5
CALL CLIK
BTFSC Switch,5
DECF Switch,F ; keep Switch from rising past 32.
RETURN
CLIK ; Track number of switch clicks
BTFSS Latch,0 ;skip next inst. if latch,0 is set.
INCF Clicks ; If Latch,0 is clear,then this is a state change from switch open => increment clicks
BCF STATUS,C ; clear the carry
RRF Latch,f ; make bit 1 into bit 0 to update Latch status for switch open/closed testing.
RETURN
END ; directive 'end of program'
Hi all:
Below is a commented, working proggie that counts the # of debounced switch clicks in the Pic Kit 2 LPC board.
Odd thing is, the LEds Start displaying the count as 1, when it should be zero.
The simulation says it IS zero.
If I Change the start value of the variable 'Clicks' from 0 to 255, it seems to work fine.
BUT I CAN"T SEE WHY that is necessary.
Thanks for any help. You can setup watch variables and see if you can shed any light on this one.
list p=16f690 ; list directive to define processor
#include <P16F690.inc> ; processor specific variable definitions
__config (_INTRC_OSC_NOCLKOUT & _WDT_OFF & _PWRTE_OFF & _MCLRE_OFF & _CP_OFF & _BOR_OFF & _IESO_OFF & _FCMEN_OFF) ; allows PortA as dig. input..
;***** VARIABLE DEFINITIONS
VARIABLES UDATA_SHR
Delay1 RES 1
Delay2 RES 1
Switch RES 1
Latch RES 1
Clicks RES 1
;**********************************************************************
ORG 0x000 ; processor reset vector
goto Main ; go to beginning of program
Main
;Switch input setup (RA3)
Banksel TRISA
BSF TRISA,3 ; Set RA3 as an input
BANKSel ANSEL;
BCF ANSEL,3 ; RA3 is digital.
Banksel Switch
CLRF Switch
CLRF Latch
CLRF Clicks ; set switch clicks to 0
; PORTC setup
BankSel TRISC
movlw b'11110000'
movwf TRISC ; MAKE I/O PIN C0,c1,c2,c3 AN OUTPUT, BY SETTING PINS 0,1,2,3 LOW.
BankSel Delay1
Longloop
CLRF Delay1
CLRF Delay2
LOOP
DECFSZ Delay1,F ; 1 inst cycle, when delay1=0 => test = true skip next line.
GOTO LOOP ; 2 inst cycles, this is looped 256 times before skipping to next line => 768 microsec at 1 MIP
CALL GETSWITCH ; this is the core loop, use to monitor switch.
DECFSZ Delay2,F ; 1 inst cycle
GOTO LOOP ; 2 inst cyles, by adding the loops = 256 * 768 + (256*3) = 257* 768 = 197376 microsec = .2 sec.
MOVF Clicks,w ; prepare to display clicks on LEDs
MOVWF PORTC ;OUTPUT TO C.
GOTO Longloop ; CONTINUE
GETSWITCH ;debounce routine of Switch on RA3
BTFSS PORTA,3 ; test RA3, skip if HI (SW is OPEN)
CALL CLOSED
BTFSC PORTA,3 ; test RA3, skip if lo (SW is CLOSED)
CALL OPEN
RETURN
OPEN ; When Switch is Open
DECFSZ Switch,F; Debounce Countdown if switch is open.
Return ; return if switch value is non zero
Incf Switch ; make switch minimum 1
BCF Latch,0 ; clear onlatch as Switch has reached lowest count.
RETURN
CLOSED: ; When switch is closed
INCF Switch,F ; Debounce count up if switch is on/closed
BTFSC Switch,5
BSF Latch,1 ; set onlatch,1 to 1 if Switch =32
BTFSC Switch,5
CALL CLIK
BTFSC Switch,5
DECF Switch,F ; keep Switch from rising past 32.
RETURN
CLIK ; Track number of switch clicks
BTFSS Latch,0 ;skip next inst. if latch,0 is set.
INCF Clicks ; If Latch,0 is clear,then this is a state change from switch open => increment clicks
BCF STATUS,C ; clear the carry
RRF Latch,f ; make bit 1 into bit 0 to update Latch status for switch open/closed testing.
RETURN
END ; directive 'end of program'
Last edited: