/* Required Arduino library function equivalents for PIC24F */

#include <xc.h>
#include "pic24_arduino.h"
#include <stdio.h>

unsigned long millis(void)
{
   unsigned long temp_lsw, temp_msw;
   temp_lsw = TMR2;       //Transfer the LSW into temp_lsw
   temp_msw = TMR3HLD;    //Transfer the MSW from the holding register to into
                          //temp_msw
   /* With 32MHz FRC clock (16MHz instruction cycle) and prescaler set to
      256, the 32-bit counter is running at 62.5KHz. So the count is
      divided by 64 for a rough millisecond value. Overflows after 19
      hours, instead of 49 days on Arduino.
    */
   return ((temp_msw << 9) | (temp_lsw >> 7));
}

unsigned long micros(void)
{
   unsigned long temp_lsw, temp_msw;
   temp_lsw = TMR2;       //Transfer the LSW into temp_lsw
   temp_msw = TMR3HLD;    //Transfer the MSW from the holding register to into
                          //temp_msw
   /* With 32MHz FRC clock (16MHz instruction cycle) and prescaler set to
      256, the 32-bit counter is running at 62.5KHz. So the count is
      multiplied by 16 for a microsecond count at 16us resolution.
    */
   return ((temp_msw << 20) | (temp_lsw << 4));
}
