Displaying the right frequency

For Flowcode users to discuss projects, flowcharts, and any other issues related to Flowcode 2 and 3.

Moderators: Benj, Mods

Post Reply
User avatar
vidor
Posts: 42
Joined: Mon Jan 28, 2008 4:56 pm
Location: Sweden

Displaying the right frequency

Post by vidor »

Hi,

I have somr problems displaying the right frequency on the LCD.
My calculation for the frequency is:

Freq = 7372 / ( 2 * ( 1 + ADC0_value ) )

The problem is, if I display the "Freq" variable on the LCD, I get the values only in kHZ.
I want the values to be the right ones in Hz.
If I multiply with 1000 I get only rounded values, not the really ones.

Best regards
Vidor

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: Displaying the right frequency

Post by Benj »

Hello Vidor

Maybe the floating point library could help you to get your program running correctly. If you have a look on our support centre under Flowcode V3 then there is a topic regarding the floating point library for Flowcode.

User avatar
vidor
Posts: 42
Joined: Mon Jan 28, 2008 4:56 pm
Location: Sweden

Re: Displaying the right frequency

Post by vidor »

Hi Ben,
If you have a look on our support centre under Flowcode V3 then there is a topic regarding the floating point library for Flowcode.
Is that library working with AVR MCUs too?
I forgot to mention that I am working on an Atmega168.

Regards
Vidor

User avatar
vidor
Posts: 42
Joined: Mon Jan 28, 2008 4:56 pm
Location: Sweden

Re: Displaying the right frequency

Post by vidor »

Hi all,

I've found the float.h file in "C:\Program Files\Matrix Multimedia\Flowcode_AVR\Tools\lib\gcc\avr\4.1.2\include\".'
The problem is that I cannot find out how to use it with Flowcode.
Is it possible to get an example using float in Flowcode4AVR?
Can somebody help me out with this?

Thanks in advance.
Regards
Vidor

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: Displaying the right frequency

Post by Benj »

Hello Vidor

Ah sorry I see yes the example floating point library calls are for Flowcode PIC only.

I have had a look at the floating point h file you mentioned but there doesnt seem to be much usefull stuff in there.

You should be able to find an example by searching AVR GCC floating point C code examples. This C code should then be able to go straight into the C code icons in Flowcode.

User avatar
vidor
Posts: 42
Joined: Mon Jan 28, 2008 4:56 pm
Location: Sweden

Re: Displaying the right frequency

Post by vidor »

Hi Ben,
You should be able to find an example by searching AVR GCC floating point C code examples. This C code should then be able to go straight into the C code icons in Flowcode.
I've searched on the web but no big success.
I found this explanation, (attached), and I tried to change the float32.fcf example file to Atmega32 but I'm stucked.
I don't know how to use it in Flowcode. It seems to be the float library into GCC compiler, but I cannot manage to work.
Is there any workaround or something to display the right frequency value on the LCD?

My best regards
Vidor
Attachments
Soft float library routines - GNU Compiler Collection (GCC) Internals.zip
(4.22 KiB) Downloaded 434 times
float_avr_M32.fcf_avr
(7 KiB) Downloaded 426 times

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times

Re: Displaying the right frequency

Post by Sean »

Hello Vidor,

The GCC compiler supports basic floating point (float) and double precision (double) calculations as part of its native instruction set. I have modified your PrintFloat and CalcArea macros and they seem to work.
float_avr_M32_2.fcf_avr
(7 KiB) Downloaded 455 times
I have also removed the #include for float.h as it is not required for these calculations.

You might need to include some additional library and header files if you require more complex functions.

There are some tricks to be learned, especially when doing calculations with mixed variable types (Example: I have passed the results of a floating point calculation to integer variables), but these are covered in most C text books.

User avatar
vidor
Posts: 42
Joined: Mon Jan 28, 2008 4:56 pm
Location: Sweden

Re: Displaying the right frequency

Post by vidor »

Hi Sean,

Thanks for the changes, but maybe I'm missing something, because it doesn't work for me.
I get zeros on the display.
It has to be done some changes on the Flowcode Options or Configuration?

Regards
Vidor

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times

