ubh said:
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.
dear friend
i am also not c onlt asm. so i give some line of timer of 84 a. i hope its can help u
list p=pic16F84A
include p16F84A.inc
__config _HS_OSC & _WDT_OFF & _PWRTE_ON & _CP_OFF
errorlevel -302 ;Eliminate bank warning
;**************** Label Definition ********************
|
|
|
|
|
;********************************************************
; Timer Subroutine for 10MHz clock
;********************************************************
;************* 1msec Timer Subroutine *****************
t1m movlw d'2' ;(1) Set loop cnt1
movwf cnt1m ;(1) Save loop cnt1
tm1lp1 movlw d'249' ;(1)*2 Set loop cnt2
movwf cnt500u ;(1)*2 Save loop cnt2
tm1lp2 nop ;(1)*249*2 Time adjust
nop ;(1)*249*2 Time adjust
decfsz cnt500u,f ;(1)*249*2 cnt500u-1=0 ?
goto tm1lp2 ;(2)*248*2 No, continue
decfsz cnt1m,f ;(1)*2 cnt1m-1=0 ?
goto tm1lp1 ;(2) No. Continue
return ;(2) Yes. Cnt end
;Total 2501*0.4usec=1msec
;************* 100msec Timer Subroutine ***************
t100m movlw d'100' ;Set loop counter
movwf cnt100m ;Save loop counter
tm2lp call t1m ;1msec subroutine
decfsz cnt100m,f ;cnt100m - 1 = 0 ?
goto tm2lp ;No. Continue
return ;Yes. Count end
;************* 500msec Timer Subroutine ***************
t500m movlw d'5' ;Set loop counter
movwf cnt500m ;Save loop counter
tm3lp call t100m ;100msec subroutine
decfsz cnt500m,f ;cnt500m - 1 = 0 ?
goto tm3lp ;No. Continue
return ;Yes. Count end
;************** 1sec Timer Subroutine *****************
t1s movlw d'2' ;Set loop counter
movwf cnt1s ;Save loop counter
tm4lp call t500m ;500msec subroutine
decfsz cnt1s,f ;cnt1s - 1 = 0 ?
goto tm4lp ;No. Continue
return ;Yes. Count end