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.

Pic debounce routine

Status
Not open for further replies.
When a key is pressed you set a flag in the key_flag file that corresponds to the key. When the scan routine comes around the next time, it looks at the key flags and if it sees a flag=1 the corresponding LED is illuminated via the LED_flag file. When the scan routine comes around the next time it sees the LED flag=1 and turns on the LED.
When the scan routine comes around a few scans later it sees if the switch is still pressed. If not it makes the flag=0. The next scan sees the flag=0 and the next time it turns off the LED. If the LED is the flashing LED, it flashes. If not, it just illuminates.
 
Dear friend:
Before I start to code the debounce/keys routine, I wish to ask one thing:
I just noted that some segments of display are showing a litlle (segments a, d and e).
For example, I programed to show number 111, and these segments also are seen (with less intensity, but can be seen).
Can you help so can I try to fix this?
 
Just in case:

This is the main loop code:
Code:
MAIN_LOOP:
	movf	TEMPER,W		; Put temperature in W
	call	BIN2BCD			; Decode Temperature to bcd
	bsf	LED_CMN			; Leds off
	movf	UNITS,W			; copy units to W
	call	DECDISP_A		; convert
	movwf	PORTA			; put it in PORTA
	movf	UNITS,W			; copy units to W
	call	DECDISP_B		; convert
	movwf	PORTB			; put it in PORTA
	bcf	DISP1			; display 1 on
	call	H_keeping		; delay 2 ms
	movf	TENS,W			; copy tens to W
	call	DECDISP_A		; convert
	bsf	DISP1			; display 1 off
	movwf	PORTA			; put it in PORTA
	movf	TENS,W			; copy tens to W
	call	DECDISP_B		; convert
	movwf	PORTB			; put it in PORTA
	bcf	DISP2			; display 2 on
	call	H_keeping		; delay 2 ms
	movf	HUNDREDS,W		; copy hundeds to W
	call	DECDISP_A		; convert
	bsf	DISP2			; display 2 off
	movwf	PORTA			; put it in PORTA
	movf	HUNDREDS,W		; copy hundeds to W
	call	DECDISP_B		; convert
	movwf	PORTB			; put it in PORTA
	bcf	DISP3			; display 3 on
	call	H_keeping		; delay 2 ms
	bsf	DISP3			; display 3 off

	; Leds code:
	bcf	LED_CMN			; Leds on
	call	H_keeping		; delay 2 ms
	bsf		LED_CMN		; Leds off

	goto	MAIN_LOOP		; Back to main loop

and this is the H_keeping code:

Code:
H_keeping:
	bsf	buz			; piezo on
	movlw	.170
	movwf	buz_file
	decfsz	buz_file,f
	goto	$-1
	bcf	buz			; piezo off        
	movlw	.170
	movwf	buz_file
	decfsz	buz_file,f
	goto	$-1
	bcf	buz			; piezo off
	decf	buz_length,F

	; Leds:
	;   7       6       5       4       3       2       1       0
	;   X     onoff    up      down   light    toast   time    temp

	btfsc	leds_s,7	; Is led blink enabled?
	goto	notenabled	; No, get out!
	btfsc	leds_s,6	; Is led on/off on?
	goto	$+3
	bcf	LED_ONOFF	; No - it's activated directly, not using LED_CMN
	goto	$+2
	bsf	LED_ONOFF	; Yes - it's activated directly, not using LED_CMN
notenabled:
	decfsz	Led_blink,F	; Blink control = 0?
	goto	contled		; No
	movlw	D'255'		; Value to preload to
	movwf	Led_blink	; Led blink time control
	btfsc	leds_s,6	; Is led on/off on?
	goto	setledoff
	bsf	leds_s,6	; Led on/off in on
	goto	contled
setledoff:
	bcf	leds_s,6	; Led on/off in off
contled:
	btfsc	leds_s,5	; Is led up on?
	goto	$+3
	bsf	LED_UP		; No
	goto	$+2
	bcf	LED_UP		; Yes
	btfsc	leds_s,4	; Is led down on?
	goto	$+3
	bsf	LED_DWN		; No
	goto	$+2
	bcf	LED_DWN		; Yes
	btfsc	leds_s,3	; Is led light on?
	goto	$+3
	bsf	LED_LIGHT	; No
	goto	$+2
	bcf	LED_LIGHT	; Yes
	btfsc	leds_s,2	; Is led toast on?
	goto	$+3
	bsf	LED_TOASTER	; No
	goto	$+2
	bcf	LED_TOASTER	; Yes
	btfsc	leds_s,1	; Is led time on?
	goto	$+3
	bsf	LED_TIMER	; No
	goto	$+2
	bcf	LED_TIMER	; Yes
	btfsc	leds_s,0	; Is led temp on?
	goto	$+3
	bsf	LED_TEMP	; No
	goto	$+2
	bcf	LED_TEMP	; YES

	retlw	00
 
