Difference between revisions of "C Code Simulation"
(6 intermediate revisions by 4 users not shown) | |||
Line 1: | Line 1: | ||
− | + | Flowcode can simulate C code inside C Code icons. Note, in order to use this feature you will need to have purchased the Professional or Academic license. | |
+ | |||
+ | This feature can be great for debugging custom C code within your project, prior to compilation to a microcontroller. It is also invaluable as a teaching tool and allows you to teach students the effects of certain C language statements on variables. When simulating C code, you can step through individual statements within a block in the same way that you step through multiple icons in your Flowchart. | ||
− | |||
Line 76: | Line 77: | ||
− | However, the behaviour of these variable types cannot be guaranteed to be the same between various compiler families. Therefore, we recommend that you use the Flowcode types from the above table in order to keep your code as portable | + | However, the behaviour of these variable types cannot be guaranteed to be the same between various compiler families. Therefore, if you plan on compiling your code to a microcontroller, we recommend that you use the Flowcode types from the above table in order to keep your code as portable. On the other hand, the native C data types can be useful for educational purposes. |
===Accessing variables created in Flowcode's UI=== | ===Accessing variables created in Flowcode's UI=== | ||
− | You can easily modify and read the values of variables that you have created in the user-interface (for example, using the Project Explorer). Variables are referred to using purely | + | You can easily modify and read the values of variables that you have created in the user-interface (for example, using the Project Explorer). Variables are referred to using purely uppercase, and with a special prefix depending on whether they are global or local variables. |
{| class="wikitable" | {| class="wikitable" | ||
Line 98: | Line 99: | ||
|} | |} | ||
+ | |||
==Calling Flowcode functions and your own Macros== | ==Calling Flowcode functions and your own Macros== | ||
Line 177: | Line 179: | ||
|Sets the function sets the value of the pins of port w given by the mask x, to z, after left-shifting z by y positions | |Sets the function sets the value of the pins of port w given by the mask x, to z, after left-shifting z by y positions | ||
|} | |} | ||
+ | |||
===User-defined macros=== | ===User-defined macros=== | ||
Line 187: | Line 190: | ||
Note that we do not have to make the "Func" part of the expression uppercase, unlike when referring to variables. | Note that we do not have to make the "Func" part of the expression uppercase, unlike when referring to variables. | ||
+ | |||
==Example== | ==Example== | ||
Line 198: | Line 202: | ||
The C Simulation feature is similar to the [[C_Code_to_Flowchart_Conversion|C Code to Flowchart Conversion]] feature that converts C code into Flowchart icons. | The C Simulation feature is similar to the [[C_Code_to_Flowchart_Conversion|C Code to Flowchart Conversion]] feature that converts C code into Flowchart icons. | ||
+ | |||
+ | To view all the c code ,ither hover over the C code box as shown above, or double click on it. |
Latest revision as of 10:23, 5 October 2023
Flowcode can simulate C code inside C Code icons. Note, in order to use this feature you will need to have purchased the Professional or Academic license.
This feature can be great for debugging custom C code within your project, prior to compilation to a microcontroller. It is also invaluable as a teaching tool and allows you to teach students the effects of certain C language statements on variables. When simulating C code, you can step through individual statements within a block in the same way that you step through multiple icons in your Flowchart.
Contents
How to simulate C code
By default no C code will be simulated until the C Simulation button is enabled. C Code icons will simply be skipped by the simulator. The C Simulation button can be found on the main toolbar and via the DEBUG menu. C Simulation is enabled when the button icon is highlighted by a bounding square.
The Conversion Messages window
When simulating C code, a helpful Conversion Messages window will appear.
Errors
The Conversion Messages window will detail any problems that Flowcode has encountered when trying to parse your C code. Certain C language features such as pointers, and advanced modifiers such as const and typedef are not supported. If Flowcode encounters a statement within your C code that it cannot simulate, this information will be displayed in the Conversion Window, and Flowcode will simply skip that statement. Flowcode will still attempt to simulate the rest of the code within the C code icon.
Temporary variable renaming
The Conversion Messages window will also detail any information that is useful for debugging the values of variables. Flowcode may temporarily rename variables within your C code when simulating. This due to the fact that multiple variables in C may use the same name, as long as they exist at different scopes (e.g. a name can be used to define a variable at the top of a function, as well as a variable inside the curly braces of a while-loop). This renaming ability allows you to use the Simulation Debugger to track the values of different variables, even if they share a name in the C code. No variable renaming performed by Flowcode will alter the behavior of your C code. If a variable is renamed by Flowcode during simulation, then this information, including the name to use in the Simulation Debugger, will appear in the Conversion Messages window. For example, we might declare "int x" inside a while-loop, which Flowcode will rename to "x2" during simulation: this can then be tracked in the Simulation Debugger by writing ".x2" in the Expression field.
Interacting with Flowcode variables in C code
We recommend viewing your project using the C code view in order to get a better understanding of how the code relates to your project.
Variable data types
When variables are normally created in Flowcode, you specify a given type (e.g. Byte). The actual types as they appear in C code can be understood using the following mapping:
Flowcode type | In C code |
---|---|
Bool | MX_BOOL |
Byte | MX_UINT8 |
Int | MX_SINT16 |
UInt | MX_UINT16 |
Long | MX_SINT32 |
ULong | MX_UINT32 |
String | MX_CHAR (array) |
Float | MX_FLOAT |
Object handle | MX_UINT32 |
The C code simulation engine also currently supports the following native C types:
int, signed int, char, float, long, signed long, unsigned int, unsigned long
However, the behaviour of these variable types cannot be guaranteed to be the same between various compiler families. Therefore, if you plan on compiling your code to a microcontroller, we recommend that you use the Flowcode types from the above table in order to keep your code as portable. On the other hand, the native C data types can be useful for educational purposes.
Accessing variables created in Flowcode's UI
You can easily modify and read the values of variables that you have created in the user-interface (for example, using the Project Explorer). Variables are referred to using purely uppercase, and with a special prefix depending on whether they are global or local variables.
Variable type | C code prefix | Example |
---|---|---|
Global | FCV_ | Global variable "var1" can be accessed with "FCV_VAR1" |
Local | FCL_ | Local variable "var1" can be accessed with "FCL_VAR1" |
Calling Flowcode functions and your own Macros
Flowcode functions
Currently, there is a limited support for simulating Flowcode-generated function calls within your own C code.
Flowcode is able to simulate the effects of the following function calls:
Function call | Parameter explanation | Effect of function |
---|---|---|
FCI_DELAYBYTE_MS(x) | x is a byte | Causes a delay of x milliseconds |
FCI_DELAYBYTE_US(x) | x is a byte | Causes a delay of x microseconds |
FCI_DELAYBYTE_S(x) | x is a byte | Causes a delay of x seconds |
FCI_DELAYINT_MS(x) | x is an int up to 2000 | Causes a delay of x milliseconds |
FCI_DELAYINT_US(x) | x is an int up to 2000 | Causes a delay of x microseconds |
FCI_DELAYINT_S(x) | x is an int up to 2000 | Causes a delay of x seconds |
GET_PORT(x) | x is a port letter (A, B, C...) | Returns the value of port x |
GET_PORT_PIN(x, y) | x is a port letter (A, B, C...), y is a pin index (0, 1, 2...) | Returns the value of pin y of port x |
GET_PORT_MASK(x, y) | x is a port letter (A, B, C...), y is a mask value (e.g. 0xFF00) | Returns the value of the pins of port x given by the mask y |
GET_PORT_SHIFT(x, y) | x is a port letter (A, B, C...), y is a shift value (e.g. 3) | Returns the value of port x after right-shifting it by y positions |
GET_PORT_MASK_SHIFT(x, y, z) | x is a port letter (A, B, C...), y is a mask value (e.g. 0xFF00), z is a shift value (e.g. 3) | Returns the value of the pins of port x given by the mask y, after right-shifting it by z positions |
SET_PORT(x, y) | x is a port letter (A, B, C...), y is a numerical value | Sets the value of port x to y |
SET_PORT_PIN(x, y, z) | x is a port letter (A, B, C...), y is a pin index (0, 1, 2...), z is a numerical value | Sets the value of pin y of port x to z |
SET_PORT_MASK(x, y, z) | x is a port letter (A, B, C...), y is a mask value (e.g. 0xFF00), z is a numerical value | Sets the value of the pins of port x given by the mask y, to z |
SET_PORT_SHIFT(x, y, z) | x is a port letter (A, B, C...), y is a shift value (e.g. 3), z is a numerical value | Sets the value of port x, to z, after left-shifting z by y positions |
SET_PORT_MASK_SHIFT(w, x, y, z) | w is a port letter (A, B, C...), x is a mask value (e.g. 0xFF00), y is a shift value (e.g. 3), z is a numerical value | Sets the function sets the value of the pins of port w given by the mask x, to z, after left-shifting z by y positions |
User-defined macros
Flowcode is also able to simulate the effects of calling your own user-defined macros. In C code, user-macros are referred to using the prefix "FCM_". For example, if we defined a user-macro called "Func", which returned a byte, and required an integer as a parameter, then we could write the following C code, which would successfully simulate.
MX_UINT8 x = FCM_Func(4);
Note that we do not have to make the "Func" part of the expression uppercase, unlike when referring to variables.
Example
Here is an example program that counts up and down in binary using Port A and a Combo Board component to show the LED signals.
The C Simulation feature is similar to the C Code to Flowchart Conversion feature that converts C code into Flowchart icons.
To view all the c code ,ither hover over the C code box as shown above, or double click on it.