Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

SIMULATIONG DELAY IN MPLAB (PIC 16F84A)

Status
Not open for further replies.

ubh

Newbie level 2
Newbie level 2
Joined
Oct 12, 2009
Messages
2
Helped
0
Reputation
0
Reaction score
0
Trophy points
1,281
Activity points
1,304
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.
 

Hi,

Am not into C only Assembler - have just proven your theory of setting TMR0 to dec 155 and it does return 102 us.

It would seem your code is not quite correct, I would think you should set the Option reg first then load the TMR0 value.
 

First of all thanks for taking the pains to check the code and reply.
I agree there is something I am not getting properly.
However setting the Option register first would not solve the problem, because in the loop I always get the wrong time period. For the first time execution your theory may be right but not for every time in the loop.

I know C produces more lines of codes than the assembly but the difference is almost double besides hi-tech C is known for its tight coding.
Thanks again.
 

Running at half speed only implies one extra instruction per loop which I would consider to be very 'tight' coding for a C compiler.

Suggestions:

1. I assume the compiler has produced an asm listing. Try compiling the asm code in MPLAB and use the stopwatch to trace the execution timing.

2. Change the code so it creates an interrupt as the timer overflows. That should remove the code polling T0IF. Simply loop in your main() section and wait for the hardware to cause an interrupt. There will still be some overhead but shifting the delay into hardware will minimize it.

Brian.
 

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
 

Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top