Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

Please help me in writing code

Status
Not open for further replies.
This code program


W EQU 0
F EQU 1
Z EQU 2
ZERO EQU 2
C EQU 0
CARRY EQU 0
GPWUF EQU 7

;========================

SW0 EQU 0 ;switch names
SW1 EQU 1
SW2 EQU 3
SW3 EQU 4

TFlag EQU 7 ;bit numbers for flags
DBTime EQU 12
;========================
; endef regester
;========================

INDF EQU H'00'
TMR0 EQU H'01'
PCL EQU H'02'
STATUS EQU H'03'
FSR EQU H'04'
OSCCAL EQU H'05'
GPIO EQU H'06'
;=======================
SleepT EQU H'07' ; inactivity counter to allow sleep
Flags EQU H'08' ; storage space for messages
ThisSw EQU H'09' ; current switch status
;=======================
; switch0
LastSw0 EQU H'0A' ; last value - for detecting change
SwState0 EQU H'0B' ; the official (debounced) status
DBTimer0 EQU H'0C' ; counter used during debouncing of the switch
; switch1
LastSw0 EQU H'0D' ; " "
SwState0 EQU H'0E' ; " "
DBTimer0 EQU H'0F' ; " "
; switch2
LastSw0 EQU H'10' ; " "
SwState0 EQU H'11' ; " "
DBTimer0 EQU H'12' ; " "
; switch3
LastSw0 EQU H'13' ; " "
SwState0 EQU H'14' ; " "
DBTimer0 EQU H'15' ; " "
;=====================
ORG H'0200'
ID0 Data H'0000'
ID1 Data H'0000'
ID2 Data H'0000'
ID3 Data H'0003'
;=====================
; MCLRDisabled, NoCodeProtect, NoWatchDog, IntRCOsc

ORG H'0FFF'
CONFIG Data H'000A'
;=====================
ORG H'00'
MOVWF OSCCAL ; store the factory osc. calibration value
Start CLRWDT ; setting up options
MOVLW B'00000010' ; use int clock input, /8 prescaler
OPTION ; pullups on, and wakeup on pin change
BTFSS STATUS,GPWUF ; don't change outputs on a wakeup
CLRF GPIO ; but output 0s on a powerup
MOVLW B'00011011' ; GP0, GP1 , GP3 and GP4 are inputs
TRIS GPIO ; GP2, GP5 are outputs
CLRF SleepT
CLRF Flags
CLRF DBTimer0
CLRF DBTimer1
CLRF DBTimer2
CLRF DBTimer3
BTFSC STATUS,GPWUF ; on wakeup, don't change the switch states
GOTO Main
CLRF LastSw0 ; but if it's a powerup,
CLRF SwState0 ; assume they're all off (open)
CLRF LastSw1
CLRF SwState1
CLRF LastSw2
CLRF SwState2
CLRF LastSw3
CLRF SwState3
;==================
Main CALL DoTime ; need clock 'running'for debounce
CALL Switch0 ; check switch0 position,
CALL Switch1 ; then switch1,
CALL Switch2 ; then switch2,
CALL Switch3 ; and switch2
CLRW ; assume no switch is pressed
MOVF SwState0,F ; check the state of sw0
BTFSS STATUS,Zero ; do nothing if it's not pressed
IORLW B'00000100' ; else set GP2 high
MOVF SwState1,F ; same for sw1
BTFSS STATUS,Zero ; do nothing if it's not pressed
IORLW B'00000100' ; output on GP2
MOVF SwState2,F ; same for sw2
BTFSS STATUS,Zero ; do nothing if it's not pressed
IORLW B'00100000' ; output on GP5
MOVF SwState3,F ; and sw3
BTFSS STATUS,Zero ; do nothing if it's not pressed
IORLW B'00100000' ; with output on GP5
MOVWF GPIO
GOTO Main
;=================
; DoTime

DoTime BTFSS TMR0,7 ; high bit of timer set?
GOTO dt1 ; no - we've overflowed
BSF Flags,TFlag ; yes, set the flag
RETLW 0

dt1 BTFSS Flags,TFlag ; have the timers been serviced?
RETLW 0 ; yes
INCF DBTimer0,F
INCF DBTimer1,F ; no, increment all the timers
INCF DBTimer2,F
INCF SleepT,F
BCF Flags,TFlag ; reset the service flag, and
BTFSS STATUS,Zero ; see if the sleep timer has overflowed
RETLW 1
; There's been no activity for 256*2mS, it's time to sleep
MOVF GPIO,W ; read the current pin status
SLEEP ; and good night
;==================
; Switch0
; GP0
Switch0 CLRF ThisSw ; assume switch is not pressed
BTFSS GPIO,Sw0 ; check sw status
INCF ThisSw,F ; the sw was pressed
; Compare to last state
MOVF LastSw0,W
XORWF ThisSw,W
BTFSS STATUS,Zero ; Zero is set if there's been no change.