Last edited:
Yes, I have, but, as for every call to H_keeping, the leds were tested and set, even in display's tests, I have to create an additional flag and process the leds in H_keeping only when this flag is on.
Now, all is working fine.
Defined the new flag:
Code:
FLAGS		EQU	0x2B		; General use flags

#define		LEDS_ON	FLAGS,0		; Flag o see if the leds should be on
The new H_keeping codes is:
Code:
H_keeping:
	bsf	buz		; piezo on
	movlw	.170
	movwf	buz_file
	decfsz	buz_file,f
	goto	$-1
	bcf	buz		; piezo off        
	movlw	.170
	movwf	buz_file
	decfsz	buz_file,f
	goto	$-1
	bcf	buz		; piezo off
	decf	buz_length,F

	; Leds:
	;   7       6       5       4       3       2       1       0
	;   X     onoff    up      down   light    toast   time    temp

	btfsc	leds_s,7	; Is led blink enabled?
	goto	notenabled	; No, get out!
	btfsc	leds_s,6	; Is led on/off on?
	goto	$+3
	bcf	LED_ONOFF	; No - it's activated directly, not using LED_CMN
	goto	$+2
	bsf	LED_ONOFF	; Yes - it's activated directly, not using LED_CMN
notenabled:
	decfsz	Led_blink,F	; Blink control = 0?
	goto	contled		; No
	movlw	D'255'		; Value to preload to
	movwf	Led_blink	; Led blink time control
	btfsc	leds_s,6	; Is led on/off on?
	goto	setledoff
	bsf	leds_s,6	; Led on/off in on
	goto	contled
setledoff:
	bcf	leds_s,6	; Led on/off in off
contled:
	btfss	LEDS_ON
	goto	endleds

	btfsc	leds_s,5	; Is led up on?
	goto	$+3
	bsf	LED_UP		; No
	goto	$+2
	bcf	LED_UP		; Yes
	btfsc	leds_s,4	; Is led down on?
	goto	$+3
	bsf	LED_DWN		; No
	goto	$+2
	bcf	LED_DWN		; Yes
	btfsc	leds_s,3	; Is led light on?
	goto	$+3
	bsf	LED_LIGHT	; No
	goto	$+2
	bcf	LED_LIGHT	; Yes
	btfsc	leds_s,2	; Is led toast on?
	goto	$+3
	bsf	LED_TOASTER	; No
	goto	$+2
	bcf	LED_TOASTER	; Yes
	btfsc	leds_s,1	; Is led time on?
	goto	$+3
	bsf	LED_TIMER	; No
	goto	$+2
	bcf	LED_TIMER	; Yes
	btfsc	leds_s,0	; Is led temp on?
	goto	$+3
	bsf	LED_TEMP	; No
	goto	$+2
	bcf	LED_TEMP	; YES
	bcf	LEDS_ON		; turn leds flag off

endleds:
	retlw	00
and, the main loop is:
Code:
MAIN_LOOP:
	movf	TEMPER,W		; Put temperature in W
	call	BIN2BCD			; Decode Temperature to bcd
	movf	UNITS,W			; copy units to W
	call	DECDISP_A		; convert
	bsf	LED_CMN			; Leds off
	movwf	PORTA			; put it in PORTA
	movf	UNITS,W			; copy units to W
	call	DECDISP_B		; convert
	movwf	PORTB			; put it in PORTA
	bcf	DISP1			; display 1 on
	call	H_keeping		; delay 2 ms
	movf	TENS,W			; copy tens to W
	call	DECDISP_A		; convert
	bsf	DISP1			; display 1 off
	movwf	PORTA			; put it in PORTA
	movf	TENS,W			; copy tens to W
	call	DECDISP_B		; convert
	movwf	PORTB			; put it in PORTA
	bcf	DISP2			; display 2 on
	call	H_keeping		; delay 2 ms
	movf	HUNDREDS,W		; copy hundeds to W
	call	DECDISP_A		; convert
	bsf	DISP2			; display 2 off
	movwf	PORTA			; put it in PORTA
	movf	HUNDREDS,W		; copy hundeds to W
	call	DECDISP_B		; convert
	movwf	PORTB			; put it in PORTA
	bcf	DISP3			; display 3 on
	call	H_keeping		; delay 2 ms
	bsf	DISP3			; display 3 off

	; Leds code:
	bsf	LEDS_ON			; Flag leds on
	bcf	LED_CMN			; Leds on
	call	H_keeping		; delay 2 ms

	goto	MAIN_LOOP		; Back to main loop
 
Last edited:
Dear colin55:

I am trying to make the keys capture work, but no luck.
I have configured, inside the H_keeping routine, the portA and portB pins of the keys, to input and have enabled the individual pull-up resistors for these keys.
The problem is that the keys are not being recognized in the circuit nor in the simulator.
I have created 3 files:
keys_s for the status of keys pressed
Key_deb for the keys debounce
Key_off for the keys off.

