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

From Flowcode Help
Jump to navigationJump to search
 
(44 intermediate revisions by 3 users not shown)
Line 1: Line 1:
<sidebar>Sidebar: What Is a Macro?</sidebar>
+
<sidebar>Sidebar: Flowcode Exercises:Ex1</sidebar>
C code is a high-level programming language widely used in industry.It has spawned a number of other programming languages, such as C#, C++, Java and Python.  
+
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.  
 
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:
 
There are two mechanisms for adding your own C code to a Flowcode flowchart:
 
[[File:Exercise_InsCode_C_suppcode.jpg|right|350px]]
 
[[File:Exercise_InsCode_C_suppcode.jpg|right|350px]]
* using the 'C Code' icon [[File:Btn C Code.gif|30px]]   ;
+
* using the 'C Code' icon   [[File:Btn C Code.gif|border]] ;
* using the 'Use supplementary code' facility within 'Project Options':
+
* using the 'Use supplementary code' facility within 'Project Options', shown opposite:
  
 +
 +
__TOC__
  
  
 +
==Why would you want to?==
  
__TOC__
+
* 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.
  
==Why would you want to ?==
+
* 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.
  
1. Flocode 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.)
 
  
 +
==Adding supplementary code==
  
2. Experienced C code
+
This feature is used when you have blocks of code containing routines, definitions, lookup tables, etc.
 +
[[File:Gen_Supplementary_Code_01.png|right|frameless]]
  
  
 +
'''Definitions and function declarations:'''
  
