• 0
MichalMikolajczyk

BASYS MX3 BTNL and BTNU not accurate in debug mode

Question

Good  afternoon,

I have the following problem with BASYS MX3: When I try using BTNL or BTNU in debug mode, they are read as 1 when they are not pressed, but only sometimes, yet regularly.

This problem does not occur when I press "Make and program device Main Project" instead of "Debug main project". Other buttons are working just fine with both debug and program mode.

The code I am using:

/* assignment1_3.c */
#include <xc.h>
#include "assignment1_3.h"

int main(){
    
TRISA = 0x00;    
TRISBbits.TRISB1 = 1; // RB1 (BTNU) configured as input
ANSELBbits.ANSB1 = 0; // RB1 (BTNU) disabled analog
TRISBbits.TRISB0 = 1; // RB1 (BTNL) configured as input
ANSELBbits.ANSB0 = 0; // RB1 (BTNL) disabled analog
TRISFbits.TRISF4 = 1; // RF0 (BTNC) configured as input
TRISBbits.TRISB8 = 1; // RB8 (BTNR) configured as input
ANSELBbits.ANSB8 = 0; // RB8 (BTNR) disabled analog
TRISAbits.TRISA15 = 1; // RA15 (BTND) configured as input
LATA = 0x00; //Turn the leds off
    int i = 0;
    
    while(1){
        if (BTNL == 1){ //going mad: BTNU, BTNL, Going well: BTND, BTNR, BTNC
            i++;
            int a = 0;
            for (a; a < 100000; a++){};
            while (BTNL){};
            for (a; a < 100000; a++){};
            LATA = i%16;
        }
    }    
    return (0);
}
#ifndef ASSIGNMENT1_3_H
#define	ASSIGNMENT1_3_H
/* ------------------------------------------------------------ */
/*						Configuration Bits		                */
/* ------------------------------------------------------------ */

// Device Config Bits in  DEVCFG1:	
#pragma config FNOSC =	FRCPLL
#pragma config FSOSCEN =	OFF
#pragma config POSCMOD =	XT
#pragma config OSCIOFNC =	ON
#pragma config FPBDIV =     DIV_2

// Device Config Bits in  DEVCFG2:	
#pragma config FPLLIDIV =	DIV_2
#pragma config FPLLMUL =	MUL_20
#pragma config FPLLODIV =	DIV_1

#pragma config JTAGEN = OFF     
#pragma config FWDTEN = OFF
#pragma config DEBUG    = ON			// Debugger Enable/Disable

/* ------------------------------------------------------------ */
/*						Definitions      		                */
/* ------------------------------------------------------------ */

#define BTNC    PORTFbits.RF0
#define BTNU    PORTBbits.RB1
#define BTNL    PORTBbits.RB0
#define BTNR    PORTBbits.RB8
#define BTND    PORTAbits.RA15

#endif	/* ASSIGNMENT1_3_H */

The idea of the program is to increment the counter whenever the button is pressed, which works fine in for all the buttons but BTNU and BTNL. For those two the button is read as 1 without pressing, but it is also read as 0, because while(BTNL) actually exits.

The questions are: why do programming the device works just fine for all the buttons, but debugging only for some of them? Why does it happen to up and left button only? What can I do to fix it?

This bug was also noticed by my teacher and one of my friends, so it does not seem to be related to a single board.

I am using compiler XC32 v1.44 and MPLAB X IDE v4.00

Many thanks for your input,
Michał Mikołajczyk

Share this post


Link to post
Share on other sites

1 answer to this question

Recommended Posts

  • 1

Hello @MichalMikolajczyk,

The problem you and your peers are running into is that BTNU and BTNL share the same lines with the PGD and PGC signals which are used during programming and debugging. My understanding is that even though you have the statement "#pragma config JTAGEN = OFF" as recommended in the Basys MX3 Reference Manual (relevant section here), because you are forcing the debug mode, those signal lines are being used regardless, producing the behavior that you are seeing.

Unfortunately, I do not have a clean way to offer to fix this aside from using buttons C, R, or D during a debugging process rather than buttons L or U.

Please let me know if you have any more questions.

Thank you,
JColvin

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now