2
\$\begingroup\$

I'm using a PIC10F220.

For some reason, this delay works perfectly fine:

__delay_ms(10);

But, when I try to run a delay with a variable, it gets an error. This error indicates that it is trying to include libraries to do floating point arithmetic.

char delayConst = 10;
__delay_ms(delayConst);

Error:

Non line specific message::: error: (1359) no space for ___ftmul parameters

This also happens for the unsigned char, int, unsigned int, and unsigned short int types using delayConst.

It continues to occur even if I block out large sections of code.

Although I used the name "delayConst", actually, I need this delay to vary throughout the code (be added and subtracted to). So that's why this is a problem.

\$\endgroup\$
0

1 Answer 1

8
\$\begingroup\$

__delay_ms and __delay_us are macro's. They are expanded at compile time by the preprocessor and thus only accept constants as parameter.

If you need a simple variable delay just create a function that executes a fixed delay in a loop:

//Delay n times 10 ms...
void MyDelay(uint8_t n)
{
    for (; n > 0; n--)
    {
        __delay_ms(10);
    }
}
\$\endgroup\$
5
  • 1
    \$\begingroup\$ it might be worth noting that these are going to simply block your CPU for a while, instead of just going to sleep for a defined period, saving a lot of power, or doing some other task in the mean time. If your code needs multiple places where you delay, and then you check some peripheral, and then you update some value, chances are you should really not be using __delay_ms and similar functionality, but a small, compact RTOS (which is really just not much code, just the bare minimum necessary to allow for going to sleep or doing some enqueued other task "while you wait". Now, sadly, \$\endgroup\$ Commented Aug 26, 2022 at 14:00
  • 1
    \$\begingroup\$ PIC10 is really a very <s>terrible</s> minimal core, and I'm not aware of any non-questionable RTOS that was ported to the 400 B of memory it has. Which would mean a good own implementation of go_to_sleep_ms(int x) would need to set up a timer, and go to sleep, itself. \$\endgroup\$ Commented Aug 26, 2022 at 14:02
  • \$\begingroup\$ Marcus, is there any way to count time using a while/for loop and a counter? (Knowing the clock speed of the controller, and perhaps additional information in MPLABX). Reason I ask is, I don't need a very precise delay. And like you said, I could be doing some other task (checking for button presses rather than blocking the CPU). But most importantly, it may also reduce the code size. Sleep in this scenario is actually not that helpful as I need to keep a GPIO pin high \$\endgroup\$
    – Mr Man
    Commented Aug 26, 2022 at 18:07
  • \$\begingroup\$ I believe it's called "STOPWATCH" in MPLABX debugging tools. \$\endgroup\$
    – Mr Man
    Commented Aug 26, 2022 at 18:13
  • 1
    \$\begingroup\$ @MrMan Yes, with the hardware timer. It can count clockcycles, with or without prescaler. \$\endgroup\$ Commented Aug 26, 2022 at 18:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.