Getting started with PIC32

Discussion in 'Embedded Systems and Microcontrollers' started by takao21203, Jan 15, 2014.

  1. takao21203

    Thread Starter AAC Fanatic!

    Apr 28, 2012
    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?
  2. ErnieM

    AAC Fanatic!

    Apr 24, 2011
    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 likes this.
  3. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    takao21203 likes this.
  4. takao21203

    Thread Starter AAC Fanatic!

    Apr 28, 2012
    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.
  5. nsaspook

    AAC Fanatic!

    Aug 27, 2009
    Timers are pretty simple to configure and use but first you should understand the basic interrupt structure of the PIC32.

    Some examples from my code.

    Code ( (Unknown Language)):
    1.  void RTC_Init() {
    2. /* code */
    3.     // High priority interrupt for RTC timer. Called 1000/sec.
    4.     ConfigIntTimer5(T5_INT_ON | T5_INT_PRIOR_7);
    5.     OpenTimer5(T5_ON | T5_SOURCE_INT | T5_PS_1_8, (50000000 / 2 / 64 / 1000)); // for 1khz
    6. }
    9. [B][COLOR=#445588]void[/COLOR][/B] [B][COLOR=#990000]Blink_Init[/COLOR][/B]([B][COLOR=#445588]void[/COLOR][/B]) {
    10. /* code */
    11.     [I][COLOR=#999988]// High priority interrupt for Blink timer. Called 1/2sec.[/COLOR][/I]
    12.     ConfigIntTimer4(T4_INT_ON [B]|[/B] T4_INT_PRIOR_7 [B]|[/B] T4_INT_SUB_PRIOR_3);
    13.     OpenTimer4(T4_ON [B]|[/B] T4_SOURCE_INT [B]|[/B] T4_PS_1_8, ([COLOR=#009999]50000000[/COLOR] [B]/[/B] [COLOR=#009999]2[/COLOR] [B]/[/B] [COLOR=#009999]64[/COLOR] [B]/[/B] [COLOR=#009999]2[/COLOR])); [I][COLOR=#999988]// for 2hz[/COLOR][/I]
    14. }
    17. void __ISR(_TIMER_5_VECTOR, IPL7AUTO) TimerRTCHandler(void) {
    18. /* code */
    19.     V.timerint_count++;
    20.     INTClearFlag(INT_T5);
    21. }
    23. [B][COLOR=#445588]void[/COLOR][/B] [B][COLOR=#990000]__ISR[/COLOR][/B](_TIMER_4_VECTOR, IPL7AUTO) TimerBlinkHandler([B][COLOR=#445588]void[/COLOR][/B]) {
    24.     [I][COLOR=#999988]// check the LED blink flags[/COLOR][/I]
    25. [I][COLOR=#999988]/* code */[/COLOR][/I]
    26.     V.blink_count[B]++[/B];
    27.     INTClearFlag(INT_T4);
    28. }
    Change Notice interrupts:

    Leture notes from PIC32 based uC classes.
    Last edited: Jan 16, 2014