I am writing a program that uses timer1 in combination with interrupts to generate a square wave at a user-settable frequency.
I had to disable timer0 because the interrupts used by the millis() micros() etc. functions were interfering with my signal generation on timer1 (sometimes, a timer overflow was not caught by the interrupt routine, and pulses were missed. I guess that the arduino's timing functions make use of cli()...). Now that I disabled timer0, everything is working fine and I do need these timing functions for my program anyway.
However, I now would like to add a LCD display and use the LiquidCrystal library. I wasn't able to find information to know whether this library requires the timing functions based on timer0.
In short, if I disable timer0, can I use the LiquidCrystal library?
The library that I am using has functions that include calls to delay() and delayMicroseconds(). I guess those functions interfere with Timer0 but I am not sure. You can check the library that you will use and see which functions include delays. Maybe you can avoid them.
This is an example function from the library that I found.
void LCD4x20::pulseEnablePin(){
digitalWrite(_enablePin,LOW);
delayMicroseconds(1);
// send a pulse to enable
digitalWrite(_enablePin,HIGH);
delayMicroseconds(1);
digitalWrite(_enablePin,LOW);
delay(1); // pause 1 ms. TODO: what delay, if any, is necessary here?
}
On the worst case, you would need to change all delay functions to equivalent dummy loops. That is the best I can offer.
Ah thanks,
So it appears that the LCD library needs the timing functions. As you say, I can modify every calls to delay() etc. with some waiting loops. I will try this...
guybrush:
Ah thanks,
So it appears that the LCD library needs the timing functions. As you say, I can modify every calls to delay() etc. with some waiting loops. I will try this...
unsigned long us1, us2;
volatile int a=0;
void setup() {
Serial.begin(9600);
}
void loop() {
us1=micros();
a++;// a delay from 1 to 4 microseconds
us2=micros();
Serial.print("start:");
Serial.print(us1);
Serial.print(" end:");
Serial.println(us2);
}
I 'll need the same thing as well. So I made a simple test. I guess " a++; " takes 2 microseconds. Because if I write just 1 line of " a++; " delay is between 1 and 4 microseconds ( we can't be more precise due to micros() function ). But if I write 3 lines of " a++; " the delay is between 4 and 8 microseconds. So 2 microseconds is probably close enough.
Upon inspecting this issue further, I realised that incrementing a volatile variable takes less than 1 microsecond if you use " a++; " more than once. But if you use a for loop instead, it takes about 1 microsecond due to loop instructions. For example, writing " a++; " 45 times produces a huge code but it takes 32 microseconds to run. Using a for loop instead does the same thing with much less code and each iteration is much closer to 1 microsecond.