//****************************************************************************************** // PIC32MX120F032B Sample Program // Project : // Program Name : // Programer : // Create date : Version 1.0 H24.07.29 // 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_CVref(void); void Init_Timer2(void); void Set_data(unsigned int); // //Define parameter // #define PI_Data_max 100 volatile unsigned int G_Data_Index; char G_Data[PI_Data_max]={ //sin(X / PI_Data_max * PI * 2 ) * 7 0,1,1,2,2,2,3,3,4,4,4,5,5,5,6,6, 6,6,6,7,7,7,7,7,7,7,7,7,7,7,6,6,6,6,6,5,5,5,4,4,4,3,3,2,2, 2,1,1,0,0, -1,-1,-1,-2,-2,-3,-3,-3,-4,-4,-5,-5,-5,-5,-6,-6,-6,-6,-7,-7, -7,-7,-7,-7,-7,-7,-7,-7,-7,-7,-7,-6,-6,-6,-6,-5,-5,-5,-5,-4, -4,-3,-3,-3,-2,-2,-1,-1,-1,0}; int main(void) { // ******************* // 1.Initialize // ******************* // (1)Define PortB TRISB = 0x0000FFFF; // 32bit LATB = 0x00000000; // 32bit PORTB = 0x00000000; // 32bit // (2)Initialized CVref Init_CVref(); // (3)Initialized Timer2 Init_Timer2(); // (4)Set Initial Value G_Data_Index=0; // ******************* // 2.Program Main // ******************* while(1); } //****************************************************************************************** // Function Name : Init_CVref // Title : Initialized COMPARATOR VOLTAGE REFERENCE // Input : // Output : // Description : //****************************************************************************************** void Init_CVref(void) { // *********************************************** // 1.Define Register (32Bit) // CVRCON[31-16] Unimplemented: Read as e0f // CVRCON[15] ON: Comparator Voltage Reference On bit(1) // 1 = Module is enabled // Setting this bit does not affect other bits in the register. // 0 = Module is disabled and does not consume current. // Clearing this bit does not affect the other bits in the register. // CVRCON[14-7] Unimplemented: Read as e0f // CVRCON[6] CVROE: CVREFOUT Enable bit // 1 = Voltage level is output on CVREFOUT pin // 0 = Voltage level is disconnected from CVREFOUT pin // CVRCON[5] CVRR: CVREF Range Selection bit // 1 = 0 to 0.67 CVRSRC, with CVRSRC/24 step size // 0 = 0.25 CVRSRC to 0.75 CVRSRC, with CVRSRC/32 step size // CVRCON[4] CVRSS: CVREF Source Selection bit // 1 = Comparator voltage reference source, CVRSRC = (VREF+) - (VREF-) // 0 = Comparator voltage reference source, CVRSRC = AVDD - AVSS // CVRCON[3-0] CVR<3:0>: CVREF Value Selection 0 <= CVR<3:0> <= 15 bits // When CVRR = 1: // CVREF = (CVR<3:0>/24)E(CVRSRC) // When CVRR = 0: // CVREF = 1/4E(CVRSRC) + (CVR<3:0>/32)E(CVRSRC) // *********************************************** CVRCONbits.ON = 1; //Module On CVRCONbits.CVROE = 1; //Connect output-PIN CVRCONbits.CVRR = 1; //Initial Value CVRCONbits.CVRSS = 0; //CVRSRC = AVDD - AVSS CVRCONbits.CVR = 0; //Initial Value } //****************************************************************************************** // 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 = 0x00005A00; // 1Hz (0x02625A00;0d40000000) // FRC 40MHz(32bit-Mode) PR2=40MHz/1(pic32)/1(prescale) / 1(Hz) // PR3 = 0x00000262; // PR2 = 0x00001A80; //100Hz(0x00061A80;0d400000) // FRC 40MHz(32bit-Mode) PR2=40MHz/1(pic32)/1(prescale) / 100(Hz) // PR3 = 0x00000006; PR2 = 0x00001F40; // 5KHz(0x00001F40;0d8000) // FRC 40MHz(32bit-Mode) PR2=40MHz/1(pic32)/1(prescale) / 5000(Hz) PR3 = 0x00000000; // ************************** // 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 : Set_data // Title : // Input : // Output : // Description : //****************************************************************************************** void Set_data(unsigned int data_index) { CVRCONbits.CVR = G_Data[data_index] + 7; } //****************************************************************************************** // 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 // ******************************** if(G_Data_Index < ( PI_Data_max - 1 )){ G_Data_Index++; }else{ G_Data_Index=0; } Set_data(G_Data_Index); }