Electronic – Debugging PIC16F886 with PICkit4

debuggingmicrochippic

MCU: Pic16F886
Programmer: PICkit4
IDE: MPLAB X IDE v5.40
Assembler: pic-as v2.30 (xc8)

Circuit:
A LED with 270 ohm resistor on RA0.
3,3k ohm resistor pullup resistor on MCLRE.
Vdd on pin 20.
Vss on pin 19.

This is the test code I want to debug and step through instruction by instruction:

PROCESSOR 16F886
PAGEWIDTH   132
RADIX DEC
    
#include <xc.inc>
 
config DEBUG = ON, LVP = OFF, FCMEN = OFF, IESO = OFF, BOREN = OFF
config CPD = OFF, CP = OFF, MCLRE = OFF, PWRTE = OFF, WDTE = OFF
config FOSC = INTRC_NOCLKOUT, LVP = OFF, BOR4V = BOR40V, WRT = OFF
    
    
    PSECT   StartCode,class=CODE,delta=2
    global  Start
Start:
    movlw 11000000B  ;set option register
    movwf OPTION_REG
    
    movlw 00100000B  ;set the status register (select bank 1)
    movwf STATUS
    
    movlw 11111110B  ;everything to input except for RA0
    movwf TRISA
    
    movlw 00000000B  ;set the status register (select bank 0)
    movwf STATUS
    
    bcf PORTA, 0  ;clear bit zero in PORTA register
    
    sleep

END Start

When I run the project the code works as expected.
But when I set a breakpoint and hit "Debug Main Project" I get the error: Reception on endpoint 129 failed (err = -10121)
Or it stays in the "build, load" state for minutes until I quit the process if I play around with config settings.
The PIC16886 has an "in-circuit debugger (on board)" according to page 1 of the datasheet and the block diagram.

Best Answer

There are a few things that may be the cause of instability in your PICkit4.

