Problem with CW12 4.5

取消
显示结果 
显示  仅  | 搜索替代 
您的意思是: 

Problem with CW12 4.5

5,590 次查看
tim_milliken
Contributor III
I am having an issue with CW12 4.5 and sprintf. The problem is that when ever I use sprintf to format a float, the value in the buffer is not the same as the float and the is no method to how far it is off from the orginal float. Here is a sample line of code. Any ideas anyone?? Tim
sprintf(MyChar, "%1.2f", MyFloat);
标签 (1)
标记 (1)
0 项奖励
回复
12 回复数

2,851 次查看
tim_milliken
Contributor III
OK, I traced that problem down to a compiler switch. Now the next problem. I have an app that will run fine as a Large memory model but not as a banked. What is the difference between them? I have not been able to find the answer yet. What the app does is goes into a func that uses some far pointers and never returns. Thanks for the help. Tim
0 项奖励
回复

2,851 次查看
J2MEJediMaster
Specialist I
Here's a summary of dealing with the HC(S)12x memory models:

Memory model for HC12 family (Tech Note 207)

Memory model Compiler option Library
------------ --------------- -------
SMALL -Ms Ansi.lib
BANKED -Mb Ansib.lib
LARGE -Ml Ansil.lib


Recommend using SMALL memory model for apps with less than 32 KB code. Otherwise, use BANKED memory model. Do NOT recommend use of LARGE memory model. (From Tech Note 238.)

SMALL memory model
-------------------
Code and data occupy 64 KB code-address space. Code < 48 KB.

BANKED memory model
-------------------
Code > 64 KB. Data < 64 KB. Put another way, uses 16-bit addressing for data and 24-bit addressing for code. Code is placed in 16 KB banks. Active bank is selected by value in page register.

Declare functions using __far and __near.

__far function: Generated code uses CALL instruction and modifies the bank register.

Used to access function code in another bank.

__near function: Generated code uses JSR/BSR instruction, page register untouched.

Used to call function in non-banked memory, or function residing in same bank as calling function. Also used to ensure that ISR routines land in non-banked memory. ISRs must be in non-banked memory becuase the vector table only supports 16-bit addresses.

LARGE memory model
------------------
Code and data > 64 KB. Both code and data use 24-bit addressing.

Because HC(S)12 family's instruction set doesn't directly support paged data, the overhead for such accesses is significant. For this reason, FSL recommends to use of only the SMALL and BANKED memory models.

I'd check to see how you're declaring your functions. If they're declared __near, the use of a far pointer might cause trouble.

Consult tech notes 207 and 238 for additional information.

---Tom
0 项奖励
回复

2,851 次查看
tim_milliken
Contributor III
O.K. I thought it was a compiler switch issue that fixed it. Now I just don't know what fixed it. Would you make a new app for a 9S12DJ* cpu and test this code for me please?
I think you will find the same as I, it does not work. I made a compltely new project and that is the only thing I did, and it did not work. I sometime step into sprintf and notice that the format that gets passed in is messed up.
PLEASE help me figure out what is wrong, else I will have to go back to CW 3.1 it is reliable and looks like 4.5 was a waste of $$.
Tim
 
unsigned char test[5] = {0};
float temp = 1.2;
void main(void)
{
sprintf(test, "%1.2f", temp);
for(;:smileywink:;
}
0 项奖励
回复

2,851 次查看
CrasyCat
Specialist III
Hello
 
When you create a new project from a stationery, make sure to specify you want to use floating point arithmetic and select the appropriate floating point format (double on 32-bit or double on 64-bit).
 
Starting from there it should work.
 
Do not specify floating point format None.
 
 
CrasyCat
0 项奖励
回复

2,851 次查看
tim_milliken
Contributor III
Interesting that you bring that up. I had already figured out the work around. The problem is the if you choose float as 32 and double as 32 CW will not include the correct lib. It puts ansib.lib instead of ansibf.lib which it will include when you choose a double as 64, which fixed all my issues :smileyhappy: . Thanks for all the help for thoose who responded. Tim
0 项奖励
回复

2,851 次查看
CrasyCat
Specialist III
Hello
 
Well if you decide to work with double encoded on 32 bit, the application need in fact to be linked with ansib.lib.
 
Did you think about including stdio.h in the source file where you are using sprintf?
 
CrasyCat
0 项奖励
回复

2,851 次查看
tim_milliken
Contributor III
Yes I have the stdio.h included, else it should/would complain. The other reply I posted seems to me to be the only fix. I will just use the 32/64 unless someone else can help me with a fix. Is there any harm in the way I am doing it? I was thinking that if I needed 32 bit valuse and it was not a float to maybe uae a long long, if that is supported. Will have to cross that bridge later if so. Tim
0 项奖励
回复

2,851 次查看
CrasyCat
Specialist III
Hello
If 32-bit long projects is not working for you, I would suggest you to submit a service request  for that.
 
To log the issue please go to following URL:
    http://www.freescale.com/TechSupport
and click on "Submit a service request"
 
Make sure to attach the whole project reproducing this behavior.
You can use the pack and go wizard that can be started from Help -> Pack And Go to collect all the necessary information.
 
CrasyCat
0 项奖励
回复

2,851 次查看
admin
Specialist II
Hello! I have the same problem, and I just use
sprintf(str,"%10.8f",-0.43123);
 
and it prints -0.00000686.
 
If it is necessary i can post the entire code for printing to my lcd. But the function to print to the lcd works fine. Sptintf returns ok with "%d" and "%s"  but not with "%f".
In a week I need to present this project and I just can't believe I stumbled on this "simple" problem...
I am using CW12 5.5.1272.
0 项奖励
回复

2,851 次查看
CrasyCat
Specialist III
Hello
 
A few stuff you can check:
  - Did you include stdio.h at the beginning of the source file where you tried to use sprintf?
     When using function with variable number of argument you MUST have a prototype
     for the called function
  - Are you using the appropriate compiler option?
     Make sure you are not using the option -D__NO_FLOAT__ to build your code.
     If this option is activated, you did specify you do not want floating point support in your application.
     You have to re-create your application WITH floating point support.
  - Are you using the appropriate library?
     Make sure you are linking with the appropriate ANSI library.
     The file {Install}\lib\hc12c\readme.txt describes which library should be used depending
     on the memory model and floating point format you are using.
  - Is the destination sting a local variable or a global one? Is it big enough?
  - Did you define enough stack?
    You may want to increase stack size and check whether this makes a difference.
 
CrasyCat
0 项奖励
回复

2,851 次查看
admin
Specialist II
It seems I have solved the problem. Although I checked I wanted to use floating point when I created my project, I have added the floating point library "ansisf.lib", at the compiler command line was _D_NOFLOAT_.....I erased that and I have also checked the button from "HC12 compiler Option Settings" -> "Code Generation" -> float=IEEE 32bit, double=IEEE 64bit, and it finaly works. Thank you very much CrasyCat for the path to the solution...
0 项奖励
回复

2,851 次查看
CompilerGuru
NXP Employee
NXP Employee
I think I saw this question a couple of times before in this forum.
Thing to check:
- enough stack space
- correct library version (see lib/hc12c/readme.txt)
- correct usage, (íncludes)
- search the forum.

Mit tip of the probable cause: you link the wrong library.

Daniel
0 项奖励
回复