//****************************************************************************************** // 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 volatile unsigned int G_cnt; volatile unsigned char LED_list; void Init_Timer(void); void Move_LED(void); 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 LED_list = 0x01; G_cnt = 0; // (4)Initialize timer Init_Timer(); while(1){ }; } //****************************************************************************************** // Function Name : Move_LED // Title : Move LED // Input : // Output : //****************************************************************************************** void Move_LED(void) { if(LED_list < 0x80) LED_list = LED_list * 2; else LED_list = 1; PORTC = LED_list; } //****************************************************************************************** // 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; Move_LED(); } // 2.Initialized timer Init_Timer(); } }