1

Is it possible to redefine delay and delaymicros function with #define ? I want to use timer2 interrupt instead of these.

Something like this:

#define delay(unisgned long ms) countMicros(uint16_t top, uint16_t storageVar)

setup(){}

loop(){
 delay();// function countMicros is called instead.
 }
void countMicros(uint16_t top, uint16_t storageVar){
//...}
6
  • Can you tell us why ? Have you heard about the XY-problem ? xyproblem.info
    – Jot
    Sep 18 '17 at 9:05
  • @Jot Now I have ! I want to use timer interrupt for several time-sensitive calculations. One is to know when exactly to turn on TRIAC on 50 Hz AC system. Second is time after turning it off. The problem is with LiquidCrystal library which I have for LCD. It uses delay, so by initializing ISR with 1uS counter nothing works.
    – flowian
    Sep 18 '17 at 11:01
  • An ISR works also when a delay is busy. You should avoid Timer0, because the Arduino uses it. You can use Timer1 and Timer2 of an Atmega328p (Arduino Uno). If you need more timers, then use the Atmega32u4 (Arduino Leonardo / Micro). You are looking in the wrong direction for the wrong solution. I think this is indeed a XY-problem.
    – Jot
    Sep 18 '17 at 14:05
  • With timer2 set it does not work with liquidcrystal functions however, when the library with functions are commented out everything works fine. So I concluded that delay in LCD library is the cause. The problem is following: red led flashes fast and LCD is frozen, when timer2 ISR is set.
    – flowian
    Sep 19 '17 at 5:18
  • The LiquidCrystal library does not use Timer2 and has nothing to do with it. Is your ram memory almost full ?
    – Jot
    Sep 19 '17 at 18:24
1

Not exactly like that. You will have to manage the different sets of parameters, i.e. convert the incoming ms into the top and storageVar that would be appropriate for calling countMicros(). Maybe something like this:

#define delay(ms) mydelay(ms)

void mydelay(unisgned long ms) {
    uint16_t top = some expression;
    uint16_t storageVar = another expression;
    countMicros(top, storageVar);
}

where both expressions are a function of ms.

If the expressions are really simple, you could even put the whole thing inside a single macro, as in

#define delay(ms) countMicros(some expression, another expression)
2
  • Do parameters in original function have to match ones in a custom one, as you have shown in the beginning of your post ? #define delay(ms) mydelay(ms) I assume that in this case parameters passed in original function will get passed into custom function. If that is so, what happens in case of your last example ? #define delay(ms) countMicros(some expression, another expression)
    – flowian
    Sep 18 '17 at 11:13
  • @flowian: You should realize that the preprocessor only does textual substitution. If you #define delay(ms) countMicros((ms)*2, (ms)-4) and then you call delay(7), then the compiler will just see countMicros((7)*2, (7)-4). Sep 18 '17 at 12:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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