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,009 次查看
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 解答
885 次查看
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 回复数
885 次查看
zhangtracker
Contributor I
I didn't include stdio.h.
After I include stdio.h, printf funtion works normally.
 
Why?

 
Thanks  Lundin and CrasyCat.
0 项奖励
回复
885 次查看
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 项奖励
回复
885 次查看
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 项奖励
回复
885 次查看
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 项奖励
回复
886 次查看
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 项奖励
回复
885 次查看
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 项奖励
回复
885 次查看
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 项奖励
回复
885 次查看
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 项奖励
回复