One of Flowcode’s real strengths is that it is very easy to use and simple flowchart programs can be created very quickly, even by first-time users. However, flowchart icons alone will only get you so far and you may need to resort to C code. Luckily, Flowcode allows you to use small snippets of C code inside your own programs, which gives you the flexibility to do almost anything. A good example of this is allowing the device to go into low power mode by using the inbuilt sleep function. One downside of C code is that Flowcode will be unable to simulate the functionality so always keep in mind to keep things as simple as possible and test every line or function of the C on the hardware as you program it in. The 100% guaranteed way to fail and get frustrated is to create a complete program by guessing the C code and then try to debug at the end. Remember this is what our forums are for. If you get stuck then my advice is β€DO NOT plough on with the rest of your programβ€, instead create a posting on the forum and we will do our best to answer quickly. Alternatively look into the problem by searching the BoostC manual (available from the Flowcode V3/boostc directory), our forums and other microcontroller forums. This is bound to increase both your microcontroller and programming knowledge even if you do not find the exact solution you were looking for.
Semi Colons
A C code statement will always end with a semi colon character β€;β€ which is used to inform the compiler that the end of the statement is ready. Here is a brief example of a C code statement.
Code: Select all
a = b + c; //assign variable a with the value of variable b plus the value of variable c
The most useful C code statements are those that control the reading and writing of the onboard peripheral control registers. Using the device datasheet you can see which registers are available and then use the C code to directly access them. Register names are always lowercase and should not include any spaces or special characters other then the occasional underscore.
Code: Select all
porta = 0; //assign the port a register with the value of 0
char x; //create a variable named x
x = porta; //assign the value in the port a register to the variable x
Flowcode variables can be accessed from the C code blocks by using a special prefix plus the variable name. A variable named count in Flowcode can be accessed in C by capitalising the variable’s name and then adding the prefix β€FCV_β€
Code: Select all
porta = FCV_COUNT; //assign the value in Flowcode variable count to the port a register
Here is a short list containing some of the more useful C code functions that can be used with the BoostC compiler used by Flowcode PIC.
Code: Select all
set_bit ( register_name, bit ); //Sets a single bit in a register
clear_bit ( register_name, bit ); //Clears a single bit in a register
delay_us( z ); //Waits for z x microseconds - will not work for some clock speeds
delay_10us( z ); //Waits for z x 10 microseconds
sleep(); //Puts the device into low power operation (if available)
Tutorial number 33 that is supplied with the Flowcode product is an example of writing an external C code function and then including this function into your Flowcode program. The tutorial can be found within the Flowcode V3/Examples directory.
The Flowcode Sleep Example
The sleep example that is attached to this article is a simple program that uses a single interrupt to detect an input change of 0 to 1 on bit 0 of PortB. This could be modified to use almost any interrupt such as an incoming RS232 signal or a completed ADC sample. Each time when we are waiting for the interrupt the chip will be placed into the low power sleep mode using the sleep(); C code function. When the interrupt is received the chip is automatically brought out of the low power mode and it will start running through its program again. Here we are incrementing a value on port A to show that the chip has woken from sleep. After this we return to sleep mode to again wait for another interrupt. Note that the simulated example program will constantly increment the value on port A as Flowcode cannot simulate the C code we have used to enable the sleep mode.