;**********************************************************************
;   This file shows some techniques for making it possible to         *
;   debug your PIC16F874 program using an oscilloscope.  It is a      *
;   waste of time to do this before having used the software          *  
;   simulator as much as possible first.  Once the simulator suggests *
;   that your program ought to work, then it is reasonable to         *
;   investigate its behavior when executed at full speed in the       *
;   microprocessor.                                                   *
;                                                                     *
;   The essential idea is to cause your program to output an extra    *
;   signal that can be used to provide a display trigger for the      *
;   oscilloscope.  Without a repetitive trigger point, an oscilloscope*
;   display is different from one sweep to the next and so is         *
;   largely unusable.                                                 *
;                                                                     *
;   This program illustrates two means of establishing a repeatable   *
;   trigger.                                                          *
;     1.  Output a short-duration pulse at the start of every pass    *
;         through the main loop of the program.                       *
;     2.  Output a coded pulse at key points in the program.  An      *
;         oscilloscope capable of detecting specific combinations     *
;         of its inputs and triggering when those combinations        *
;         occur can take advantage of the code to locate different    *
;         portions of the code, so to speak.                       *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Filename:	    OscopeDebugging.asm                               *
;    Date:          January 31, 2006                                  *
;    File Version:  1                                                 *
;                                                                     *
;    Author:        CDR Charles B. Cameron                            *
;    Company:       United States Naval Academy                       *
;                                                                     * 
;                                                                     *
;**********************************************************************
;                                                                     *
;    Files required:                                                  *
;                  p16f874.inc                                                   *
;                                                                     *
;                                                                     *
;**********************************************************************
;                                                                     *
;    Notes:                                                           *
;                                                                     *
;           Assume that Port D is being used to operate a circuit.    *
;           Assume that Port B Bit 0 is being used to output a        *
;              trigger pulse every time the program executes the      *
;              main loop.                                             *
;           Assume that Port C bits 3-0 are being used to output     *
;              a 4-bit code at various points in the program.         *
;                                                                     *
;**********************************************************************


	list      p=16f874            ; list directive to define processor
	#include <p16f874.inc>        ; processor specific variable definitions
	
; Watch dog timer is disabled by default.
	__CONFIG _CP_OFF & _WDT_OFF & _BODEN_ON & _PWRTE_ON & _HS_OSC & _WRT_ENABLE_ON & _LVP_ON & _CPD_OFF


;***** VARIABLE DEFINITIONS
Trigger	equ	0x20	; This register holds the trigger code before it
					; is shipped out to Port C

TriggerBit	equ	0	; Bit 0 of Port B carries an oscilloscope trigger
TRISBMask	equ B'11111110'	; Bit 0 has to be an output to support the
							; TriggerBit.

TriggerCodeMask equ B'00001111' ; Bits 3-0 of Port C carry an
                                ; oscilloscope trigger code
TRISCMask	equ B'00000000'		;  All bits of Port C are outputs.
TriggerCodeB	equ 4	; Code = 4 to indicate location B in the program
TriggerCodeF	equ 5	; Code = 5 to indicate location F in the program
TriggerCodeJ	equ 12	; Code = 12 to indicate location J in the program

TRISDMask	equ B'00000000'	; Initialize Port D for outputs.
							; The Port D bits are meant to simulate operation
							; an external circuit.


;**********************************************************************
		ORG     0x000             ; processor reset vector
		clrf    PCLATH            ; ensure page bits are cleared
  		goto    main              ; go to beginning of program



main

; Initialize Port B direction bits
	bcf		STATUS,RP1	; Select Bank 1
	bsf		STATUS,RP0
	movlw	TRISBMask	; Initialize TRISB
	movwf	TRISB
	
; Initialize Port C direction bits
	movlw	TRISCMask	; Initialize TRISC
	movwf	TRISC
	
; Initialize Port D direction bits
	movlw	TRISDMask	; Initialize TRISD
	movwf	TRISD
	
	bcf		STATUS,RP0	; Revert to Bank 0
	
	; Clear Port B
	clrf	PORTB
	
	; Place B'1011' in bits 7 to 4 of Port C to illustrate that
	; trigger codes can change but these bits will not be
	; affected.
	movlw	B'10110000'
	movwf	PORTC

	; Clear Port D
	clrf	PORTD
	
loop
	call	OutputOneBitTrigger
	
	; Manipulate Port D to simulate operating a circuit
	movlw	5
	movwf	PORTD
	addlw	14
	movwf	PORTD
	sublw	5
	movwf	PORTD
	
	; Code point B
	movlw	TriggerCodeB
	call	OutputTriggerCode
	call	ClearTriggerCode
	
	movlw	35
	movwf	PORTD
	sublw	15
	movwf	PORTD
	addlw	16
	movwf	PORTD

	; Code point B
	movlw	TriggerCodeJ
	call	OutputTriggerCode
	call	ClearTriggerCode
	
	; Manipulate Port D a little more
	swapf	PORTD,F
	decf	PORTD,F
	
	; Code point F
	movlw	TriggerCodeF
	call	OutputTriggerCode
	call	ClearTriggerCode
	
	goto loop


OutputOneBitTrigger
	bsf	PORTB,TriggerBit	; Pulse the Trigger for one cycle
	bcf	PORTB,TriggerBit
	return

OutputTriggerCode
	; Assume the trigger code is register W.
	; Output it to Port C for a few cycles without
	; disturbing other bits of Port C.
	movwf	Trigger
	movf	PORTC,W	; Retrieve current value of Port C
	andlw	~TriggerCodeMask	; Clear the code bits
	iorwf	Trigger,W	; Merge with the code bits
	movwf	PORTC	; Output the new code
	return
	
ClearTriggerCode
	movlw	~TriggerCodeMask
	andwf	PORTC,F	; Clear the code.
	return


		END                       ; directive 'end of program'


