testing app for DS1307 RTC and 1-wire driver for DS8B20

Dependencies:   DS1307 OneWire mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "DS1307.h"
00003 #include "1wire.h"
00004 
00005 DS1307 rtc(PTE0, PTE1);
00006 OneWire ow(A4, A2, A3);
00007 
00008 Serial pc(USBTX, USBRX, "pc");
00009 
00010 
00011 int main() 
00012 {
00013     Timer t;
00014     char c;
00015 
00016     pc.printf("\n\n\n\n*************************************\n");
00017     pc.printf("* r - reads the clock\n");
00018     pc.printf("* w - write the clock\n");
00019     pc.printf("* c - convert temperatures\n");
00020     pc.printf("* t - search 1wire and list read temperatures\n");
00021     pc.printf("*************************************\n\n");
00022     
00023     while (1) 
00024     {
00025         c = pc.getc();
00026         if (c == 'r') {
00027             //  perform read
00028             t.reset();
00029             t.start();
00030             time_t m_time = rtc.now();
00031             t.stop();
00032 
00033             struct tm *now;
00034             now = localtime(&m_time);
00035             
00036             pc.printf("Current time is %lu, %02d:%02d:%02d, %d.%d.%04d\n", 
00037                 m_time, 
00038                 now->tm_hour, now->tm_min, now->tm_sec, 
00039                 now->tm_mday, now->tm_mon+1, now->tm_year+1900
00040             );
00041             pc.printf("Internal datetime format is %s\n", asctime(now));
00042             pc.printf("Read complete, elapsed %uus\n", t.read_us());
00043             
00044         }
00045         else if (c == 'w') {
00046             //  perform write
00047             int date, month, year, hours, minutes, seconds;
00048             pc.printf("Enter the date (date 1..31)\n"); pc.scanf("%d", &date);
00049             pc.printf("Enter the date (month 1..12)\n"); pc.scanf("%d", &month);
00050             pc.printf("Enter the date (year >2000)\n"); pc.scanf("%d", &year);
00051             pc.printf("Enter the time (hours 0..23)\n"); pc.scanf("%d", &hours);
00052             pc.printf("Enter the time (minutes 0..59)\n"); pc.scanf("%d", &minutes);
00053             pc.printf("Enter the time (seconds 0..59)\n"); pc.scanf("%d", &seconds);
00054             
00055             struct tm now = {seconds, minutes, hours, date, month-1, year-1900};
00056             time_t m_time = mktime(&now);
00057                 
00058             t.reset();
00059             t.start();
00060             bool b = rtc.set_time(m_time);
00061             t.stop();
00062             
00063             pc.printf("Write complete (UNIX %lu, result %d), elapsed %uus\n", m_time, b, t.read_us());
00064         }
00065         else if (c == 'c') {
00066             ow.ConvertAll(true);
00067             pc.printf("Convert done\n");
00068         }
00069         else if (c == 't') {
00070             int result, temp;
00071             uint8_t rom[8];
00072             
00073             memset(rom, 0, sizeof(rom));
00074             result = ow.First(rom);
00075             while (result == OW_FOUND) {
00076                 for (int i = 0; i < 8; i++) pc.printf("%.2X", rom[i]);
00077                 
00078                 t.reset();
00079                 t.start();
00080                 int b = ow.ReadTemperature(rom, &temp);
00081                 t.stop();
00082                 if (b) pc.printf(": read failed, code 0x%.4x, elapsed %uus\n", b, t.read_us());
00083                 else pc.printf(": read ok, temperature %.2f'C, elapsed %uus\n", temp / 100., t.read_us());
00084                 
00085                 result = ow.Next(rom);
00086             }
00087             pc.printf("Done\n");
00088         }
00089         else {
00090             pc.printf("Syntax error\n");
00091         }
00092         pc.printf("\n");
00093     }
00094 }