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.

long delay time with 16f84a

Status
Not open for further replies.
help me, 2 seconds time error in along delay time
hi,
i have written this program in assembly language, having two delays:
(5 seconds and 3595 seconds) , the sum of them equals 1 hour.
the program must turn on ALED for five seconds,then turning it off for 3595 seconds
then repeating the process.in other words,(( the led must turn on every 1 hour exactly)).
the problem is as follows: when connecting the circuit actually, the program goes well for the first 6 hours , but after that , the LED turns on 2 seconds before the expected time!!!!!!!!!!! having errors.
The tow delayshave been made by PIC_Delay program. and the crystal frequency 4MHZ,
with tow capacitors 22PF.
PLEASE EXPLAIN FOR ME WHAT IS THE REASON OF ERRORS?? I HAVE CALCULATED NUMBER OF MACHINE CYCLES WITH MPLAB STOP WATCH AND EVERY THING IS OK.

PROCESSOR 16f84a ; Defining processor
#include "p16f84a.inc" ; Microchip INC database
__CONFIG _CP_OFF & _WDT_OFF & _PWRTE_ON & _XT_OSC
; frequency 4MHZ
CBLOCK 0X0C
COUNT,D1,D2,D3,D4
ENDC

org 0x00
goto first

first
bsf STATUS,5
CLRF TRISB
BCF STATUS,5
CLRF PORTB

DELAY BSF PORTB,0
movlw .1
movwf D1
movlw .86
movwf D2
movlw .26
movwf D3
decfsz D1,F
goto $-1
clrwdt
decfsz D2,F
goto $-4
decfsz D3,F
goto $-6
nop
nop
BCF PORTB,0

movlw .153
movwf D1
movlw .112
movwf D2
movlw .62
movwf D3
movlw .72
movwf D4
decfsz D1,F
goto $-1
decfsz D2,F
goto $-3
decfsz D3,F
goto $-5
decfsz D4,F
goto $-7
nop
nop
GOTO DELAY



END

---------- Post added at 13:51 ---------- Previous post was at 13:03 ----------

please ,can any one help me???
 

Hi,
The error is, the delay is 5 secs and the next delay is 3595 secs, between these, you lose some time by calling, turning the LED on/off etc. These seem okay for some time but then accumulate over time to cause inaccuracy. For accuracy, use timers and interrupts, then you'll have it run more or less accurate.

Hope this helps.
Tahmid.

---------- Post added at 14:26 ---------- Previous post was at 13:34 ----------

OK, THANK YOU .
IF ALL THESE FACTORS( TEMPERATURE,crystal frequency, deviation over time......) HAPPENED: WHAT IS THE DEGREE OF ACCURACY DO YOU THINK WE WILL HAVE IN THIS SITUATION??
IS IT ACCEPTABLE ??

If you plan to use this for a prolonged period, you should rather use the timer than a software delay as this will maintain your delay time through out. For short times, this delay time is accurate.

Hope this helps.
Tahmid.
 
  • Like
Reactions: fs789

    fs789

    Points: 2
    Helpful Answer Positive Rating
ok,
CAN YOU PLEASE GIVE ME AN INTERRUPT PROGRAM ??
 

Hi,
Check the datasheet for PIC16F84A, check the interrupts section - the OPTION_REG, INTCON registers, check the TIMER0 section and check the code examples. It should be pretty clear there. I don't plan on writing any more assembly codes - it's tedious work.

Hope this helps.
Tahmid.
 
  • Like
Reactions: fs789

    fs789

    Points: 2
    Helpful Answer Positive Rating
hi,
i think the errors in my project are due to stability of crystal affected by temperature variations.
i plan to use asmall crystal from awatch (( i think it is 32768 HZ)). and to apply the same code to the pic after modifying numbers in delay loops.
BUT THE QUESTION IS: is this crystal compensated for temperature ???
please help me.
 

Usually, crystals don't vary greatly due to temperature, usually. It may, but normal crystals don't.
 
  • Like
