//****************************************************************************************** // PIC32MX120F032B Sample Program // Project : // Program Name : // Programer : // Create date : Version 1.0 H24.07.10 // History : // Laungage : MPLAB C32 //****************************************************************************************** // (1)Description // Primary Oscillator 8MHz Crystal ==> PLL 40MHz (8MHz/2*20/2) //****************************************************************************************** //#include #include // //Define configration // #pragma config PMDL1WAY = OFF //Peripheral Module Disable Configuration:Allow multiple reconfigurations #pragma config IOL1WAY = OFF //Peripheral Pin Select Configuration:Allow multiple reconfigurations #pragma config FUSBIDIO = OFF //USB USID Selection:Controlled by Port Function #pragma config FVBUSONIO = OFF //USB VBUS ON Selection:Controlled by Port Function //#pragma config FPLLIDIV= DIV_1 //PLL Input Divider:1x Divider (DIV_1,DIV_2,DIV_3,DIV_4,DIV_5,DIV_6,DIV_10,DIV_12) #pragma config FPLLIDIV = DIV_2 //#pragma config FPLLMUL = MUL_15 //PLL Multiplier:15x Multiplier(MUL_15,MUL_16,MUL_17,MUL_18,MUL_19,MUL_20,MUL_21,MUL_24) #pragma config FPLLMUL = MUL_20 //#pragma config FPLLODIV= DIV_1 //System PLL Output Clock Divider:PLL Divide by 1(DIV_1,DIV_2,DIV_4,DIV_8,DIV_16,DIV_32,DIV_64,DIV_256) #pragma config FPLLODIV = DIV_2 //#pragma config FNOSC = FRC //Oscillator Selection Bits:Fast RC Osc (FRC) (FRC,FRCPLL,PRI,PRIPLL,SOSC,LPRC,FRCDIV16,FRCDIV) //#pragma config FNOSC = FRCPLL #pragma config FNOSC = PRIPLL #pragma config FSOSCEN = OFF //Secondary Oscillator Enable:Disabled #pragma config IESO = OFF //Internal/External Switch Over:Disabled //#pragma config POSCMOD = OFF //Primary Oscillator Configuration:Primary osc disabled (EC,XT,HS,OFF) ==> Used Primary Oscillator !!! #pragma config POSCMOD = HS //Primary Oscillator Configuration:Primary osc disabled (EC,XT,HS,OFF) #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 (DIV_1,DIV_2,DIV_4,DIV_8) #pragma config FCKSM = CSDCMD //Clock Switching and Monitor Selection:Clock Switch Disable, FSCM Disabled (CSECME,CSECMD,CSDCMD) #pragma config WDTPS = PS1 //Watchdog Timer Postscaler:1:1 (PS1,,,,PS1048576) #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 = WINSZ_50//Watchdog Timer Window Size:Window Size is 50% (WINSZ_75,WINSZ_50,WINSZ_37,WISZ_25) #pragma config DEBUG = OFF //Background Debugger Enable:Debugger is Disabled #pragma config JTAGEN = OFF //JTAG Enable:JTAG Disabled #pragma config ICESEL = RESERVED//ICE/ICD Comm Channel Select:Reserved (RESERVED,ICS_PGx3,ICS_PGx2,ICS_PGx1) #pragma config PWP = OFF //Program Flash Write Protect: #pragma config BWP = OFF //Boot Flash Write Protect bit: #pragma config CP = OFF //Code Protect: // //Define function // void InitOSC(void); void Init_Timer2(void); void mvLED(unsigned int *); // //Define parameter // volatile unsigned int G_LEDlst; // 32bit int main(void) { // ******************* // 1.Initialize // ******************* // (1)Define PortB TRISB = 0x0000FF00; // 32bit LATB = 0x00000000; // 32bit PORTB = 0x00000000; // 32bit // (2)Initialized Timer2 Init_Timer2(); // (3)Set Initial Vale G_LEDlst = 0x00000001; PORTB = G_LEDlst; // ******************* // 2.Program Main // ******************* while(1); } //****************************************************************************************** // Function Name : mvLED // Title : Move LED function // Input : // Output : //****************************************************************************************** void mvLED(unsigned int *LEDlst) { if(*LEDlst == 1){ *LEDlst = 2; }else if(*LEDlst > 127){ *LEDlst = 1; }else{ *LEDlst = *LEDlst * 2; } PORTB = *LEDlst; } //****************************************************************************************** // Function Name : Init_Timer2 // Title : Timer 2 Function // Input : // Output : // Description : 40MHz, 0.1[S], 32Bit-Mode //****************************************************************************************** void Init_Timer2(void) { // *********************************************** // 1.Define T2CON Register (32Bit) // T2CON(31-16) Unimplemented: Read as e0f // T2CON(15) ON: Timerx On bit // 1 = Starts / 0 = Stop // T2CON(14) Unimplemented: Read as e0f // T2CON(13) SIDL: Stop in Idle Mode bit // 1 = Discontinue module operation when device enters Idle mode // 0 = Continue module operation in Idle mode // T2CON(12-8) Unimplemented: Read as e0f // T2CON(7) TGATE: Timer Gated Time Accumulation Enable bit(3) // When TCS = 1: // This bit is ignored and is read as e0f. // When TCS = 0: // 1 = Gated time accumulation is enabled // 0 = Gated time accumulation is disabled // T2CON(6-4) TCKPS1:TCKPS0: Timerx Input Clock Prescale Select bits // 111 = 1:256 prescale value // 110 = 1:64 prescale value // 101 = 1:32 prescale value // 100 = 1:16 prescale value // 011 = 1:8 prescale value // 010 = 1:4 prescale value // 001 = 1:2 prescale value // 000 = 1:1 prescale value // T2CON(3) T32: 32-Bit Timer Mode Select bit(1) // 1 = Odd numbered and even numbered timers form a 32-bit timer // 0 = Odd numbered and even numbered timers form a separate 16-bit timer // T2CON(2) Unimplemented: Read as e0f // T2CON(1) TCS: Timerx Clock Source Select bit(2) // 1 = External clock from TxCK pin // 0 = Internal peripheral clock // T2CON(0) Unimplemented: Read as e0f // *********************************************** // T2CON = 0b00000000000000001000000001100000; // 1/64, 16Bit-Mode // T2CON = 0b00000000000000001000000001110000; // 1/256,16Bit-Mode // T2CON = 0b00000000000000000000000001110000; // 1/256,16Bit-Mode T2CON = 0b00000000000000000000000000001000; // 1/1, 32Bit-Mode // *********************************************** // 2.Set PR2-Register (PR2[31-16]:Unimplemented, PR2[15-0]timer ) // *********************************************** // PR2 = 0x00003D09; //10Hz (0x3D09;0d15625) // FRC 40MHz(16bit-Mode) PR2=40MHz/1(pic32)/256(prescale)/10(Hz) PR2 = 0x00005A00; // 1Hz (0x02625A00;0d40000000) // FRC 40MHz(32bit-Mode) PR2=40MHz/1(pic32)/1(prescale) / 1(Hz) PR3 = 0x00000262; // ************************** // 3.Define Interrupt // ************************** // 1)INTCON: INTERRUPT CONTROL REGISTER INTCONbits.SS0 = 0; INTCONbits.MVEC = 1; //Multi Vector Configuration bit : 1 = Interrupt controller configured for multi vectored mode INTCONbits.TPC = 0b010; //Temporal Proximity Control bits : Interrupts of group priority 2 or lower start the TP timer INTCONbits.INT4EP = 0; //External Interrupt 4 Edge Polarity Control bit : 1 = Rising edge / 0 = Falling edge INTCONbits.INT3EP = 0; //External Interrupt 3 Edge Polarity Control bit : 1 = Rising edge / 0 = Falling edge INTCONbits.INT2EP = 0; //External Interrupt 2 Edge Polarity Control bit : 1 = Rising edge / 0 = Falling edge INTCONbits.INT1EP = 0; //External Interrupt 1 Edge Polarity Control bit : 1 = Rising edge / 0 = Falling edge INTCONbits.INT0EP = 0; //External Interrupt 0 Edge Polarity Control bit : 1 = Rising edge / 0 = Falling edge // 2)INTSTAT: INTERRUPT STATUS REGISTER INTSTATbits.SRIPL = 0b000; //The priority level of the latest interrupt presented to the CPU (This value should only be used when the interrupt controller is configured for Single Vector mode) INTSTATbits.VEC = 0b000000;//The interrupt vector that is presented to the CPU (This value should only be used when the interrupt controller is configured for Single Vector mode) // 3)TPTMR: TEMPORAL PROXIMITY TIMER REGISTER // TPTMR = 0; Temporal Proximity Timer Reload bits // 4)IFSx: INTERRUPT FLAG STATUS REGISTER : Interrupt Flag Status bits (OK!!) IFS0bits.T3IF = 0; // 1 = Interrupt request has occurred, 0 = No interrupt request has occurred // 5)IECx: INTERRUPT ENABLE CONTROL REGISTER : Interrupt Enable bits IEC0bits.T3IE = 1; // 1 = Interrupt is enabled, 0 = Interrupt is disabled // 6)IPCx: INTERRUPT PRIORITY CONTROL REGISTER IPC3bits.T3IP = 0b010; //T2:Interrupt Priority bits(111 = Interrupt priority is 7,,, 000 = Interrupt is disabled) IPC3bits.T3IS = 0b00; //T2:Interrupt Subpriority bits(11 = Interrupt subpriority is 3,,, 00 = Interrupt subpriority is 0) // 7)enable interrupts (ei command !!!!) __asm__ ("ei"); // *********************************************** // 3.Timer // *********************************************** T2CONbits.ON = 1; } //****************************************************************************************** // Function Name : // Title : Interrupt TIMER2 (32bit-Mode) // Input : // Output : //****************************************************************************************** void __ISR(_TIMER_3_VECTOR, ipl2) Intrupt_TIMER3(void) { unsigned char IO_char; // ******************************** // 1.Clear the Timer2 interrupt Flag(32bit-Mode) // ******************************** IFS0bits.T3IF = 0; // ******************************** // 2.Move LED // ******************************** mvLED(&G_LEDlst); PORTB = G_LEDlst; }