9 Replies Latest reply on Jul 23, 2019 4:15 PM by mike-0rc

    PIC32 delay help

    sanyika95

      Hello everyone!

       

      I just started to learn PIC32 programming with a PIC32 Ethernet Starter kit. I wanted to blink its led, 1 second on - 1 second off.

       

      Here is the code for the delay:

      #define USHORT unsigned short
      
      void delayMs(USHORT milisec) {
          T1CON = 0x8010;
          TMR1 = 0x00;
          USHORT i = 0;
          PR1 = 0x40;
          while(i != milisec) {
              while(TMR1) {
              }
              i++;
              TMR1 = 0x00;
          }
          T1CON = 0x00;
          return;
      }

       

      It just works (or it looks like to work, I haven't measured it with an oscilloscope, just with a "stopwatch") and I don't know why. Can someone explain me?

       

       

      The datasheet of the kit:

      http://ww1.microchip.com/downloads/en/DeviceDoc/61166A.pdf

       

      The datasheet of its PIC:

      http://ww1.microchip.com/downloads/en/devicedoc/60001156j.pdf

        • Re: PIC32 delay help
          fmilburn

          I am not familiar with the PIC32 but that is strange code.  You need to know what the clock speed is and how the dividers to the timer are set.  A quick look at the datasheet shows that T1CON is the timer control register for TIMER1.  It looks like the variable milisec holds the value 1000 and that the register TMR1 is incremented once per millisecond and then the while will loop 1000 times using the variable i as a counter.  Of course normally you would have the timer count to the specified number of milliseconds and trigger an interrupt without blocking.

          2 of 2 people found this helpful
            • Re: PIC32 delay help
              sanyika95

              I don't really know the clock speed. In the datasheets there were a 32kHz and a 8MHz clock as internal. I did my calculation with both of them and none of them worked.

               

              As you said, the T1CON is the control register of TIMER1. In the code, the T1CON is set to Enable timer, set its prescaler to 8 and use the internal clock.

               

              You wrote the TMR1 is incremented once per milisecond, but if I understood the timing well, the TMR1 register will increase at every hit of the clock! So it have to reach X bit, depending on the clock and the prescaler to wait 1 milisecond. The PR1 is the value when to reset the TMR1.

              So when the TMR1 reaches 64 (in decimal) is 1 milisecond.

               

              The question is to understand the math behind it to be able to reproduce it on other MCU-s, with different clock speed and prescaler.

              2 of 2 people found this helpful
                • Re: PIC32 delay help
                  fmilburn

                  I would not want to reproduce that code on another MCU regardless of clock speed and prescalar settings :-).  It is straight forward to track milliseconds on the microcontrollers I am familiar with - set the timer to count up and when it reaches 1 millisecond trigger an interrupt that increments the millisecond counter in an ISR.  Of course you must know how to set the clocks and timers to do this and the code you are working from does not seem to do this in a very clear manner.  Suggest finding a better example as suggested below....

                  1 of 1 people found this helpful
              • Re: PIC32 delay help
                rusgray

                Have you done the simple beginner Harmony tutorial project? It implements a blinking LED, and does a good job of covering the basics of working with Harmony. It also serves as a good starting point for writing firmware for any hardware involving a supported Microchip MCU, as the heartbeat will tell you if your state machine stops functioning properly.

                4 of 4 people found this helpful
                • Re: PIC32 delay help
                  fmilburn

                  I was going to suggest the same thing as rusgray, i.e. find a good tutorial.  The code you posted is confusing and not the sort of thing I would want to learn from.

                  • Re: PIC32 delay help

                    As I am getting you right, You said TMR1 is incrementing once per millisecond, but the TMR1 register will increase at every hit of the clock! So it has to reach X bit, depending on the clock and the prescale to wait 1 millisecond. The PR1 is the value when to reset the TMR1.

                    Here if you want to start your Embedded Training in Noida then join them.

                    • Re: PIC32 delay help
                      mike-0rc

                      I would suspect that the WDT is kicking in causing a reset, I have seen this before.

                      If you want a good timer then use the call back feature from the Harmony and use the DRV_Handle call back feature, this then allows you to pass a value to the timer function that runs in the background and interrupts after the elapsed time. As there are so many different approaches to this, If you have a Core timer available you can calculate the time in ms from this although you will not be able to exit the loop until it ends, this is the simplest way but does stop the rest of the code from running and if using communication or other important tasks you run the risk of a WDT reset or data being missed.

                       

                      I hope this helps.

                       

                      As a point Microchip changed the function __delay_   And you have to create this yourself. I will try finding some of my code and post it

                       

                      Mike Clapham.

                        • Re: PIC32 delay help
                          mike-0rc

                          I have scoured my home PC and I have no examples as I have not done one on this PC yet, Instead you can look at the following as there are too many steps to write down here (You have to edit a lot of headers and c files to implement the timer call back (I started here while learning how to implement them in to my code).

                          https://microchipdeveloper.com/harmony:example-tmr-sys-mz-ef-sk  As I am using a high end PIC32MZ2048EFH250 you may need to modify the code or have a look at the code (They should be cross device compatible) to get an idea on how this works.

                           

                          Regards,

                          Mike Clapham.