XC8 C delays for PIC micros not right timing

Status
Not open for further replies.
T

treez

Guest
Hi,
When i program a PIC16FXXXX with XC8 , it doesnt give the right delay time
__delay_(20m) gives 80ms or sometimes 160ms delay.
Do you know why?
Also, the program goes wrong if i put in too big a delay value...do you know whats the max?
 

The delay statement may go wrong if the compiler can't determine the actual clock frequency in effect, a matter of your code structure.

Parameter range is specified in the XC8 doc, I think. I don't have it at hand now.
 
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
The delay is a software loop and therefore dependent on the clock frequency and maybe (depending on the PIC type) an internal divider option. Check the clock frequency you have specified, in MCC if you use it or directly in the source code.
If you need accurate timing it is far better to use interrupts and a timer module, they are more accurate in most cases and do not block normal code execution.

Brian.
 
Reactions: treez

    T

    Points: 2
    Helpful Answer Positive Rating
The max value for the delay macro is in the XC8 user guide.
The delay is also affected by interrupts.
Have you set the correct value of _XTAL_FREQ?
You have not told us the clock source (or anything else of real value) so without a short code example that demonstrates the problem, it is really hard to advise you.
Susan
 
Reactions: treez

    T

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

Similar threads

Cookies are required to use this site. You must accept them to continue using the site. Learn more…