Any idea on what could be happening?

Code:
H_keeping:
	bsf	buz			; piezo on
	movlw	.170
	movwf	buz_file
	decfsz	buz_file,F
	goto	$-1
	bcf	buz			; piezo off        
	movlw	.170
	movwf	buz_file
	decfsz	buz_file,F
	goto	$-1
	bcf	buz			; piezo off
	decf	buz_length,F

	; Leds:
	;   7       6       5       4       3       2       1       0
	; blink   onoff     up     down   light    toast   time    temp

	btfss	leds_s,7	; Is led blink enabled?
	goto	contled		;notenabled	; No, get out!

	btfsc	leds_s,6	; Is led on/off on?
	goto	$+3
	bcf	LED_ONOFF	; No - it's activated directly, not using LED_CMN
	goto	$+2
	bsf	LED_ONOFF	; Yes - it's activated directly, not using LED_CMN

;notenabled:
	decfsz	Led_blink,F	; Blink control = 0?
	goto	contled		; No
	movlw	D'255'		; Value to preload to
	movwf	Led_blink	; Led blink time control
	btfsc	leds_s,6	; Is led on/off on?
	goto	setledoff
	bsf	leds_s,6	; Led on/off in on
	goto	contled
setledoff:
	bcf	leds_s,6	; Led on/off in off

contled:
	btfss	LEDS_ON
	goto	endleds
	btfsc	leds_s,5	; Is led up on?
	goto	$+3
	bsf	LED_UP		; No
	goto	$+2
	bcf	LED_UP		; Yes
	btfsc	leds_s,4	; Is led down on?
	goto	$+3
	bsf	LED_DWN		; No
	goto	$+2
	bcf	LED_DWN		; Yes
	btfsc	leds_s,3	; Is led light on?
	goto	$+3
	bsf	LED_LIGHT	; No
	goto	$+2
	bcf	LED_LIGHT	; Yes
	btfsc	leds_s,2	; Is led toast on?
	goto	$+3
	bsf	LED_TOASTER	; No
	goto	$+2
	bcf	LED_TOASTER	; Yes
	btfsc	leds_s,1	; Is led time on?
	goto	$+3
	bsf	LED_TIMER	; No
	goto	$+2
	bcf	LED_TIMER	; Yes
	btfsc	leds_s,0	; Is led temp on?
	goto	$+3
	bsf	LED_TEMP	; No
	goto	$+2
	bcf	LED_TEMP	; YES
endleds:


	; Test_Keys (keys_s):
	;   7       6       5       4       3       2       1       0
	;   X      up     timer   onoff    light   toast   temp    down

	bsf	STATUS,RP0		; Bank 1
	bcf	STATUS,RP1		;  "   "
	bcf	OPTION_REG,NOT_RABPU ; Activate pull-up resistors - Clear RABPU flag

	movlw	B'00111100'		;
	movwf	TRISA			; Make RA2, RA4, RA3 and RA5 inputs (RA3 is a sensor)
	movlw	B'11110000'		;
	movwf	TRISB			; Make RB4, RB5, RB6 and RB7 inputs
	movlw	B'00110100'		;
	movwf	WPUA			; Enable RA2, RA4 and RA5 pull-up internal resistors
	bcf	STATUS,RP0		; Bank 2
	bsf	STATUS,RP1		;  "   "
	movlw	B'11110000'		;
	movwf	WPUB			; Enable RB4, RB5, RB6 and RB7 pull-up internal resistors

	bcf	STATUS,RP0		; Bank 0
	bcf	STATUS,RP1		;  "   "

	; Here start the code to test the keys

	btfsc	KEY_UP			; Key Up pressed?
	goto	$+3				; No
	bsf	keys_s,6		; Yes, set flag
	goto	$+2				; Exit
	bcf	keys_s,6		; No, reset flag
	btfsc	KEY_TIMER		;
	goto	$+3				;
	bsf	keys_s,5		;
	goto	$+2				;
	bcf	keys_s,5		;
	btfsc	KEY_ONOFF		;
	goto	$+3				;
	bsf	keys_s,4		;
	goto	$+2				;
	bcf	keys_s,4		;
	btfsc	KEY_LIGHT		;
	goto	$+3				;
	bsf	keys_s,3		;
	goto	$+2				;
	bcf	keys_s,3		;
	btfsc	KEY_TOASTER		;
	goto	$+3				;
	bsf	keys_s,2		;
	goto	$+2				;
	bcf	keys_s,2		;
	btfsc	KEY_TEMP		;
	goto	$+3				;
	bsf	keys_s,1		;
	goto	$+2				;
	bcf	keys_s,1		;
	btfsc	KEY_DWN			;
	goto	$+3				;
	bsf	keys_s,0		;
	goto	$+2				;
	bcf	keys_s,0		;

	btfss	KEY_UP
	incf	TEMPER,F

	;btfsc	KEY_UP			; KEY_UP pressed?
	;goto	nextkey			; No, process next key
