![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Demo Clock with Nucleo-F303RE and Starter Shield
Dependencies: DS1307 TM1636 mbed
main.cpp@0:7dfbdbd8aa83, 2017-05-18 (annotated)
- Committer:
- rogerzuber
- Date:
- Thu May 18 08:03:59 2017 +0000
- Revision:
- 0:7dfbdbd8aa83
- Child:
- 1:dacca338a30b
Demo Clock with Nucleo-F303RE and Starter Shield
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rogerzuber | 0:7dfbdbd8aa83 | 1 | #include "mbed.h" |
rogerzuber | 0:7dfbdbd8aa83 | 2 | #include "ds1307.h" |
rogerzuber | 0:7dfbdbd8aa83 | 3 | #include "TM1636.h" |
rogerzuber | 0:7dfbdbd8aa83 | 4 | |
rogerzuber | 0:7dfbdbd8aa83 | 5 | DigitalOut myled(D2); |
rogerzuber | 0:7dfbdbd8aa83 | 6 | DigitalIn button_K1(D9); |
rogerzuber | 0:7dfbdbd8aa83 | 7 | |
rogerzuber | 0:7dfbdbd8aa83 | 8 | Serial pc(USBTX, USBRX); // tx, rx for debug and usb pc comunications |
rogerzuber | 0:7dfbdbd8aa83 | 9 | |
rogerzuber | 0:7dfbdbd8aa83 | 10 | DS1307 my1307(I2C_SDA, I2C_SCL); // DS1307 Objekt für Zugriff auf RTC auf Shield (Pins I2C) |
rogerzuber | 0:7dfbdbd8aa83 | 11 | // Verbindung zwischen Shield und Nucleo A4-D14 und A5-D15 zwingend nötig |
rogerzuber | 0:7dfbdbd8aa83 | 12 | |
rogerzuber | 0:7dfbdbd8aa83 | 13 | TM1636 tm1636(D7,D8); // Display driver for 7-Segment-Display |
rogerzuber | 0:7dfbdbd8aa83 | 14 | |
rogerzuber | 0:7dfbdbd8aa83 | 15 | int sec = 0; |
rogerzuber | 0:7dfbdbd8aa83 | 16 | int min = 0; |
rogerzuber | 0:7dfbdbd8aa83 | 17 | int hours = 0; |
rogerzuber | 0:7dfbdbd8aa83 | 18 | int day = 0; |
rogerzuber | 0:7dfbdbd8aa83 | 19 | int date = 0; |
rogerzuber | 0:7dfbdbd8aa83 | 20 | int month = 0; |
rogerzuber | 0:7dfbdbd8aa83 | 21 | int year = 0; |
rogerzuber | 0:7dfbdbd8aa83 | 22 | |
rogerzuber | 0:7dfbdbd8aa83 | 23 | int8_t temp[4]; |
rogerzuber | 0:7dfbdbd8aa83 | 24 | |
rogerzuber | 0:7dfbdbd8aa83 | 25 | void test_rw(int test) { |
rogerzuber | 0:7dfbdbd8aa83 | 26 | if (test == 0) { |
rogerzuber | 0:7dfbdbd8aa83 | 27 | // pc.printf("Last R/W operaion passed!\n\r"); |
rogerzuber | 0:7dfbdbd8aa83 | 28 | } |
rogerzuber | 0:7dfbdbd8aa83 | 29 | else { |
rogerzuber | 0:7dfbdbd8aa83 | 30 | pc.printf("Last R/W operation failed!\n\r"); |
rogerzuber | 0:7dfbdbd8aa83 | 31 | } |
rogerzuber | 0:7dfbdbd8aa83 | 32 | } |
rogerzuber | 0:7dfbdbd8aa83 | 33 | |
rogerzuber | 0:7dfbdbd8aa83 | 34 | |
rogerzuber | 0:7dfbdbd8aa83 | 35 | int main() { |
rogerzuber | 0:7dfbdbd8aa83 | 36 | |
rogerzuber | 0:7dfbdbd8aa83 | 37 | button_K1.mode(PullUp); |
rogerzuber | 0:7dfbdbd8aa83 | 38 | tm1636.init(); |
rogerzuber | 0:7dfbdbd8aa83 | 39 | int junk = 0; |
rogerzuber | 0:7dfbdbd8aa83 | 40 | |
rogerzuber | 0:7dfbdbd8aa83 | 41 | while(1){ |
rogerzuber | 0:7dfbdbd8aa83 | 42 | |
rogerzuber | 0:7dfbdbd8aa83 | 43 | if (!button_K1) |
rogerzuber | 0:7dfbdbd8aa83 | 44 | { |
rogerzuber | 0:7dfbdbd8aa83 | 45 | test_rw(my1307.settime( 00, 00, 12, 3, 17, 5, 17)); |
rogerzuber | 0:7dfbdbd8aa83 | 46 | pc.printf("time is set to 12:00:00 - 17.05.2017\n\r"); |
rogerzuber | 0:7dfbdbd8aa83 | 47 | |
rogerzuber | 0:7dfbdbd8aa83 | 48 | junk = 0x39; // just a junk value do read and write test to DS1307 ram |
rogerzuber | 0:7dfbdbd8aa83 | 49 | test_rw(my1307.write( 0x20, junk)); // this should write the value of junk to register 0x20 (a ram location) in the ds1307. |
rogerzuber | 0:7dfbdbd8aa83 | 50 | pc.printf("Value written to register 0x20 %.2X \n\r",junk); |
rogerzuber | 0:7dfbdbd8aa83 | 51 | } |
rogerzuber | 0:7dfbdbd8aa83 | 52 | else |
rogerzuber | 0:7dfbdbd8aa83 | 53 | { |
rogerzuber | 0:7dfbdbd8aa83 | 54 | test_rw(my1307.gettime( &sec, &min, &hours, &day, &date, &month, &year)); |
rogerzuber | 0:7dfbdbd8aa83 | 55 | pc.printf("time is %.2D.%.2D.%.2D - %.2D-%.2D-20%.2D \r", hours, min, sec, date, month, year); |
rogerzuber | 0:7dfbdbd8aa83 | 56 | |
rogerzuber | 0:7dfbdbd8aa83 | 57 | temp[0] = hours / 10; |
rogerzuber | 0:7dfbdbd8aa83 | 58 | temp[1] = hours % 10; |
rogerzuber | 0:7dfbdbd8aa83 | 59 | temp[2] = min / 10; |
rogerzuber | 0:7dfbdbd8aa83 | 60 | temp[3] = min % 10; |
rogerzuber | 0:7dfbdbd8aa83 | 61 | tm1636.display(temp); |
rogerzuber | 0:7dfbdbd8aa83 | 62 | |
rogerzuber | 0:7dfbdbd8aa83 | 63 | //junk = 0; // clear junk to show that when the register is read from the correct value is obtained |
rogerzuber | 0:7dfbdbd8aa83 | 64 | //test_rw(my1307.read( 0x20, &junk)); // this should read register 0x20 |
rogerzuber | 0:7dfbdbd8aa83 | 65 | //pc.printf("Value read from register 0x20 %.2X \n\r",junk); |
rogerzuber | 0:7dfbdbd8aa83 | 66 | } |
rogerzuber | 0:7dfbdbd8aa83 | 67 | // if ((sec % 2 == 1)) |
rogerzuber | 0:7dfbdbd8aa83 | 68 | // { |
rogerzuber | 0:7dfbdbd8aa83 | 69 | // tm1636.point(true); |
rogerzuber | 0:7dfbdbd8aa83 | 70 | // } |
rogerzuber | 0:7dfbdbd8aa83 | 71 | // else |
rogerzuber | 0:7dfbdbd8aa83 | 72 | // { |
rogerzuber | 0:7dfbdbd8aa83 | 73 | // tm1636.point(false); |
rogerzuber | 0:7dfbdbd8aa83 | 74 | // } |
rogerzuber | 0:7dfbdbd8aa83 | 75 | wait(1.0); |
rogerzuber | 0:7dfbdbd8aa83 | 76 | myled = ! myled; |
rogerzuber | 0:7dfbdbd8aa83 | 77 | } |
rogerzuber | 0:7dfbdbd8aa83 | 78 | } |