ubh
Newbie level 2
I have visited PIC based developments after a long time. I am having a problem with timer based delays, though the task is very simple but I am not getting hold of the proceedings.
I am using MPLAB version 8.36 and PIC16F84A. I wish to produce a delay of 100 micro seconds which I later intend to use in PWM for servo control. I am using Hitech demo version.
The code is :
#include <pic.h>
__CONFIG(WDTDIS & PROTECT & HS); //0x3FF1);
#define PORTBIT(adr, bit) ((unsigned)(&adr)*8+(bit))
static bit LED0 @ PORTBIT(PORTB, 0);
main()
{
TRISB=0;
PORTB = 0;
GIE=0; //DISABLE INTERRUPTS
TMR0 = 155; // Use TMR0 for a 100 micro sec Delay
OPTION = 0b11010000; // No Prescaler to TMR0
T0IF = 0;
LED0=0;
while(1 == 1)
{
while(!T0IF); // Wait for TMR0 to Overflow
T0IF=0;
TMR0=155;
LED0=!LED0; //Break point
}//END WHILE
} // End MAIN
For a 4MHz Fosc, I expect each instruction to take 1 micro second. Therefore for a 100 micro seconds delay I preload the Timer0 with a value of 155.
The value of time I get in Stop watch is 208 micro seconds at the break point in the while loop that is double than what I expect to get. I have set the Processor frequency equal to 4MHz.
I shall appreciate guidance.
I am using MPLAB version 8.36 and PIC16F84A. I wish to produce a delay of 100 micro seconds which I later intend to use in PWM for servo control. I am using Hitech demo version.
The code is :
#include <pic.h>
__CONFIG(WDTDIS & PROTECT & HS); //0x3FF1);
#define PORTBIT(adr, bit) ((unsigned)(&adr)*8+(bit))
static bit LED0 @ PORTBIT(PORTB, 0);
main()
{
TRISB=0;
PORTB = 0;
GIE=0; //DISABLE INTERRUPTS
TMR0 = 155; // Use TMR0 for a 100 micro sec Delay
OPTION = 0b11010000; // No Prescaler to TMR0
T0IF = 0;
LED0=0;
while(1 == 1)
{
while(!T0IF); // Wait for TMR0 to Overflow
T0IF=0;
TMR0=155;
LED0=!LED0; //Break point
}//END WHILE
} // End MAIN
For a 4MHz Fosc, I expect each instruction to take 1 micro second. Therefore for a 100 micro seconds delay I preload the Timer0 with a value of 155.
The value of time I get in Stop watch is 208 micro seconds at the break point in the while loop that is double than what I expect to get. I have set the Processor frequency equal to 4MHz.
I shall appreciate guidance.