Chris_WMS
Newbie level 4
Hi guys, I would like some help.
I am using the Timer 1 to use as a delay using the initial value method for delaying 1 sec.
The FOSC used is 20MHz.
Here are the ways I use to calculate the initial value.
PIC Input frequency = 20MHz/4=5MHz
Prescaler = 1:1
Tick counter frequency = 1/5MHz = 0.2u sec
Full scale time = 0.2u sec * 65536 = 13.11 m sec
10m sec is chosen as a time to count for every cycle and repeated 100 times to obtain 1 sec.
Timer count = 10m sec/0.2 u sec = 50000
Register value = 65536-50000 = 15535 = 3CAF (hex)
Below is the attachment of the code I am using and the I am using MPLAB IDE v8.92 and the figure shows what I obtain from the logic analyzer.
As the Figure shows, the delay time is only 0.5m sec which is not as what I expected to obtain. Therefore, I would like to ask that which is the parts that I done wrong that causes this error?
I am using the Timer 1 to use as a delay using the initial value method for delaying 1 sec.
The FOSC used is 20MHz.
Here are the ways I use to calculate the initial value.
PIC Input frequency = 20MHz/4=5MHz
Prescaler = 1:1
Tick counter frequency = 1/5MHz = 0.2u sec
Full scale time = 0.2u sec * 65536 = 13.11 m sec
10m sec is chosen as a time to count for every cycle and repeated 100 times to obtain 1 sec.
Timer count = 10m sec/0.2 u sec = 50000
Register value = 65536-50000 = 15535 = 3CAF (hex)
Below is the attachment of the code I am using and the I am using MPLAB IDE v8.92 and the figure shows what I obtain from the logic analyzer.
As the Figure shows, the delay time is only 0.5m sec which is not as what I expected to obtain. Therefore, I would like to ask that which is the parts that I done wrong that causes this error?
Code:
#include<htc.h>
#define _XTAL_FREQ 20000000
__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_ON & BOREN_OFF & LVP_OFF);
int Count = 0;
void main ()
{
TRISD=0x00; //configurate PORTD as output
T1CON=0b00000000; //disable Timer 1
TMR1H=0x3C; //setting initial value as 15535
TMR1L=0xAF;
T1CON=0b00000001; //starting Timer 1
while (1)
{
while(TMR1IF=1)
{
TMR1IF = 0; //clear interrupt flag
Count ++; //increase count bit by 1
if (Count ==100)
{
Count = 0; //clear count bit
PORTD=~PORTD; //toggle PORT D
}
TMR1H=0x3C;
TMR1L=0xAF;
}
}
}