Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
6 years, 2 months ago.
Ticker v Timeout priority
I have three types of interrupt in some nascent code below: Ticker, Timeout, pushbutton interrupt. I'm using the Timeout to turn off a LCD backlight. I'm using a Ticker to call a function to sample an ADC input periodically.
I have assigned (I think) the lowest priority to the pushbutton. The next lowest priority should be Timeout because I don't want the Timeout to be dealt with if it is going to change the timing between samples. Please could somebody suggest a modification to the code to make Timeout a lower priority than Ticker?
Is there a more elegant way to switch off the backlight after a delay? Or is my approach ok? Thank you.
Turn off LCD backlight after 3 seconds
#include "mbed.h" #include "TextLCD.h" TextLCD lcd(p28,p27,p26,p25,p24,p23); // LCD 4bit bus: rs, e, d4, d5, d6, d7 InterruptIn pushbutton(p30); //Switch pulls up pin 30 when pressed AnalogIn signalIn(p20); //ADC input Ticker sampleTicker; //sets the sample rate DigitalOut backlight(p22); //Backlight turned on by FET (gate on pin 22) Timeout backlight_duration; float ADCdata; void backlightOff(void) { backlight=0; } void backlightOn(void) { backlight=1; backlight_duration.attach(&backlightOff, 3.0); } void sampleInput(void){ ADCdata = signalIn; //Add value to memory buffer } int main(){ backlight=0; NVIC_SetPriority(EINT3_IRQn, 255); //Lowest priority setting to "InterruptIn" lcd.printf("Hello World!\n"); pushbutton.rise(&backlightOn); while(1) { // other stuff happening } }
1 Answer
6 years, 2 months ago.
Hello Francis,
You might consider using of the EventQueue API. For example as below:
#include "mbed.h" #include "TextLCD.h" TextLCD lcd(p28,p27,p26,p25,p24,p23); // LCD 4bit bus: rs, e, d4, d5, d6, d7 InterruptIn pushbutton(p30); //Switch pulls up pin 30 when pressed AnalogIn signalIn(p20); //ADC input Ticker sampleTicker; //sets the sample rate DigitalOut backlight(p22); //Backlight turned on by FET (gate on pin 22) EventQueue queue(32 * EVENTS_EVENT_SIZE); Thread t; float ADCdata; void backlightOff(void) { backlight=0; } void backlightOn(void) { backlight=1; queue.call_in(3000, backlightOff); } void sampleInput(void){ ADCdata = signalIn; //Add value to memory buffer } int main(){ t.start(callback(&queue, &EventQueue::dispatch_forever)); backlight=0; lcd.printf("Hello World!\n"); pushbutton.rise(queue.event(backlightOn)); while(1) { // other stuff happening } }
Zoltan - thank you! And I really appreciate you modifying my code, which gives me a head start. EventQueue is completely unknown to me so I will research how it works on the API link you posted.
I tried compiling your modified code and the online compiler reported back a compiler error 20 at line 9: Error: Identifier "EventQueue" is undefined in "main.cpp", Line: 11, Col: 2
Am I missing a library file to be included at the top of the code?
Thanks again!
posted by 02 Oct 2018That's weird. I used the online compiler too with the latest mbed-os version and mbed LPC1768 as target board. However, to avoid importing the LCD library into my project, I commented out lines 2, 4 and 32. The project compiled smoothly with no errors.
No library have to be imported. But make sure that you are using mbed-os 5. There is no EventQueue API available in mbed-os 2!
Thank you for replying again. My problem is that I'm using the mbed online compiler, unfortunately I have no access to an offline compiler so I'm stuck with os-2. Appreciate your solution though, Zoltan.
posted by 03 Oct 2018
It works! Thank you Zoltan. I will experiment some more. (os-5 issues lots of warning messages about the TestLCD library, although it still compiles.)
Appreciate your help and interest regarding my question.
posted by S Francis 04 Oct 2018