Using Flowcode and C part 1 - Sleep Function

Tips, Tricks and methods for programming, learn ways of making your programming life easier, and share your knowledge with others.

Moderators: Benj, Mods

Post Reply

Please rate this article out of 5: (1 being the worst and 5 being the best)

1
0
No votes
2
1
11%
3
0
No votes
4
0
No votes
5
8
89%
 
Total votes: 9

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Using Flowcode and C part 1 - Sleep Function

Post by Benj »

Image

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
Register Names

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

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
Useful Common C Functions

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)
Writing C code functions or sub routines

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.
Attachments
C_code_example.fcf
(5.5 KiB) Downloaded 1913 times

Genilsonsa
Posts: 150
Joined: Mon Oct 15, 2012 5:19 pm
Has thanked: 12 times
Been thanked: 18 times
Contact:

Re: Using Flowcode and C part 1 - Sleep Function

Post by Genilsonsa »

Boa noite eu quero agradecer toda a equipe uso o flowcode já faz anos e ele é muito bom .Estava precisando fazer um programa que tivesse baixo consumo e estava dias tentando fazer e não conseguia até encontrar este tópico.Muito obrigado

User avatar
Enamul
Posts: 1772
Joined: Mon Mar 05, 2012 11:34 pm
Location: Nottingham, UK
Has thanked: 271 times
Been thanked: 814 times
Contact:

Re: Using Flowcode and C part 1 - Sleep Function

Post by Enamul »

Olá
Essa bom tutorial ajudá-lo? Eu sinto sim. Por favor, deixe-nos saber.
Enamul
University of Nottingham
enamul4mm@gmail.com

brandonb
Posts: 438
Joined: Mon Aug 29, 2011 12:26 am
Location: arizona
Has thanked: 175 times
Been thanked: 173 times
Contact:

Re: Using Flowcode and C part 1 - Sleep Function

Post by brandonb »

ben, thanks for documenting, can never have enough c code examples, more please :D

Tomas2012
Posts: 3
Joined: Mon Feb 11, 2013 7:46 am
Contact:

Re: Using Flowcode and C part 1 - Sleep Function

Post by Tomas2012 »

Good morning

Could you please help me with sleep mode for AVR?

I made flovcode according your example but without success.

Could you please make your example for ATmega328p.

Thanks in advance

Tomas

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Using Flowcode and C part 1 - Sleep Function

Post by Benj »

Hello Tomas,

Here is the AVR GCC page for the sleep functions.

http://www.nongnu.org/avr-libc/user-man ... sleep.html

Let us know how your getting on.

Tomas2012
Posts: 3
Joined: Mon Feb 11, 2013 7:46 am
Contact:

Re: Using Flowcode and C part 1 - Sleep Function

Post by Tomas2012 »

Thank you Ben

I will try it and I will let you know.

Regards


Tomas

Tomas2012
Posts: 3
Joined: Mon Feb 11, 2013 7:46 am
Contact:

Re: Using Flowcode and C part 1 - Sleep Function

Post by Tomas2012 »

Hi Ben

I have checked link from you (very good link) and tied to make example with AVR, but again without success.

I can't find out solution.

Do you have other helpfull links

Thanks in advance

Tomas

User avatar
Benj
Matrix Staff
Posts: 15312
Joined: Mon Oct 16, 2006 10:48 am
Location: Matrix TS Ltd
Has thanked: 4803 times
Been thanked: 4314 times
Contact:

Re: Using Flowcode and C part 1 - Sleep Function

Post by Benj »

Hello Tomas,

This has some interesting information.

http://www.avrfreaks.net/index.php?name ... c&p=466513

This also looks like it may be useful.

http://www.avrfreaks.net/index.php?modu ... highlight=

This was my Google search phrase - avr gcc sleep

Post Reply