Report
5 years, 11 months ago.

How generate a random signal using mbed NXP LPC1768

Hello please could anyone give me an hint on how to start. i need to generate a random signal using mbed which should work like this:

it should be between 0-3.3volts. with offset of 0.1volts.

that is, the highest voltage should be 3.2volt and lowest 0.1volts, anything above 3.2v or below 0.1v should be read as noise.

thanks in advance

Comment on this question

1 Answer

5 years, 11 months ago.

Try this:

#include "mbed.h"

AnalogOut signal(p18);      //Only for p18
Serial pc(USBTX, USBRX);    //Debug

int main()
{
    float min = 0.1 / 3.3;  //Define 0.1 V as ~03% from 3.3 V
    float max = 3.2 / 3.3;  //Define 3.2 V as ~97% from 3.3 V

    while(1) 
    {
        signal = min + ((float)rand()/RAND_MAX) * (max - min);  // Generate random voltage output from 0.1 to 3.2
        printf("Voltage: %1.2f [V]\n", signal.read()* 3.3);     // Debug
        wait(1);                                                // Time to keep random voltage before next change
    }
}

EDIT: Repair rand() and added debug messages

Did you try if this code is really working?

I thought AnalogOut has a range of 0.0 to 1.0 where 0.0 = 0.0V and 1.0 = 3.3V. Am I wrong?

Also I am missing an initialization of random. Normally you use srand(time(NULL)); for this purpose.

posted by David Golz 12 Jul 2013

Yes it really works, but I programmed this on paper without testing. The only thing I changed is small mistake at rand() (added devide by RAND_MAX), because I thought that rand() returns 0.0-1.0. And added debug messages for comparison with measured data.

posted by Martin Wolker 12 Jul 2013

I tested it also. It's really working. I am a little confused, because the class reference tells me that the range is 0.0 to 1.0.

Look here: https://mbed.org/users/mbed_official/code/mbed/docs/b3110cd2dd17/classmbed_1_1AnalogOut.html#a04593bbcefdddb53406c0943a711f293

Anyway your code looks better now.

posted by David Golz 15 Jul 2013

Reference is okay. I apologize if this isn't obvious at first sight, but i realy use range 0.0-1.0. There is a hidden conversion from real volts to percentages of 3.3V (0.0-1.0).

posted by Martin Wolker 15 Jul 2013

Oh yes, now I get it. I think I missed the divisions at line 8 and 9. I hope the questioner could solve his problem with your helpful code.

posted by David Golz 17 Jul 2013

To post an answer, please log in.