我已经声明了一个比特字段,如下所示。
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):警告:#77-D:此声明没有存储空间 类或类型说明符oled_flag.screenflag = 1; .... \ Src \ main.c(95):错误:#147:声明与...不兼容 " struct oled_flag" (在第92行宣布)
oled_flag.screenflag = 1; .... \ Src \ main.c(95):错误:#65:预期a &#34 ;;" oled_flag.screenflag = 1; .... \ Src \ main.c(96):警告:#77-D: 此声明没有存储类或类型说明符
oled_flag.logoflag = 0; .... \ Src \ main.c(96):错误:#147:声明 与" struct oled_flag"不兼容(在线宣布 95)oled_flag.logoflag = 0; .... \ Src \ main.c(96):错误:#65: 预期a&#34 ;;" oled_flag.logoflag = 0; .... \ Src \ main.c(97):警告:77-D:此声明没有存储类或类型说明符oled_flag.oledflag = 1; .... \ Src \ main.c(97):错误:#147:声明
与" struct oled_flag"不兼容(在线宣布 96)oled_flag.oledflag = 1; .... \ Src \ main.c(97):错误:#65: 预期a&#34 ;;" oled_flag.oledflag = 1; .... \ Src \ main.c(98):警告:
77-D:此声明没有存储类或类型说明符oled_flag.animationflag = 0; .... \ Src \ main.c(98):错误:#147:
声明与" struct oled_flag"不相容 (在第97行声明)oled_flag.animationflag = 0; .... \ Src \ main.c(98):错误:#65:预期a&#34 ;;"
oled_flag.animationflag = 0; .... \ Src \ main.c(99):警告:#77-D:这个 声明没有存储类或类型说明符
oled_flag.clockdialflag = 1; .... \ Src \ main.c(99):错误:#147: 声明与" struct oled_flag"不相容 (在第98行宣布)oled_flag.clockdialflag = 1; .... \ Src \ main.c(99):错误:#65:预期a&#34 ;;"
oled_flag.clockdialflag = 1; .... \ Src \ main.c(100):警告:#77-D: 此声明没有存储类或类型说明符
等。
初始化代码是:
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()函数中初始化位字段的元素时,它工作正常。
答案 0 :(得分:2)
您不能在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
};
答案 1 :(得分: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 = ...