|
楼主 |
发表于 2015-2-16 22:48:04
|
显示全部楼层
先完成了技术中的1,2,7 上个图.
本想用NANO. 但可能是接错,烧了一块. 还是先用UNO了.
现在能够使用串口设置时间, 使用1602显示DS3231中的时间.
显示部分代码,但农历的程序还没有写. - void ReadDS3231()
- {
- int second,minute,hour,date,month,year,week,temperature,NLMonth,NLDate;
- second=Clock.getSecond();
- minute=Clock.getMinute();
- hour=Clock.getHour(h12, PM);
- date=Clock.getDate();
- month=Clock.getMonth(Century);
- year=Clock.getYear();
- week=Clock.getDoW();
- NLMonth = 12; //Chinese date
- NLDate = 13;
- temperature=Clock.getTemperature();
- char sState[1];
- sState[0]='C';
- sprintf(m_s0, "20%02d-%02d-%02d W%1d %2d", year,month,date,week,temperature);
- sprintf(m_s1, "%02d:%02d:%02d %.1s %2d#%2d", hour,minute, second,sState,NLMonth,NLDate);
- // lcd.clear();
- lcd.setCursor(0,0);
- lcd.print(m_s0);
- lcd.setCursor(0,1);
- lcd.print(m_s1);
-
- }
复制代码
另外串口部分也值得说说. 我使用的编码方式是一个"C"后面跟12个数字,表示年月日时分秒. 在分解串时保留了扩展其它命令的可能. - void S_ReadSerial()
- {
- byte bTreated;
- while (Serial.available() > 0) {
- byte inChar = Serial.read();
- bTreated = 0;
- if ((inChar>='a' && inChar<='z') || (inChar>='A' && inChar<='Z')) //command
- {
- m_cCommand=inChar; //record the command
- m_p = 0; //
- m_inString=""; //
- bTreated = 1;//mark treated
- }
- if (isDigit(inChar)) {
- // convert the incoming byte to a char
- // and add it to the string:
- m_inString += (char)inChar;
- m_p=m_p+1;
- bTreated = 2; //mark treated as digit
- if (m_p>=12)
- {
- //send command and number
-
- FCommand(m_cCommand);
- m_p=0;
- m_cCommand='#'; //no command
- }
- }
- if (bTreated==0)
- {
- m_cCommand='#'; //clear Command
- }
- }
- }
复制代码
这部分处理命令,现在只有一个命令. - int FCommand(char c)
- {
- switch (c)
- {
- case 'C': //Change time calibrate
- //split string and go
- S_SetTime();
- break;
- default:
- break;
- }
- }
复制代码
分解字符串和设置时间部分. - void S_SetTime()
- {
- //split the m_inString
- byte y, m, d, h, minute, s;
- y = F_GetValueFromString(0,2);
- m = F_GetValueFromString(2,2);
- d = F_GetValueFromString(4,2);
- h = F_GetValueFromString(6,2);
- minute = F_GetValueFromString(8,2);
- s = F_GetValueFromString(10,2);
- Clock.setSecond(s);//Set the second
- Clock.setMinute(minute);//Set the minute
- Clock.setHour(h); //Set the hour
- Clock.setDate(d); //Set the date of the month
- Clock.setMonth(m); //Set the month of the year
- Clock.setYear(y); //Set the year (Last two digits of the year)
- }
- byte F_GetValueFromString(int p,int len)
- {
- byte v = 0;
- byte i,j;
- for (i=p;i<p+len;i++)
- {
- v=v*10+(m_inString[i]-'0');
- }
- return v;
- }
复制代码
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有帐号?注册
x
|