Depurando PIC16F886 com PICkit4

Sep 26 2020

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

Circuito:
Um LED com resistor de 270 ohms em RA0.
Resistor pullup resistor de 3,3k ohm em MCLRE.
Vdd no pino 20.
Vss no pino 19.

Este é o código de teste que desejo depurar e percorrer instrução por instrução:

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

Quando executo o projeto, o código funciona conforme o esperado.
Mas quando defino um ponto de interrupção e clico em "Depurar projeto principal", recebo o erro: Reception on endpoint 129 failed (err = -10121)
Ou ele permanece no estado "compilar, carregar" por minutos até que eu saia do processo se eu brincar com as configurações.
O PIC16886 possui um "debugger in-circuit (on board)" de acordo com a página 1 da ficha técnica e o diagrama de blocos.

Respostas

Dan1138 Sep 29 2020 at 23:59

Existem algumas coisas que podem ser a causa da instabilidade no seu PICkit4.

O código que você postou poderia ser muito melhor, então aqui está um exemplo de linguagem assembly PIC16F886 customizada para você:

    ;
    ; 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

Outra coisa que pode ajudar ou travar seu PICkit4 é usar a ferramenta MPLABX Integrated-Programming-Environment (IPE) para fazer uma "Hardware Tool Emergency Boot Firmware Recovery".

Este é um processo complicado que pode exigir algumas tentativas antes de ser executado corretamente. Você precisará desconectar e conectar o PICkit4 nas etapas adequadas do processo.

O pior caso para você é que a instalação do MPLABX v5.40 está de alguma forma corrompida. Isso exigirá que você o desinstale, certifique-se de que quaisquer resquícios de instalações anteriores do MPLABX tenham sido excluídos e instale tudo novamente. Depois de fazer apenas 10 ou 15 vezes, torna-se apenas monstruosamente irritante.