51单片机中使用结构体位域组织变量

定义结构体

typedef struct//时间结构体
{
	unsigned char year;
	unsigned char mon;
	unsigned char day;
	unsigned char week;
	unsigned char shi;
	unsigned char fen;
	unsigned char miao;
}timeTypeDef;

typedef struct//位域
{
	unsigned char one1OnOff : 1;
	unsigned char one0OnOff : 1;
	unsigned char two1OnOff : 1;
	unsigned char two0OnOff : 1;
	unsigned char three1OnOff : 1;
	unsigned char three0OnOff : 1;
	unsigned char four1OnOff : 1;
	unsigned char four0OnOff : 1;
}modeTypeDef;

typedef struct//温度湿度结构体
{
	unsigned int wenDu;
	unsigned char shiDu;
}tianqiTypeDef;

typedef struct//定时时间结构体
{
	unsigned char shiOne1;
	unsigned char fenOne1;
	unsigned char shiTwo1;
	unsigned char fenTwo1;
	unsigned char shiThree1;
	unsigned char fenThree1;
	unsigned char shiFour1;
	unsigned char fenFour1;

	unsigned char shiOne0;
	unsigned char fenOne0;
	unsigned char shiTwo0;
	unsigned char fenTwo0;
	unsigned char shiThree0;
	unsigned char fenThree0;
	unsigned char shiFour0;
	unsigned char fenFour0;
}dingShiTypeDef;

定义结构体变量

timeTypeDef time;
modeTypeDef mode;
dingShiTypeDef dingShi;
tianqiTypeDef tianqi;

初始化结构体变量

void DingShiInit()
{
	time.year=23;
	time.mon=2;
	time.day=19;
	time.week=7;
	//计时
	time.shi=0;
	time.fen=0;
	time.miao=0;
	//设定时间

	mode.one1OnOff=1;
	mode.one0OnOff=1;
	mode.two1OnOff=1;
	mode.two0OnOff=1;
	mode.three1OnOff=1;
	mode.three0OnOff=1;
	mode.four1OnOff=1;
	mode.four0OnOff=1;

	dingShi.shiOne1=0;
	dingShi.fenOne1=1;
	dingShi.shiOne0=0;
	dingShi.fenOne0=2;

	dingShi.shiTwo1=0;
	dingShi.fenTwo1=1;
	dingShi.shiTwo0=0;
	dingShi.fenTwo0=2;

	dingShi.shiThree1=0;
	dingShi.fenThree1=1;
	dingShi.shiThree0=0;
	dingShi.fenThree0=2;

	dingShi.shiFour1=0;
	dingShi.fenFour1=1;
	dingShi.shiFour0=0;
	dingShi.fenFour0=2;
	//设定天气
	tianqi.wenDu=26;
	tianqi.shiDu=51;

}

你可能感兴趣的:(51,51单片机,c语言,51单片机,c语言)