;up_pressed:
	;btfss	leds_s,7		; blink set?
	;goto	setblink		; No, turn it on!
	;bcf	leds_s,7		; Yes, turn it off!
	;goto	nextkey			; Continue with next key
;setblink:
	;bsf	leds_s,7		; Turn blink on
;nextkey:
	; Continue to test the keys...
	; ..........
	; Here, the code to test the keys ends.
;contkey:

	bsf	STATUS,RP0	; Bank 1
	bcf	STATUS,RP1	;  "   "
	bsf	OPTION_REG,NOT_RABPU; Deactivate pull-up resistors - Set RABPU flag

	movlw	B'00001000'	; All PORTA pins outputs, but RA3
	movwf	TRISA		;
	movlw	B'00000000'	; All PORTB pins outputs
	movwf	TRISB		;

	bcf	STATUS,RP0	; Back to Bank 0
	bcf	STATUS,RP1	;  "	"   "

	retlw	00
 
Last edited:
keys_s for the status of keys pressed keys_s should be k_pressed

The reason the keys are not being recognised may be this:
The 47k pullup resistors in the chip can not oversome the low impedance of the LED.
You have to make physically sure all the lines are high impedance when being tested so that the switch will pull them down to 0v.
 
Ok, I will change the Keys_s to k_pressed.
I already made the code for the keys, which is inside the H_keeping, work before.
In this code, the internal pull-up resistors of the chip are enabled (thru WPUA and WPUB registers), and the bit NOT_RABPU of register OPTION_REG is also cleared to enable these internal pull-up resistors.
I don't know why, now, it's not working anymore.
I will make a test out of the scope of H_keeping, in main loop, just to see if the keys will work.
 
You have to make a program that halts just at the point where it is looking at the keys and measure the voltage on the top of the button when it is pressed and not pressed.
 
Done.
With the program below, after it reaches the goto $, I measured the voltage on the keys lines and they are:
for every key, when not pressed: 5.1 Volts. When pressed: 0 Volts.

Code:
;/****************************************************************************
;* DESCRIPTION: System definitions.
;*****************************************************************************/ 

	ERRORLEVEL -302 ;remove message about using proper bank

	LIST  P=16F677
	INCLUDE <P16F677.INC>
	__CONFIG _BOR_OFF & _FCMEN_OFF & _IESO_OFF & _CPD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTOSCIO

keys_s		EQU	0x20						; Keys pressed state

; Displays
#define		DISP1				PORTC,3		; Anode display DIS0
#define		DISP2				PORTC,4		; Anode display DIS1
#define		DISP3				PORTC,5		; Anode display DIS2

; Leds
#define		LED_CMN				PORTC,2		; Leds Anodes

	ORG		0x0000
	goto	START

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

START:
	bcf		STATUS,RP0		; Bank 2
	bsf		STATUS,RP1		;
	clrf	ANSEL			; digital I/O
	clrf	ANSELH			;	"	"	"
	movlw	B'00000000'
	movwf	CM1CON0			; disable comparator
	movlw	B'00000000'		; Not necessary?
	movwf	CM2CON0			; disable 2nd comparator
	movlw	B'00000000'		; Not necessary?
	movwf	CM2CON1			; disable 3rd comparator
	bsf		STATUS,RP0		; Bank 1
	bcf		STATUS,RP1		;
	movlw	B'00001000'		;
	movwf	TRISA			;
	movlw	B'00000000'		;
	movwf	TRISB			;
	movlw	B'10000010'		;
	movwf	TRISC			;
	bcf		STATUS,RP0		; Bank 0
	bcf		STATUS,RP1		;
	bcf		SSPCON,SSPEN	; Disable Synchronous Serial Port

	; Turn off all displays and leds
	bsf		LED_CMN			; Leds off
	bsf		DISP1			; Display1 off
	bsf		DISP2			; Display2 off
	bsf		DISP3			; Display3 off
	bsf		STATUS,RP0		; Bank 1
	bcf		STATUS,RP1		;  "   "
	bcf		OPTION_REG,NOT_RABPU ; Activate pull-up resistors - Clear RABPU flag

	movlw	B'00111100'		;
	movwf	TRISA			; Make RA2, RA4, RA3 and RA5 inputs (RA3 is a sensor)
	movlw	B'11110000'		;
	movwf	TRISB			; Make RB4, RB5, RB6 and RB7 inputs
	movlw	B'00110100'		;
	movwf	WPUA			; Enable RA2, RA4 and RA5 pull-up internal resistors
	bcf		STATUS,RP0		; Bank 2
	bsf		STATUS,RP1		;  "   "
	movlw	B'11110000'		;
	movwf	WPUB			; Enable RB4, RB5, RB6 and RB7 pull-up internal resistors

	bcf		STATUS,RP0		; Bank 0
	bcf		STATUS,RP1		;  "   "

	; Here start the code to test the keys

	btfsc	PORTB,7			; Key Up pressed?
	goto	$+3				; No
	bsf		keys_s,6		; Yes, set flag
	goto	$+2				; Exit
	bcf		keys_s,6		; No, reset flag
	btfsc	PORTB,6			;
	goto	$+3				;
	bsf		keys_s,5		;
	goto	$+2				;
	bcf		keys_s,5		;
	btfsc	PORTB,5			;
	goto	$+3				;
	bsf		keys_s,4		;
	goto	$+2				;
	bcf		keys_s,4		;

	goto	$

	bsf		STATUS,RP0	; Bank 1
	bcf		STATUS,RP1	;  "   "
	bsf		OPTION_REG,NOT_RABPU; Deactivate pull-up resistors - Set RABPU flag

	movlw	B'00001000'	; All PORTA pins outputs, but RA3
	movwf	TRISA		;
	movlw	B'00000000'	; All PORTB pins outputs
	movwf	TRISB		;

	bcf		STATUS,RP0	; Back to Bank 0
	bcf		STATUS,RP1	;  "	"   "

	goto START

	END
 
