Home
Login / Signup
 di 

Debugging PIC32 - How to Start

  • user warning: Table './devemc/sessions' is marked as crashed and last (automatic?) repair failed query: SELECT COUNT(sid) AS count FROM sessions WHERE timestamp >= 1419223576 AND uid = 0 in /home/devemc/public_html/dev/includes/session.inc on line 157.
  • user warning: Table './devemc/sessions' is marked as crashed and last (automatic?) repair failed query: SELECT COUNT(DISTINCT s.uid) FROM sessions s WHERE s.timestamp >= 1419223576 AND s.uid > 0 in /home/devemc/public_html/dev/modules/user/user.module on line 790.
Debugging PIC32 - How to Start

A good friend of mine gave a PIC32MX starter kit to me saying: power it up and play. Easy to say, not easy to do.
The PIC32MX Starter Kit contains the following items:
• PIC32 Starter Kit Installation CD-ROM
It contains Data Sheet, family reference manual, peripheral library manual, code examples, schematics and PCB drawing files.
• PIC32MX Starter Kit Board + USB MINI-B cable
The board includes mainly:
PIC32MX360F512L 32-bit micro-controller
PIC18LF4550 USB micro-controller for on-board debugging
Green power-indicator LED
Three push-button switches for user-defined inputs
Three user-defined indicator LEDs
Below there is the PIC32 starter kit demo board layout.

PIC32 starter kit demo board layout

In the CD there is an automatic installation application that guided me to install all the tools and the necessary documents for my new adventure. I followed, step by step, all the indications " Building the project ", I used the example codes already preprogrammed to be sure that the board was working properly, at the end I was ready to build my own project. What could I do with three led and three switches ?
I could simulate with switch number 1 a sensor (e.g. photoelectric cell) that detects a person entering in a room, this event sets on a LED, then I could reset this device pressing a second switch. To set on, to set off a LED I used the following instructions:

mPORTDSetBits(BIT_0); (BIT_0  is referred to LED1) 
mPORTDClearBits(BIT_0);

The main problem that I had to face ( at least for me ) was the correlation switch - led. In which way the PIC32 board could "understand" I was pressing a switch? The switches should have a value being pressed and another value being unpressed. How to catch these values? Searching between documentation and example codes I found that the two switches and the LED are defined in the following way:

/* PORTD */
#define _RD0 PORTDbits.RD0     // LED1
#define _RD6 PORTDbits.RD6, Change Notice ( CN15 )    //  SW1
#define _RD7 PORTDbits.RD7, Change Notice ( CN16 )    //  SW2 
So I put in my code the following definitions:
#define PINS1        (CN15_ENABLE)
#define PINS2        (CN16_ENABLE)
#define PULLUPS1     (CN15_PULLUP_ENABLE)
#define PULLUPS2     (CN16_PULLUP_ENABLE)

Initially, to enable the switches n° 1 I wrote:

mCNOpen(CONFIG,PINS1,PULLUPS1);
then to enable the switches n° 2 :
mCNOpen(CONFIG,PINS2,PULLUPS2);

I got, as consequence, that only the switch n° 2 was enabled. There was something wrong in my thinking. Searching again in the documentation I found:
br>
_pins
This argument contains one or more bit masks bitwise OR’d together. Select one or more from the following bit masks.
Note: An absent mask symbol assumes corresponding bit(s) are disabled, or default value, and will be set = 0.
CN Enable Pins
CN0_ENABLE
CN1_ENABLE
CN2_ENABLE
...
CN21_ENABLE
CN_DISABLE_ALL

_pullups
This argument contains one or more bit masks bitwise OR’d together. Select one or more from the following bit masks. Note: An absent mask symbol assumes corresponding bit(s) are disabled, or default value, and will be set = 0.

CN Enable Pullups
CN0_PULLUP_ENABLE
CN1_PULLUP_ENABLE
CN2_PULLUP_ENABLE
...
CN21_PULLUP_ENABLE
CN_PULLUP_DISABLE_ALL

Code Example: /* enable pullups on change notice pins 10,11 */
ConfigCNPullups(CN10_PULLUP_ENABLE | CN11_PULLUP_ENABLE);

So, logically, I had to do the OR between my instructions and not write them separately: As I was doing previously, I was enabling SW1 first, then, as I changed my mind, I was enabling SW2.
Therefore the correct instruction was:

mCNOpen(CONFIG,PINS1|PINS2,PULLUPS1|PULLUPS2);

The switches do not have any debounce circuitry and require the use of internal pullup resistors. When Idle, the switches are pulled high (+3.3V); when pressed, they are grounded. So, to solve the problem, I needed to see them transition values. I got a big help from Watch window , Breakpoint and Step Into, three debugger tools. In the watch window I selected the "temp" variable, in the code I put a Breakpoint ( see the picture below ). When I pressed SW1 the program execution stopped at the Breakpoint and I got the value - EFBE; when I unpressed it, by Step Into, I got the value - EFFE. So the "temp" variable transition was from "B" to "F", therefore the transition value that I was searching was 0x40 ( hexadecimal value). In the same way when I pressed SW2 I got the value - FF7C, when I unpressed it I got the value - FFFC. So the "temp" variable transition was from "7" to "F", the transition value I was searching was 0x80. In the pictures below You can follow all the sequence above mentioned.

SW1 transition value

SW1 transition value

 

SW1 transition value

SW2 transition value

SW2 transition value

N.B. Later, unfortunately, I discovered that the transition values were already defined. In the "pic32mx360f512l.h" file there were the following definitions:

