Program Segments

There are certain chunks of program which perform a general task, which can be useful for all sorts of programs. A large number of these can be found in PIC : Your Personal Introductory Course, however I have compiled a short list of useful bits and pieces here. If you have any suggestions for this sections, don't hesitate to email me with the program segment and an explanation of its function and capabilities.

  1. Clearing a long list of file registers
  2. Creating an approximate one second delay using a clock frequency of 4MHz
  3. Creating an exact one second delay using a clock frequency of 2.4576MHz

Top
1. Clearing a long list of file registers :

The FSR should be initialised to the first register to be cleared. STARTReg is the first register of the continuous block that is to be cleared and ENDReg is the file register after the last file register to be cleared. (You can simply replace StartReg and ENDReg in the program segment with the corresponding file registers addresses - or rename them with other used file register names).

	movlw    STARTReg
        movwf    FSR
clrloop	
	clrf	INDF		; clears the selected file register
	incf	FSR, f		; moves on to next file register

	movlw	ENDReg		; has it got to the last file reg. ?
	subwf	FSR, w		; ?
	btfss	STATUS, Z	; ?
	b	clrloop		; no, so loops back up.

Top
2. Creating an approximate one second delay using a clock frequency of 4MHz.

(By the way, movf FileReg,w is the same as movwf FileReg)
(Also, the TMR0 has been prescaled by 256 using the OPTION register).


	movlw	d'16'		; sets up postscaler
	movwf	Post16		;

_1SecDelay	
	movf	TMR0, w		; is number in TMR0, 0?
	btfss	STATUS, Z	; ?		
	goto	_1SecDelay	; no, so keeps looping
	
	incf	TMR0, f		; avoides multiple zero read of TMR0
	decfsz	Post16, f	; has this happened 16 times?
	goto	_1SecDelay	; no, so keeps looping

Top
3. Creating an exact one second delay using a clock frequency of 2.4576MHz.

(The TMR0 has been prescaled by 256 using the OPTION register).

	
	movlw   d'80'		; sets up marker to 80 more than 
        addwf   TMR0, w		;  the current value in TMR0
        movwf   Mark80		; 
	movlw	d'30		; sets up postscaler
	movwf	Post30		;

_1SecDelay	
	movf	Mark80, w	; has TMR0 reached marker?
	subwf	TMR0, w		; ?
	btfss	STATUS, Z	; ?		
	goto	_1SecDelay	; no, so keeps looping
	
	movlw	d'80'		; updates marker
	addwf	Mark80, f		
	decfsz	Post30		; has this happened 30 times?
	goto	_1SecDelay	; no, so keeps looping
Copyright ©2006 Perfect Square Ltd.