/******************************************************************************* 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 "math.h" #include "definitions.h" // SYS function prototypes DRV_HANDLE spi_Handle; bool LED; short int sinValue[360]; //sinテーブル値 int delay_Clock = 200000000; //200MHz void delay_us(volatile unsigned int usec) //1μsec遅延 { volatile int count; count = (int)(delay_Clock/20000000)*usec; do //実測 at 200MH (Clock=200000000) { //delay_us(1000):1000.4μsec delay_us(100):100.6μsec delay_us(10):10.5μsec delay_us(1):1.5μsec asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP");asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); asm("NOP"); count--; }while(count != 0); } void delay_ms(volatile unsigned int msec) //1msec遅延 { volatile unsigned int i; //実測:at200MH (Clock=200000000)//delay_ms(1): 1.0006msec delay_ms(100):100.04msec for(i=0; i<msec; i++) delay_us(1000); } void SinTable() //Sin のテーブル作成 { short int i; float PAI = 3.1416; int value; for(i = 0; i < 360; i++) { value = (1 + sinf(2*PAI*i/360))/2*4095/2; //sinf():単精度 vs sin():倍精度//MCP4922 if( value < 0) value = 0; sinValue[i] = (short int)value; //Vout = 2.048×2×Dn/4095 Vout(max) = 4.096[V] ( > 3.3V(電源電圧)) 4.096×3.3/5 = 2.7V (< (3.3V時オペアンプ飽和電圧)) } } void TMR1_Callback_Func(uint32_t status, uintptr_t context) //コールバック関数 //2msec毎に呼び出される { if(LED == false) { LED = true; LED1_Set(); //RG15: LED点灯 //plib_gpio.hで定義 // GPIO_PinSet(GPIO_PIN_RG15); //Harmonyライブラリの関数で制御 // LATGbits.LATG15 = 1; //レジスタ直接制御 } else { LED = false; LED1_Clear(); //RG15: LED消灯 // GPIO_PinClear(GPIO_PIN_RG15); // LATGbits.LATG15 = 0; } //LED1_Toggle(); //RG15:LED 点灯/消灯 } // ***************************************************************************** // ***************************************************************************** // 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. */ SinTable(); //Sinテーブル作成 TMR1_Start(); //タイマ1 スタート //T1CONbits.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; TMR1_CallbackRegister(TMR1_Callback_Func, NULL); //コールバック関数設定 spi_Handle = DRV_SPI_Open(DRV_SPI_INDEX_0, DRV_IO_INTENT_WRITE); //SPIハンドル生成 if (appInitialized) { appData.state = APP_STATE_SERVICE_TASKS; } break; } case APP_STATE_SERVICE_TASKS: { 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 */