/*******************************************************************************
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.
*******************************************************************************/

// DOM-IGNORE-BEGIN
/*******************************************************************************
Copyright (c) 2013-2014 released Microchip Technology Inc. All rights reserved.

Microchip licenses to you the right to use, modify, copy and distribute
Software only when embedded on a Microchip microcontroller or digital signal
controller that is integrated into your product or third party product
(pursuant to the sublicense terms in the accompanying license agreement).

You should refer to the license agreement accompanying this Software for
additional information regarding your rights and obligations.

SOFTWARE AND DOCUMENTATION ARE PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND,
EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION, ANY WARRANTY OF
MERCHANTABILITY, TITLE, NON-INFRINGEMENT AND FITNESS FOR A PARTICULAR PURPOSE.
IN NO EVENT SHALL MICROCHIP OR ITS LICENSORS BE LIABLE OR OBLIGATED UNDER
CONTRACT, NEGLIGENCE, STRICT LIABILITY, CONTRIBUTION, BREACH OF WARRANTY, OR
OTHER LEGAL EQUITABLE THEORY ANY DIRECT OR INDIRECT DAMAGES OR EXPENSES
INCLUDING BUT NOT LIMITED TO ANY INCIDENTAL, SPECIAL, INDIRECT, PUNITIVE OR
CONSEQUENTIAL DAMAGES, LOST PROFITS OR LOST DATA, COST OF PROCUREMENT OF
SUBSTITUTE GOODS, TECHNOLOGY, SERVICES, OR ANY CLAIMS BY THIRD PARTIES
(INCLUDING BUT NOT LIMITED TO ANY DEFENSE THEREOF), OR OTHER SIMILAR COSTS.
*******************************************************************************/
// DOM-IGNORE-END


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

#include "app.h"


#include "peripheral/oc/plib_oc.h" //要追加 OC_ID_5のコンパイル


DRV_HANDLE myHandle;
bool Timer_Trigger;

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


void tmrISR(uintptr_t context, uint32_t alarmCount ) //タイマ1コールバック関数
{
Timer_Trigger = 1;
};

// *****************************************************************************
// *****************************************************************************
// 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.
*/




//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
PLIB_TMR_Period16BitSet(TMR_ID_2, myPR); //PWMの周期を設定 //Set period

PLIB_TMR_Start(TMR_ID_2);




DRV_OC0_Enable(); //必須
DRV_OC0_Start(); //必須

myHandle = DRV_TMR_Open ( DRV_TMR_INDEX_0, DRV_IO_INTENT_EXCLUSIVE );
//タイマ1ハンドル取得

uint32_t divider = 3907; //周期レジスタ初期値 //最初の割り込みまでの時間をセット
//5 nsec x2 x 3907 x 256 = 10.00192msec = 10 msec at Fblclk3( = 10MHz)

DRV_TMR_AlarmRegister ( myHandle, divider, true, 0, tmrISR );
//コールバック関数繰り返し呼び出しをセットアップ
// bool DRV_TMR_AlarmRegister(
// DRV_HANDLE handle, //ハンドル
// uint32_t divider, //周期レジスタ初期値
// bool isPeriodic, //周期性? true:繰り返し、false:非繰り返し
// uintptr_t context, //コールバック関数へ渡す(文字列などの)ポインタ
// DRV_TMR_CALLBACK callBack //コールバック関数名
//);


DRV_TMR_Start(myHandle); //タイマ1割り込み許可、タイマ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)
{

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)(PR2 * Duty); //OC5RS = (int)(PR2 * Duty);
PLIB_OC_PulseWidth16BitSet(OC_ID_5, 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
*/