Now create a program with just a few lines of code that looks at the switch and displays the corresponding LED.
The program should have no more than 15 lines of code. Don't have anything other than the needed lines of code.
Just get the recognition of the keys worked out.
 
Dar friend colin55:

I thank you for all your help, but I am going to tell those people that if they don't allow to change the processor and some of the circuit, I will give up on this. We need to use a processor which have all display's cathodes, leds catodes and keys tied to one port which have internal pull-up resitors (for example PORTB).
As I said before, this is a board which is used in a commercial product and it works. I don't know why they used this processor and this architecture, because it's very difficult to make work.
Using another architecture, with another decent processor (decent for this application), it's a matter of putting the data in portb, then change it's tris register to input, enable pull-up resistors and read it to have the keys status.
With this processor, we need to test pin by pin, an this is not working.
Anyway, I will change the processor and make a test circuit, so we can continue this project, if you agree to continue to help me.
I really thank you for your continuous support and kind help.

Sergio
 
I told you, right from the beginning, in post #11, that this processor and the circuit was an absolute mess, but we have nearly finished the design and it is absolutely untrue that the testing of the keys will not work.
You have not followed my instructions to create a routine that just has the code necessary to test the keys. Show me the code you have produced.
You really don't know how to follow instructions. I have already given you sets of instructions as a 1,2,3,4, and so far you have not replied as 1 2 3 4 . . . done.
That's the only way to work on a complex project like this.
 
Last edited:
I have done it, but it won't work either.
I really don't know what to do, because this circuit works in the oven (they borrow me one, to test)!
I am lost, I hope you forgive me.
The code is bigger than the 15 lines you told, because I need to enable the WUPB bits and NOT_RABPU.

Code:
	ERRORLEVEL -302 ;remove message about using proper bank

	LIST  P=16F677
	INCLUDE <P16F677.INC>
	__CONFIG _BOR_OFF & _FCMEN_OFF & _IESO_OFF & _CPD_OFF & _CP_OFF & _MCLRE_OFF & _PWRTE_ON & _WDT_OFF & _INTOSCIO

dcount		EQU	0x21
portatmp	EQU	0x22
portbtmp	EQU	0x23

	ORG		0x0000
	goto	START

START:
	bsf		STATUS,RP0		; Bank 1
	bcf		STATUS,RP1		;
	movlw	B'10111111'		;
	movwf	TRISB			; PORTB,6 output (led)
	movlw	B'11000011'		;
	movwf	TRISC			; PORTC,2,3,4,5 output (leds & displays enable)
	movlw	B'10000000'		;
	movwf	TRISB			; Make RB7 input
	bcf		STATUS,RP0		; Bank 0
	bcf		STATUS,RP1		;  "   "
	movlw	D'255'
	movwf	PORTB
	movlw	D'255'
	movwf	PORTA
	movlw	B'11111111'
	movwf	PORTC

	bcf		STATUS,RP0		; Bank 2
	bsf		STATUS,RP1		;  "   "
	movlw	B'11110000'		;
	movwf	WPUB			; Enable all portb pull-up internal resistor

	bsf		STATUS,RP0		; Bank 1
	bcf		STATUS,RP1		;  "   "
	bcf		OPTION_REG,NOT_RABPU ; Activate pull-up resistors - Clear RABPU flag
	
	bcf		STATUS,RP0		; Bank 0
	bcf		STATUS,RP1		;  "   "
	movlw	PORTB			; move portb to W
	movwf	portbtmp		; to tmp
	bsf		STATUS,RP0		; Bank 1
	bcf		STATUS,RP1		;  "   "
	bsf		OPTION_REG,NOT_RABPU; Deactivate pull-up resistors - Set RABPU flag

	bcf		STATUS,RP0		; Back to Bank 0
	bcf		STATUS,RP1		;  "	"   "
	btfsc	portbtmp,7		; Key Up pressed?
	goto	$+3				; No
	bcf		PORTB,6			; Yes, set led
	goto	$+2				; Exit
	bsf		PORTB,6			; No, reset led
	bcf		PORTC,2			; enable led
	CLRF	dcount
