/*******************************************************************************
  MPLAB Harmony Application Source File

  Company:
    Microchip Technology Inc.

  File Name:
    app.c

  Summary:
    This file contains the source code for the MPLAB Harmony application.

  Description:
    This file contains the source code for the MPLAB Harmony application.  It
    implements the logic of the application's state machine and it may call
    API routines of other MPLAB Harmony modules in the system, such as drivers,
    system services, and middleware.  However, it does not call any of the
    system interfaces (such as the "Initialize" and "Tasks" functions) of any of
    the modules in the system or make any assumptions about when those functions
    are called.  That is the responsibility of the configuration-specific system
    files.
 *******************************************************************************/

// *****************************************************************************
// *****************************************************************************
// Section: Included Files
// *****************************************************************************
// *****************************************************************************

#include "app.h"

#include "definitions.h"                //レジスタ直接制御で必須


bool Timer_Trigger;
//bool LED;
float Duty = 0; //PWMのディューティ
int myPR; //PWM周期のレジスタ値( = PR2 at Timer2)
int myDuty; //PWM出力ON時間に相当するレジスタ値
int PS_T2 = 16; //タイマ2のプリスケール値
int Fpwm = 1000;    //PWMの周波数[Hz] 
int ix;





void TMR1_Callback_Fn(uint32_t status, uintptr_t context) //タイマ1のコールバック関数   //10msec毎に呼び出される
{ 
    Timer_Trigger = 1;
    
//     if(LED == false)
//        {
//            LED = true;  
//                LATGbits.LATG15 = 1;  
//        }
//        else
//        {
//            LED = false; 
//                LATGbits.LATG15 = 0;
//        }
}



// *****************************************************************************
// *****************************************************************************
// Section: Global Data Definitions
// *****************************************************************************
// *****************************************************************************

// *****************************************************************************
/* Application Data

  Summary:
    Holds application data

  Description:
    This structure holds the application's data.

  Remarks:
    This structure should be initialized by the APP_Initialize function.

    Application strings and buffers are be defined outside this structure.
*/

APP_DATA appData;

// *****************************************************************************
// *****************************************************************************
// Section: Application Callback Functions
// *****************************************************************************
// *****************************************************************************

/* TODO:  Add any necessary callback functions.
*/

// *****************************************************************************
// *****************************************************************************
// Section: Application Local Functions
// *****************************************************************************
// *****************************************************************************


/* TODO:  Add any necessary local functions.
*/


// *****************************************************************************
// *****************************************************************************
// Section: Application Initialization and State Machine Functions
// *****************************************************************************
// *****************************************************************************

/*******************************************************************************
  Function:
    void APP_Initialize ( void )

  Remarks:
    See prototype in app.h.
 */

void APP_Initialize ( void )
{
    /* Place the App state machine in its initial state. */
    appData.state = APP_STATE_INIT;



    /* TODO: Initialize your application's state machine and other
     * parameters.
     */
    
    
    //T2CONbits.TCKPS = 0b100;    //タイマ2プリスケーラ= 1/16   //MHCで設定済み
  
    //PWM周期  Fpwm = 1000Hz = 1 KHz    //分解脳 1/100000000/16  / 1/1000 = 16/100000 = 0.00016 = 0.016%
    //PR(PRレジスタ値) = Fpbclk3(ペリフェラルバスクロック周波数)/Fpwm(PWM周波数)/タイマプリスケール値  - 1;
    myPR = (int)(100000000/Fpwm/PS_T2 -1);  //=100000000/1000/16 -1 // Fpbclk3 = 100 MHz 
    TMR2_PeriodSet(myPR);   //周期レジスタ設定
    //PR2 = myPR; 

    TMR2_Start();   //タイマ2スタート
    //T2CONbits.ON = 1;
    OCMP5_Enable ();    //OC5イネーブル
    //OC5CONbits.ON = 1;
}


/******************************************************************************
  Function:
    void APP_Tasks ( void )

  Remarks:
    See prototype in app.h.
 */

void APP_Tasks ( void )
{

    /* Check the application's current state. */
    switch ( appData.state )
    {
        /* Application's initial state. */
        case APP_STATE_INIT:
        {
            bool appInitialized = true;


            if (appInitialized)
            { 
                  TMR1_PeriodSet(3907);  //5nsec x2  x 3907 x 256 = 100001920nsec = 10.000192msec = 10msec
                //PR1 = 3907;    //レジスタ直接
                TMR1_CallbackRegister(TMR1_Callback_Fn, NULL);  //コールバック関数設定
                                                    //void TMR1_CallbackRegister(TMR_CALLBACK callback_fn, uintptr_t context);
                TMR1_Start();   //タイマ1スタート
                //T1CONbits.ON = 1;

                appData.state = APP_STATE_SERVICE_TASKS;
            }
            break;
        }

        case APP_STATE_SERVICE_TASKS:
        {
            
              if(Timer_Trigger == 1)
            {
                Timer_Trigger = 0;
                
                ix++;
                if(ix == 100)ix = 0;
           
                Duty = (float)ix/100;   //float: 
                myDuty = (int)((float)PR2 * Duty);
                OCMP5_CompareSecondaryValueSet(myDuty); //OC5のデューティ設定
                // OC5RS = myDuty;
            }

            break;
        }

        /* TODO: implement your application state machine.*/


        /* The default state should never be executed. */
        default:
        {
            /* TODO: Handle error in application's state machine. */
            break;
        }
    }
}


/*******************************************************************************
 End of File
 */