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 ago.
Arduino code to mbed
Hi, can you please convert the following code to mbed. Thank you!
#include <LiquidCrystal.h> LiquidCrystal lcd(2, 3, 4, 5, 6, 7); int Htime; //integer for storing high time int Ltime; //integer for storing low time float Ttime; // integer for storing total time of a cycle float frequency; //storing frequency void setup() { pinMode(8,INPUT); lcd.begin(16, 2); } void loop() { lcd.clear(); lcd.setCursor(0,0); lcd.print("Frequency of signal"); Htime=pulseIn(8,HIGH); //read high time Ltime=pulseIn(8,LOW); //read low time Ttime = Htime+Ltime; frequency=1000000/Ttime; //getting frequency with Ttime is in Micro seconds lcd.setCursor(0,1); lcd.print(frequency); lcd.print(" Hz"); delay(500); }
2 Answers
6 years ago.
You didn't specify which LCD you want to run or which mbed target you are using. Is this the Hitachi HD44780 part? This mbed library says it is use for that chip - could give that a try:
https://os.mbed.com/users/simon/code/TextLCD/
Mbed targets with LCD screens exists, using one of those might be the path of least resistance. Project wizards are generally available that will give you a working project. For example:
https://os.mbed.com/platforms/ST-Discovery-L476VG/
6 years ago.
Hello Shane,
Below is one way how to do it. I tried to substitute the Arduino code (commented lines) with similar MBED one.
- You should import the https://os.mbed.com/users/wim/code/TextLCD/ library into your project before compiling it.
- See also the https://os.mbed.com/users/wim/notebook/textlcd-enhanced/ page for more information on how to use LCDs with mbed.
NOTE: Please notice that I haven't tested the code with a real mbed board.
#include "mbed.h" //#include <LiquidCrystal.h> #include "TextLCD.h" //int Htime; //integer for storing high time //int Ltime; //integer for storing low time //float Ttime; //integer for storing total time of a cycle volatile int hTime; //integer for storing high time volatile int lTime; //integer for storing low time int tTime; //integer for storing total time of a cycle float frequency; //storing frequency volatile bool dataAvailable = false; //flag to indicate that new data is available volatile bool hTimeAvailable = false; //flag to indicate that new high time is available volatile bool riseDetected = false; //flag to indicate detection of signal rising edge //LiquidCrystal lcd(A2, A3, A4, 4, 3, 2); // RS, E, D4, D5, D6, D7 TextLCD lcd(A2, A3, A4, D4, D3, D2, TextLCD::LCD16x2); // RS, E, D4, D5, D6, D7 /* void setup() { pinMode(8,INPUT); lcd.begin(16, 2); } */ InterruptIn input(D8); //signal input pin Timer timer; //timer used for measurement /** * @brief Interrupt Service Routine (ISR) * @note Called on input signal rising edge * @param * @retval */ void onRise() { lTime = timer.read_us(); //read input signal's low time timer.reset(); //reset the timer and start measuring high time riseDetected = true; //flag that signal rising edge was detected dataAvailable = hTimeAvailable; //flag that new data is available hTimeAvailable = false; //reset the flag for next use in falling edge ISR } /** * @brief Interrupt Service Routine (ISR) * @note Called on input signal falling edge * @param * @retval */ void onFall() { if (riseDetected) { hTime = timer.read_us(); //read input signal's high time timer.reset(); //reset the timer and start measuring low time riseDetected = false; //reset the flag for next use in rising edge ISR hTimeAvailable = true; //flag that 'high' time is available } } int main() { //setup(); input.rise(&onRise); //attach a function to be called on signal rising edge input.fall(&onFall); //attach a function to be calle on signal falling edge lcd.cls(); lcd.printf("Connect a signal"); timer.start(); //void loop() while (true) { //check whether a measurement is complete if (dataAvailable) { dataAvailable = false; //clear the flag for next use in rising edge ISR //lcd.clear(); //lcd.setCursor(0,0); //lcd.print("Frequency of signal"); lcd.cls(); lcd.printf("Signal frequency"); //hTime=pulseIn(8,HIGH); //read high time //tTime=pulseIn(8,LOW); //read low time tTime = hTime + lTime; frequency = 1000000 / (float)tTime; //lcd.setCursor(0,1); //lcd.print(frequency); //lcd.print(" Hz"); lcd.locate(0, 1); lcd.printf("%f Hz", frequency); //delay(500); wait_ms(500); } } }
this one is good answer.. can you make it for machine learning too sir.. I have several questions https://os.mbed.com/questions/80922/KNN-using-c-for-nRF51822-any-suggestion-/ https://os.mbed.com/questions/80921/How-to-convert-arduino-code-to-mbed-onli/ https://os.mbed.com/questions/80920/How-to-make-K-Neareset-Neighbor-possible/
please help me too sir..
posted by 17 Apr 2018