Re: Displaying the right frequency

Post by Sean »

Hello Vidor,

I used all the standard configuration settings when I compiled the modified program.
The compiler creates a .lst file when you download a program. If you want to post the .lst file created when you compiled my version of the program, I will compare it with my copy and see if there are any differences.
You can also post the .hex file and I will try to download and run it on my system.

User avatar
vidor
Posts: 42
Joined: Mon Jan 28, 2008 4:56 pm
Location: Sweden

Re: Displaying the right frequency

Post by vidor »

Hello Sean,

Here are the files.

Best regards
Vidor
Attachments
float_avr_M32_2.zip
The .lst file zipped
(16.03 KiB) Downloaded 426 times
float_avr_M32_2.hex
(10.85 KiB) Downloaded 402 times

Sean
Valued Contributor
Valued Contributor
Posts: 548
Joined: Tue Jun 26, 2007 11:23 am
Has thanked: 6 times
Been thanked: 44 times

Re: Displaying the right frequency

Post by Sean »

Hello Vidor,

The .lst files were almost identical (slight differences expected) and the .hex files (read by the programmer) were identical, so your program should have been operating correctly with the floating point calculations.

Can you confirm that the new programs are being downloaded correctly?
What programmer are you using?
Have you programmed any of the device fuse bits that might affect downloads?

Try changing part of the program (the radius value or the text that is displayed on the LCD) to prove that new programs are being downloaded successfully.

If everything seems to be working, but you are not getting the expected results, post the .msg.txt file that is created and I will check for compiler and programmer messages.

User avatar
Steve
Matrix Staff
Posts: 3433
Joined: Tue Jan 03, 2006 3:59 pm
Has thanked: 114 times
Been thanked: 422 times

Re: Displaying the right frequency

Post by Steve »

Remember that the simulation will not show the correct values because it does not simulate user C code. But the values should be displayed correctly when running on the chip.

User avatar
vidor
Posts: 42
Joined: Mon Jan 28, 2008 4:56 pm
Location: Sweden

Re: Displaying the right frequency

Post by vidor »

steve wrote:Remember that the simulation will not show the correct values because it does not simulate user C code.
I think here is the problem, I expected to see the results on the Display in Flowcode simulation.
I have to make the hardware to see if it works on the MCU.

Thanks a lot
Best regards
Vidor

User avatar
mytekcontrols
Posts: 95
Joined: Sun Aug 19, 2007 6:38 pm
Location: Santa Rosa, California
Has thanked: 4 times
Been thanked: 7 times
Contact:

Re: Displaying the right frequency

Post by mytekcontrols »

Vidor --- I had a similar problem of getting a precision result while limited to the 15-bit (un-signed) aspect of the FlowCode integers. Basically I had a need to multiply a gain factor of less than 1, with at least 3 digits of precision (.948). The value I was manipulating originally came from a 16-bit ADC looking at the output of a wattage transducer, although for my application I had limited its gain to keep the number within the 15-bit domain.

So what to do without resorting to C-Code and/or floating point routines?

I did my calculation on a digit-by-digit basis, thereby keeping the math within the 15-bit un-signed integer confines.

Here is how I did it:
Basically it is a calculation that takes a number like 6330 (ADC equivalent for 6000 watts) and applies a gain
factor of .948 to it, so that the actual result will equal the true reading of 6000. This is done by first breaking it into the 4
individual "decimalized" digits:
6
3
3
0

I get re-use out of a display formatting macro I wrote in FlowCode called "decimalize" that stores an ascii version of each digit in
a string called temp_dec . It's in ascii format because I need this for other areas of my program where I'll be displaying it on an LCD, and wish to keep it in a 4 digit format with leading zeros.


Then each digit gets multiplied by the gain factor, after first converting it from ascii into an actual number.

After the gain factor has been used, each number is manipulated based on it's place value (integer math
truncates result). All manipulated place values are then added together to get final result.

Here is the math showing each (digit) being multiplied by the gain factor of .948 as done in the FlowCode calculation box (see attached image below).

Code: Select all

  (0)*948/10 = 0        # of 1's
+ (3)*948 = 2844        # of 10's
-----------------
  (2844)/10 = 284       adjust (result) for next operation
