/*
程序: 联合体+结构体+位域 演示程序
公众号:孤独的二进制
日期:2022-05-06
*/
#define DATAPIN 2
#define CLOCKPIN 3
#define LATCHPIN 4
void setup()
{
Serial.begin(9600);
pinMode(DATAPIN, OUTPUT);
pinMode(CLOCKPIN, OUTPUT);
pinMode(LATCHPIN, OUTPUT);
//demo1();
demo2();
}
void demo1() {
byte data = 255;
shiftOut(DATAPIN, CLOCKPIN, MSBFIRST, data);
digitalWrite(LATCHPIN, HIGH);
digitalWrite(LATCHPIN, LOW);
}
void demo2() {
//联合体+结构体+位域
union LED {
byte all;
struct {
byte led0: 1;
byte led1: 1;
byte led2: 1;
byte led3: 1;
byte led4: 1;
byte led5: 1;
byte led6: 1;
byte led7: 1;
} group;
};
LED led;
led.all = 0;
led.group.led0 = 1;
Serial.print("led memory size : ");
Serial.print(sizeof(led));
Serial.println(" byte");
shiftOut(DATAPIN, CLOCKPIN, MSBFIRST, led.all);
digitalWrite(LATCHPIN, HIGH);
digitalWrite(LATCHPIN, LOW);
}
void loop()
{
}