Page 1 of 1

Problem in SPI project

Posted: Thu Mar 06, 2014 4:24 am
by damstech8888
Hi, I am happy with FLOWCODE V6 but has problem in SPI project.

I got an answer (number) from a calculation, Ex: 858993459 and should be : 0x33333333. It is correct.

My goal is sending out 0x33 0x33 0x33 0x33 ( 0011 0011 0011 0011 0011 0011 0011 0011) to control other device by SPI.

How should I do that?

I try to use "sendchar" with byte 33 but it send 0x21 by SPI.
I try to use "sendstring" with "33" but is send 0x03 0x03 (ASCII) by SPI ..

And even I send 0x21 4 times by by SPI. If you minitor the SPI, sendchar will give you different resold like in JPG. Only 2 has been send, why ????
SPI.jpg
(689.24 KiB) Downloaded 714 times

Any idea?
SPI.fcfx
(9.5 KiB) Downloaded 206 times

Re: Problem in SPI project

Posted: Thu Mar 06, 2014 12:25 pm
by Benj
Hello,

SPI has a number of different operating modes which can make the data appear different on scopes etc. Try playing around with the SPI component properties e.g. the clock polarity, phase and sample point to see if the values change to match what your expecting.

Let me know how your getting on.

Re: Problem in SPI project

Posted: Thu Mar 06, 2014 2:26 pm
by LeighM
Hi

You’ve come across a bug in our system. It seems that Flowcode NumberToHex$ target function only works with 16 bit numbers, hence your 3rd and 4th bytes are missing from the string for the target device, although it works for 32 bit numbers in the simulator.

I have attached a fixed version of AVR_CAL_String.c which will need to be copied into your CAL\AVR folder.

To take your program further you also need to look at how data byte numbers are expressed, and the difference between decimal, binary, hexadecimal etc. Have a search for ASCII table.
In short, you need sendchar(0x33), not sendchar(33) or sendstring(“33”)

Rather than converting the result into a hexadecimal string you maybe need to think about how to separate out the 4x 8 bit values from your 32 bit result.

Regards,
Leigh

Re: Problem in SPI project

Posted: Fri Mar 07, 2014 6:55 am
by damstech8888
Dear Ben and Leigh,

Thanks for your fast reply and thanks for Leigh's reminder and update!

The SPI can send 4 times for sure and I found the way to get what I want!

But one more issue need your help!!!

I found that once the number is boo big ( 32bit? issue again), like if....

B4 = fmod ( 858993459 ,256 )

I can't send correct B4 data by SPI sendchar even I got the right answer ( B4 = 51 )

Please see this SPI monitoring to get my point:
screenshot.png
(165.85 KiB) Downloaded 688 times
And also LCD can't show the correct ans number "858993459" in the simulation.

Now my program is like below:
SPI.fcfx
(9.48 KiB) Downloaded 207 times

Re: Problem in SPI project

Posted: Fri Mar 07, 2014 12:29 pm
by LeighM
Hi

The AVR lib function fmod expects a double (32 bit wide float), so that could be where the problem arises.

LCD Print Number only accepts 16 bit numbers

In this kind of application you would be better using bit shifting.
B4 = ans1
B3 = ans1 >> 8
B2 = ans1 >> 16
B1 = ans1 >> 24

Regards
Leigh

Re: Problem in SPI project and floating point...HELP!!

Posted: Tue Mar 11, 2014 2:46 am
by damstech8888
Dear Leigh,

Thanks for your fast reply and quick solution!

But I think I found my question due to I hit the wall again! :cry:

My question is: How to convert 32 bit floating point calculation answer into ULONG?

Ex:
I need to know:

200001745 * 4.294967296 = X
Then Round "X"
And send out "X" by SPI.

In Flowcode V6:

----------assign----------
Fout [ULong] = 200001745
Floa [floating point]
FTWout [ULong]

--------calculation ----------
Floa2 = int2float (Fout)
Floa2 = fmul (Floa2,4.294967296)
Floa2 = round (Floa2)
FTWout = float2int (Floa2) <--------------------- I found it only convert 8 bit from float2int , I try many different calculation but I stuck...I can only got 0x00 0x00 0x33 0x00 from SPI.

B4 = FTWout
B3 = FTWout >> 8
B2 = FTWout >> 16
B1 = FTWout >> 24 <-----------------------Thanks for your help, This part is OK, If I named FTWout = 858993459, It send out 0x33 0x33 0x33 0x33 perfectly!


If you can help me out, I will buy you a beer! :D

Re: Problem in SPI project

Posted: Thu Mar 13, 2014 10:26 am
by damstech8888
Any Update???

Re: Problem in SPI project

Posted: Thu Mar 13, 2014 1:07 pm
by LeighM
I’m not too familiar with float calculations on AVR gcc, but I don’t think you are going to be able to get a 32 bit integer result from float calculations.
If you have a ulong variable that is to be multiplied by a fractional constant, then I would suggest that you use ulong only maths and calculate the fractional part separately then add it back into your result.

Re: Problem in SPI project

Posted: Fri Mar 14, 2014 10:10 am
by damstech8888
Thank You, I got that!