Report
3 months 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);
}
Comment on this question

2 Answers

3 months 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/

3 months ago.

Hello Shane,

Below is one way how to do it. I tried to substitute the Arduino code (commented lines) with similar MBED one.

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);
        }
    }
}


To post an answer, please log in.