//****************************************************************************************** // PIC18F14K50 Sample Program // Project : // Program Name : // Programer : // Create date : Version 1.0 H24.06.01 // History : // Laungage : XC8 C Laungage Version 2.31 //****************************************************************************************** // (1)Description // //****************************************************************************************** #include #include #include #include #include #include //config words #pragma config CPUDIV=NOCLKDIV //CPU System Clock Selection bit(No CPU System Clock divide) #pragma config USBDIV = OFF //USB Clock Selection bit(USB Clock comes directly from the OSC1/OSC2 oscillator block; no divide) //#pragma config FOSC = HS //Oscillator Selection bits(HS oscillator) #pragma config FOSC = IRC //Oscillator Selection bits(Internal RC oscillator) //#pragma config PLLEN = OFF //X PLL Enable bit(Oscillator multiplied by 4) For 16MHz #pragma config PLLEN = ON //X PLL Enable bit(Oscillator multiplied by 4) For 32MHz #pragma config PCLKEN = OFF //Primary Clock Enable Bit(Primary clock is under software control) #pragma config FCMEN = OFF #pragma config IESO = OFF //Internal/External Oscillator Switchover bit(Oscillator Switchover mode disabled) #pragma config PWRTEN = OFF,BOREN = OFF,BORV = 19 #pragma config WDTEN = OFF,WDTPS = 1 #pragma config MCLRE = OFF //MCLR Pin Enable bit(RE3 input pin enabled; MCLR disabled) #pragma config HFOFST = OFF,STVREN = OFF,LVP = OFF,BBSIZ = OFF,XINST = OFF #pragma config CP0 = OFF,CP1 = OFF,CPB = OFF,CPD = OFF,WRT0 = OFF,WRT1 = OFF,WRTB = OFF,WRTC = OFF,WRTD = OFF,EBTR0 = OFF,EBTR1 = OFF,EBTRB = OFF // #define PI_NORMAL 0 #define PI_FAILER 1 #define PI_ON 2 #define PI_OFF 3 // #define P_LED_G PORTCbits.RC6 #define P_LED_R PORTCbits.RC7 #define P_LED_0 PORTCbits.RC0 #define P_LED_1 PORTCbits.RC1 #define P_LED_2 PORTCbits.RC2 #define P_LED_3 PORTCbits.RC3 #define P_LED_4 PORTCbits.RC4 #define P_LED_5 PORTCbits.RC5 #define P_CHECK PORTBbits.RB5 #define P_POWER PORTBbits.RB4 // unsigned char G_counter0_status = PI_OFF; unsigned char G_counter0 = 0; unsigned char G_counter1 = 0; unsigned char G_counter2 = 0; unsigned char G_counter3 = 0; unsigned char G_counter4 = 0; unsigned char G_counter5 = 0; // void InitOSC(void) ; void Wait_1mS(unsigned int) ; void Timer(void) ; void nop10(void) ; void counter0(void) ; void counter1(void) ; void counter2(void) ; void counter3(void) ; void counter4(void) ; void counter5(void) ; void main(void) { unsigned char lp; // ***************************** // 1.Initialized // ***************************** // (1)Define OSC InitOSC(); //Internal OSC 32MHz // (2)Define I/O port TRISA = 0b11111111; TRISB = 0b11101111; TRISC = 0b00000000; LATC = 0b00000000; PORTA = 0x00; PORTB = 0x00; PORTC = 0x00; // (3)Select A/D Converter ANSEL = 0b00000000; //ALL Degital PORT ANSELH = 0b00000000; //ALL Degital PORT // (4)Demo LED for(lp=0;lp<2;lp++){ P_LED_R = 1; P_LED_G = 1; Wait_1mS(500); P_LED_R = 0; P_LED_G = 0; Wait_1mS(500); } // ***************************** // 2.Program main // ***************************** while (1) { P_POWER = 1; nop10(); if(P_CHECK == 0){ G_counter0_status = PI_OFF; P_LED_R = 1; }else{ P_LED_R = 0; } nop10(); P_POWER = 0; nop10(); if( P_CHECK == 0){ P_LED_G = 0; }else{ if(G_counter0_status == PI_OFF){ counter0() ; G_counter0_status = PI_ON; } P_LED_G = 1; } nop10(); } } //****************************************************************************************** // Function Name : counter0 // Title : // Input : // Output : //****************************************************************************************** void counter0(void) { if(G_counter0 >= 9){ G_counter0 = 0; counter1(); if(P_LED_0 == 1){ P_LED_0 = 0; }else{ P_LED_0 = 1; } }else{ G_counter0++; } } //****************************************************************************************** // Function Name : counter1 // Title : // Input : // Output : //****************************************************************************************** void counter1(void) { if(G_counter1 >= 19){ G_counter1 = 0; counter2(); if(P_LED_1 == 1){ P_LED_1 = 0; }else{ P_LED_1 = 1; } }else{ G_counter1++; } } //****************************************************************************************** // Function Name : counter2 // Title : // Input : // Output : //****************************************************************************************** void counter2(void) { if(G_counter2 >= 19){ G_counter2 = 0; counter3(); if(P_LED_2 == 1){ P_LED_2 = 0; }else{ P_LED_2 = 1; } }else{ G_counter2++; } } //****************************************************************************************** // Function Name : counter3 // Title : // Input : // Output : //****************************************************************************************** void counter3(void) { if(G_counter3 >= 19){ G_counter3 = 0; counter4(); if(P_LED_3 == 1){ P_LED_3 = 0; }else{ P_LED_3 = 1; } }else{ G_counter3++; } } //****************************************************************************************** // Function Name : counter4 // Title : // Input : // Output : //****************************************************************************************** void counter4(void) { if(G_counter4 >= 19){ G_counter4 = 0; counter5(); if(P_LED_4 == 1){ P_LED_4 = 0; }else{ P_LED_4 = 1; } }else{ G_counter4++; } } //****************************************************************************************** // Function Name : counter5 // Title : // Input : // Output : //****************************************************************************************** void counter5(void) { if(G_counter5 >= 19){ G_counter5 = 0; if(P_LED_5 == 1){ P_LED_5 = 0; }else{ P_LED_5 = 1; } }else{ G_counter5++; } } //****************************************************************************************** // Function Name : InitOSC // Title : // Input : // Output : // Description : Internal 16MHz //****************************************************************************************** void InitOSC(void) { // ******************************************************* // 1.Define OSCCON(OSCILLATOR CONTROL REGISTER) // OSCCON(7) IDLEN: Idle Enable bit // 1 = Device enters Idle mode on SLEEP instruction // 0 = Device enters Sleep mode on SLEEP instruction // OSCCON(6-4) IRCF<2:0>: Internal Oscillator Frequency Select bits // 111 = 16 MHz // 110 = 8 MHz // 101 = 4 MHz // 100 = 2 MHz // 011 = 1 MHz(3) // 010 = 500 kHz // 001 = 250 kHz // 000 = 31 kHz(2) // OSCCON(3) OSTS: Oscillator Start-up Time-out Status bit(1) // 1 = Device is running from the clock defined by FOSC<2:0> of the CONFIG1 register // 0 = Device is running from the internal oscillator (HFINTOSC or LFINTOSC) // OSCCON(2) HFIOFS: HFINTOSC Frequency Stable bit // 1 = HFINTOSC frequency is stable // 0 = HFINTOSC frequency is not stable // OSCCON(1-0) SCS<1:0>: System Clock Select bits // 1x = Internal oscillator block // 01 = Secondary (Timer1) oscillator // 00 = Primary clock (determined by CONFIG1H[FOSC<3:0>]). // ******************************************************* // OSCCON = 0b11111111; //Internal 16MHz OSCCON = 0b11101100; //Internal 32MHz // ******************************************************* // 2.Define OSCCON2(OSCILLATOR CONTROL REGISTER 2) // OSCCON2(7-3) Unimplemented: Read as '0' // OSCCON2(2) PRI_SD: Primary Oscillator Drive Circuit shutdown bit // 1 = Oscillator drive circuit on // 0 = Oscillator drive circuit off (zero power) // OSCCON2(1) HFIOFL: HFINTOSC Frequency Locked bit // 1 = HFINTOSC is in lock // 0 = HFINTOSC has not yet locked // OSCCON2(0) LFIOFS: LFINTOSC Frequency Stable bit // 1 = LFINTOSC is stable // 0 = LFINTOSC is not stable // ******************************************************* OSCCON2 = 0b00000000; // ******************************************************* // 3.Define OSCTUNE(OSCILLATOR TUNING REGISTER) // OSCTUNE(7) INTSRC: Internal Oscillator Low-Frequency Source Select bit // 1 = 31.25 kHz device clock derived from 16 MHz HFINTOSC source (divide-by-512 enabled) // 0 = 31 kHz device clock derived directly from LFINTOSC internal oscillator // OSCTUNE(6) SPLLEN: Software Controlled Frequency Multiplier PLL bit // 1 = PLL enabled (for HFINTOSC 8 MHz only) // 0 = PLL disabled // OSCTUNE(5-0) TUN<5:0>: Frequency Tuning bits // 011111 = Maximum frequency // 011110 = // // 000001 = // 000000 = Oscillator module is running at the factory calibrated frequency. // 111111 = // // 100000 = Minimum frequency // ******************************************************* OSCTUNE = 0b00000000; } //****************************************************************************************** // Function Name : Wait_1mS // Title : wait function // Input : // Output : //****************************************************************************************** void Wait_1mS(unsigned int no) { unsigned int lp; for (lp = 0; lp < no; lp++) { Timer(); } } //****************************************************************************************** // Function Name : Timer // Title : wait function (1mS) // Input : // Output : For 32MHz OSC //****************************************************************************************** void Timer(void) { // ******************************************************* // 1.Define T1CON(TIMER1 CONTROL REGISTER) Register //* T1CON(7) RD16: 16-bit Read/Write Mode Enable bit // 1 = Enables register read/write of TImer1 in one 16-bit operation // 0 = Enables register read/write of Timer1 in two 8-bit operations //* T1CON(6) Unimplemented: Read as '0' //* T1CON(5-4)T1CKPS<1:0>: Timer1 Input Clock Prescale Select bits // 11 = 1:8 Prescale value // 10 = 1:4 Prescale value // 01 = 1:2 Prescale value // 00 = 1:1 Prescale value //* T1CON(3) T1OSCEN: Timer1 Oscillator Enable bit // 1 = Timer1 oscillator is enabled // 0 = Timer1 oscillator is shut off // The oscillator inverter and feedback resistor are turned off to eliminate power drain. //* T1CON(2) T1SYNC: Timer1 External Clock Input Synchronization Select bit // When TMR1CS = 1: // 1 = Do not synchronize external clock input // 0 = Synchronize external clock input // When TMR1CS = 0: // This bit is ignored. Timer1 uses the internal clock when TMR1CS = 0. //* T1CON(1) TMR1CS: Timer1 Clock Source Select bit // 1 = External clock from the T13CKI pin (on the rising edge) // 0 = Internal clock (FOSC/4) //* T1CON(0) bit 0 TMR1ON: Timer1 On bit // 1 = Enables Timer1 // 0 = Stops Timer1 // ******************************************************* // T1CON = 0b10000001; // 16-bit Read/Write Mode, 1:1 Prescale value //For 16MHz OSC T1CON = 0b10010001; // 16-bit Read/Write Mode, 1:2 Prescale value //For 32MHz OSC // ******************************************************* // 2.Define TMR1 Register // Caution(16-bit Read/Write Mode) !!!!: // (a)Writing to TMR1H does not directly affect Timer1. // Instead, the high byte of Timer1 is updated with the contents of TMR1H when a write occurs to TMR1L. // This allows all 16 bits of Timer1 to be updated at once. // (b)A read from TMR1L will load the contents of the high byte of Timer1 into the Timer1 high byte buffer. // This provides the user with the ability to accurately read all 16 bits of Timer1 without the need to // determine whether a read of the high byte, followed by a read of the low byte, has become invalid due // to a rollover or carry between reads. // ******************************************************* // TMR1 data ( OSC:10MHz, 4X PLLON, 1:1 Prescale value, 0.001[S];1000Hz ) // 1mS(1000Hz): 10MHz * 4(PLLON) / 4(PIC) / 1(Prescale) / 1000(1000Hz)==> 0d10,000(Count) ==> 0x2710(Count) // TMR1 = 0xFFFF - 0x2710 ==> 0xD8EF // 1mS(1000Hz): 16MHz / 4(PIC) / 1(Prescale) / 1000(1000Hz)==> 0d4,000(Count) ==> 0x0FA0(Count) // TMR1 = 0xFFFF - 0x0FA0 ==> 0xF05F // TMR1H = 0xF0; TMR1L = 0x5F; // ******************************************************* // 3.Timer // ******************************************************* PIR1bits.TMR1IF = 0; while (1) { if (PIR1bits.TMR1IF != 0)break; } } //****************************************************************************************** // Function Name : nop10 // Title : // Input : // Output : //****************************************************************************************** void nop10(void) { unsigned char lp; for(lp=0;lp<10;lp++){ asm("nop"); } }