/******************************************************************************* Main Source File Company: Microchip Technology Inc. File Name: main.c Summary: This file contains the "main" function for a project. Description: This file contains the "main" function for a project. The "main" function calls the "SYS_Initialize" function to initialize the state machines of all modules in the system *******************************************************************************/ // ***************************************************************************** // ***************************************************************************** // Section: Included Files // ***************************************************************************** // ***************************************************************************** #include <stddef.h> // Defines NULL #include <stdbool.h> // Defines true #include <stdlib.h> // Defines EXIT_FAILURE #include "definitions.h" // SYS function prototypes #include "stdio.h" #include "1lcd_lib_XC32.h" bool Timer_Trigger; //AD変換タイミングフラグ int Count_10msec = 0; //10msec経過カウンタ int delay_Clock = 200000000; //200MHz char Buf[32]; bool LED; 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 TMR9_Callback_Fn(uint32_t status, uintptr_t context) //コールバック関数 //1秒毎に呼び出される { Count_10msec++; if(Count_10msec >= 25) //250msec 経過 { Count_10msec = 0; Timer_Trigger = true; } } void AdcFunc(void) { unsigned short int AdcValue4 = 0; unsigned short int AdcValue45 = 0; double V_AN4; double V_AN45; //AD変換開始 Trigger a conversion //ADCHS_ChannelConversionStart(ADCHS_CH0);//変換開始(グローバルソフトウェアエッジトリガの場合) // ADCHS_ChannelConversionStart();//変換開始(グローバルソフトウェアエッジトリガの場合) //コンパイルエラー: too few arguments to function 'ADCHS_ChannelConversionStart' ADCCON3bits.GSWTRG = 1; //変換開始(グローバルソフトウェアエッジトリガの場合) //Trigger conversion for ADC inputs that have selected the GSWTRG bit as the trigger signal, either //through the associated TRGSRC<4:0> bits in the ADCTRGx registers or through the STRGSRC<4:0> //bits in the ADCCON1 register //Class1 の場合、"ADCCON1bits.STRGSRC = 0; //スキャン開始トリガなし変換が終了する”(default)にすると //変換終了後、自動的にサンプリングが実行される。 //変換完了を待つ Wait the conversions to complete //AN4 // while(!ADCHS_ChannelResultIsReady(ADCHS_CH4)); while (ADCDSTAT1bits.ARDY4 == 0); //結果の取り出し fetch the result // AdcValue4 = ADCHS_ChannelResultGet(ADCHS_CH4); AdcValue4 = ADCDATA4; //AN45 //AN0 while(!ADCHS_ChannelResultIsReady(ADCHS_CH0)); // while (ADCDSTAT1bits.ARDY0 == 0); //結果の取り出し fetch the result AdcValue45 = ADCHS_ChannelResultGet(ADCHS_CH0) + 0x800; // AdcValue45 = ADCDATA0 + 0x800; //★ +0x800: 実験による補正 //コンパイラにバグ? //AdcValue45 = ADCDATA0; //本来ならこのコードで正常動作するはず。 Harmony v2.04ではこのコードでOKである。 lcd_cmd(0x80); //1目の先頭へ V_AN4 =(double)AdcValue4/4095*3.3; sprintf(Buf,"V_AN4=%.2f[V] ",V_AN4); //文字列としてバッファーに収納 lcd_str(Buf); // 開始メッセージ1行目表示 lcd_cmd(0xC0); //2行目の先頭へ V_AN45 =(double)AdcValue45/4095*3.3; sprintf(Buf,"V_AN45=%.2f[V] ",V_AN45); //文字列としてバッファーに収納 lcd_str(Buf); // 開始メッセージ2行目表示 } // ***************************************************************************** // ***************************************************************************** // Section: Main Entry Point // ***************************************************************************** // ***************************************************************************** int main ( void ) { /* Initialize all modules */ SYS_Initialize ( NULL ); ADCTRGMODEbits.SH0ALT = 1; //入力ポート変更: AN0 --> AN45 //★出力フォーマット: unsigned data format ADCIMCON1bits.SIGN4 = 0; //出力フォーマット: unsigned short int // unsigned data format lcd_init(); // LCD初期化 lcd_cmd(0b00001100); // カーソル:OFF ブリンク:OFF lcd_cmd(0x80); //1目の先頭へ sprintf(Buf,"From Now ");// lcd_str(Buf); //液晶表示 lcd_cmd(0xC0); //2行目の先頭へ sprintf(Buf," AD Start !! "); // lcd_str(Buf); // 開始メッセージ1行目表示 delay_ms(2000); TMR9_PeriodSet(3907); //50 nsec x2 x 3907 x 256 = 1000192μsec = 10.00192msec //クロック:100MHz TMR9_Start(); //タイマ1 スタート //T1CONbits.ON = 1; //レジスタ直接制御 TMR9_CallbackRegister(TMR9_Callback_Fn, NULL); //コールバック関数設定 //void TMR1_CallbackRegister(TMR_CALLBACK callback_fn, uintptr_t context); while ( true ) { /* Maintain state machines of all polled MPLAB Harmony modules. */ SYS_Tasks ( ); delay_us(10); //必須 if(Timer_Trigger == true) //AD変換、表示のタイミング { AdcFunc(); //AD変換、表示 Timer_Trigger = 0; if(LED == 0) { LED = 1; LATGbits.LATG15 = 1; //LED ON } else { LED = 0; LATGbits.LATG15 = 0; //LED OFF } } } /* Execution should not come here during normal operation */ return ( EXIT_FAILURE ); } /******************************************************************************* End of File */