Jump to content
43oh

Using MSP430 as a relay between a function generator and MATLAB


Recommended Posts

Hello, 

 

Problem Statement: I am trying to read in a square waveform from a function generator using the MSP430G2553 and whenever there is a falling edge the chip has to send out a ping to my laptop (I am using serial communication here for lack of knowledge on a better way). I then have to use MATLAB to read this "ping" coming from the MSP430G2553 and trigger another device.

 

By reviewing some basics on serial communication and examples on the Energia documentation, I am able to generate a ping solely from the MSP430 chip (without any input from the function generator) and cause MATLAB to read it. However I am a little lost on two parts of the problem.

 

1) How can I make the MSP430 accept the waveform that is continuously generated from the function generator (which is a TTL) and then output the necessary to the laptop. In other words, how can I interface the MSP430 launchpad to the function generator so that I can have a continuous input from the generator into the launchpad and then send the "ping" for every falling edge that is detected?

 

I have zero experience in serial communication and the very little I learnt from reading hasn't quite helped.

 

2) How can I make MATLAB continously look for the "ping" from the MSP430 chip. (irrelevant to this forum may be? I can work on figuring this out myself.)

 

 Any help will be greatly appreciated.

Link to post
Share on other sites

How you implement this will depend on the expected frequency of your square wave. You would need different implementations for 10 kHz signal vs a 0.01 Hz signal. A slower signal will be lot easier to do as the time jitter will be negligible.

 

As for input signal, TTL is usually 0 to 5V which will nicely fry the GPIO pins on the MSP430 so you will need to do level shifting/converting.

 

As for the program, you can use the attachInterrupt function. See http://energia.nu/AttachInterrupt.html

 

Attach your interrupt routine to a GPIO pin (can pick rising or falling). This routine can send a message through the serial port using Serial.print().

 

As for interfacing to MATLAB, I haven't used MATLAB in ages so I don't know. I would imagine you would need MATLAB to monitor the serial port (one of the COM ports in Windows or one of the /dev/tty* devices in linux/mac).

Link to post
Share on other sites

see file:http://energia.nu/AttachInterrupt.html

use this function:

attachInterrupt(interrupt, function, mode);

first change the intrrupt with your input pin and then

in place of mode put: RISING

and in place of function write you function which one you want to call

here you want to send data using serial port so  write inside your function :

void function(void){
 Serial.println("High Pulse");
}

so your final program is like:

void setup()
{
 Serial.begin(9600);
 attachInterrupt(P2_2, serial_call, RISING);
  
}

void loop()
{
  // put your main code here, to run repeatedly:
  
}
void serial_call(void)
{
  Serial.println("HIGH PULSE");
  delay(100);
}
Link to post
Share on other sites

Thank you very much.

 

Yes, I am using a voltage divider circuit to prevent the pins from frying. :-) It basically isn't a continuous square wave, the generator will be outputting two squares with a 10ms interval or less and then this pair of squares will be repeated after a couple of seconds. So do you think that would be fine?

 

I will try out the "attachinterrupt" now. Thank you very much to the both of you for your help.

Link to post
Share on other sites

While this might technically work for slow signals, using Serial.println() and delay() in an interrupt routine is a bad idea as it will prevent receiving any other interrupts during that time.

The example provided by @@srv is a good start and I would adapt it as following:

- declare a global variable: volatile int pulse;
- set it to 0 in setup() before you configure the interrupt
- in the interrupt routine, set it to 1
- in the loop add:

if(pulse)
{
  pulse = 0;
  Serial.write('H');
}

Still, at 9600 baud Serial might be too slow for your case. Sending one character takes at least 1ms. If the next signal comes faster than 10ms you will experience significant delays or miss signals.

 

For faster serial communication the MSP430F5529 LaunchPad could work.
 

Link to post
Share on other sites
Yes, I am using a voltage divider circuit to prevent the pins from frying. :-) It basically isn't a continuous square wave, the generator will be outputting two squares with a 10ms interval or less and then this pair of squares will be repeated after a couple of seconds. So do you think that would be fine?

 

Depends. Do you care about the timing between the <10 ms pulses? If yes, then your approach will probably not work unless you really don't care about latency/jitter. If you are only concerned between the seconds between the pulse pairs, then you are fine.

 

If you want to accurate measurement of the time between the <10 ms pulses, have the microcontroller measure the interval-- can be measured with sub microsecond precision (and accuracy if you properly calibrate).

Link to post
Share on other sites

Hello all,

 

Thank you very much for your insights. I think for the most part I have been able to work with the 9600 baud rate which basically mains outputting a character approximately every 1ms. This so far I believe is sufficient for my needs, though eventually I might need a higher baud rate chip (like the MSP430F5529 that was suggested.)

 

I am currently trying to fix the Matlab aspects of the problem and when will test out this setup. I will keep you updated on my progress.

 

Thank you very much again.

 

P.S: Oh...and yes, I will need to detect the falling edges for each of the pulses that form the pair as well. (i.e the two pulses that are spaced <10ms in time will also have to produce an output onto the serial port.) But like I said, I think so far it seems to be working fine. Will know once I actually test it to completion along with the MATLAB part of the setup. Thanks again.

Link to post
Share on other sites

Hello all,

 

Thank you very much for your insights. I think for the most part I have been able to work with the 9600 baud rate which basically mains outputting a character approximately every 1ms. This so far I believe is sufficient for my needs, though eventually I might need a higher baud rate chip (like the MSP430F5529 that was suggested.)

 

