/*
© [2024] Microchip Technology Inc. and its subsidiaries.

    Subject to your compliance with these terms, you may use Microchip 
    software and any derivatives exclusively with Microchip products. 
    You are responsible for complying with 3rd party license terms  
    applicable to your use of 3rd party software (including open source  
    software) that may accompany Microchip software. SOFTWARE IS ?AS IS.? 
    NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS 
    SOFTWARE, INCLUDING ANY IMPLIED WARRANTIES OF NON-INFRINGEMENT,  
    MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT 
    WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, 
    INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY 
    KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 
    MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 
    FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP?S 
    TOTAL LIABILITY ON ALL CLAIMS RELATED TO THE SOFTWARE WILL NOT 
    EXCEED AMOUNT OF FEES, IF ANY, YOU PAID DIRECTLY TO MICROCHIP FOR 
    THIS SOFTWARE.
*/

// Section: Included Files
#include "tmr1.h"

// Section: TMR1 Module APIs

void TMR1_Initialize (void)
{
    //TCS FOSC/2; TSYNC disabled; TCKPS 1:8; TGATE disabled; TECS ; PRWIP Write complete; TMWIP Write complete; TMWDIS disabled; TSIDL disabled; TON disabled; 
    T1CON = 0x0004;
    //TMR 0x0; 
    TMR1 = 0x0;
    //Period 1 ms; Frequency 100,000,000 Hz; PR 12499; 
    PR1 = 0x4F;

//    TMR1_Start();
}

void TMR1_Deinitialize (void)
{
    TMR1_Stop();
    
    T1CON = 0x0;
    TMR1 = 0x0;
    PR1 = 0xFFFF;
}

void TMR1_Start( void )
{
    //Clear interrupt flag
    IFS0bits.T1IF = 0;
    //Enable the interrupt
    IEC0bits.T1IE = 1;
    
    // Start the Timer 
    T1CONbits.TON = 1;
}

void TMR1_Stop( void )
{
    // Stop the Timer 
    T1CONbits.TON = 0;
    
    //Disable the interrupt
    IEC0bits.T1IE = 0;
}

void __attribute__ ((interrupt, no_auto_psv)) _T1Interrupt(void)
{
    (*TMR1_TimeoutHandler)();
    IFS0bits.T1IF = 0;
}