; No change since last scan, but are we in a debounce phase?
GOTO sw01
MOVF SwState0,W
XORWF ThisSw,W ; compare present state of sw to the official state
BTFSC STATUS,Zero ; Zero is Clr if they differ (we're in debounce)
GOTO sw02

; Switch is changing, have we gone past the debounce time?
MOVLW DBTime ; get the debounce time
SUBWF DBTimer0,W ; and compare to elapsed
BTFSS STATUS,Carry ; Carry is set if DBTimer >= DBTicks
GOTO sw02

; we've exceeded the debounce time - change the official state of the switch
MOVWF SwState0 ; store current state in SwState
GOTO sw02
; the switch input is changing state - prepare to debounce
sw01 MOVF ThisSw,W
MOVWF LastSw0 ; remember this passes' state
CLRF SleepT ; we've had activity - reset the sleep timer
CLRF DBTimer0 ; and reset the debounce timer

; we're done for this pass...
sw02 BTFSS SwState0,0 ; check the switch state, and
RETLW 0 ; return with 0 if not pressed,
RETLW 1 ; 1 if pressed
; *********************************************
; Switch1
; GP1
Switch1 CLRF ThisSw ; assume switch is not pressed
BTFSS GPIO,Sw1 ; check sw status
INCF ThisSw,F ; the sw was pressed

; Compare to last state
MOVF LastSw1,W
XORWF ThisSw,W
BTFSS STATUS,Zero ; Zero is set if there's been no change
GOTO sw11

; No change since last scan, but are we in a debounce phase?
MOVF SwState1,W
XORWF ThisSw,W ; compare present state of sw to the official state
BTFSC STATUS,Zero ; Zero is Clr if they differ (we're in debounce)
GOTO sw12

; Switch is changing, have we gone past the debounce time?
MOVLW DBTime ; get the debounce time
SUBWF DBTimer1,W ; and compare to elapsed
BTFSS STATUS,Carry ; Carry is set if DBTimer >= DBTicks
GOTO sw12

; we've exceeded the debounce time - change the official state of the switch
MOVF ThisSw,W
MOVWF SwState1 ; store current state in SwState
GOTO sw12

; the switch input is changing state - prepare to debounce
sw11 MOVF ThisSw,W
MOVWF LastSw1 ; remember this passes' state.
CLRF SleepT ; we've had activity - reset the sleep timer
CLRF DBTimer1 ; and reset the debounce timer

; we're done for this pass...
sw12 BTFSS SwState1,0 ; check the switch state, and
RETLW 0 ; return with 0 if not pressed,
RETLW 1 ; 1 if pressed

; *********************************************
; Switch2
; GP3

Switch2 CLRF ThisSw ; assume switch is not pressed
BTFSS GPIO,Sw2 ; check sw status
INCF ThisSw,F ; the sw was pressed

; Compare to last state
MOVF LastSw2,W
XORWF ThisSw,W
BTFSS STATUS,Zero ; Zero is set if there's been no change
GOTO sw21

; No change since last scan, but are we in a debounce phase?
MOVF SwState2,W
XORWF ThisSw,W ; compare present state of sw to the official state
BTFSC STATUS,Zero ; Zero is Clr if they differ (we're in debounce)
GOTO sw22

; Switch is changing, have we gone past the debounce time?
MOVLW DBTime ; get the debounce time
SUBWF DBTimer2,W ; and compare to elapsed
BTFSS STATUS,Carry ; Carry is set if DBTimer >= DBTicks
GOTO sw22

; we've exceeded the debounce time - change the official state of the switch
MOVF ThisSw,W
MOVWF SwState2 ; store current state in SwState
GOTO sw22

; the switch input is changing state - prepare to debounce
sw21 MOVF ThisSw,W
MOVWF LastSw2 ; remember this passes' state
CLRF SleepT ; we've had activity - reset the sleep timer
CLRF DBTimer2 ; and reset the debounce timer

; we're done for this pass...
sw22 BTFSS SwState2,0 ; check the switch state, and
RETLW 0 ; return with 0 if not pressed,
RETLW 1 ; 1 if pressed
; *********************************************
; Switch3
; GP4

Switch3 CLRF ThisSw ; assume switch is not pressed
BTFSS GPIO,Sw3 ; check sw status
INCF ThisSw,F ; the sw was pressed

; Compare to last state
MOVF LastSw3,W
XORWF ThisSw,W
BTFSS STATUS,Zero ; Zero is set if there's been no change
GOTO sw31

; No change since last scan, but are we in a debounce phase?
MOVF SwState3,W
XORWF ThisSw,W ; compare present state of sw to the official state
BTFSC STATUS,Zero ; Zero is Clr if they differ (we're in debounce)
GOTO sw32

; Switch is changing, have we gone past the debounce time?
MOVLW DBTime ; get the debounce time
SUBWF DBTimer3,W ; and compare to elapsed
BTFSS STATUS,Carry ; Carry is set if DBTimer >= DBTicks
GOTO sw32

; we've exceeded the debounce time - change the official state of the switch
MOVF ThisSw,W
MOVWF SwState3 ; store current state in SwState
GOTO sw32

; the switch input is changing state - prepare to debounce
sw31 MOVF ThisSw,W
MOVWF LastSw3 ; remember this passes' state
CLRF SleepT ; we've had activity - reset the sleep timer
CLRF DBTimer3 ; and reset the debounce timer

; we're done for this pass...
sw33 BTFSS SwState3,0 ; check the switch state, and
RETLW 0 ; return with 0 if not pressed,
RETLW 1 ; 1 if pressed

END
 
Thank you Norlin

I want to put the results GP2 And GP5 In other words
SW1 OR SW2 = GP2
SW3 OR SW4 = GP5
 
Last edited:
Pommie said:
It uses the internal weak pullups.

Mike.
Oh, never thought of that, I never used inernal pullup even for MCLR as well.
I usually use pull down and make it to be active high input.

Thanks
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top