I am currently trying to fix the Matlab aspects of the problem and when will test out this setup. I will keep you updated on my progress.

 

Thank you very much again.

 

P.S: Oh...and yes, I will need to detect the falling edges for each of the pulses that form the pair as well. (i.e the two pulses that are spaced <10ms in time will also have to produce an output onto the serial port.) But like I said, I think so far it seems to be working fine. Will know once I actually test it to completion along with the MATLAB part of the setup. Thanks again.

You still have not mentioned whether you are interested in the short time between the pulse pairs or whether you are interested in the timing between he distant pulse pairs or both. Choosing a faster micro controller will only get you so much. If you utilize the timer on the MSP430G2553, you can measure the short pulses with sub-microsecond resolution and then send this time over the serial port to MATLAB.

 

Even  if you switch to the 5529 chip, there will be all sorts of latency and jitter issues introduced from the micro controller to the debugger (conversion to USB), and then all the latency issues involved in handling the USB packets once received in your computer. If you are happy with latency/jitter on the order of 10 to 100 ms or more, then this is fine. But if you utilize the on-board timer, you can get 4-5 orders of magnitude better resolution/accuracy.

Link to post
Share on other sites

I am sorry. To answer your question in short, yes I would like to be able to have the microcontroller respond to pulses that are separated < 10ms in time as well as when they are separated by seconds. (that's because they are repeated after usually a delay of 1 or 2 seconds).

 

Basically the pulse pair is generated from an MR scanner which repeats the sequence after a set amount of time (TR).  Within this TR (usually in seconds), I would be receiving the pair of pulses which are generally separated by a time delay in the order of ms. (usually <10ms).

 

So the general sequence goes...

 

*TR1begin* PulseA...(<10ms)...PulseB.....................................................*TR2begin*PulseC.....(<10ms).....PulseD.............................

 

I would need the microcontroller to basically react to all these incoming pulses (PulseA, PulseB, PulseC, PulseD) and output onto the serial port each time it detects a pulse. This is then read by MATLAB.

 

I think with a baud rate of 9600 (basically output of 1 character per 1ms) this serves what I want to do. I would however be limited on how quickly MATLAB can process the information on the serial port and trigger the third device in time. Isn't that correct? So basically MATLAB will be working as a relay between the microcontroller's detection and the third device causing them to work in sync. (obviously there is a lag, but I want to minimize this as much as possible while I am still able to detect each incoming pulse.)

 

 

I am sorry but did I do a better job explaining the problem?

Link to post
Share on other sites

I am sorry. To answer your question in short, yes I would like to be able to have the microcontroller respond to pulses that are separated < 10ms in time as well as when they are separated by seconds. (that's because they are repeated after usually a delay of 1 or 2 seconds).

 

Basically the pulse pair is generated from an MR scanner which repeats the sequence after a set amount of time (TR).  Within this TR (usually in seconds), I would be receiving the pair of pulses which are generally separated by a time delay in the order of ms. (usually <10ms).

 

So the general sequence goes...

 

*TR1begin* PulseA...(<10ms)...PulseB.....................................................*TR2begin*PulseC.....(<10ms).....PulseD.............................

 

I would need the microcontroller to basically react to all these incoming pulses (PulseA, PulseB, PulseC, PulseD) and output onto the serial port each time it detects a pulse. This is then read by MATLAB.

 

I think with a baud rate of 9600 (basically output of 1 character per 1ms) this serves what I want to do. I would however be limited on how quickly MATLAB can process the information on the serial port and trigger the third device in time. Isn't that correct? So basically MATLAB will be working as a relay between the microcontroller's detection and the third device causing them to work in sync. (obviously there is a lag, but I want to minimize this as much as possible while I am still able to detect each incoming pulse.)

 

 

I am sorry but did I do a better job explaining the problem?

How computation intensive is MATLAB's job? If the third device is a physical device and not a virtual computer device, why don't you interface the MSP430 directly to the "third device"? Using a MSP430 micro controller followed  by a computer running MATLAB to interface a MRI to a "third device" seems a bit Rube Goldberg. Why can't you go MRI -> MSP430 -> "third device?" If the computations involved are complicated, use a Tiva (ARM) core that is orders of magnitude faster at floating point. I guess it depends on what the physical interface to the "third device" is, but if it can be directly connected to a micro controller, get rid of the computer in between. If you need data-logging on MATLAB, you can still send data through the serial port to a computer running MATLAB.

 

If you are just computing time between pulses with MATLAB, this is something that can easily be done with sub-microsecond resolution by a micro controller. You can then send the timer intervals over the serial port.

 

Problem with trying to measure time of arrival using a serial port is that the resolution and accuracy are going to be horrible-- the latency/jitter may be on the order of several ms.

Link to post
Share on other sites

I assume you considered the option of doing away with the MSP430 and just running your signal into a sound card (assuming running Matlab on a PC or something

similar that has sound inputs)?

 

I don't know that much about Matlab, but if it doesn't have the werewithall to detect edges on an audio input, undoubtedly there is something else up to that.

Since sound systems are usually designed for higher speed signals and to minimize latency/slew/etc. this seems like might be an easier way into the PC

(assuming you can find some software to quickly throw away all the excess data a sound system would generate in this application.)

 

Would have to check impedence/voltage/etc. requirements, but shouldn't be too hard to massage signal so wouldn't fry an audio line in.

Link to post
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...