#define _PORTD_RD6_POSITION                      0x00000006
#define _PORTD_RD6_MASK                             0x00000040
#define _PORTD_RD6_LENGTH                        0x00000001
#define _PORTD_RD7_POSITION                      0x00000007
#define _PORTD_RD7_MASK                            0x00000080
#define _PORTD_RD7_LENGTH                        0x00000001

Another problem that I had to face, unfortunately it took time to solve it, was to use the powerful debugging tools that only the PIC32 micro-controller has compared to the other Microchip micro-controllers families. These debugging tools allow MPLAB to interact with the micro-controller in interactive manner. The first debugging tool allows to see if we get the wanted result by the insertion of a letter or a character from the keyboard. In the following example we can toggle the three led (red, green, orange) by R,G,O input only, we input X to exit the loop. Below there is the code, the output window and the window that appears asking to enter the information to send to micro-controller.

// Display the menu
DBPRINTF("Basic Starter Kit Lab (" __DATE__ "," __TIME__ ")\n");
DBPRINTF("type E to echo the Input String. \n");
DBPRINTF("type R to toggle the RED LED. \n");
DBPRINTF("type O to toggle the ORANGE LED. \n");
DBPRINTF("type G to toggle the GREEN LED. \n");
DBPRINTF("type X to exit the Loop. \n");  
DBPRINTF("Enter a menu choice (e,r,o,g) \n");
MPLAB PIC32 debug
Debug TargetIN

The second debugging tool allows to send to the output window all messages we want. In this way we can follow, step by step, where we are (in which loop, in which routine, in which interrupt) and to see the variable values.
For example, if we insert in our code the following library function:
DBPUTS (" I am in the interrupt routine");
the TargetOUT will be : I am in the interrupt routine

Debug TargetOUT

I said that it took time to get these debugging tools working because I had to find that the macro definition "PIC32_STARTER_KIT" needed to be added to the C32 compiler options and the "db_utils.a" file had to be added to the Library Files. (see the pictures below)

Debug Library Files

Below there is the code of my easy "project", but everything is easy if You know what to do, otherwise....
Platform: PIC32MX Starter Kit
Description:
This example set on LED1 (PORTD.RD0) when SW1 (PORTD.RD6) is pressed, causing a Change Notice interrupt; set off LED1 when SW2 ( PORTD.RD7 ) is pressed, causing a Change Notice interrupt.
PIC32MX Starter Kit Led and Switches assignments:
LED1 is on PORTD.RD0
SW1 is on PORTD.RD6, Change Notice (CN15)
SW2 is on PORTD.RD7, Change Notice (CN16)
No resistor pullups are provided for SW1,SW2. PORTD internal pullups should be enabled.

#include "db_utils.h"
#include 

#include 
// Configuration Bit settings
// SYSCLK = 72 MHz (8MHz Crystal/ FPLLIDIV * FPLLMUL / FPLLODIV)
// PBCLK = 36 MHz
// Primary Osc w/PLL (XT+,HS+,EC+PLL)
// WDT OFF
#pragma config FPLLMUL=MUL_18,FPLLIDIV=DIV_2, FPLLODIV=DIV_1,FWDTEN=OFF
#pragma config POSCMOD=HS, FNOSC=PRIPLL,FPBDIV=DIV_2
#define CONFIG      (CN_ON | CN_IDLE_CON )
#define PINS1        (CN15_ENABLE)        //SW1 enabling
#define PINS2        (CN16_ENABLE)       // SW2 enabling
#define PULLUPS1     (CN15_PULLUP_ENABLE)
#define PULLUPS2     (CN16_PULLUP_ENABLE)
#define INTERRUPT   (CHANGE_INT_ON | CHANGE_INT_PRI_2)
#define FOSC         72E6
int main(void)
{
unsigned int temp;
DBINIT();     // Initialize the IO channel
// STEP 1. Configure cache, wait states and peripheral bus clock
SYSTEMConfigPerformance(FOSC);
// STEP 2. configure the port registers
PORTSetPinsDigitalOut(IOPORT_D,BIT_0);
PORTSetPinsDigitalIn(IOPORT_D,BIT_6);
PORTSetPinsDigitalIn(IOPORT_D,BIT_7);
// STEP 3. initialize the port pin states = outputs low
mPORTDClearBits(BIT_0);
// STEP 4. enable change notice, enable discrete pins and weak pullups
mCNOpen(CONFIG,PINS1|PINS2,PULLUPS1|PULLUPS2);
// STEP 5. read port(s) to clear mismatch on change notice pins
temp=mPORTDRead();  
// STEP 6. clear change notice interrupt flag
ConfigIntCN(INTERRUPT);
// STEP 7. enable multi-vector interrupts
INTEnableSystemMultiVectoredInt();
while(1)
{
DBPUTS(" I am waiting for an input ");
}
}
// STEP 8. configure the CN interrupt handler
void __ISR(_CHANGE_NOTICE_VECTOR, ipl2) ChangeNotice_Handler(void)
{
DBPUTS(" I am in the interrupt routine");
unsigned int temp;
temp=mPORTDRead();  
if((temp & 0x40) ==0 )// SW1 transition value
{
mPORTDSetBits(BIT_0); // Switch ON LED1
}
if((temp & 0x80)==0)  // SW2 transition value
{ 
mPORTDClearBits(BIT_0); //Switch OFF LED1
}
mCNClearIntFlag();      // Clear interrupt flag
}

Debugging PIC32 - How to Start

good explanation of how to debug PIC32

Who's online

There are currently users and guests online.

Recent comments