无法将 C 中的位域初始化为全局
Can't initialise Bit Field in C as global
我已经声明了一个位域,如下所示。
struct {
volatile uint8_t screenflag:1;
volatile uint8_t logoflag:1;
volatile uint8_t oledflag:1;
volatile uint8_t animationflag:1;
volatile uint8_t clockdialflag:1;
volatile uint8_t update_screen:1;
volatile uint8_t BLE_activity:1;
uint8_t ble_status:1;
} oled_flag;
但是当我试图从 Main() 函数中初始化 Bitfield 的元素时,它在编译时显示以下错误。
....\Src\main.c(95): warning: #77-D: this declaration has no storage
class or type specifier oled_flag.screenflag=1;
....\Src\main.c(95): error: #147: declaration is incompatible with
"struct oled_flag" (declared at line 92)
oled_flag.screenflag=1; ....\Src\main.c(95): error: #65: expected a
";" oled_flag.screenflag=1; ....\Src\main.c(96): warning: #77-D:
this declaration has no storage class or type specifier
oled_flag.logoflag=0; ....\Src\main.c(96): error: #147: declaration
is incompatible with "struct oled_flag" (declared at line
95) oled_flag.logoflag=0; ....\Src\main.c(96): error: #65:
expected a ";" oled_flag.logoflag=0; ....\Src\main.c(97): warning:
77-D: this declaration has no storage class or type specifier oled_flag.oledflag=1; ....\Src\main.c(97): error: #147: declaration
is incompatible with "struct oled_flag" (declared at line
96) oled_flag.oledflag=1; ....\Src\main.c(97): error: #65:
expected a ";" oled_flag.oledflag=1; ....\Src\main.c(98): warning:
77-D: this declaration has no storage class or type specifier oled_flag.animationflag=0; ....\Src\main.c(98): error: #147:
declaration is incompatible with "struct oled_flag"
(declared at line 97) oled_flag.animationflag=0;
....\Src\main.c(98): error: #65: expected a ";"
oled_flag.animationflag=0; ....\Src\main.c(99): warning: #77-D: this
declaration has no storage class or type specifier
oled_flag.clockdialflag=1; ....\Src\main.c(99): error: #147:
declaration is incompatible with "struct oled_flag"
(declared at line 98) oled_flag.clockdialflag=1;
....\Src\main.c(99): error: #65: expected a ";"
oled_flag.clockdialflag=1; ....\Src\main.c(100): warning: #77-D:
this declaration has no storage class or type specifier
等..
初始化代码为:
oled_flag.screenflag=1;
oled_flag.logoflag=0;
oled_flag.oledflag=1;
oled_flag.animationflag=0;
oled_flag.clockdialflag=1;
oled_flag.update_screen=0;
oled_flag.BLE_activity=0;
oled_flag.ble_status=1;
但是当我在 Main() 函数中初始化位字段的元素时,它工作正常。
在 C 中不能在函数外有语句。您需要将语句移动到函数内或将全局变量与其声明一起初始化:
struct {
volatile uint8_t screenflag:1;
volatile uint8_t logoflag:1;
volatile uint8_t oledflag:1;
volatile uint8_t animationflag:1;
volatile uint8_t clockdialflag:1;
volatile uint8_t update_screen:1;
volatile uint8_t BLE_activity:1;
uint8_t ble_status:1;
} oled_flag = {
.screenflag = 1,
.logoflag = 0,
.oledflag = 1,
.animationflag = 0,
.clockdialflag = 1,
.update_screen = 0,
.BLE_activity = 0,
.ble_status = 1
};
执行此操作的另一种方法是将您的位域结构放入这样的联合中:
union {
volatile uint8_t byte;
struct {
volatile uint8_t screenflag:1;
volatile uint8_t logoflag:1;
volatile uint8_t oledflag:1;
volatile uint8_t animationflag:1;
volatile uint8_t clockdialflag:1;
volatile uint8_t update_screen:1;
volatile uint8_t BLE_activity:1;
uint8_t ble_status:1;
}bit_field
}oled_flag;
现在有了联合,您可以使用 oled_flag.byte = {0x95}
在主函数之外手动初始化值。您仍然可以使用 oled_flag.bit_field.screenflag = ...
访问您的位
我已经声明了一个位域,如下所示。
struct {
volatile uint8_t screenflag:1;
volatile uint8_t logoflag:1;
volatile uint8_t oledflag:1;
volatile uint8_t animationflag:1;
volatile uint8_t clockdialflag:1;
volatile uint8_t update_screen:1;
volatile uint8_t BLE_activity:1;
uint8_t ble_status:1;
} oled_flag;
但是当我试图从 Main() 函数中初始化 Bitfield 的元素时,它在编译时显示以下错误。
....\Src\main.c(95): warning: #77-D: this declaration has no storage class or type specifier oled_flag.screenflag=1; ....\Src\main.c(95): error: #147: declaration is incompatible with "struct oled_flag" (declared at line 92)
oled_flag.screenflag=1; ....\Src\main.c(95): error: #65: expected a ";" oled_flag.screenflag=1; ....\Src\main.c(96): warning: #77-D: this declaration has no storage class or type specifier
oled_flag.logoflag=0; ....\Src\main.c(96): error: #147: declaration is incompatible with "struct oled_flag" (declared at line 95) oled_flag.logoflag=0; ....\Src\main.c(96): error: #65: expected a ";" oled_flag.logoflag=0; ....\Src\main.c(97): warning:77-D: this declaration has no storage class or type specifier oled_flag.oledflag=1; ....\Src\main.c(97): error: #147: declaration
is incompatible with "struct oled_flag" (declared at line 96) oled_flag.oledflag=1; ....\Src\main.c(97): error: #65: expected a ";" oled_flag.oledflag=1; ....\Src\main.c(98): warning:
77-D: this declaration has no storage class or type specifier oled_flag.animationflag=0; ....\Src\main.c(98): error: #147:
declaration is incompatible with "struct oled_flag" (declared at line 97) oled_flag.animationflag=0; ....\Src\main.c(98): error: #65: expected a ";"
oled_flag.animationflag=0; ....\Src\main.c(99): warning: #77-D: this declaration has no storage class or type specifier
oled_flag.clockdialflag=1; ....\Src\main.c(99): error: #147: declaration is incompatible with "struct oled_flag" (declared at line 98) oled_flag.clockdialflag=1; ....\Src\main.c(99): error: #65: expected a ";"
oled_flag.clockdialflag=1; ....\Src\main.c(100): warning: #77-D: this declaration has no storage class or type specifier
等..
初始化代码为:
oled_flag.screenflag=1;
oled_flag.logoflag=0;
oled_flag.oledflag=1;
oled_flag.animationflag=0;
oled_flag.clockdialflag=1;
oled_flag.update_screen=0;
oled_flag.BLE_activity=0;
oled_flag.ble_status=1;
但是当我在 Main() 函数中初始化位字段的元素时,它工作正常。
在 C 中不能在函数外有语句。您需要将语句移动到函数内或将全局变量与其声明一起初始化:
struct {
volatile uint8_t screenflag:1;
volatile uint8_t logoflag:1;
volatile uint8_t oledflag:1;
volatile uint8_t animationflag:1;
volatile uint8_t clockdialflag:1;
volatile uint8_t update_screen:1;
volatile uint8_t BLE_activity:1;
uint8_t ble_status:1;
} oled_flag = {
.screenflag = 1,
.logoflag = 0,
.oledflag = 1,
.animationflag = 0,
.clockdialflag = 1,
.update_screen = 0,
.BLE_activity = 0,
.ble_status = 1
};
执行此操作的另一种方法是将您的位域结构放入这样的联合中:
union {
volatile uint8_t byte;
struct {
volatile uint8_t screenflag:1;
volatile uint8_t logoflag:1;
volatile uint8_t oledflag:1;
volatile uint8_t animationflag:1;
volatile uint8_t clockdialflag:1;
volatile uint8_t update_screen:1;
volatile uint8_t BLE_activity:1;
uint8_t ble_status:1;
}bit_field
}oled_flag;
现在有了联合,您可以使用 oled_flag.byte = {0x95}
在主函数之外手动初始化值。您仍然可以使用 oled_flag.bit_field.screenflag = ...