Difference between revisions of "Exercise - Inserting Code Into Flowcode"

From Flowcode Help
Jump to navigationJump to search
(Replaced content with "For an excellent introduction guide, we recommend [https://www.flowcode.co.uk/education/ Introduction to microcontroller programming]")
Tag: Replaced
 
(23 intermediate revisions by 4 users not shown)
Line 1: Line 1:
<sidebar>Sidebar: What Is a Macro?</sidebar>
+
For an excellent introduction guide, we recommend [https://www.flowcode.co.uk/education/ Introduction to microcontroller programming]
C code is a high-level programming language widely used in industry.<br />
 
It has spawned a number of other programming languages, such as C#, C++, Java and Python.
 
 
 
Flowcode programs are first compiled into C before being crunched down eventually into hex code.
 
 
 
There are two mechanisms for adding your own C code to a Flowcode flowchart:
 
[[File:Exercise_InsCode_C_suppcode.jpg|right|350px]]
 
* using the 'C Code' icon  [[File:Btn C Code.gif|30px]] ;
 
* using the 'Use supplementary code' facility within 'Project Options', shown opposite:
 
 
 
 
 
__TOC__
 
 
 
 
 
==Why would you want to?==
 
 
 
* Flowcode programs are compiled to 'C', but produce many lines of code.
 
: They do so because they must be able to cope with any function or structure that the designer wishes to use.
 
: Experienced C programmers may wish to use code more efficiently, and so insert a few lines of code to replace many lines which Flowcode would generate. (This usually applies to the 'Adding supplementary code' option.)
 
 
 
* Experienced programmers can insert a short section of C code in order to reduce the length, or complexity, of the Flowcode program.
 
 
 
* Students learning to program in C can test short sections of code by inserting them into a Flowcode program. This avoids having to write the full program in C.
 
 
 
 
 
==Adding supplementary code==
 
[[File:Exercise InsCode C suppcode props.png |right|350px]]
 
 
 
This feature is used when you have blocks of code containing routines, definitions, lookup tables, etc. 
 
 
 
 
 
'''Definitions and function declarations:'''
 
 
 
This is the first section. It allows users to initialise the program, by adding structures such as 'defines', 'includes', and other function declarations.  This section must be placed at the beginning of the C file so that the code is 'visible' to all parts of the program.
 
 
 
 
'''Function implementations:'''
 
 
 
This second section allows for the addition of the main function code.
 
 
 
 
 
Structured thus, the code is accessible to any Flowcode macro, and vice-versa. 
 
 
 
 
 
==Warning==
 
 
 
Be sure that the added C code is correct! <br />
 
 
 
Flowcode does not simulate C Code, so programs that make use of supplementary code may not simulate correctly.
 
Where Flowcode fails to compile, it may be that the supplementary code contains errors.
 
 
 
 
 
==Using the 'Code' icon==
 
[[File:Exercise_InsCode_C_flowchart.jpg|200px|right]]
 
 
 
* [[Opening Flowcode|Open Flowcode]].
 
* On the Startup screen, click on 'New project' to [[Creating Flowcharts|create a new flowchart]].
 
* Add the icons shown in the flowchart:
 
::* a loop icon, configured as an infinite loop;
 
::* a calculation icon:
 
:::* use it to create two byte variables, 'var1' and 'add',
 
:::* set them to the following values:
 
::::: var1 = 2, add = 3;
 
::* a Code icon [[File:Btn C Code.gif|30px]]- details given below;
 
::* a delay icon configured to give a two second delay.
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
==The C Code==
 
 
 
Configure the Code icon by typing in the following code, exactly as written here:
 
 
 
trisb = 0;                        //Set all of Port B to output<br />
 
portb = FCV_VAR1;                  //Output the value of var1 to port B<br />
 
trisa = 1;                        //Convert port A bit 0 to input<br />
 
if (test_bit(porta,0))            //Check to see if A0 is high<br />
 
(<br />
 
portb = FCV_VAR1 + FCV_ADD;        //If so, portb = 2 + 3 = 5 (0101 in binary)<br />
 
)<br />
 
else<br />
 
(<br />
 
portb = FCV_VAR1;                //Otherwise portb = 2<br />
 
)<br />
 
 
 
'''Some notes on this code:'''
 
* The ports on the PIC chip are bi-directional - they can act as either input or output.
 
* Their behaviour is controlled by the contents of two more registers - TRISA (for Port A) and TRISB (for Port B.)
 
:* A value of '0' in the control register makes the corresponding bit of the port into an output bit.
 
:* A value of '1' in the control register makes the corresponding bit of the port into an input bit.
 
* Commands are finished by adding a semicolon (;).
 
* The characters '//' are used to indicate comments - there to aid understanding of the program. These are ignored by the compiler.
 
* Where variables are defined in the Flowcode program, the prefix 'FCV_' must be added to the name of the variable, which is written in upper case lettering.
 
* The 'if-else' construction examines a possible condition - "Is Port A bit 0 high?"
 
:* If true, the values of the two variables are added together and sent to Port B.
 
:* If false, only the value of 'var1' is sent to Port B.
 
[[File:Exercise_InsCode_C_code_props.jpg|right|350px]]
 
 
 
* All of this could have been done using 'pure' Flowcode, but it would have required around five more icons in the flowchart.
 
 
 
==Heading==
 
 
 
 
 
==The flowchart sequence==
 
 
 
The flowchart sequence:
 
:* Check if switch 1 is pressed.
 
:: If it isn't, make the red LED flash slowly.
 
::
 
 
 
==The main program==
 
 
 
* [[Creating Flowcharts|Start a new Flowcode flowchart]], using the default microcontroller.
 
 
 
* Make sure that the [[System Panel]] is visible. If necessary, click on [[View]] and then select 'System Panel' a check-box will appear next to the option when enabled.
 
 
 
* Drag and drop a [[Loop Icon Properties|Loop icon]] between the BEGIN and END icons.
 
 
 
* Inside the loop, drag and drop an [[Input Icon Properties|Input icon]] from the [[Tools and Views#1) Icons Toolbar|Icons toolbar]].
 
 
 
* Drag and drop a [[Decision Icon Properties|Decision icon]] after the 'Input' icon.
 
 
 
Next, add icons to control what happens when switch is not pressed, (and so the program follows the 'No' branch
 

Latest revision as of 13:29, 25 April 2023

For an excellent introduction guide, we recommend Introduction to microcontroller programming