I am using PIC16F886/ 4MHz external Frequency/ timer 1 (100ms)/prescaler is 1. I am trying to turn on off LED for every 100ms. In below code I need to change any setting to get exact value. or my formula is wrong.
If I do reverse calculation me getting 20Mhz
Count=65536-((Time*FOSC)/4*Prescalar))
3A98(hex)=15000
15000=65536-(time*4XMhz)/4*1
time=0.050s
OR
Count =65536-(100mS*4Mhz)/4*1
count=34464
#include <htc.h>
#define _XTAL_FREQ 4000000
__CONFIG(FOSC_HS & WDTE_ON& PWRTE_ON & CP_OFF & BOREN_ON & LVP_OFF & CPD_OFF & DEBUG_OFF&MCLRE_ON &BOREN_ON&FCMEN_ON &WRT_OFF);
#define LED_RX RC7
unsigned char Counter=0;
void InitController() {
// OSCCON=0X08;
//OSCTUNE = 0x00;
WREN=0;
WDTCON = 0b00010111;
PORTA = 0b00000000;
TRISA = 0b00111000;
TRISB =0b00001000;
PORTB = 0b00000000;
WPUB = 0b00000000;
ANSEL = 0b00000000;
ADCON0 = 0b00000000;
TRISC = 0X00;
PORTC = 0b00000000;
ANSELH = 0b00000000;
ADDEN = 0;
//T1OSCEN = 0;
//INTEDG = 0; //falling edge
//INTF = 0;
//INTE = 1;
PIE1=0b00000001;
PIR1=0x01;
INTCON = 0;
//T1CON = 0b00000101;
T1CON=0X15;
TMR1IE = 1;
PEIE = 1;
GIE = 1;
}
//===========================================================================
// Main program starts here.
//===========================================================================
void main(void) {
unsigned char value;
unsigned char conter = 0;
InitController();
while(1) {
}
}
//==========================================================================
// Timer Interrupt - 5mSec
//==========================================================================
void interrupt isr(void) {
asm("clrwdt");
if (TMR1IF) {
TMR1IF = 0;
// TMR1H = 0x3A;
// TMR1L = 0x98;
TMR1H = 0x0B;
TMR1L = 0xDB;
Counter++;
if(Counter>1) {
Counter=0;
LED_RX=!LED_RX;
}
}
}