解除引用联盟所需的帮助 [英] Help needed in dereferencing a union
问题描述
在嵌入式控制器上工作时,我通常会发现定义以下取消引用的指针很方便:
#define portb(*(无符号) char *)(0x03u))
这使我可以按如下方式写入端口:
portb = 0x0f;
但是当我想按如下方式定义一个寄存器时,代码没有
编译:
typedef union >
{
uint8 byte;
struct
{
uint8:4 ;
uint8 tffca:1;
uint8 tsfrz:1;
uint8 tswai:1;
uint8 ten :1; / *定时器启用* /
}位;
} tSCR1;
#define scr1(*( tSCR1 *)(0x4c))
用法:
scr1.bit.ten = 1; / *编译器抱怨这个* /
我发现如果可能的话,使用这个方案非常方便。只需通过阅读代码即可清除被操纵的内容。我更喜欢
这个:scr1 = 0x01;。是否有可能以与第一个例子相同的方式取消引用此联盟
?非常感谢提前。
Working on an embedded controller, I generally find it convenient to
define the following de-referenced pointer:
#define portb (*(unsigned char *)(0x03u))
This enable me to write to a port as follows:
portb = 0x0f;
But when I want to define a register as follows, the code does not
compile:
typedef union
{
uint8 byte;
struct
{
uint8 : 4;
uint8 tffca : 1;
uint8 tsfrz : 1;
uint8 tswai : 1;
uint8 ten : 1; /* timer enable */
}bit;
}tSCR1;
#define scr1 (*(tSCR1 *)(0x4c))
useage:
scr1.bit.ten = 1; /* the compiler complains about this */
I find it very convenient to use this scheme if possible. It is very
clear what is being manipulated just by reading the code. I prefer
this over: "scr1 = 0x01;". Is it possible to dereference this union
in the same fashion as the first example? Thanks very much in advance.
推荐答案
2005年5月31日星期二19:25:27 +0200,jlara< jl * **@wowway.com>写道:
On Tue, 31 May 2005 19:25:27 +0200, jlara <jl***@wowway.com> wrote:
typedef union
uint8 byte;
struct
{
uint8:4;
uint8 bla:4;
uint8 tffca:1;
uint8 tsfrz:1;
uint8 tswai:1;
} bit;
} tSCR1;
#define scr1(*(tSCR1 *)(0x4c))
scr1.bit.ten = 1; / *编译器抱怨这个* /
typedef union
{
uint8 byte;
struct
{
uint8 : 4;
uint8 bla : 4;
uint8 tffca : 1;
uint8 tsfrz : 1;
uint8 tswai : 1;
uint8 ten : 1; /* timer enable */
}bit;
}tSCR1;
#define scr1 (*(tSCR1 *)(0x4c)) scr1.bit.ten = 1; /* the compiler complains about this */
不,它抱怨丢失的字段名称。
Jirka
No, it complains about a missing field name in bit.
Jirka
Jirka Klaue< jk **** @ tkn.tu-berlin.de>写道:
Jirka Klaue <jk****@tkn.tu-berlin.de> wrote:
2005年5月31日星期二19:25:27 +0200,jlara< jl *** @ wowway.com>写道:
On Tue, 31 May 2005 19:25:27 +0200, jlara <jl***@wowway.com> wrote:
typedef union
{u>8 byte;
struct
{
uint8:4;
uint8 bla:4;
uint8 tffca:1;
uint8 tsfrz:1;
uint8 tswai:1;
uint8 ten:1; / * timer enable * /
} bit;
} tSCR1;
#define scr1(*(tSCR1 *)(0x4c))
scr1.bit.ten = 1; / *编译器抱怨这个* /
typedef union
{
uint8 byte;
struct
{
uint8 : 4; uint8 bla : 4; uint8 tffca : 1;
uint8 tsfrz : 1;
uint8 tswai : 1;
uint8 ten : 1; /* timer enable */
}bit;
}tSCR1;
#define scr1 (*(tSCR1 *)(0x4c)) scr1.bit.ten = 1; /* the compiler complains about this */
不,它抱怨丢失字段名称。
No, it complains about a missing field name in bit.
这不是我的编译器(gcc)抱怨的缺少字段名称
关于(标准允许使用未命名的位字段)但是
类型用于比特字段的变量 - unit8不是一个标准类型,它必须是uint8_t。 (假设一个C99 compli-
蚂蚁编译器)然后只为有符号字段签名和无符号
整数可以按照<可移植使用(和C99中的_Bool) br />
标准。有了它,它可以毫无问题地编译,
OP指示的行被接受而没有投诉。
如果OPs编译器存在实际问题只有那一行(
我想不应该是这种情况)另一种选择是尝试
使用指向联盟的指针
#defined scr1((tSCR1 *)0x4c)
然后再做
scr1-> bit .ten = 1;
或用于设置重置位的已定义宏,即
#define TIMER_ENABLE()do {(( tSCR1 *)0x4c) - > bit.ten = 1; } while(0)
#define TIMER_DISABLE()do {((tSCR1 *)0x4c) - > bit.ten = 0; } while(0)
,因为这似乎主要是关于代码的可读性,
可能导致最容易阅读的代码。 />
问候,Jens
-
\ Jens Thoms Toerring ___ Je *********** @ physik.fu-berlin.de
\ __________________________ http://www.toerring.de
jlara写道:
代码无法编译:
typedef union
{u>8 byte;
结构
{/ uint8:4;
uint8 tffca:1;
uint8 tsfrz:1;
uint8 tswai:1;
uint8 ten:1; / *定时器启用* /
}位;
} tSCR1;
#define scr1(*(tSCR1 *)(0x4c))
scr1.bit.ten = 1; / *编译器抱怨这个* /
the code does not compile:
typedef union
{
uint8 byte;
struct
{
uint8 : 4;
uint8 tffca : 1;
uint8 tsfrz : 1;
uint8 tswai : 1;
uint8 ten : 1; /* timer enable */
}bit;
}tSCR1;
#define scr1 (*(tSCR1 *)(0x4c))
scr1.bit.ten = 1; /* the compiler complains about this */
代码对我来说是正确的(假设uint8是
unsigned char,而你的编译器支持uint8作为
位域类型)。
确切的编译错误是什么?
另一件事尝试使用除''bit''
和''ten'之外的其他名称,以防这些符号是#defined宏。我使用的一个
编译器有#define bit< something>在其标准
标题中。
The code looks correct to me (assuming that uint8 is
unsigned char, and that your compiler supports uint8 as a
bitfield type).
What is the exact compiler error?
Another thing to try would be to use other names than ''bit''
and ''ten'', in case those symbols are #defined macros. One
compiler I use has #define bit <something> in its standard
headers.
这篇关于解除引用联盟所需的帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!