+ (3)*948 = 2844        # of 100's
-----------------
  (3128)/10 = 312       adjust (result) for next operation
+ (6)*948 = 5688        # of 1000's
-----------------
6000 = Final Result
The accuracy I obtained by this method yielded +/- 1 watt across the entire range.

With a little customization, you should be able to apply this idea to solve your frequency rounding problem. This is also fully simulation capable in FlowCode.

I hope this helps :mrgreen:
Attachments
Watts_Display.gif
Watts_Display.gif (21.96 KiB) Viewed 13326 times
Michael St. Pierre
FlowCode V3&V4 Pro Registered User
Manufacture: Heat Load Controllers,
and a variety of other widgets.

User avatar
vidor
Posts: 42
Joined: Mon Jan 28, 2008 4:56 pm
Location: Sweden

Re: Displaying the right frequency

Post by vidor »

Hi Michael,

Thank you for your idea.
I hope I can apply it to my project.

Best regards
Vidor

User avatar
vidor
Posts: 42
Joined: Mon Jan 28, 2008 4:56 pm
Location: Sweden

Displaying the right frequency-Solution

Post by vidor »

Hi all,

Here comes my solution to display the frequency on LCD, without using floating point routines. :lol:
This little program (completely runnable) is just a bit of my Project I'm working on.
The point is that you can control the frequency through RS232 communication from a PC.
After you've started the program just type in a value between 100 and 255 in the RS232 queued characters.
The LCD will display the frequency in Khz with two decimal accuracy. 8)
Pay attention that the DutyCycle is always 50%. 8)

I hope it will help many users to develop their own Projects based on this work, or just maybe give some
answers to their questions.

Hope you like it... :roll:
My best regards
Vidor
Attachments
RS232_freq_contr_M168.fcf_avr
(8.5 KiB) Downloaded 308 times

User avatar
vidor
Posts: 42
Joined: Mon Jan 28, 2008 4:56 pm
Location: Sweden

Displaying the right frequency fixed some bugs :)

Post by vidor »

Hi,

I've discovered that some wrong frequencies are displayed.
For example if you send 175 to PC, the frequency displayed is
"20.0- Khz".. which is wrong.
I made some modifications to fix that.
So you are welcome to download the new file.

My best regards
Vidor
Attachments
RS232_freq_contr_M168_2.fcf_avr
(8.5 KiB) Downloaded 389 times
Last edited by vidor on Mon May 12, 2008 4:20 pm, edited 1 time in total.

User avatar
mytekcontrols
Posts: 95
Joined: Sun Aug 19, 2007 6:38 pm
Location: Santa Rosa, California
Has thanked: 4 times
Been thanked: 7 times
Contact:

Re: Displaying the right frequency

Post by mytekcontrols »

I wish I could see what you did, but your file appears to be intended for the AVR version of FlowCode, and it wont open in my PIC version.
Michael St. Pierre
FlowCode V3&V4 Pro Registered User
Manufacture: Heat Load Controllers,
and a variety of other widgets.

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: Displaying the right frequency

Post by Benj »

Hello

The latest version of Flowcode for PICmicros that was released today includes an import feature that allows you to open programs from Flowcode AVR and Flowcode ARM. Simply goto File -> Import and then open the program you wish.

User avatar
vidor
Posts: 42
Joined: Mon Jan 28, 2008 4:56 pm
Location: Sweden

Re: Displaying the right frequency

Post by vidor »

Hi,

You can always import an AVR Flowcode into PIC-Flowcode.
I used to do that when I want to see some examples which are done for PIC.
Hope this helps. :roll:
Best regards
Vidor

User avatar
vidor
Posts: 42
Joined: Mon Jan 28, 2008 4:56 pm
Location: Sweden

Re: Displaying the right frequency

Post by vidor »

Hi Ben,

I thought the older version of Flowcode for PIC has had that import option. :shock:

Regards
Vidor

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: Displaying the right frequency

Post by Benj »

Hello Vidor

Must have been replying at the same time ;) You may be right about that sorry just had a lot of different versions kicking about for a while now and confusing myself.

Post Reply