PIC 16-bit Timer1

PIC 16-bit Timer1
Çeşme:
http://www.exploreembedded.com/wiki/PIC18F4520_Timer.
  1. PIC16F877A
// PIC16F877A Configuration Bit Settings // 'C' source line config statements // CONFIG #pragma config FOSC = HS // Oscillator Selection bits (LP oscillator) #pragma config WDTE = OFF // Watchdog Timer Enable bit (WDT disabled) #pragma config PWRTE = ON // Power-up Timer Enable bit (PWRT enabled) #pragma config BOREN = OFF // Brown-out Reset Enable bit (BOR disabled) #pragma config LVP = OFF // Low-Voltage (Single-Supply) In-Circuit Serial Programming Enable bit (RB3 is digital I/O, HV on MCLR must be used for programming) #pragma config CPD = ON // Data EEPROM Memory Code Protection bit (Data EEPROM code-protected) #pragma config WRT = HALF // Flash Program Memory Write Enable bits (0000h to 0FFFh write-protected; 1000h to 1FFFh may be written to by EECON control) #pragma config CP = OFF // Flash Program Memory Code Protection bit (All program memory code-protected) // #pragma config statements should precede project file includes. // Use project enums instead of #define for ON and OFF. #include <xc.h> char value = 0; #define SBIT_PS1 5 #define SBIT_PS0 4 //Delay Calculations for 100ms @20Mhz with Prescalar as 8: //$$RegValue = 65536-(Delay * Fosc)/(Prescalar*4)) = 65536-((100ms * 20Mhz)/(8*4)) = 3036 = 0xBDC$$ void interrupt isr() { if (PIR1bits.TMR1IF == 1) { value = ~value; TMR1H = 0x0B; TMR1L = 0xDC; /* 1 = TMR1 register overflowed (must be cleared in software) 0 = TMR1 register did not overflow */ PIR1bits.TMR1IF = 0; } } void main() { TRISD = 0x00; T1CON = (1<<SBIT_PS1)|(1<<SBIT_PS0); TMR1H = 0x0B; TMR1L = 0xDC; /* 1 = Enables the TMR1 overflow interrupt 0 = Disables the TMR1 overflow interrupt */ PIE1bits.TMR1IE = 1; /* 1 = Enables all unmasked interrupts 0 = Disables all interrupts */ INTCONbits.GIE = 1; /* 1 = Enables all unmasked peripheral interrupts 0 = Disables all peripheral interrupts */ INTCONbits.PEIE = 1; /* 1 = External clock from pin RC0/T1OSO/T1CKI (on the rising edge) 0 = Internal clock (FOSC/4) */ T1CONbits.TMR1CS = 0; /* 1 = Enables Timer1 0 = Stops Timer1 */ T1CONbits.TMR1ON = 1; while(1) { PORTD = value; } }