MATLAB: How to program microcontroller in matlab

microcontroller programmingrs232

Hello,
I am new to matlab, so help me out a bit here. I have a 3 axis controller (X,Y,Z) that I am trying to program using Matlab via RS232. So I know how to open the port and set the settings to the port. What I don't know, or I am having trouble understanding is how to send the commands to control each axis. According to the manufacturer the command to set the output for the X,Y and Z axis are XV, YV, and ZV. I know I can use the fprintf to write data in the ucontroller,, but how do you tell it to output lets say XV = 10 to move the X axis to that point. Is this even possible in matlab?. Thanks in advance.
s = serial('COM1')
set(s, 'BaudRate', 115200)
set(s, 'DataBits', 8)
set(s, 'Parity', 'none')
set(s, 'StopBits', 1)
set(s, 'FlowControl', 'none')
fopen(s)

Best Answer

You create s in the beginning of your code, which is a serial port object. It can be used as any file handle, in particular with fprintf() as mentioned in your question. If you tried, e.g. with
fprintf(s, 'XV=10') ;
.. but the command was not taken into account by the microcontroller, my first guess would be that you don't send the correct 'terminator'. See serial.fprintf and your device manual. Another thing that you could do is to read on your port to check whether you get some information, either after sending commands, or when you power your device on.. just to be sure that the device is not inert.
Also, you don't "program a microcontroller" per se. The microcontroller was programmed with a code (a tiny "operating system" stored in flash, e2prom, or any kind of non-volatile memory) that is loaded at power on and manages serial communication as well as the electronics for driving motors on your device. This OS understands a set of commands (like XV=..) that was defined by people who built the device. So you don't want to "program a microcontroller in MATLAB", but send these "high level commands" to the device in the correct format.
EDIT: if you need to fine tune your management of serial port/communications, look at <http://www.mathworks.com/help/matlab/serial-port-devices.html>
Related Question