//****************************************************************************************** // PIC16F877 Sample Program // Project : // Program Name : // Programer : // Create date : // History : // Laungage : XC8 C Laungage Version 2.20 //****************************************************************************************** // Description //****************************************************************************************** // #include #pragma config FOSC = HS, WDTE = OFF, PWRTE = OFF, CP = OFF, BOREN = OFF, LVP = OFF, CPD = OFF, WRT = OFF, DEBUG = OFF //For 16F877 //#pragma config FOSC = HS, WDTE = OFF, PWRTE = OFF, BOREN = OFF, LVP = OFF, CPD = OFF, WRT = OFF, DEBUG = OFF, CP = OFF //For 16F877A #define PI_TIM_LP 1000 #define PI_ON 1 #define PI_OFF 2 volatile unsigned int G_TIMER; volatile unsigned int G_cnt; void Init_Timer(void); void Timer_05mS_sub(void); void Timer_1mS(unsigned int); void main() { // ************************* // 1.Initialize // ************************* // (1)TRIS Register TRISA=0xff; TRISB=0xff; TRISC=0x00; TRISD=0xff; // TRISE=0xff; // (2)PORT Register PORTA=0x00; PORTB=0x00; PORTC=0x00; PORTD=0x00; PORTE=0x00; // (3)Initialize value G_cnt = 0; G_TIMER = PI_OFF; // (4)Initialize timer Init_Timer(); while(1){ if(G_TIMER == PI_ON ){ PORTC = 0xff; Timer_1mS(1000); PORTC = 0x00; G_TIMER = PI_OFF; G_cnt=0; } } } //****************************************************************************************** // Function Name : Timer_1mS // Title : Wait function (1mS) * Number (argument) // Input : // Output : //****************************************************************************************** void Timer_1mS(unsigned int Number) { unsigned int lp; unsigned int chk_wdt = 0; Number = Number *2; for(lp=0;lp 1000){ CLRWDT(); chk_wdt = 0; } chk_wdt++; Timer_05mS_sub(); } } //****************************************************************************************** // Function Name : Timer_05mS_sub // Title : Wait function (1mS) // Input : // Output : //****************************************************************************************** void Timer_05mS_sub(void) { // ****************************************************************** // 1.Define T1CON: TIMER1 CONTROL REGISTER (ADDRESS 10h) // T1CON[7-6] Unimplemented: Read as '0' // T1CON[5-4] T1CKPS1:T1CKPS0: 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 Control bit // 1 = Oscillator is enabled // 0 = Oscillator is shut-off (the oscillator inverter is turned off to eliminate power drain) // T1CON[2] T1SYNC: Timer1 External Clock Input Synchronization Control 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 pin RC0/T1OSO/T1CKI (on the rising edge) // 0 = Internal clock (FOSC/4) // T1CON[0] TMR1ON: Timer1 On bit // 1 = Enables Timer1 // 0 = Stops Timer1 // // Define T2CON: TIMER2 CONTROL REGISTER (ADDRESS 12h) // T1CON[7] *Unimplemented: Read as ‘0’ // T1CON[6-3] TOUTPS3:TOUTPS0: Timer2 Output Postscale Select bits // 0000 = 1:1 postscale // 0001 = 1:2 postscale // 0010 = 1:3 postscale // ** // ** // ** // 1111 = 1:16 postscale // T1CON[2] TMR2ON: Timer2 On bit // 1 = Timer2 is on // 0 = Timer2 is off // T1CON[1-0] T2CKPS1:T2CKPS0: Timer2 Clock Prescale Select bits // 00 = Prescaler is 1 // 01 = Prescaler is 4 // 1x = Prescaler is 16 //***************************************************************** // T1CON = 0b00000101; //Since timer1 is used as an interrupt timer, it is changed to timer2. T2CON = 0b00000110; // ******************************************************* // 2.Define TMR1 Register // ******************************************************* // (1)TMR1 data ( OSC:16MHz, 1:1 Prescale value, 0.001[S];1000Hz ) // 1mS(1000Hz): 16MHz / 4(FOSC/4) / 1000(1000Hz)==> 0d4000(Count) ==> 0x0FA0(Count) // TMR1 = 0xFFFF - 0x0FA0 ==> 0xF05F // TMR1H = 0xF0; // TMR1L = 0x5F; // // (2)TMR1 data ( OSC:20MHz, 1:1 Prescale value, 0.001[S];1000Hz ) // 1mS(1000Hz): 20MHz / 4(FOSC/4) / 1000(1000Hz)==> 0d5000(Count) ==> 0x1338(Count) // TMR1 = 0xFFFF - 0x1338 ==> 0xECC7 // // (3)TMR2 data ( OSC:20MHz, 1:1 Prescale value, 0.001[S];1000Hz ) // 1mS(1000Hz): 20MHz / 4(FOSC/4) / 16(Prescale) / 500(2000Hz)==> 0d156(Count) ==> 0x9C(Count) // TMR1 = 0xFF - 0x9C ==> 0x63 // TMR1H = 0xEC; //Since timer1 is used as an interrupt timer, it is changed to timer2. // TMR1L = 0xC7; //Since timer1 is used as an interrupt timer, it is changed to timer2. TMR2 = 0x63; // ******************************************************* // 3.Timer // ******************************************************* PIR1bits.TMR2IF = 0; while(1){ if(PIR1bits.TMR2IF != 0 )break; } } //****************************************************************************************** // Function Name : Timer_1mS_sub // Title : Wait function (1mS) // Input : // Output : //****************************************************************************************** void Init_Timer(void) { // ****************************************************************** // 1.Define T1CON: TIMER1 CONTROL REGISTER (ADDRESS 10h) // T1CON[7-6] Unimplemented: Read as '0' // T1CON[5-4] T1CKPS1:T1CKPS0: 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 Control bit // 1 = Oscillator is enabled // 0 = Oscillator is shut-off (the oscillator inverter is turned off to eliminate power drain) // T1CON[2] T1SYNC: Timer1 External Clock Input Synchronization Control 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 pin RC0/T1OSO/T1CKI (on the rising edge) // 0 = Internal clock (FOSC/4) // T1CON[0] TMR1ON: Timer1 On bit // 1 = Enables Timer1 // 0 = Stops Timer1 // ****************************************************************** T1CON = 0b00000101; // ******************************************************* // 2.Define TMR1 Register // ******************************************************* // (1)TMR1 data ( OSC:16MHz, 1:1 Prescale value, 0.001[S];1000Hz ) // 1mS(1000Hz): 16MHz / 4(FOSC/4) / 1000(1000Hz)==> 0d4000(Count) ==> 0x0FA0(Count) // TMR1 = 0xFFFF - 0x0FA0 ==> 0xF05F // TMR1H = 0xF0; // TMR1L = 0x5F; // // (2)TMR1 data ( OSC:20MHz, 1:1 Prescale value, 0.001[S];1000Hz ) // 1mS(1000Hz): 20MHz / 4(FOSC/4) / 1000(1000Hz)==> 0d5000(Count) ==> 0x1338(Count) // TMR1 = 0xFFFF - 0x1338 ==> 0xECC7 TMR1H = 0xEC; TMR1L = 0xC7; // ******************************************************* // 3.Define Interrupts // ******************************************************* PIR1bits.TMR1IF = 0; //Timer1 Overflow Interrupt Flag bit INTCONbits.GIE = 1; //Grobal Interrupt Ebable (->1) INTCONbits.PEIE = 1; //Peripheral Interrupt Ebable (->1) PIE1bits.TMR1IE = 1; //Timer1 Overflow Interrupt Enable bit(->1) } //****************************************************************************************** // Function Name : Int_tim // Title : Interrupt function // Input : // Output : //****************************************************************************************** void __interrupt() isr(void) { // ********************************** // 1.Initiarize // ********************************** // 1.Stop Interrupt INTCONbits.GIE = 0 ; // Grobal Interrupt Disable (->1) // ********************************** // 2.Timer 1 // ********************************** if(PIR1bits.TMR1IF){ // 1.Check Global Counter if(G_cnt < PI_TIM_LP ){ G_cnt++; }else{ G_cnt =0; G_TIMER = PI_ON; } // 2.Initialized timer Init_Timer(); } }