我正在尝试运行我的程序arduino计时器或RTC。伪代码看起来像这样
如果rtc失败 是的:阅读arduino时间 否则:读rtc时间
我们可以检测RTC中是否有错误我的问题是如何在arduino time上运行arduino board。从哪里停止。
的main.c
#include "glob.h"
#include "rtc.h"
#include "config.h"
void setup()
{
Serial.begin(9600);
rtcSetup();
}
void loop()
{
getRTCDateTime();
convertUTCtoLocal();
Print_Date();
Print_Time();
RTC_Error_Check();
Serial.println("................................");
delay(1000);
}
void Print_Date(){
Serial.print("Local Date:");
Serial.print(local_day);
Serial.print("/");
Serial.print(local_month);
Serial.print("/");
Serial.println(local_year);
}
void Print_Time()
{ Serial.print("local_time is:");
Serial.print(local_h);
Serial.print("-");
Serial.print(local_m);
Serial.print("-");
Serial.println(local_s);
}
的config.h
#ifndef CONFIG_H
#define CONFIG_H
#define DEFAULT_TIMEZONE 5.5;
//
#define USER_DATA //User set time in UTC
#define USER_DAY 3;
#define USER_MONTH 1;
#define USER_YEAR 2013;
#define USER_H 7;// MAKE IT AS 14
#define USER_M 32;// MAKE IT AS 30 FOR SETTING TIME TO 9.0
#define USER_S 0;
#endif
rtc.h
#ifndef RTC_H
#define RTC_H
void rtcSetup();
void getRTCDateTime();
#endif
rtc_c
#include "glob.h"
#include "rtc.h"
#include "Wire.h"
void rtcSetup(){
Wire.begin();
}
#define DS1307_ADDRESS 0x68
byte zero = 0x00; //workaround for issue #527
void getRTCDateTime()
{
// Reset the register pointer
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero);
Wire.endTransmission();
Wire.requestFrom(DS1307_ADDRESS, 7);
s = bcdToDec(Wire.read());
m = bcdToDec(Wire.read());
h = bcdToDec(Wire.read() & 0b111111); //24 hour time
wkDay = bcdToDec(Wire.read()); //0-6 -> sunday - Saturday
dd = bcdToDec(Wire.read());
mm = bcdToDec(Wire.read());
}
void rtc_write(){
Wire.beginTransmission(DS1307_ADDRESS);
Wire.write(zero); //stop Oscillator
Wire.write(decToBcd(s));
Wire.write(decToBcd(m));
Wire.write(decToBcd(h));
Wire.write(decToBcd(wkDay));
Wire.write(decToBcd(dd));
Wire.write(decToBcd(mm));
Wire.write(decToBcd(getLastTwoDigOfYear(yy)));
Wire.write(zero); //start
Wire.endTransmission();
}
byte decToBcd(byte val){
// Convert normal decimal numbers to binary coded decimal
return ( (val/10*16) + (val%10) );
}
byte bcdToDec(byte val) {
// Convert binary coded decimal to normal decimal numbers
return ( (val/16*10) + (val%16) );
}
int getLastTwoDigOfYear(int y){
return(y%1000);
}
void getuserdate()
{
byte s = 45; //0-59
byte m =47; //0-59
byte h =15; //0-23
byte wkDay =2; //1-7
byte dd =20;
byte mm = 3; //1-12
byte yy = 13; //0-99
}
calc.h
#ifndef CALC_H
#define CALC_H
void calcTime();
void convertSecondsToLocalHMS(int secs);
void convertUTCtoLocal();
static int MNTH_DAYS[12]={
31,28,31,30,31,30,31,31,30,31,30,31};
#endif
calc_time.c
#include"timecalc.h"
#include"config.h"
#include "glob.h"
#include "rtc.h"
#include "Time.h"
#include<math.h>
static long tzone_offset_s = tzone*3600UL;
void calcTime(){
#ifndef USER_DATA
getRTCDateTime();
#else
getDefaultDateTime();
#endif
convertUTCtoLocal();
hdec = local_h + float(local_m)/60;//hours in decimal
}
void getDefaultDateTime(){
Serial.println("DEFAULT DATE AND TIME IS USING");
latitude = DEFAULT_LATITUDE;
longitude = DEFAULT_LONGITUDE;
mm = USER_MONTH;
yy = USER_YEAR;
dd = USER_DAY;
h = USER_H;
m = USER_M;
s = USER_S;
}
void convertUTCtoLocal(){
long no_s_day_start= h*3600UL + m*60 + s; //no of seconds since day start
long day_change_cutoff_s = 86400UL-tzone_offset_s;//max utc before changing date
long additional_s;//additional seconds after day change
local_year = yy;
if(no_s_day_start>day_change_cutoff_s){
if(dd+1 > MNTH_DAYS[mm-1]){
local_day = 1;
if(mm==12){
local_month=1;
local_year = yy+1;
}
else
local_month = mm+1;
}
else{
local_day=dd+1;
local_month=mm;
}
additional_s = no_s_day_start - day_change_cutoff_s;
convertSecondsToLocalHMS(additional_s);
}
else{
local_day = dd;
local_month = mm;
convertSecondsToLocalHMS(no_s_day_start+tzone_offset_s);
}
}
void convertSecondsToLocalHMS(long secs){
local_h = secs/3600; //hour
local_m=(secs%3600)/60;//min
local_s = secs - local_h*3600UL - local_m*60;
}
int sign(int v)
{
return v >= 0 ? 1 : -1;
}
diag.h
#ifndef DIAG_H
#define DIAG_H
static int Error_count;
static int RTC_ERROR;
void RTC_Error_Check();
static int ph;
static int pm;
static int ps;
static int start_time;
static int p_time;
static int rtcwrite_count;
#endif
diag.c
#include"glob.h"
#include"rtc.h"
#include"diag.h"
void RTC_Error_Check()
{
int count=0;
Serial.println("take reading fun called");
ph=h;
pm=m;
ps=s;
RTC_ERROR=Check_RTC();
if(RTC_ERROR=0)
{
Error_count=0;
}else
{
Error_count=Error_count+1;
}
if(Error_count==6)
{
Get_Ard_time();ps=ps+1;
}
}
void Get_Ard_time()
{
ph=h;
pm=m;
ps=s;
start_time=(3600*h)+(60*m)+s;
Serial.println("start time is:");
Serial.println(start_time);
Serial.println("previous time is:");
Serial.println(p_time);
if(start_time==p_time)
{
h=ph;
m=pm;
s=ps;
if(s>59)
{s=0;
m=m+1;
}
if(m>59)
{ m=0;
h=h+1;
}
if(h>23)
{
h=0;}
rtcWrite();
p_time=start_time;
}else
{
getRTCDateTime();
}
}