Flowcode is often used by C programmers as a method of managing their code and allowing less experienced team members to take advantage of more complex C or Assembly code routines. If you have blocks of C or Assembly code that contain routines, definitions, lookup tables, etc. that you need to call later in the program then you should use this feature.  This is an advanced feature that requires an understanding of C or Assembly language.
+
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:'''
  
Definitions and function declarations
+
This second section allows for the addition of the main function code.
  
The first section, Definitions and function declarations, is added at the beginning of the C Code to allow users to add in defines, includes, function declarations and any other initialization code needed.  This section is placed at the beginning of the generated C file to allow the code to be visible to all parts of the program.
 
  
+
Structured thus, the code is accessible to any Flowcode macro, and vice-versa.
  
The second section, Function implementations, allows for the addition of the main function code.
 
  
+
==Warning==
  
The code is split into these two sections to allow every Flowcode macro to use any of the supplementary functions defined, and to allow these supplementary functions to use any user or component macros that exist in the Flowcode program.  It also ensures that supplementary functions can call other supplementary functions.
+
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.
  
Warning: If Flowcode fails to compile you may need to examine the supplementary code for errors.
 
  
 
  
Note also that Flowcode does not simulate C Code, so programs that make use of supplementary code may not simulate correctly.  
+
==Using the 'Code' icon==
 +
[[File:Exercise_InsCode_C_Flowchart.png|right|294x261px]]
  
==Set up the flowchart==
 
 
* [[Opening Flowcode|Open Flowcode]].
 
* [[Opening Flowcode|Open Flowcode]].
 +
 
* On the Startup screen, click on 'New project' to [[Creating Flowcharts|create a new flowchart]].
 
* On the Startup screen, click on 'New project' to [[Creating Flowcharts|create a new flowchart]].
  
Add icons step by step and explain code.
 
 
Interrupts are a way of grabbing the microcontroller's attention immediately.
 
 
They are very widely used, both in microcontrollers and microprocessors. For example, the keyboard and mouse in your computer probably use interrupts to talk to the CPU.
 
  
They can also be used to save energy. In many battery-powered applications, the microcontroller is 'put to sleep' when inactive, and so requires little energy. An interrupt is used to 'awaken' the controller, and bring it back into operation, when needed.
+
* 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|border]]- details given below;
 +
::* a delay icon configured to give a two second delay.
  
  
This exercise shows how to use an interrupt to sense when a switch is closed, (an external interrupt).
+
==The C Code==
  
 +
Configure the Code icon by typing in the following code, exactly as written here:
  
 +
<pre class="brush:[cpp]" style="float:left;">
 +
trisb = 0;                        //Set all of Port B to output
 +
portb = FCV_VAR1;                  //Output the value of var1 to port B
 +
trisa = 1;                        //Convert port A bit 0 to input
 +
if (test_bit(porta,0))            //Check to see if A0 is highelse</pre>
  
==Polling vs interrupt==
+
The 'Code' icon properties box resembles that shown opposite.
  
The microcontroller is often connected to a large number of peripheral input devices - switches, sensors, memory, timers etc.
+
[[File:Exercise_InsCode_C_Code_Properties.png|center]]
There are two broad ways in which one of these devices can be serviced by the microcontroller:
 
* polling - each device is 'asked' in turn if it has data to transfer to the controller;
 
* interrupts - allow the device to interrupt the task being carried out by the controller.
 
  
The exercise aims to show the difference between polling and using interrupts.  
+
The software recognizes instructions and comments, and colour-codes them accordingly.
  
  
==The system==
+
'''Some notes on this code:'''
  
This exercise sets up a system with LEDs controlled by two switches:
+
* The ports on the PIC chip are bi-directional - they can act as either input or output.
::* Switch 1 is polled by the program, at regular intervals.<br />
+
* Their behaviour is controlled by the contents of two more registers - TRISA (for Port A) and TRISB (for Port B.)
::* Switch 2 initiates an interrupt.
+
:* 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.
  
  
The polled switch, switch 1, is checked at regular (and predictable) intervals - every six seconds in this example.
+
* All of this could have been done using 'pure' Flowcode, but it would have required around five more icons in the flowchart.
Switch 2 is connected so that as soon as it operates, the processor stops what it is doing and jumps to the Interrupt Service Routine (ISR). In the process, the return address is stored, so that at the end of the ISR, the processor can return to where it left the main program. In Flowcode, the ISR takes the form of a macro, configured like any other.
 
  
  
==The flowchart==
+
==Testing==
  
The flowchart sequence:
+
As pointed out earlier, Flowcode will not simulate added C code, and so there is no value in simulating this program.<br />
:* Check if switch 1 is pressed.
+
Instead, compile it to the PIC chip which you specified as the target.
:: If it isn't, make the red LED flash slowly.
 
:: If it is, make the yellow LED flash slowly.
 
:* Go back to the beginning and repeat the process.
 
:* Whenever switch 2 is pressed, immediately make both LEDs flash quickly ten times and then go back to the main program.
 
  
  
==The main program==
+
Add the following E-Blocks units to the EB-008 Multiprogrammer holding the PIC chip:
 +
:: a switch unit to Port A,
 +
:: a LED unit to Port B.
 +
Press the reset switch on the Multiprogrammer.
  
* [[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.
+
Test the program by noticing the output displayed on the LEDs when switch 0 on the switch unit is pressed.
 +
* When pressed, LEDs D2 and D0 should light, indicating an output 0101 (i.e. the number 5,)
 +
* When not pressed, LED D1 should light, indicating an output 0010, (i.e. the number 2.)
  
* 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]].
+
==Download the exercise==
 +
You can download the file created by this exercise and open it in Flowcode to identify errors in your program/file or you could also download the file to skip to the next exercise.
  
* Drag and drop a [[Decision Icon Properties|Decision icon]] after the 'Input' icon.
+
*To download the file, click on the link below and then either:
 +
:*Click on the file name.
 +
:*Right click the file name and select 'Save link as...' or 'Save target as...' (depending on your browser).
  
Next, add icons to control what happens when switch is not pressed, (and so the program follows the 'No' branch):
+
:::{{Fcfile|Exercise - Inserting Code Into Flowcode.fcfx|Exercise - Inserting Code Into Flowcode}}

Latest revision as of 14:32, 13 March 2014

<sidebar>Sidebar: Flowcode Exercises:Ex1</sidebar> C code is a high-level programming language widely used in industry.
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:

Exercise InsCode C suppcode.jpg
  • using the 'C Code' icon Btn C Code.gif ;
  • using the 'Use supplementary code' facility within 'Project Options', shown opposite:



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

This feature is used when you have blocks of code containing routines, definitions, lookup tables, etc.

Error creating thumbnail: sh: /usr/bin/convert: No such file or directory Error code: 127


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!

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

Exercise InsCode C Flowchart.png


  • 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 Btn C Code.gif- 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 
portb = FCV_VAR1;                  //Output the value of var1 to port B
trisa = 1;                         //Convert port A bit 0 to input
if (test_bit(porta,0))             //Check to see if A0 is highelse

The 'Code' icon properties box resembles that shown opposite.

Exercise InsCode C Code Properties.png

The software recognizes instructions and comments, and colour-codes them accordingly.


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.


  • All of this could have been done using 'pure' Flowcode, but it would have required around five more icons in the flowchart.


Testing

As pointed out earlier, Flowcode will not simulate added C code, and so there is no value in simulating this program.
Instead, compile it to the PIC chip which you specified as the target.


Add the following E-Blocks units to the EB-008 Multiprogrammer holding the PIC chip:

a switch unit to Port A,
a LED unit to Port B.

Press the reset switch on the Multiprogrammer.


Test the program by noticing the output displayed on the LEDs when switch 0 on the switch unit is pressed.

  • When pressed, LEDs D2 and D0 should light, indicating an output 0101 (i.e. the number 5,)
  • When not pressed, LED D1 should light, indicating an output 0010, (i.e. the number 2.)


Download the exercise

You can download the file created by this exercise and open it in Flowcode to identify errors in your program/file or you could also download the file to skip to the next exercise.

  • To download the file, click on the link below and then either:
  • Click on the file name.
  • Right click the file name and select 'Save link as...' or 'Save target as...' (depending on your browser).
FC6 Icon.png

Exercise - Inserting Code Into Flowcode