time1_sosc.c - Demonstrates use of Timer1 with a secondary oscillator.ΒΆ

Only works a device with a secondary oscillator (many PIC24H/F, dsPIC33F devices, few PIC24E/dsPIC33E).

#include <stdio.h>
#include <pic24_all.h>

#ifndef _LPOSCEN
# warning "This example only works with a device that has a secondary oscillator."
int main(void) {
  return 0;
}
#else


volatile uint16_t u16_seconds = 0;

void _ISRFAST _T1Interrupt (void) {
  u16_seconds++;
  _T1IF = 0;  //clear interrupt flag
}

void  configTimer1(void) {
  T1CON = T1_OFF | T1_IDLE_CON | T1_GATE_OFF
          | T1_SYNC_EXT_OFF
          | T1_SOURCE_EXT //ext clock
          | T1_PS_1_1 ;  // prescaler of 1
  PR1 = 0x7FFF;          //period is 1 second
  _T1IF = 0;             //clear interrupt flag
  _T1IP = 1;             //choose a priority
  _T1IE = 1;             //enable the interrupt
  T1CONbits.TON = 1;     //turn on the timer
}


int main(void) {
  __builtin_write_OSCCONL(OSCCON | 0x02);    //OSCCON.LPOSCEN=1;
  configBasic(HELLO_MSG);   //say Hello!
  configTimer1();
  while (1) {
    outString("Seconds: ");
    outUint16Decimal(u16_seconds);
    outString("\n");
    while(!IS_TRANSMIT_COMPLETE_UART1());
    SLEEP();
  }
}
#endif