# help : write a microcontroller program from matlab

#### walid el masry

Joined Mar 31, 2009
132
is that true that i can write a program to a microcontroller from matlab tool box cause some program will be easy to be written with matlab tool box instead assembly and c either and if that possible is there any examples for this operation assuming we program for pic microcontroller take PIC16F877A as an example

#### russ_hensel

Joined Jan 11, 2009
825
I doubt it unless you mean use it as a text editor.

#### walid el masry

Joined Mar 31, 2009
132
no no not like a text editor what i mean is similar to what matlab 2009 do with FPGA when it convert what i wrote in matlab to VHDL code so that i can download it on FPGA so what i want here is to write a program in matlab specially using the simulink or tool box of matlab and then download it on a microcontroller

#### Papabravo

Joined Feb 24, 2006
12,497
I know of no such product. In part this is due to the extreme mismatch between matlab operations and the capabilities of a typical microcontroller which may have to do a multiply or divide without the benefit of a hardware instruction. In addition the limited amounts of RAM would make matrix operations quite limited.

There may be interpreted languages that are suitable for download and execution on a microcontroller, but matlab and simulink would not be my first choice.

What are you trying to accomplish exactly?

#### walid el masry

Joined Mar 31, 2009
132
about what i want to do i intent to measure the frequency , capacitance & inductance and i have been told that matlab simulink have a stong ability to do that so i thought if i wrote the program on matlab and download it on a microcontroller it will be a great way with a high efficiency to accomplish that

#### Chaabane

Joined Nov 30, 2009
37
Hello,
read this,its the same question i guess

what i know is that you can use simulink to interfere with your real system (AC motor,...) .
Using a system called Dspace , so you can simulate filters,modulators,... in simulink and control a real system.
but you can use a pic with simulink,for example:you need to send a feedback to your pic, using an expensive non-linear filter,and you're not sure if this will work or not,why wasting money !! use simulink to simulate this filter...

#### walid el masry

Joined Mar 31, 2009
132
what i know is that you can use simulink to interfere with your real system
what i want to do is to interface the real system usin a microcontroller with a program written with simulink and burnt on the microcontoller

you can use a pic with simulink,for example:you need to send a feedback to your pic
you mean that the feedback is done by a program written b simulink and executed on the pc but what i want is to reduce the need of the pc and depend only on the microcontroller to reduce the size and cost

what i really want is the feature of the matlab using the microcontroller

#### Papabravo

Joined Feb 24, 2006
12,497
I'm sorry but I think this is a quest which is not grounded in reality. If you can figure out a way for a microcontroller to run matlab or simulink code you'll be well on your way to a promising career.

#### walid el masry

Joined Mar 31, 2009
132
i don't mean it that way i mean run matlab or simulink code with the limitation of the hardware of the microcontroller will better 100 times from writing a program from the scratch in assembly even in c it is like using the predefined library in c instead of writing your own in assembly even in c

#### Papabravo

Joined Feb 24, 2006
12,497

Matlab and Simulink are interpreted languages. What does this mean? It means there is a program, the matlab interpreter, that takes as input a string representing a statement. An example would be the definition of a 3x3 matrix
Rich (BB code):
A = [1, 2, 3 ; 4, 5, 6 ; 7, 8, 9]
From this string the matlab interpreter would define and initialize a data structure to contain the 3x3 array named "A". Next consider a statement to take the inverse of A. The matlab interpreter would look at the string:
Rich (BB code):
AINV = inv(A)
or
AINV = A^-1
and invoke the intrinsic procedure for inverting the matrix A

In order for matlab to be executable on a microcontroller there must either be a matlab interpreter that runs on the microcontroller that can interpret matlab statements, or there must be a compiler on the PC which can compile matlab statemnts into executable code for the microcontroller of interest. There are no other ways of doing this.

What I am saying is that there are unlikely to be matlab interpreters for microcontrollers, and there are unlikely to be compilers that will compile matlab statements into executable code for microcontrollers. If there is a counter example to my assertions then let the lucky party please step forward and help the OP.

#### Nanophotonics

Joined Apr 2, 2009
383
I'm not aware of any compiler that translates matlab codes into executable codes for microcontrollers. I know one can use matlab codes to interact with microcontrollers and that with the help of a PC. Matlab isn't a programming language like others in a sense but is rather a numerical computing environment. The OP is probably new to the field.

#### Papabravo

Joined Feb 24, 2006
12,497
Matlab is a language which is highly optimized for matrix operations. It is really really poor at doing the loops that are common in microcontroller code. The reason for this is that each statement in a loop has to be recompiled each time the statement is executed. In a loop that runs a few million times that can amount to considerable overhead. I became aware of this matlab shortcoming while trying to program the Crank-Nicolson algorithm to solve financial derivative PDEs. Using loops required the program to run for several hours. Using matrix operations shortened that time considerably.

http://en.wikipedia.org/wiki/Crank%E2%80%93Nicolson_method

#### aznblood

Joined Jan 13, 2010
1
Hi, I used to use simulink to design a PID controller. But that was all theory when i was in college getting my degree in mechanical engineering. I am very comfortable in programming Atmel microcontrollers in C or assembly, after having gained real work experience in the software industry. However, nobody ever taught me on integrating a PID controller on a uC. For example, how the software is designed using matrix/array to compute results from transfer functions. Would anyone please explain how it is done, or at least give me a how-to link. Thank you.

#### Dragonblight

Joined Aug 19, 2009
35
If you're simply looking into easy coding of microcontrollers, I suggest the picaxe. I haven't read up a whole lot into it, but it supports flow-charts and basic statements in a native manner.

#### nescartel

Joined Mar 10, 2010
1
i've actually used Matlab Simulink to create & simulate applications and later convert into C code and later into hex file. this hex file is then flashed into a 32-bit fujitsu microcontroller. but this 'bridge' between Matlab and microcontroller hardware is developed exclusively for Siemens. what i am trying to say is it is not impossible, but i'd say it is really hard to do.