//****************************************************************************************** // PIC32MX120F032B Sample Program // Project : // Program Name : // Programer : // Create date : Version 1.0 H24.06.09 // History : // Laungage : MPLAB C32 //****************************************************************************************** // (1)Description // Internal OSC 8MHz //****************************************************************************************** //#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 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 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 FNOSC = FRC //Oscillator Selection Bits:Fast RC Osc (FRC) (FRC,FRCPLL,PRI,PRIPLL,SOSC,LPRC,FRCDIV16,FRCDIV) //#pragma config FNOSC = FRCPLL #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) #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 // #define TIM 1000 void wat(void); void mvLED(unsigned int *); int main(void) { unsigned int LEDlst; // 32bit // ******************* // 1.Define PORT // ******************* // (1)Define PortB TRISB = 0x00000000; // 32bit LATB = 0x00000000; // 32bit PORTB = 0x00000000; // 32bit // (2)Set Initial Vale LEDlst = 0x00000001; PORTB = LEDlst; // ******************* // 2.Program Main // ******************* while(1){ wat() ; mvLED(&LEDlst); } } //****************************************************************************************** // 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 : wat // Title : wait function // Input : // Output : //****************************************************************************************** void wat(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 T2CON = 0b00000000000000001000000001110000; // 1/256,16Bit // *********************************************** // 2.Set PR2-Register (PR2[31-16]:Unimplemented, PR2[15-0]timer ) // *********************************************** PR2 = 0x00007A12; // 1Hz (0x7A12;0d31250) // FRC 8MHz 8MHz/1(pic32)/256(prescale)/1(Hz) // PR2 = 0x0000186A; // 5Hz (0x186A;0d6250) // FRC 8MHz 8MHz/1(pic32)/256(prescale)/5(Hz) TMR2 = 0; // *********************************************** // 3.Timer // *********************************************** IFS0bits.T2IF = 0; while(1){ if(IFS0bits.T2IF != 0 )break; } }