delay1:
	DECFSZ	dcount,F		; give led some time to show
	GOTO 	delay1
	goto START
	END
 
Ok, I have searched the older source codes of this project and have found a keyboard routine which was working.
I have put it, in the H_keeping, after the leds test. With this, the key test is working and, when I press the up button, the value in display is incremented (very fast, but it's just to test):
Code:
H_keeping:
	bsf	buz			; piezo on
	movlw	.170
	movwf	buz_file
	decfsz	buz_file,F
	goto	$-1
	bcf	buz			; piezo off        
	movlw	.170
	movwf	buz_file
	decfsz	buz_file,F
	goto	$-1
	bcf	buz			; piezo off
	decf	buz_length,F


	; Leds:
	;   7       6       5       4       3       2       1       0
	; blink   onoff     up     down   light    toast   time    temp

	btfss	leds_s,7	; Is led blink enabled?
	goto	contled		; notenabled	; No, get out!

	btfsc	leds_s,6	; Is led on/off on?
	goto	$+3
	bcf	LED_ONOFF	; No - it's activated directly, not using LED_CMN
	goto	$+2
	bsf	LED_ONOFF	; Yes - it's activated directly, not using LED_CMN

;notenabled:
	decfsz	Led_blink,F	; Blink control = 0?
	goto	contled		; No
	movlw	D'255'		; Value to preload to
	movwf	Led_blink	; Led blink time control
	btfsc	leds_s,6	; Is led on/off on?
	goto	setledoff
	bsf	leds_s,6	; Led on/off in on
	goto	contled
setledoff:
	bcf	leds_s,6	; Led on/off in off

contled:
	btfss	LEDS_ON
	goto	endleds
	btfsc	leds_s,5	; Is led up on?
	goto	$+3
	bsf	LED_UP		; No
	goto	$+2
	bcf	LED_UP		; Yes
	btfsc	leds_s,4	; Is led down on?
	goto	$+3
	bsf	LED_DWN		; No
	goto	$+2
	bcf	LED_DWN		; Yes
	btfsc	leds_s,3	; Is led light on?
	goto	$+3
	bsf	LED_LIGHT	; No
	goto	$+2
	bcf	LED_LIGHT	; Yes
	btfsc	leds_s,2	; Is led toast on?
	goto	$+3
	bsf	LED_TOASTER	; No
	goto	$+2
	bcf	LED_TOASTER	; Yes
	btfsc	leds_s,1	; Is led time on?
	goto	$+3
	bsf	LED_timer	; No
	goto	$+2
	bcf	LED_timer	; Yes
	btfsc	leds_s,0	; Is led temp on?
	goto	$+3
	bsf	LED_TEMP	; No
	goto	$+2
	bcf	LED_TEMP	; YES
endleds:
	bcf	LEDS_ON


	; Test_Keys (k_pressed):
	;   7       6       5       4       3       2       1       0
	;   X      up     timer   onoff    light   toast   temp    down

	bsf	STATUS,RP0		; Bank 1
	bcf	STATUS,RP1		;  "   "
	bcf	OPTION_REG,NOT_RABPU ; Activate pull-up resistors - Clear RABPU flag

	movlw	B'00111100'		;
	movwf	TRISA			; Make RA2, RA4, RA3 and RA5 inputs (RA3 is a sensor)
	movlw	B'11110000'		;
	movwf	TRISB			; Make RB4, RB5, RB6 and RB7 inputs
	movlw	B'00110100'		;
	movwf	WPUA			; Enable RA2, RA4 and RA5 pull-up internal resistors
	bcf	STATUS,RP0		; Bank 2
	bsf	STATUS,RP1		;  "   "
	movlw	B'11110000'		;
	movwf	WPUB			; Enable RB4, RB5, RB6 and RB7 pull-up internal resistors
	bsf	STATUS,RP0		; Bank 1
	bcf	STATUS,RP1		;
	bsf	OPTION_REG,7		; Set RABPU flag
	bcf	STATUS,RP0		; Bank 0
	bcf	STATUS,RP1		;

	btfss	KEY_UP			; KEY_UP pressed?
	incf	temper,F		; Yes

	btfsc	KEY_UP			; Key Up pressed?
	goto	$+3				; No
	bsf	k_pressed,6		; Yes, set flag
	goto	$+2				; Exit
	bcf	k_pressed,6		; No, reset flag
	btfsc	KEY_timer		;
	goto	$+3				;
	bsf	k_pressed,5		;
	goto	$+2				;
	bcf	k_pressed,5		;
	btfsc	KEY_ONOFF		;
	goto	$+3				;
	bsf	k_pressed,4		;
	goto	$+2				;
	bcf	k_pressed,4		;
	btfsc	KEY_LIGHT		;
	goto	$+3				;
	bsf	k_pressed,3		;
	goto	$+2				;
	bcf	k_pressed,3		;
	btfsc	KEY_TOASTER		;
	goto	$+3				;
	bsf	k_pressed,2		;
	goto	$+2				;
	bcf	k_pressed,2		;
	btfsc	KEY_TEMP		;
	goto	$+3				;
	bsf	k_pressed,1		;
	goto	$+2				;
	bcf	k_pressed,1		;
	btfsc	KEY_DWN			;
	goto	$+3				;
	bsf	k_pressed,0		;
	goto	$+2				;
	bcf	k_pressed,0		;

	bsf	STATUS,RP0		; Bank 1
	bcf	STATUS,RP1		;
	movlw	B'00000000'
	movwf	WPUA
	bcf	STATUS,RP0		; Bank 2
	bsf	STATUS,RP1		;
	movlw	B'00000000'
	movwf	WPUB
	bsf	STATUS,RP0		; Bank 1
	bcf	STATUS,RP1		;
	bcf	OPTION_REG,7		; Set RABPU flag
	movlw	B'00001000'		;
	movwf	TRISA			;
	movlw	B'00000000'		;
	movwf	TRISB			;
	bcf	STATUS,RP0		; Bank 0
	bcf	STATUS,RP1		;

	retlw	00

Now, the problem is that the leds are blinking, from time to time, when I press the up button, but I am trying to figure out why this is happening.
 
Last edited:
I have removed the unwated lines of code.

The program is written in Page 0 and some of the registers are in page1. All the rgisters you ahve used are in page 1 so you don't have to deal with page2.

Check through what I have changed

The weak pullups are turned ON when bit 7 is "0"

The beep must be incorportated in the scan, just like the flashing LED, otherwise, when it is called, the flash rate will change.

I am not saying my changes are all correct, so only change a few things at a time and try it and come back.

Does the program turn on the correct LED when the key is pressed.





Code:
H_keeping:

beep    ;beep has to be incorporated into scan routine it cannot take up extra time as this will 
     ;change the flash-rate 


    ;bsf    buz            ; piezo on
    ;movlw    .170
    ;movwf    buz_file
    ;decfsz    buz_file,F
    ;goto    $-1
    ;bcf    buz            ; piezo off        
    ;movlw    .170
    ;movwf    buz_file
    ;decfsz    buz_file,F
    ;goto    $-1
    ;bcf    buz            ; piezo off
    ;decf    buz_length,F


    ; Leds:
    ;   7       6       5       4       3       2       1       0
    ; blink   onoff     up     down   light    toast   time    temp

    btfss    leds_s,7    ; Is led blink enabled?
    goto    contled        ; notenabled    ; No, get out!

    btfsc    leds_s,6    ; Is led on/off on?
    goto    $+3
    bcf    LED_ONOFF    ; No - it's activated directly, not using LED_CMN
    goto    $+2
    bsf    LED_ONOFF    ; Yes - it's activated directly, not using LED_CMN

;notenabled:
    decfsz    Led_blink,F    ; Blink control = 0?
    goto    contled        ; No
    movlw    D'255'        ; Value to preload to
    movwf    Led_blink    ; Led blink time control
    btfsc    leds_s,6    ; Is led on/off on?
    goto    setledoff
    bsf    leds_s,6    ; Led on/off in on
    goto    contled
setledoff:
    bcf    leds_s,6    ; Led on/off in off

contled:
    btfss    LEDS_ON
    goto    endleds
    btfsc    leds_s,5    ; Is led up on?
    goto    $+3
    bsf    LED_UP        ; No
    goto    $+2
    bcf    LED_UP        ; Yes
    btfsc    leds_s,4    ; Is led down on?
    goto    $+3
    bsf    LED_DWN        ; No
    goto    $+2
    bcf    LED_DWN        ; Yes
    btfsc    leds_s,3    ; Is led light on?
    goto    $+3
    bsf    LED_LIGHT    ; No
    goto    $+2
    bcf    LED_LIGHT    ; Yes
    btfsc    leds_s,2    ; Is led toast on?
    goto    $+3
    bsf    LED_TOASTER    ; No
    goto    $+2
    bcf    LED_TOASTER    ; Yes
    btfsc    leds_s,1    ; Is led time on?
    goto    $+3
    bsf    LED_timer    ; No
    goto    $+2
    bcf    LED_timer    ; Yes
    btfsc    leds_s,0    ; Is led temp on?
    goto    $+3
    bsf    LED_TEMP    ; No
    goto    $+2
    bcf    LED_TEMP    ; YES
endleds:
    bcf    LEDS_ON


    ; Test_Keys (k_pressed):
    ;   7       6       5       4       3       2       1       0
    ;   X      up     timer   onoff    light   toast   temp    down

    bsf    STATUS,RP0        ; Bank 1
                            ;bcf    STATUS,RP1        ;  "   "
    bcf    OPTION_REG,NOT_RABPU ; Activate pull-up resistors - Clear RABPU flag

    movlw    B'00111100'        ;
    movwf    TRISA            ; Make RA2, RA4, RA3 and RA5 inputs (RA3 is a sensor)
    movlw    B'11110000'        ;
    movwf    TRISB            ; Make RB4, RB5, RB6 and RB7 inputs
    movlw    B'00110100'        ;
    movwf    WPUA            ; Enable RA2, RA4 and RA5 pull-up internal resistors
    movlw    B'11110000'
    movwf    WPUB            ; Enable RB4, RB5, RB6 and RB7 pull-up internal resistors
    
    bsf        OPTION_REG,7        ; Set RABPU flag
    
                            ;bcf    STATUS,RP0        ; Bank 2
                            ;bsf    STATUS,RP1        ;  "   "
            ;
    
                            ;bsf    STATUS,RP0        ; Bank 1
                            ;bcf    STATUS,RP1        ;
    
    bcf    STATUS,RP0        ; Bank 0
                            ;bcf    STATUS,RP1        ;

    btfss    KEY_UP            ; KEY_UP pressed?
    incf    temper,F        ; Yes

    btfsc    KEY_UP            ; Key Up pressed?
    goto    $+3                ; No
    bsf    k_pressed,6        ; Yes, set flag
    goto    $+2                ; Exit
    bcf    k_pressed,6        ; No, reset flag
    btfsc    KEY_timer        ;
    goto    $+3                ;
    bsf    k_pressed,5        ;
    goto    $+2                ;
    bcf    k_pressed,5        ;
    btfsc    KEY_ONOFF        ;
    goto    $+3                ;
    bsf    k_pressed,4        ;
    goto    $+2                ;
    bcf    k_pressed,4        ;
    btfsc    KEY_LIGHT        ;
    goto    $+3                ;
    bsf    k_pressed,3        ;
    goto    $+2                ;
    bcf    k_pressed,3        ;
    btfsc    KEY_TOASTER        ;
    goto    $+3                ;
    bsf    k_pressed,2        ;
    goto    $+2                ;
    bcf    k_pressed,2        ;
    btfsc    KEY_TEMP        ;
    goto    $+3                ;
    bsf    k_pressed,1        ;
    goto    $+2                ;
    bcf    k_pressed,1        ;
    btfsc    KEY_DWN            ;
    goto    $+3                ;
    bsf    k_pressed,0        ;
    goto    $+2                ;
    bcf    k_pressed,0        ;

    bsf    STATUS,RP0        ; Bank 1
                                ;bcf    STATUS,RP1        ;
                                ;movlw    B'00000000'    why do you want to set and clear the weak pullups??
    bcf        WPUA,7
                            ;bcf    STATUS,RP0        ; Bank 2
                            ;bsf    STATUS,RP1        ;
                                ;movlw    B'00000000'
    bcf        WPUB,7
                        ;bsf    STATUS,RP0        ; Bank 1
                        ;bcf    STATUS,RP1        ;
    bcf    OPTION_REG,7        ; Set RABPU flag
    movlw    B'00001000'        ;
    movwf    TRISA            ;
    movlw    B'00000000'        ;
    movwf    TRISB            ;
    bcf    STATUS,RP0        ; Bank 0
                        ;bcf    STATUS,RP1        ;

    retlw    00
 
Last edited:
I was using the page 2 because the WPUB register is in page 2 (page 28 of 16f677 manual).
Now, the leds are more brighter and displays less brighter.
When pressed the up key, the value of temper in display increments very fast and locks at 200.
why do you want to set and clear the weak pullups??
I though that it was necessary to make the leds/display work fine (never tested if not necessary.
 
for WPUB go to page 2 and back to page 0

You are taking too long in the 4 sector of SCAN .

What are you doing in the fourth part of SCAN?

You should never be doing anything other than setting bits and testing bits
All the turning on and off of the blinking LED is done via a counter that counts the number of scans and the same with the beep. Post your simplified code.
 
Last edited:
Status
Not open for further replies.

Latest threads

New Articles From Microcontroller Tips

Back
Top