/****************************************************************** * easy_fra_test_3 プログラム * * <ハード概要> * 周波数スイープとして、自作の「PIC18F_DDS_AF_OSC_V2」を使用する。 * 今回は、ログアンプを使用。縦軸は、dB軸とする。 * そして、全体のハードは、PIC24H_オシロ(初号機)を一部改造 * して、ログアンプ出力を、AN0に接続して実験を進める。 * * Graphic LCD(Monochrome): SG12864A * * Condition: * 8MHz External X'tal Oscillator, 10x PLL (8MHzx10= 80MHz) * Fcy=80MHz/2=40MHz, Tcy=25ns * * MPU: PIC24HJ12GP202 * * N.Ishii 2015.9.16 ********************************************************************/ #include #include "glcd_lib.h" /// Set Configuration Word: 8MHz External X'tal OSC _FBS(BSS_NO_FLASH & BWRP_WRPROTECT_OFF ) _FGS(GSS_OFF & GCP_OFF & GWRP_OFF ) _FOSCSEL(FNOSC_PRIPLL & IESO_ON) _FOSC(FCKSM_CSECME & IOL1WAY_OFF & POSCMD_XT ) _FWDT(FWDTEN_OFF & WINDIS_OFF) _FPOR(FPWRT_PWR32 & ALTI2C_OFF) /// A Variable (Global) char sync_m0; // NEWテンポラリ char sync_m1; // OLDテンポラリ char sync_m2; // EDGEテンポラリ int x; /// Function Prottypes void AxisDraw(void); void dotyline(int x0); void dotxline(int y0); /************************************************ * Function Main ************************************************/ int main(void){ /* Initialize Clock */ CLKDIVbits.PLLPRE = 0; // PLLPRE=1/2 to 4MHz PLLFBDbits.PLLDIV = 40; // PLLDIV=1/40 to 160MHz CLKDIVbits.PLLPOST = 0; // PLLPOST=1/2 to 80MHz then 40MIPS /* Initialize Port */ AD1PCFGL = 0xFFFA; // AN0,2 are Analog(AN1は、RA1のデジタル入力(スイープ同期信号入力)として使用) LATB = 0xFF7F; // only LCD_E is low TRISA = 0x000F; // RA4 is Out, Other is Input TRISB = 0x000F; // Set GLCD Port Direction CNPU1 = 0x00E0; // RB1,2,3 pull up // LATAbits.LATA1 = 1; // Green LED OFF Add 110506 /* Initialize GLCD */ lcd_Init(); lcd_Clear(0); lcd_Str(0, 0, "Start Test"); Delay1m(1000); /* Initialize ADC */ AD1CON1= 0x0000; // SAMP bit= 0 ends sampling... and starts converting(手動AD変換) AD1CHS0= 0x0000; // Connect AN0 as CH0 input AD1CSSL= 0x0000; // 自動スキャンしない AD1CON3= 0x0002; // 手動サンプル、Tad= internal 2Tcy AD1CON2= 0x0000; // Vref=AVdd-AVss,入力スキャンしない、CH0を変換 AD1CON1bits.ADON = 1; // ADC Start /****** Main Loop ****/ while(1){ do { sync_m0 = PORTAbits.RA1; //New Sync Signal sync_m2 = sync_m1^(sync_m0 & sync_m1); //Neg_Edge Sence Sync Signal sync_m1 = sync_m0; //Chenge New Data to Old Data m1 } while(!sync_m2); // スイープ同期パルスの立下りエッジが検知されるまでエッジ検知を繰り返す。 // (立下りエッジから、スイープを開始する。) AxisDraw(); // 座標表示 /// 73回、x座標をずらしながら、サンプル→ 変換→ ドット描画を繰返し、 /// 1画面分のグラフを描画 for(x= 0; x < 73; ++x) { // Delay1m(500); // 実測、477mS Delay1m(524); // 実測、499.5mS AD1CON1bits.SAMP= 1; // サンプリング開始 // Delay1m(100); // 実測、95.4mS Delay1m(105); // 実測、100.08mS AD1CON1bits.SAMP= 0; // AD変換開始 while(!AD1CON1bits.DONE); // AD変換終了待ち /// 変換値/16 がドット描画の、Y座標(レベル)になるので、そこにドット描画する。 lcd_Pixel(x, ADC1BUF0/16, 1); // Delay1m(400); // 実測、381mS(これで次回から、0.5+0.1+0.4= 1秒周期のサンプリングになる。) Delay1m(420); // 実測、400.3mS } } } /****************************************** * Drawing Coordinate Axis *******************************************/ void AxisDraw(void){ lcd_Clear(0); lcd_Line(0, 8, 0, 58); // Y axis 10Hz dotyline(19); // 100 dotyline(37); // 1k dotyline(55); // 10k lcd_Line(72, 8, 72, 58); // 100kHz lcd_Line(0, 8, 72, 8); // X axis -40dB 10dot間隔に修正:150912 dotxline(19); // -30dB dotxline(29); // -20dB dotxline(39); // -10dB dotxline(49); // 0dB lcd_Line(0, 58, 73, 58); // +20dB /// キャラクタ表示 lcd_Str(7, 0, "10"); lcd_Str(7, 4, "1k"); lcd_Str(7, 9, "100k[Hz]"); lcd_Str(6, 11, "-40"); lcd_Str(5, 11, "-30"); lcd_Str(4, 11, "-20"); lcd_Str(3, 11, "-10"); lcd_Str(1, 11, "0[dB]"); lcd_Str(0, 11, "+10"); } /******************************************* * Assist Line X Coordinate Axis (Dot Line) ********************************************/ void dotxline(int y0){ int i; // for(i=0; i<128; i+=3) for(i=0; i<74; i+=3) lcd_Pixel(i, y0-1, 1); } /******************************************* * Assist Line Y Coordinate Axis (Dot Line) ********************************************/ void dotyline(int x0){ int i; // for(i=0; i<64; i+=3) for(i=8; i<59; i+=3) lcd_Pixel(x0-1, i, 1); }