结构体可以定义结构体指针
typedef struct _reg1
{
unsigned char reg1_0 :1;
unsigned char reg1_1 :1;
unsigned char reg1_2 :1;
unsigned char reg1_3 :1;
unsigned char reg1_4 :1;
unsigned char reg1_5 :1;
unsigned char reg1_6 :1;
unsigned char reg1_7 :1;
}reg1;
#define REG1 (struct reg1*)0x140
void main(void)
{
REG1->reg1_0 = 1;
}
但是把结构体改成union就没法用,高手指教。
typedef union _reg1
{
unsigned char BYTE;
struct _Bit{
unsigned char reg1_0 :1;
unsigned char reg1_1 :1;
unsigned char reg1_2 :1;
unsigned char reg1_3 :1;
unsigned char reg1_4 :1;
unsigned char reg1_5 :1;
unsigned char reg1_6 :1;
unsigned char reg1_7 :1;
}bits;
}reg1;
#define REG1 (union reg1*)0x140
void main(void)
{
REG1->BYTE = 0;
REG1->bits.reg1_0 = 1;
}
------解决方案--------------------------------------------------------
看起来是可以的,具体出了什么问题?
------解决方案--------------------------------------------------------
没什么问题啊?出了什么问题?下面是main函数中的测试代码,编译,运行都没有问题。
unsigned char a;
reg1* test;
test = (reg1 *)(&a);
test->BYTE = 1;
test->bits.reg1_0=1;
printf(" %d", a);
------解决方案--------------------------------------------------------
单片机 口都是如此定义的
------解决方案--------------------------------------------------------
no problem~~~~~~~~~~~