我是新来的和编程。我正在尝试根据DS3231的字节打印日期。因为它是二进制到十进制,所以我的输出在一周中的1到7之间以及当月的1到12之间。
我希望输出与十进制数相对应的字符名称。
#include "Wire.h"
#define rtcAddress 0x68
#include <BH1750_mega.h>
BH1750_mega LightSensor;
byte date_second,
date_minute,
date_hour,
date_DOW,
date_dayOfMonth,
date_month,
date_year;
byte bin2dec(byte val){ return byte( (val/16*10) + (val%16) );}
void date_get() {
Wire.beginTransmission(rtcAddress);
Wire.write(0);
Wire.endTransmission();
Wire.requestFrom(rtcAddress, 7);
// A few of these need masks because certain bits are control bits
date_second = bin2dec(Wire.read() & 0x7f);
date_minute = bin2dec(Wire.read());
date_hour = bin2dec(Wire.read() & 0x3f); // Need to change this if 12 hour am/pm
date_DOW = bin2dec(Wire.read());
date_dayOfMonth = bin2dec(Wire.read());
date_month = bin2dec(Wire.read());
date_year = bin2dec(Wire.read());
}
void setup() {
Serial.begin(9600);
Wire.begin();
LightSensor.begin();
LightSensor.SetAddress(Device_Address_L); //Address 0x23
LightSensor.SetMode(Continuous_H_resolution_Mode2);
}
void loop() {
date_get();
if (date_DOW = 6) Serial.print("Monday");
else if (date_DOW = 2) Serial.print("Tuesday");
else if (date_DOW = 3) Serial.print("Wednesday");
else if (date_DOW = 4) Serial.print("Thursday");
else if (date_DOW = 5) Serial.print("Friday");
else if (date_DOW = 1) Serial.print("Sunday");
else Serial.print("Saturday");
Serial.print(", ");
if (date_month = 1) Serial.print("Jan");
else if (date_month = 2) Serial.print("Feb");
else if (date_month = 3) Serial.print("Mar");
else if (date_month = 4) Serial.print("Apr");
else if (date_month = 5) Serial.print("May");
else if (date_month = 6) Serial.print("Jun");
else if (date_month = 7) Serial.print("Jul");
else if (date_month = 8) Serial.print("Aug");
else if (date_month = 9) Serial.print("Sep");
else if (date_month = 10) Serial.print("Oct");
else if (date_month = 11) Serial.print("Nov");
else if (date_month = 12) Serial.print("Dec");
Serial.print(". ");
Serial.print(date_dayOfMonth); Serial.print(", ");
Serial.print(2000 + date_year); Serial.print(" | ");
Serial.print(date_hour); Serial.print(": ");
Serial.print(date_minute); Serial.print(": ");
Serial.print(date_second); Serial.print(" -- ");
uint16_t lux = LightSensor.GetLightIntensity();
Serial.print("Lux: ");
Serial.println(lux);
delay(1000);
}
我总是得到“星期一”和“1月”的输出。
结果:
答案 0 :(得分:0)
在C中,使用单个等于:
为变量赋值=
将一个变量与一个值进行比较,使用double equals:
==
上面的代码想要进行比较,而是使用单个等号,这会导致赋值。
将非零值赋值给变量视为真实。
这就是代码输出“Monday”和“Jan”的原因。
HTH。