• Witaj na Forum Arduino Polska! Zapraszamy do rejestracji!
  • Znajdziesz tutaj wiele informacji na temat hardware / software.
Witaj! Logowanie Rejestracja


Ocena wątku:
  • 0 głosów - średnia: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Arduino Nano i RTC- program ustawiający czas
#1
Witam. Robię pierwszy w życiu projekt z Arduino i mam problem programistyczny. 
Arduino i RTC podłączyłem w taki sposób:
[Obrazek: F2_SU9_LOIKYCU3_O2_MEDIUM.jpg] [Obrazek: FFQHP8_SIK4_CJYL8_SMALL.jpg]


Następnie w visuino zrobiłem taki projekt:
[Obrazek: Bez_tytu_u.png]

Visuino wygenerował mi taki oto kod:
Kod:
#define VISUINO_ARDUINO_NANO

#include <OpenWire.h>
#include <Mitov.h>
#include <Mitov_FormattedSerial.h>
#include <Wire.h>
#include <Mitov_Basic_I2C.h>
#include <Mitov_RTC_DS1307.h>

// Arduino Board Declarations

namespace BoardDeclarations
{
Mitov::SerialPort<SERIAL_TYPE, &Serial> SerialPort0;
Mitov::ArduinoSerialObjectInput<Mitov::SerialPort<SERIAL_TYPE, &Serial>, &SerialPort0, Mitov::TDateTime> SerialPort0_DateTime_1;
} // BoardDeclarations

// Declarations

namespace Declarations
{
Mitov::RTC_DS1307 RealTimeClock1;
} // Declarations

//The setup function is called once at startup of the sketch
void setup()
{
 Wire.begin();
 Declarations::RealTimeClock1.OutputPin.Connect( BoardDeclarations::SerialPort0_DateTime_1.InputPin );

 OpenWire::Component::_SystemInit();
}

// The loop function is called in an endless loop
void loop()
{
 OpenWire::Component::_SystemLoop();
}



Rozumiem, że w powyższym kodzie opisane są dokładnie piny po jakich jest połącznie pomiędzy Arduino i RTC. 
Następnie zalazłem gotowy program, który pobiera czas z komputera i zapisuje go i oczywiście RTC ma go podtrzymywać. Program jaki zanazłem próbowałem scalić z tym co wygenerował mi visuino ( czyli np. to co było w loop() to wkleiłem do powyższego kodu też do loop() itd.)  Niestety mam problem, ponieważ moje arduino nie chce połączyć się z RTC.  Poniżej zamieszczam kod, który znalazłem i chciałem scalić. I teraz wielka prośba ? Moze ktoś mi napisać w jaki sposób poprawnie scalić te dwa programy ?

Kod:
#include <DS1307RTC.h>
#include <TimeLib.h>  
#include <Time.h>
#include <Wire.h>

const char *monthName[12] = {
 "Jan", "Feb", "Mar", "Apr", "May", "Jun",
 "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"
};

tmElements_t tm;

void setup() {
 bool parse=false;
 bool config=false;

 // get the date and time the compiler was run
 if (getDate(__DATE__) && getTime(__TIME__)) {
   parse = true;
   // and configure the RTC with this info
   if (RTC.write(tm)) {
     config = true;
   }
 }

 Serial.begin(9600);
 while (!Serial) ; // wait for Arduino Serial Monitor
 delay(200);
 if (parse && config) {
   Serial.print("DS1307 configured Time=");
   Serial.print(__TIME__);
   Serial.print(", Date=");
   Serial.println(__DATE__);
 } else if (parse) {
   Serial.println("DS1307 Communication Error :-{");
   Serial.println("Please check your circuitry");
 } else {
   Serial.print("Could not parse info from the compiler, Time=\"");
   Serial.print(__TIME__);
   Serial.print("\", Date=\"");
   Serial.print(__DATE__);
   Serial.println("\"");
 }
}

void loop() {
}

bool getTime(const char *str)
{
 int Hour, Min, Sec;

 if (sscanf(str, "%d:%d:%d", &Hour, &Min, &Sec) != 3) return false;
 tm.Hour = Hour;
 tm.Minute = Min;
 tm.Second = Sec;
 return true;
}

bool getDate(const char *str)
{
 char Month[12];
 int Day, Year;
 uint8_t monthIndex;

 if (sscanf(str, "%s %d %d", Month, &Day, &Year) != 3) return false;
 for (monthIndex = 0; monthIndex < 12; monthIndex++) {
   if (strcmp(Month, monthName[monthIndex]) == 0) break;
 }
 if (monthIndex >= 12) return false;
 tm.Day = Day;
 tm.Month = monthIndex + 1;
 tm.Year = CalendarYrToTm(Year);
 return true;
}
 
Odpowiedź
  


Skocz do:


Przeglądający: 1 gości