/*********************************************************************** * タイマー割り込みによるLチカ_2 * * 原典先:田仲健治さん「ケンケンのHP」 * * Condition: * 10MHz External X'tal Oscillator, 1/2 x 20x PLL (5MHzx20= 100MHz) * Fcy= Fosc= 100MHz, Tcy=10ns * * CPU: PIC32MX370F512H * * N.Ishii 2017.9.21 ************************************************************************/ #include #include /// 外部クロック 100MHzで動作、周辺クロック:システムクロック = 1:1(外部X'tal Oscillator= 10MHz) // DEVCFG3 #pragma config FSRSSEL = PRIORITY_7 // Shadow Register Set Priority Select (SRS Priority 7) #pragma config PMDL1WAY = OFF // Peripheral Module Disable Configuration (Allow multiple reconfigurations) #pragma config IOL1WAY = OFF // Peripheral Pin Select Configuration (Allow multiple reconfigurations) // DEVCFG2 #pragma config FPLLIDIV = DIV_2 // PLL Input Divider (2x Divider) #pragma config FPLLMUL = MUL_20 // PLL Multiplier (20x Multiplier) #pragma config FPLLODIV = DIV_1 // System PLL Output Clock Divider (PLL Divide by 1) // DEVCFG1 #pragma config FNOSC = PRIPLL // Oscillator Selection Bits (External X'tal Osc with PLL) #pragma config FSOSCEN = OFF // Secondary Oscillator Enable (Disabled) #pragma config IESO = OFF // Internal/External Switch Over (Disabled) #pragma config POSCMOD = XT // Primary Oscillator Configuration (XT osc mode) #pragma config OSCIOFNC = OFF // CLKO Output Signal Active on the OSCO Pin (Disabled) #pragma config FPBDIV = DIV_1 // Peripheral Clock Divisor (Pb_Clk is Sys_Clk/1) #pragma config FCKSM = CSDCMD // Clock Switching and Monitor Selection (Clock Switch Disable, FSCM Disabled) #pragma config WDTPS = PS1048576 // Watchdog Timer Postscaler (1:1048576) #pragma config WINDIS = OFF // Watchdog Timer Window Enable (Watchdog Timer is in Non-Window Mode) #pragma config FWDTEN = OFF // Watchdog Timer Enable (WDT Disabled (SWDTEN Bit Controls)) #pragma config FWDTWINSZ = WISZ_25 // Watchdog Timer Window Size (Window Size is 25%) // DEVCFG0 #pragma config DEBUG = OFF // Background Debugger Enable (Debugger is Disabled) #pragma config JTAGEN = OFF // JTAG Enable (JTAG Disabled) #pragma config ICESEL = ICS_PGx1 // ICE/ICD Comm Channel Select (Communicate on PGEC1/PGED1) #pragma config PWP = OFF // Program Flash Write Protect (Disable) #pragma config BWP = OFF // Boot Flash Write Protect bit (Protection Disabled) #pragma config CP = OFF // Code Protect (Protection Disabled) int counter=0; /******************************************************** * タイマー2割り込み処理ルーチン * 1秒毎に、LEDをブリンク *********************************************************/ void __ISR(_TIMER_2_VECTOR, IPL4AUTO) T2Interrupt(void) { counter++; if (counter == 10) { LATGbits.LATG8 ^= 1; // RG8反転 counter = 0; } IFS0bits.T2IF = 0; // Reset interrupt flag } int main(void) { // Data Memory SRAM wait states: Default Setting = 1; set it to 0 BMXCONbits.BMXWSDRM = 0; // SRAMのウェイトステートを0にする // Flash PM Wait States: MX Flash runs at 3 wait states @ 100 MHz CHECONbits.PFMWS = 3; // フラッシュのプリフェッチを3ウェイトステートに対応(100MHz動作時) // Prefetch-cache: Enable prefetch for cacheable PFM instructions CHECONbits.PREFEN = 1; //プリフェッチキャッシュ有効 __builtin_mtc0(16, 0, (__builtin_mfc0(16, 0) & 0xfffffff8 | 3)); // キャッシュ有効化 // Set the interrupt controller for multi-vector mode INTCONSET = _INTCON_MVEC_MASK; //割り込みをマルチベクタモードに設定 // Set the CP0 Status IE bit to turn on interrupts globally __builtin_enable_interrupts(); //割り込み有効化 /// I/O設定→ 簡易6CHロジアナ実験回路と同じ設定にしておく。(最終目的:PIC32MX370Fでの、ロジアナ実験) // AN*ピンと共用ピンになっているピンを、全てデジタル・ピンに設定(デフォルト=アナログ・ピン設定なので注意) ANSELB = 0x0000; // RB0-15として使用 ANSELD = 0x0000; // RD1-3として使用 ANSELE = 0x0000; // RE2,RE4-7として使用 ANSELG = 0x0000; // RG6-9として使用 TRISB = 0xFFFF; // RB1:TRG_SLOPE_SW, RB2:HOLD_SW入力, RB3:DSW-1入力、RB4:DSW-2入力、RB5:DSW-4入力、他未使用ピンも入力設定 TRISD = 0xFF07; // LCD_RS,CS,WR,RESET:全て出力・HOLD_LED:出力・SW2-5:入力、他未使用ピンは入力設定 TRISE = 0x0000; // LCDデータバス(DB0-DB7)オール出力 TRISF = 0xFFFF; // RF0-5:CH1-6入力(5Vトレラント・ピン) TRISG = 0xFEFF; // RG6:TRIG_MODE_SW, RG7:READY_SW入力, RG8:READY_LED出力 LATDbits.LATD3 = 1; // HOLD_LED消灯 LATGbits.LATG8 = 1; // READY_LED消灯 // 内部プルアップ設定(今回は未使用) CNPUBSET = 0x003F; // DSW-8,DSW-4.DSW-2,DSW-1,SLP_SW,HLD_SW CNPUGSET = 0x00C0; // RDY_SW,TRG_MODE //タイマー2割り込み周期設定= Tcy*PS*n= 0.000000010mS*256*39063 = 100mS T2CON = 0x70; // prescale 1:256, 16bit mode PR2 = 39062; // PR2= n-1= 39062 TMR2 = 0; // clear Timer 2 counter IPC2bits.T2IP = 4; // T2割り込み優先度4に設定 IFS0bits.T2IF = 0; // T2割り込みフラグをクリア IEC0bits.T2IE = 1; // T2割り込み有効化 T2CONbits.TON = 1; // タイマー2開始 while (1) { /// 割り込み待ち } }