- 打卡等级:初来乍到
- 打卡总天数:7
- 最近打卡:2025-04-01 23:18:33
高级会员
- 积分
- 979
|
keil c51用着一直没问题,c251用一下感觉让人有点怀疑。
有几个小例子请专家帮忙指点
问题1
void test1(void)
{
unsigned char i8;
unsigned int i16;
unsigned long i32;
i8 = 255;
i16 = 12345;
i32 = 70000;
printf("%bu %u %lu\n", i8, i16, i32);
}
上面的printf输出结果是错误的,printf的参数u8类型不可用%bu输出,会导致后续所有输出出错。
u8和u16均用%d才可正确输出。
在c51中,单字节变量在printf中要用%bd。C251即使弃用b前缀也可以,但是在keil c251的帮助文件和例子中,都是规定用%bd。
Note - The optional characters l or L may immediately precede the type character to respectively specify long types for d, i, u, o, x, and X.
- The optional characters b or B may immediately precede the type character to respectively specify char types for d, i, u, o, x, and X.
问题2
函数内局部变量不能用code属性。
void test(void)
{
unsigned char code TAB[8] = {1,2,3,4,5,6,7,8};
unsigned char i, value;
for(i=0; i<8; i++)
{
value = TAB;
printf("TAB[%d] = %d\n", i, value);
}
}
上述程序在C51中运行无问题,在C251中编译通过但运行出错,看C编译后的汇编代码就很离谱,其向FF地址区域即CODE区域写数据。
将这句unsigned char code TAB[8] = {1,2,3,4,5,6,7,8};放在函数外,即变为全局变量,结果就对了。
另外,在函数内用const unsigned char TAB[8] = {1,2,3,4,5,6,7,8};这样运行结果是对的,但是不会将数组放在code区,而是放在data区然后用代码逐个赋初值。
问题3
sbit LED1 = P1^0;
LED1 = !LED1;
LED1 = ~LED1;
关于两种取反的区别,之前已有专家提过了,这里就不多做解释了。
感觉现在STC,内部人才济济,能够做出异常强大的芯片设计,但受限于编译器落后,很多技能无法施展。
|
|