Reactions: fs789

    fs789

    Points: 2
    Helpful Answer Positive Rating
i will now replace the 4MHZ crystal with 32768HZ crystal from awatch, and measure accuracy in 24 hours.
if errors are still happening,then i will think of interrupt routines.
 

Untrimmed, a crystal could be +/-100ppm which means you could have 8.6 seconds error in 24 hours. If accuracy is important then you need to buy a crystal which is better than 100ppm and trim it to frequency. Then you will be left with temperature and aging effects.

Swapping to a 32kHz crystal will probably help because +/-20ppm is usually the worst tolerance you will find although that is still 1.7 seconds error in 24 hours if you don't trim it.

None of this negates the comments Tahmid has made about the use of delays and interrupts. I always use interrupts. If you want to use a delay loop and want some accuracy then you have to calculate the number of instruction cycles for every possible software route and compensate for it. That is too much like hard work compared to making a simple tick counter from an interrupt.

Keith.
 

Untrimmed, a crystal could be +/-100ppm which means you could have 8.6 seconds error in 24 hours. If accuracy is important then you need to buy a crystal which is better than 100ppm and trim it to frequency. Then you will be left with temperature and aging effects.

Swapping to a 32kHz crystal will probably help because +/-20ppm is usually the worst tolerance you will find although that is still 1.7 seconds error in 24 hours if you don't trim it.

None of this negates the comments Tahmid has made about the use of delays and interrupts. I always use interrupts. If you want to use a delay loop and want some accuracy then you have to calculate the number of instruction cycles for every possible software route and compensate for it. That is too much like hard work compared to making a simple tick counter from an interrupt.

Keith.
OK,THANK YOU.
SUPPOSE I HAVE USED a simple tick counter from an interrupt , with the a 32kHz crystal .
is the 1.7 seconds error in 24 hours will be removed?
and if not, can we compensate it by software?
and how can i trim the crystal??
 

1.7 seconds in 24 hours is the maximum error from a +/-20ppm crystal even if your software is perfect so if you want better than that then you will need to trim. You could "software" trim by adjusting your counters to compensate for a known error. Normally it would be done by using a variable capacitor in parallel with one of the two crystal capacitors. Make the fixed value under-sized and add the trimmer so you can go above & below the nominal value in the datasheet.

Keith.
 
  • Like
Reactions: fs789

    fs789

    Points: 2
    Helpful Answer Positive Rating
Hi,
You can compensate by adjusting the time of interrupt by trial and error and checking which is most accurate and checking how close you get. It's possible and will require some experimentation.
 
  • Like
Reactions: fs789

    fs789

    Points: 2
    Helpful Answer Positive Rating
Hi,
You can compensate by adjusting the time of interrupt by trial and error and checking which is most accurate and checking how close you get. It's possible and will require some experimentation.
thank you,
i think the best solution is adjusting the time of the last interrupt, which goes directly to the beginning of next day, until error is minimized. of course this requires patience and waiting every 24 hours.
in this way the error will not accumulate with time.
i will try this method and i hope to succeed .
 
Last edited:

i think the error was resulted from actual frequency of crystal.
because i thought the frequency was exactly 32.768 KHZ , but when measuring its frequency with amultimeter , it was
26.5 KHZ!!!!!!!!!!!!!!!!!!!! i was very surprise
 

That is almost certainly incorrect. A crystal error will be quite small. Your multimeter is either misreading or affecting oscillation. If it had that much error you would only have to wait a few seconds to see a 2 seconds error.

Keith
 
  • Like
Reactions: fs789

    fs789

    Points: 2
    Helpful Answer Positive Rating
is there any way to determine exactly the frequency of the crystal??
can i use ordinary oscilloscope?
 

You need something with better accuracy than the crystal you are trying to measure. An oscilloscope is not likely to be good enough and they aren't actually very good at measuring frequency. You really need a frequency counter. However a watch usually isn't bad although it takes a while to get a result.

Keith
 
  • Like
Reactions: fs789

    fs789

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.

Similar threads

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top