Exercise - Inserting Code Into Flowcode

From Flowcode Help
Revision as of 11:31, 16 August 2013 by JohnVerrill (talk | contribs)
Jump to navigationJump to search

<sidebar>Sidebar: What Is a Macro?</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':




Why would you want to ?

1. 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.)

2. Experienced programmers can insert a short section of C code in order to reduce the length, or complexity, of the Flowcode program.

3. 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.


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.


Definitions and function declarations

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.


The second section, Function implementations, allows for the addition of the main function code.


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.


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.

Set up the 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.


This exercise shows how to use an interrupt to sense when a switch is closed, (an external interrupt).


Polling vs interrupt

The microcontroller is often connected to a large number of peripheral input devices - switches, sensors, memory, timers etc. 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 system

This exercise sets up a system with LEDs controlled by two switches:

  • Switch 1 is polled by the program, at regular intervals.
  • Switch 2 initiates an interrupt.


The polled switch, switch 1, is checked at regular (and predictable) intervals - every six seconds in this example. 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

The flowchart sequence:

  • Check if switch 1 is pressed.
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

  • 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 between the BEGIN and END icons.

Next, add icons to control what happens when switch is not pressed, (and so the program follows the 'No' branch):