Help with using INCFSZ (possible bug?)

For C and ASSEMBLY users to post questions and code snippets for programming in C and ASSEMBLY. And for any other C or ASM course related questions.

Moderators: Benj, Mods

Post Reply
econnections
Posts: 35
Joined: Fri Dec 26, 2008 4:02 pm
Contact:

Help with using INCFSZ (possible bug?)

Post by econnections »

PIC: 16F84a
Assembler: Matrix Multimedia ASM - IDE


I've been trying to use the INFSZ command in a loop but find it causes the PIC to demonistrate unusual behaviour, particularly when values are sent to PORTB.
The program will work when run in RC mode at around 100hz but behaves oddly XT mode.

The program below shows this and I wondered if anyone has seen this problem; also am I doing something wrong?

The program sets all bits on PORTB as outputs then clears the port. It then lights LEDS connected to RB7 + RB6 to show the start of the program.
Next it loads an initial value into the variable COUNT via register w.
The program then begins to loop until COUNT overflows.
PORTB is cleared and LEDS on RB0 and RB1 are illuminated to show the end of the program.


The problems that occur are:
Any LEDS illuminated before INCFSZ (ie RB6 and RB7) cannot be switched off. ie RB6 and RB7 are not cleared by CLRF.
If the initial value for COUNT is set to H'F0' then the LEDS are particially turnned off.

Code: Select all

; The following line embeds configuration data into the PICmicro
	__CONFIG H'3FF9'                ; XT mode
;	__CONFIG H'3FFB'                ; RC mode
;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 
				
include "C:\PROGRA~1\MPLAB\P16F84a.INC"

COUNT	EQU H'21'

; Program Start:
        ORG 0
        GOTO   005            
        NOP                   
        NOP          
        NOP                   
        NOP 
	BSF STATUS,5	;BANK1
	CLRF TRISB	;SET-UP PORTB AS OUTPUTS
	BCF STATUS,5	;BANK0
	CLRF PORTB	;CLEAR PORTB (LEDS OFF)
	BSF PORTB,6	;LIGHT RB6
	BSF PORTB,7	;LIGHT RB7
	MOVLW	H'0'	;PREPARE INITIAL VALUE FOR COUNT
	MOVWF	COUNT	;LOAD COUNT WITH INITIAL VALUE
NEXT	INCFSZ	COUNT,1	;INCREMENNT COUNT UNTIL OVERFLOW
	GOTO NEXT		;LOOP
	GOTO FINISH	;WHEN OVERFLOW OCCURS GOTO FINISH
FINISH	CLRF PORTB	;CLEAR PORTB (ALL LEDS OFF)
	BSF PORTB,0	;LIGHT RB0 to show program finished
	BSF PORTB,1	;LIGHT RB1
	END


EXAMPLE 2
In the second example the value of COUNT is sent to PORTB so changes can be monitored when run in RC mode at 100Hz. Again every thing appears OK but when run in XT mode (4Mhz crystal) the LEDS remain on after exiting the loop when in fact they should be off as a result of the CLRF COUNT command.

I have run the program on the Matrixmultimedia v2 Development Board and on a prototype board using different 16F84a chips but still get the same problem. It also occurs when using DECFSZ.

Code: Select all

; The following line embeds configuration data into the PICmicro
	__CONFIG H'3FF9'                ; xt mode
;	__CONFIG H'3FFB'                ; RC mode
;::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: 
				
include "C:\PROGRA~1\MPLAB\P16F84a.INC"

	radix hex

COUNT	EQU H'21'
DELAY	EQU H'22'

; Program Start:
	ORG	0		;Reset vector
	GOTO	5		;Goto start of program
	ORG	4		;Interrupt vector
	GOTO	5		;Goto start of program
	ORG	5		;Start of program memory
	BSF STATUS,5		;bank1
	bcf OPTION_REG, 7		;enable PORTB pullup resistors
	MOVLW 0
	MOVWF TRISB		;set-up PORTB as outputs
	BCF STATUS,5		;bank0
	CLRF PORTB		;clear PORTB (LEDS off)
	CLRF COUNT		;set COUNT= = 0
LOOP	INCFSZ COUNT,1		;increment COUNT until overflow
	GOTO NEXT			
	GOTO FINISH
NEXT	MOVF COUNT,0		;copy COUNT to w
	MOVWF PORTB		;show value of COUNT on PORTB using w
	GOTO LOOP			;continue in loop
FINISH	CLRF PORTB		;after exiting loop clear PORtB (ALL LEDS off)
	BSF PORTB,0		;light RB0 to show program finished
	END

User avatar
Steve
Matrix Staff
Posts: 3422
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: Help with using INCFSZ (possible bug?)

Post by Steve »

I have 2 thoughts on this:

1) If your clock speed is greater than 4MHz, then you should be using the HS oscillator mode - perhaps that will work better.

2) When running very fast, the program will wrap around in memory and so will restart many times each second. If you put a command such as "STOP goto STOP" at the end of your program, this should prevent the program restarting each time.

econnections
Posts: 35
Joined: Fri Dec 26, 2008 4:02 pm
Contact:

Re: Help with using INCFSZ (possible bug?)

Post by econnections »

Steve,

I added the loop to the end of the program as you suggested and that appears to have resolved the problem. I had assumed that the command END did the job but maybe that is for use by the assembler. Anyway, problem resolved and thank you for your help.

Someone suggested that I use an 18 series PIC rather than the 16 as this would save swapping banks...
Is there one you would recommend for use with the v2 development board for writing and testing simple programs.
Last edited by econnections on Wed Feb 04, 2009 11:58 am, edited 1 time in total.

User avatar
Steve
Matrix Staff
Posts: 3422
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times
Contact:

Re: Help with using INCFSZ (possible bug?)

Post by Steve »

The "END" statement simply tells the assembler not to look beyond that point - it does not generate any code.

As for an 18F PIC, it really does depend on what you want to do. If it's simplicity, then the cheapest one you can get hold of should be fine (although you might want a 40-pin device).

Post Reply