printf() problem used in the mc9s12dj64 in the CW 5.7.0

キャンセル
次の結果を表示 
表示  限定  | 次の代わりに検索 
もしかして: 

printf() problem used in the mc9s12dj64 in the CW 5.7.0

ソリューションへジャンプ
3,198件の閲覧回数
zhangtracker
Contributor I
I define the start_up_server function as follow.
 
unsigned short start_up_server (void * p_device)
{
...
reg_value = 0x55;
printf("**reg_value = 0x%02x\n",reg_value);
...
}
 
Call the start_up_server in the main function.  The printf function can't work normally.
output:
**reg_value = 0x30f9
note: 30f9 is the address of the p_device.
 
why?
ラベル(1)
タグ(1)
0 件の賞賛
返信
1 解決策
1,074件の閲覧回数
CrasyCat
Specialist III
Hello
 
Do you have included stdio.h on top of the source file where you are using printf?
Where is reg_value allocated (at which address)?
Can you try
printf("**reg_value = 0x%02x\n",(unsigned int) reg_value); instead?
CrasyCat

元の投稿で解決策を見る

0 件の賞賛
返信
8 返答(返信)
1,074件の閲覧回数
zhangtracker
Contributor I
I didn't include stdio.h.
After I include stdio.h, printf funtion works normally.
 
Why?

 
Thanks  Lundin and CrasyCat.
0 件の賞賛
返信
1,074件の閲覧回数
CrasyCat
Specialist III
Hello
 
  printf is a function with an open parameter list. Parameter passing scheme is different for such  
   functions, so the compiler needs to know it should use a special parameter passing scheme.
 
CrasyCat
0 件の賞賛
返信
1,074件の閲覧回数
zhangtracker
Contributor I
Thank you for your reply.
I have try to define "reg_value" variable to volatile. But the output of printf function doesn't change.
 
You said:
>I have experienced strange behavior like this before because the optimizer wasn't behaving.
 
What dose  the optimizer need to be set?  I use none optimization. My complie command  line arguments is "-CPUHCS12 -Mb".
 
 
0 件の賞賛
返信
1,074件の閲覧回数
Lundin
Senior Contributor IV
That is quite a complicated topic. Some optimization is enabled by default, some is not. The Freescalers are better suitable for answering this question than me, though I would at least recommend -Onf which turns off "Create sub-functions with common code", since that one usually creates problems and does more harm than good.
0 件の賞賛
返信
1,075件の閲覧回数
CrasyCat
Specialist III
Hello
 
Do you have included stdio.h on top of the source file where you are using printf?
Where is reg_value allocated (at which address)?
Can you try
printf("**reg_value = 0x%02x\n",(unsigned int) reg_value); instead?
CrasyCat
0 件の賞賛
返信
1,074件の閲覧回数
zhangtracker
Contributor I
reg_value is a variable not a pointer. It is defined as follow.
 
unsigned char reg_value;
 
And  I use the termio.c to make the output to UART0.
0 件の賞賛
返信
1,074件の閲覧回数
Lundin
Senior Contributor IV
Then indeed there is something wrong with the printf function, or perhaps with the optimizer settings. Have you tried looking at the disassembly or single-stepping through it? I have experienced strange behavior like this before because the optimizer wasn't behaving. You could try to set all variables to volatile and see if it works.
0 件の賞賛
返信
1,074件の閲覧回数
Lundin
Senior Contributor IV
What type is reg_value? If it is an allocated variable, did you try typecasting it to unsigned short? If it is a pointer, perhaps you forget the '*' operator?
0 件の賞賛
返信