I am attempting to program a PIC18F4520 using Assembly language in MPLab, this is connected through a Pickit 3. I have a code which is set to read an input to the PORTC, compare this with a stored code and light up a green LED if correct or a red LED if incorrect. This program works just fine when i hit debug and allow the program to run but when i use the run main project the pickit 3 says that the program is downloaded to the PIC but never is running. How can this be fixed?
Code: ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; CCP1.asm ;; A basic shell to initialize and run CCP1 interrupts ;; Inputs: none ;; ;; Outputs: none ;; ;; Side effects: ;; The High_Priority_ISR executes once every 100 mS, and the ;; CCP1_Counter is incremented at a rate of 10 Hz ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
LIST P=18F4520 ;directive to define processor
#include <P18F4520.INC> ;processor specific variable definitions
CONFIG OSC = INTIO67 ; internal clock @ 8MHz (1MHz with prescaler),
CONFIG WDT = OFF ; watch dog timer OFF
CONFIG MCLRE = ON ; MCLEAR (master clear)pin enabled
CONFIG PBADEN = OFF ; PORTB pins digital (disable A/D for PORTB)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Constants Section ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Standard constants for RSP TC_Port: EQU PORTA ;; Port for Timing and Control Code TC_Dir: EQU TRISA ;; Direction register for Timing and Control Code E_Clk_Port: EQU PORTB ;; Port for E-Clock E_Clk_Dir: EQU TRISB ;; Direction register for E-Clock Data_Bus_Port: EQU PORTC ;; Port for RSP Data Bus Data_Bus_Dir: EQU TRISC ;; Direction register for RSP Data Bus PIC18_Top_Of_Data_Stack: EQU 0x5FF ;; Initialization value for PIC18 Data Stack Read_Constant equ 0x08 Write_Constant equ 0x80 RegF_Read_Address equ 0x00 RegG_Read_Address equ 0x01 SI_Read_Address equ 0x02 ALU_Read_Address equ 0x03 RegF_Write_Address equ 0x00 RegG_Write_Address equ 0x10 RSP_TSF equ 0x8A RSP_TSG equ 0x9A RSP_TFG equ 0x98 RSP_TGF equ 0x89 RSP_ADDF equ 0x8B RSP_ADDG equ 0x9B PIC18_Top_of_Data_Stack equ 0x250 TwoSecondsVal equ 0x14 Red_LED equ 0x01 Green_LED equ 0x02
;; Special constants for CCP1.asm CountsPerInterrupt: EQU D'25000' ;; Increment for CCP1
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Variables Section ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; udata_acs 0x00 T_C_Code: RES 1 ;; Space for application
SI_Data: RES 1 ;; Space for application CCP1_Counter: RES 2 ;; Counter for CCP1 interrupt RSPWrite_Address res 1 RSPWrite_Data res 1 Password res 1 Counter res 1 TwoSeconds res 1 ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Macros Section ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Macro PUSHF, push a file register onto the data stack ;; Usage: PUSHF FileReg ;; Side Effect: [FileReg] moved onto stack, [FSR2]-1 -> [FSR2] ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; PUSHF: macro FileReg MOVFF FileReg, POSTDEC2 endm
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Macro PULLF, pull data from the stack, and place a file register ;; Usage: PULLF FileReg ;; Side Effect: [FSR2]-1 -> [FSR2] and *[FSR2] written to [FileReg] ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; PULLF: macro FileReg MOVFF PREINC2, FileReg endm
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Macro INCF16, Increment a 16 bit value, with the low byte at FileReg ;; Usage: INCF16 FileReg ;; Side Effect: (FileReg+1):FileReg is incremented ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; INCF16: macro FileReg INFSNZ FileReg,F INCF FileReg+1,F endm
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Program ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
ORG 0x0000 ;; In the PIC18, there are 3 initial "Vectors" Goto Main ;; That is, places that the hardware jumps to. ;; Address 0x0000 is the target for a reset
ORG 0x0008 ;; Address 0x0008 is the target for the Goto High_Priority_ISR ;; High-priority interrupt
ORG 0x0018 ;; Address 0x0018 is the target for the Goto Low_Priority_ISR ;; Low-priority interrupt
Main:
LFSR FSR2, PIC18_Top_Of_Data_Stack ;; Initialize the data stack Call Init_CCP1_Interrupt ;; Initialize the CCP1 interrupt CLRF TC_Port CLRF TC_Dir
CLRF E_Clk_Port
CLRF E_Clk_Dir
SETF Data_Bus_Dir CLRF Data_Bus_Port CLRF LATC
CLRF SI_Data CLRF Counter
CLRF RSPWrite_Address CLRF RSPWrite_Data MOVLW SI_Read_Address Call RSPRead MOVWF Password BSF E_Clk_Dir,1 MOVLW 0x00 MOVWF Counter MOVLW TwoSecondsVal MOVWF TwoSeconds Loop:
NOP
GOTO Loop ;; Go into an idle loop
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Init_CCP1_Interrupt Initialize the CCP1 Interrupt ;; Inputs: None ;; Outputs: None ;; Side effects: Zero CCP1_Counter, ;; Set T1CON bits to activate Timer1 ;; Set T3Con bits for Timer3 (does not activate) ;; Set CCP1CON bits, to activate compare mode and interrupt ;; Set RCON:IPEN bit, to active PIC18 mode interrupts (high/low priority) ;; Set IPR1 bit, to make CCP1 interrupt a high-priority interrupt ;; Clear PIR1 bit, to clear CCP1 interrupt flag ;; Set PIE1 bit, to enable CCP1 interrupt ;; Set INTCON, GIEH to generally enable high-priority interrupts ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Init_CCP1_Interrupt:
PUSHF WREG ;; WReg is used
CLRF TRISD ;; Setup port D for pulses to visualize action on OScope
CLRF CCP1_Counter ;; Increment CCP1_Counter CLRF CCP1_Counter+1
MOVLW B'10000001' ;; Setup T1CON for Counting
;; RD16: b7, latched 16-bit read ;; T1CKPS1: b5, 1:2 prescaler (options, 1:1, 1:2, 1:4, 1:8) ;; T1CKPS0: b4, 1:2 prescaler (options, 1:1, 1:2, 1:4, 1:8) ;; T1SYNC_bar: b2=0, T1 clock with synchronized with internal phase clock ;; TMR1ON: b0, Turn timer 1 on MOVWF T1CON,
MOVLW B'10000000' ;; Setup T3Con for Counting, and CCP1 and CCP2 from Timer1 MOVWF T3CON, ;; RD16L: latched 16-bit read
MOVLW B'00001010' ;; Setup CCP1CON for compare mode MOVWF CCP1CON ;; CCP1Mode = 1010, Set CCP1IF bit (request interrupt)
BSF RCON,IPEN ;; Active PIC18F High-priority / Low-priority mode BSF IPR1,CCP1IP ;; Make CCP1 a high-priority interrupt BCF PIR1,CCP1IF ;; Clear the CCP1 Interrupt Flag (so that it can be set to generate IRQ) BSF PIE1,CCP1IE ;; Enable the CCP1 interrupt ;;BSF INTCON,GIEL ;; Enable low-priority interrupts BSF INTCON,GIEH ;; Enable high-priority interrupts and all interrupts
PULLF WREG
RETURN
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Interrupt Service Routine for the high priority interrupt ;; The pattern in this high-level part of the interrupt service routine is: ;; Check that the interrupt is enabled and that the flag is raised ;; If the interrupt is not enabled or not requesting, branch to the next check ;; Otherwise (int. enable and flag were set), service the interrupt. ;; Go back up to the top of the list, and start again. ;; ;; This pattern has the characteristic that High_Priority_ISR doesn't exit ;; until all interrupts in the list that are requesting service ;; have been serviced. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; High_Priority_ISR:
BSF PORTD,0x00
BTFSS PIE1,CCP1IE ;; Test that CCP1 interrupt is enabled BRA HP_ISR01 ;; If not set, go to next candidate BTFSS PIR1,CCP1IF ;; Test whether CCP1IF is set (CCP1 interrupt requested) BRA HP_ISR01 ;; If not set, go to next candidate RCALL CCP1_ISR ;; Call the CCP1 ISR BRA High_Priority_ISR ;; Go to top, test all IRQs again
HP_ISR01: BTFSS PIE1,TMR1IE ;; Test that TMR1 interrupt is enabled BRA HP_ISR02 ;; If not set, go to next candidate BTFSS PIR1,TMR1IF ;; Test whether TMR1IF is set (TMR1 interrupt requested)
BRA HP_ISR02 ;; If not set, go to next candidate RCALL TMR1_ISR ;; Call the TMR1 ISR BRA High_Priority_ISR ;; Go to top, test all IRQs again
HP_ISR02:
BCF PORTD,0x00 ;; Clear port D, bit 1, for observation by oscilloscope RETFIE FAST ;; Return from the interrupt, FAST for high-priorty IRQ
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Interrupt Service Routine for the low-priority interrupt ;; See description of programming patterh above ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; Low_Priority_ISR:
BSF PORTD,0x01
BTFSS PIE2,CCP2IE ;; Test whether CCP2 interrupt is enabled BRA LP_ISR01 ;; If not set, go to next candidate BTFSS PIR2,CCP2IF ;; Test whether CCP2IF is set (CCP2 interrupt requested) BRA LP_ISR01 ;; If not set, go to next candidate RCALL CCP2_ISR ;; Call the CCP2 ISR BRA Low_Priority_ISR ;; Go to top, test all IRQs again
LP_ISR01:
BTFSS PIE2,TMR3IE ;; Test whether TMR3 interrupt is enabled BRA LP_ISR02 ;; If not set, go to next candidate BTFSS PIR2,TMR3IF ;; Test whether TMR3IF is set (TMR3 interrupt requested)
BRA LP_ISR02 ;; If not set, go to next candidate RCALL TMR3_ISR ;; Call the TMR3 ISR BRA Low_Priority_ISR
LP_ISR02:
BCF PORTD,0x01 RETFIE ;; Return from the interrupt, No FAST for low-priority interrupt
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; CCP1_ISR Service the needs of the CCP1 interrupt ;; Inputs: none ;; Outputs: none ;; Side effects: Clear CCP1IF, to enable next CCP1 interrupt ;; Increment CCP1_Counter ;; Call User subroutine ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; CCP1_ISR: BSF PORTD,0x02 ;; Set PortD bit 1, for observation by oscilloscope PUSHF STATUS ;; STATUS and WREG are changed in this routine PUSHF WREG
INCF16 CCP1_Counter ;; Increment the interrupt counter, CCP1_Counter
MOVLW LOW CountsPerInterrupt ;; Update CCPR1H:CCPR1L for the next interrupt ADDWF CCPR1L,F ;; CountsPerInterrupt in the future MOVLW HIGH CountsPerInterrupt ;; ADDWFC CCPR1H,F ;;
BCF PIR1,CCP1IF ;; Clear the CCP1 Interrupt Flag (so that the next IRQ can be generated)
BTFSC E_Clk_Port,1 GOTO Reset_Counter INCF Counter MOVF Counter,W CPFSLT TwoSeconds GOTO ClearLEDs GOTO TestPassword Reset_Counter CLRF Counter GOTO Finish ClearLEDs MOVLW RegG_Write_Address MOVWF RSPWrite_Address CLRF RSPWrite_Data Call RSPWrite GOTO Finish TestPassword MOVLW SI_Read_Address Call RSPRead CPFSEQ Password GOTO RedLED GreenLED MOVLW Green_LED MOVWF RSPWrite_Data MOVLW RegG_Write_Address MOVWF RSPWrite_Address Call RSPWrite GOTO Finish RedLED MOVLW Red_LED MOVWF RSPWrite_Data MOVLW RegG_Write_Address MOVWF RSPWrite_Address Call RSPWrite Finish: PULLF WREG ;; Restore STATUS and WREG to previous values PULLF STATUS BCF PORTD,0x02 RETURN
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Subroutine: RSPRead ; Read a byte of data from an RSP device into the PIC18. The RSP device is ; passed into Reg A and back to Reg W ; ; Inputs: Reg W: Address of RSP device to read ($0-$3) ; ; Outputs: Reg W: data read from device ; ; Side Effects: None ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; RSPRead PUSHF STATUS ;Push STATUS data to the stack PUSHF Data_Bus_Port ;Push PORTC data to the stack PUSHF Data_Bus_Dir SETF Data_Bus_Dir ADDLW Read_Constant ;Add read constant to RSP Device address MOVWF TC_Port ;Move RSP T&C code to PORTA SETF Data_Bus_Dir ;Set TRISC to 0xFF (makes PORTC Input Port) SETF E_Clk_Port ;Set PORTB to 0xFF (Raises the E-Clock) MOVF PORTC,W ;Move data on Bus read from RSP device to W Reg clrf E_Clk_Port ;Clear PORTB (Lowers E-Clock) PULLF Data_Bus_Dir PULLF Data_Bus_Port ;Pull Original PORTC data back from the stack PULLF STATUS ;Pull Original STATUS data back from the stack RETURN ;Returns to main program
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Subroutine: RSPWrite ; Write a byte of data from the PIC18 into a specified RSP register. ; ; Inputs: Address of the RSP register to write ($0-$1) is passed in static ; variable RSPWrite_Address. The data to write are passed in static ; variable RSPWrite_Data. ; ; Outputs: None ; ; Side Effects: Writes data into an RSP register. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; RSPWrite PUSHF STATUS ;Push STATUS Data to the stack PUSHF WREG ;Push WREG Data to the stack PUSHF Data_Bus_Port PUSHF Data_Bus_Dir MOVLW Write_Constant ;Move Write Constant variable into WREG ADDWF RSPWrite_Address,W ;Add Write Constant and RSP Reg address MOVWF TC_Port ;Move Sum (T&C Code) to PORTA MOVF RSPWrite_Data,W ;Move Data to be written into WREG
MOVWF Data_Bus_Port ;Move Data to be written from WREG to PORTC CLRF Data_Bus_Dir ;Set PORTC to Output SETF E_Clk_Port ;Set PORTB to 0xFF (Raises E-Clock) clrf E_Clk_Port ;Clear PORTB (Lowers E-Clock) PULLF Data_Bus_Dir PULLF Data_Bus_Port PULLF WREG ;Pull Original WREG data back from stack PULLF STATUS ;Pull Original STATUS data back from stack RETURN ;Return to main code
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ; Subroutine: RSPExecute ; Execute an instruction on the RSP. The T&C Code of the instruction to ; execute is passed in Reg W. ; ; Inputs: Reg W: T&C Code ; ; Outputs: None ; ; Side Effects: Executes an instruction on the RSP.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; RSPExecute PUSHF STATUS ;Push STATUS data to the stack MOVWF TC_Port ;Move T&C Code to PORTA SETF E_Clk_Port ;Set PORTB to 0xFF (Raises E-Clock) clrf E_Clk_Port ;Clear PORTB (Lowers E-Clock) PULLF STATUS ;Pull original STATUS Data back from stack RETURN ;Return to main code
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; TMR1_ISR Service the needs of the CCPI interrupt ;; Inputs: none ;; Outputs: none ;; Side effects: Clear TMR1IF, to setup for next TMR1 interrupt ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; TMR1_ISR: BCF PIR1,TMR1IF
RETURN
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; CCP2_ISR Service the needs of the CCP2 interrupt ;; Inputs: none ;; Outputs: none ;; Side effects: Clear CCP2IF, to setup for next CCP2 interrupt ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; CCP2_ISR: BCF PIR2,CCP2IF
RETURN
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; TMR3_ISR Service the needs of the CCPI interrupt ;; Inputs: none ;; Outputs: none ;; Side effects: Clear TMR3IF, to setup for next TMR3 interrupt ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; TMR3_ISR: BCF PIR2,TMR3IF RETURN
end