The code you posted could be a lot better so here is a custom crafted PIC16F886 assembly language example for you:

    ;
    ; File:     main.S
    ; Target:   PIC16f886
    ; Author:   dan1138
    ; Date:     2020-09-29
    ; Compiler: pic-as(v2.20)
    ; IDE:      MPLABX v5.40
    ;  
    ;                            PIC16F886  
    ;                     +---------:_:---------+
    ;          VPP RE3 -> :  1 MCLRn     PGD 28 : <> RB7 PGD
    ;              RA0 <> :  2 AN0       PGC 27 : <> RB6 PGC
    ;              RA1 <> :  3 AN1      AN13 26 : <> RB5
    ;              RA2 <> :  4 AN2      AN11 25 : <> RB4
    ;              RA3 <> :  5 AN3   AN9/PGM 24 : <> RB3
    ;              RA4 <> :  6 T0CKI     AN8 23 : <> RB2
    ;              RA5 <> :  7 AN4      AN10 22 : <> RB1
    ;              GND -> :  8 VSS      AN12 21 : <> RB0
    ;              RA7 <> :  9 OSC1      VDD 20 : <- 5v0
    ;              RA6 <> : 10 OSC2      VSS 19 : <- GND
    ;              RC0 <> : 11 SOSCO      RX 18 : <> RC7  
    ;              RC1 <> : 12 SOSCI      TX 17 : <> RC6  
    ;              RC2 <> : 13 CCP1      SDO 16 : <> RC5  
    ;              RC3 <> : 14 SCL       SDI 15 : <> RC4  
    ;                     +---------------------+
    ;                             DIP-28
    ; Description:
    ;
    ;   Example project for the PIC16F886 controller using the pic-as(v2.20) tool chain.
    ;
    ; Add this line in the project properties box, pic-as Global Options -> Additional options: 
    ;   -Wa,-a -Wl,-pPor_Vec=0h,-pIsr_Vec=4h

        PROCESSOR   16F886
        PAGEWIDTH   132
        RADIX       DEC

    #include <xc.inc>

    ; PIC16F887 Configuration Bit Settings

    ; 'C' source line config statements

    ; CONFIG1
     config FOSC = INTRC_NOCLKOUT; Oscillator Selection bits (INTOSCIO oscillator: I/O function on RA6/OSC2/CLKOUT pin, I/O function on RA7/OSC1/CLKIN)
     config WDTE = OFF       ; Watchdog Timer Enable bit (WDT disabled and can be enabled by SWDTEN bit of the WDTCON register)
     config PWRTE = OFF      ; Power-up Timer Enable bit (PWRT disabled)
     config MCLRE = ON       ; RE3/MCLR pin function select bit (RE3/MCLR pin function is MCLR)
     config CP = OFF         ; Code Protection bit (Program memory code protection is disabled)
     config CPD = OFF        ; Data Code Protection bit (Data memory code protection is disabled)
     config BOREN = OFF      ; Brown Out Reset Selection bits (BOR disabled)
     config IESO = ON        ; Internal External Switchover bit (Internal/External Switchover mode is enabled)
     config FCMEN = OFF      ; Fail-Safe Clock Monitor Enabled bit (Fail-Safe Clock Monitor is disabled)
     config LVP = OFF        ; Low Voltage Programming Enable bit (RB3 pin has digital I/O, HV on MCLR must be used for programming)

    ; CONFIG2
     config BOR4V = BOR40V   ; Brown-out Reset Selection bit (Brown-out Reset set to 4.0V)
     config WRT = OFF        ; Flash Program Memory Self Write Enable bits (Write protection off)

    ;
    ; Power-On-Reset entry point
    ;
        PSECT   Por_Vec,global,class=CODE,delta=2
        global  resetVec
    resetVec:
        PAGESEL main                ;jump to the main routine
        goto    main

    ;
    ;   Data space use by interrupt handler to save context
        PSECT   Isr_Data,global,class=RAM,space=1,delta=1,noexec
    ;
        GLOBAL  WREG_save,STATUS_save
    ;
    WREG_save:      DS  1
    STATUS_save:    DS  1
    PCLATH_save:    DS  1
    ;
    ;   Interrupt vector and handler
        PSECT   Isr_Vec,global,class=CODE,delta=2
        GLOBAL  IsrVec
    ;
    IsrVec:
        movwf   WREG_save
        swapf   STATUS,W
        movwf   STATUS_save
        movf    PCLATH,W
        movwf   PCLATH_save
    ;
    IsrHandler:
    ;
    IsrExit:
        movf    PCLATH_save,W
        movwf   PCLATH
        swapf   STATUS_save,W
        movwf   STATUS
        swapf   WREG_save,F
        swapf   WREG_save,F
        retfie                      ; Return from interrupt
    ;
    ; Main application
    ;
        PSECT   MainCode,global,class=CODE,delta=2
    main:
        clrf    INTCON              ; disable all interrupts
        banksel PIE1
        clrf    BANKMASK(PIE1)
        clrf    BANKMASK(PIE2)
        banksel OSCCON              ; Select INTOSC at 8MHz
        movlw   0x70
        movwf   BANKMASK(OSCCON)
        banksel ANSEL               ; Make all GPIO us digital mode
        clrf    BANKMASK(ANSEL)
        clrf    BANKMASK(ANSELH)
        banksel PORTA               ; Make all output zero
        clrf    BANKMASK(PORTA)
        clrf    BANKMASK(PORTB)
        clrf    BANKMASK(PORTC)
        banksel TRISA               ; Select input and output GPIOs
        clrf    BANKMASK(TRISA)     ; Make RA0-RA7 outputs
        movlw   0x01
        movwf   BANKMASK(TRISB)     ; Make RB0 an input, RB1-RB7 outputs
        clrf    BANKMASK(TRISC)     ; Make all PORTC outputs
        banksel OPTION_REG
        movlw   0xDF
        movwf   BANKMASK(OPTION_REG) ; Set TIMER0 clock source to FOSC/4, prescale 1:1, WDT prescale 1:128
    ;
    ;
    ;
        banksel WDTCON              ; Enable WDT timeout
        bsf     BANKMASK(WDTCON),WDTCON_SWDTEN_POSITION
    loop:
        banksel PORTA
        movlw   0x01
        xorwf   PORTA,F
        sleep               ; typical sleep time 2.048 seconds
        nop
        pagesel loop
        goto    loop
    ;
    ; Tell linker the power on reset entry point
    ;
        END     resetVec

Another thing that may help or brick your PICkit4 is to use the MPLABX Integrated-Programming-Environment(IPE) tool to do a "Hardware Tool Emergency Boot Firmware Recovery".

enter image description here

This is a tricky process that may require a few tries before you do it correctly. You will need to unplug and plug in the PICkit4 at the proper steps in the process.

The worst case for you is that your MPLABX v5.40 installation is somehow corrupt. This will require you to uninstall it, be sure that any remnants of previous MPLABX installs have been deleted, the install it all again. After doing the only 10 or 15 times it becomes only monstrously annoying.