# Getting started with PIC32

#### takao21203

Joined Apr 28, 2012
3,696
I wanted to do it for a long time, but never found enough time to do it until recently.

I have connected two PIC32 chips so far with ICSP and I can read them out with a PICKIT3.

Also I created a project with MPLABX2.00, it includes several C source files.

I studied some of the included headers, for instance there are routines to enable certain interrupts, and to clear the bit in the ISR.

I want to make the LED blink.

The tristate must be configured, and the configuration bits are all set in the code to values that seem to make sense.

Are I am missing something? Do I need to partition the memory, or is there some default? I downloaded all the files from the reference manual.

It is a MX210 16 I think, 44 pins, and no crystal, but a secondary 32 KHz crystal.

I tried to set some stuff in order to use the TIMER2.

And I wrote a code to toggle a flag in the main routine, whenever a flag was set in the ISR (leaving immediately).

Then I also need to set/reset a port bit for the LED (I did not code that this evening).

It seems to be I am getting there.

Any ideas, or information I should be aware of?

I know there are Evalution boards but I somehow like to use blank chips and see how it goes.

Yes I googled it, but MPLABX2.00 is brand new...

Should I try the simulator first before flashing?

#### ErnieM

Joined Apr 24, 2011
8,019
It's been a couple of years since I fired up a '32 so I don't have anything to offer off the top of my head except my motto:

"If it doesn't sim, then you can't win."

Yes, try the simulator. If it doesn't work on that no way it works in the real world.

Of course, the real world may not be ready for code (bad oscillator, MCLR not pulled, no power...), but you've ruled out one cause for failure by sim-ing.

Lucio Di Jasio's "Programming 32-bit Microcontrollers in C" is an excellent resource for starting out.

#### takao21203

Joined Apr 28, 2012
3,696
I have some test code for that PIC32 family for a SDCARD data logging type application. It's for the MX250 with a external 10mhz clock but should have some basic PPS, I/O and timer setup code.

https://github.com/nsaspook/nidaq700/tree/master/mx_test.X
This is part of my investigation, how to open and configure the timer.

Does the priority level matter?

Do I have to configure the timer, before opening it, or can I just open the timer?

The lines I wrote just open the timer 2 with IPL7.

Guess I can figure out myself, but with the post I hope to save some time.

I have all the PDFs on my small netbook now + MPLABX2.00

In theory, I could try right now in the night with the simulator.

#### nsaspook

Joined Aug 27, 2009
6,935
Timers are pretty simple to configure and use but first you should understand the basic interrupt structure of the PIC32.
https://github.com/nsaspook/nidaq700/blob/master/mx_test.X/C32Interrupts.pdf

Some examples from my code.

Rich (BB code):
 void RTC_Init() {
/* code */
// High priority interrupt for RTC timer. Called 1000/sec.
ConfigIntTimer5(T5_INT_ON | T5_INT_PRIOR_7);
OpenTimer5(T5_ON | T5_SOURCE_INT | T5_PS_1_8, (50000000 / 2 / 64 / 1000)); // for 1khz
}

/* code */
// High priority interrupt for Blink timer. Called 1/2sec.
ConfigIntTimer4(T4_INT_ON | T4_INT_PRIOR_7 | T4_INT_SUB_PRIOR_3);
OpenTimer4(T4_ON | T4_SOURCE_INT | T4_PS_1_8, (50000000 / 2 / 64 / 2)); // for 2hz
}

void __ISR(_TIMER_5_VECTOR, IPL7AUTO) TimerRTCHandler(void) {
/* code */
V.timerint_count++;
INTClearFlag(INT_T5);
}

}