longchijie 发表于 2016-11-10 11:34

c语言.h头文件的一些问题

下面是在.h 头文件里对一个32位寄存器的定义,有些地方不明白,请教一下,先谢谢啦!
1)为什么这里typedef union有4个 struct? 是不是针对不同情况?如果我使用第一个struct,就不能用其他的么?
2)如果我对某一位赋值,我可以使用, 比如PMCONbits.CS2P=1;(因为定义了unsigned CS2P:1;)
但是有些位没有定义,比如unsigned:1;是不是说明该位不能被赋值?如果能,怎么赋值?
3) 能不能用PMCON对整个寄存器赋值,比如PMCON=0xFFFFFF00
4)    PMCONCLR是不是所有为置0, PMCONSET所有为置1?




#define PMCON PMCON
extern volatile unsigned int   PMCON __attribute__((section("sfrs")));
typedef union {
struct {
    unsigned RDSP:1;
    unsigned WRSP:1;
    unsigned :1;
    unsigned CS1P:1;
    unsigned CS2P:1;
    unsigned ALP:1;
    unsigned CSF:2;
    unsigned PTRDEN:1;
    unsigned PTWREN:1;
    unsigned PMPTTL:1;
    unsigned ADRMUX:2;
    unsigned SIDL:1;
    unsigned :1;
    unsigned ON:1;
    unsigned :1;
    unsigned DUALBUF:1;
    unsigned :5;
    unsigned RDSTART:1;
};
struct {
    unsigned :6;
    unsigned CSF0:1;
    unsigned CSF1:1;
    unsigned :3;
    unsigned ADRMUX0:1;
    unsigned ADRMUX1:1;
};
struct {
    unsigned :13;
    unsigned PSIDL:1;
    unsigned :1;
    unsigned PMPEN:1;
};
struct {
    unsigned w:32;
};
} __PMCONbits_t;
extern volatile __PMCONbits_t PMCONbits __asm__ ("PMCON") __attribute__((section("sfrs")));
extern volatile unsigned int      PMCONCLR __attribute__((section("sfrs")));
extern volatile unsigned int      PMCONSET __attribute__((section("sfrs")));
extern volatile unsigned int      PMCONINV __attribute__((section("sfrs")));

sdnumcu 发表于 2016-11-15 22:47

头文件........你想明白啥
页: [1]
查看完整版本: c语言.h头文件的一些问题