Electronic Projects, forums and more.

Go Back   Electronic Circuits Projects Diagrams Free > Electronics Categories > Micro Controllers


Micro Controllers Discuss all aspects of micro controllers - building them, coding them, etc. All controllers are welcome - PIC, BASIC, Z8 Encore!, etc.

Reply
 
Tools
Old 4th June 2007, 06:36 PM   #1
Default Please help me in writing code

Please help me in writing code

This is the design

Attached Files
File Type: asm Switch.asm (9.1 KB, 21 views)

Last edited by abdosat2000; 4th June 2007 at 06:44 PM.
abdosat2000 is offline  
Old 4th June 2007, 06:46 PM   #2
Default

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
abdosat2000 is offline  
Old 4th June 2007, 06:53 PM   #3
Default

I correct mistakes were evaluated and a new program even know what mistakes Thank you
abdosat2000 is offline  
Old 4th June 2007, 06:53 PM   #4
Default

This looks curiously identical to:
http://www.nalanda.nitc.ac.in/indust...itch/2_003.pdf
Yet no reference is made to credit the original coder...hmmmm

Last edited by Norlin; 4th June 2007 at 06:56 PM.
Norlin is offline  
Old 4th June 2007, 07:09 PM   #5
Default

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 by abdosat2000; 4th June 2007 at 07:12 PM.
abdosat2000 is offline  
Old 5th June 2007, 08:34 AM   #6
Default

Hi,
Shouldn't the inputs GP0, GP1, GP3 and GP4 be connected by pull up resistors?
__________________
bananasiong
bananasiong is offline  
Old 5th June 2007, 09:19 AM   #7
Default

It uses the internal weak pullups.

Mike.
Pommie is offline  
Old 5th June 2007, 09:29 AM   #8
Default

Quote:
Originally Posted by Pommie
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
__________________
bananasiong

Last edited by bananasiong; 5th June 2007 at 09:33 AM.
bananasiong is offline  
Old 5th June 2007, 02:37 PM   #9
Default

Thank you, I want to correct the mistakes and I will do compared them
abdosat2000 is offline  
Old 5th June 2007, 02:41 PM   #10
Default

I am new in the programming of Pic
Please help me and thank you
abdosat2000 is offline  
Reply

Tags
code, writing

Thread Tools
Display Modes


Similar
Title Starter Forum Replies Latest
Accessing EEPROM Clayton Micro Controllers 15 23rd March 2007 04:00 AM
Need some help with a code provided by ATMEL ikalogic Micro Controllers 1 23rd January 2007 03:46 PM
Question on writing code for PIC johnnyquest Micro Controllers 3 21st December 2004 01:05 PM
Tough assembly program for the PIC16F84 asmpic Micro Controllers 34 3rd December 2004 07:50 PM
An error in pic16f84a, why? Zener_Diode Micro Controllers 6 11th April 2004 03:55 AM



All times are GMT. The time now is 01:06 PM.


Electronic Circuits  |  Learning Electronics
eXTReMe Tracker