An analog clock for LPC1768 or LPC1114 with Sharp Memory LCD and DS1307

Dependencies:   Adafruit_SHARP_Memory_Display RTC-DS1307 mbed

Analog Clock

This is a small example of an analog clock based on a TinyRTC (DS1307) for LPC1768 and LPC1114. The clock is displayed on a small (96x96) Sharp Memory LCD. http://www.adafruit.com/products/1393 The TinyRTC is something like that (a simple DS1307 with some capacitors and a crystal does the job too): http://www.amazon.com/SainSmart-DS1307-AT24C32-memory-Arduino/dp/B00E37VTWY The RTC-lib created by Henry Leinen is used and the algorithm for calculation the clock-hands is based on input from http://arduino-project.net. /media/uploads/worstcase_ffm/analog_clock.png

Committer:
worstcase_ffm
Date:
Sun Dec 06 15:28:18 2015 +0000
Revision:
0:202fb7a03a00
initial version

Who changed what in which revision?

UserRevisionLine numberNew contents of line
worstcase_ffm 0:202fb7a03a00 1 /*
worstcase_ffm 0:202fb7a03a00 2 * Project: Analog Clock - Display an analog clock with NXP LPC1768 or LPC1114
worstcase_ffm 0:202fb7a03a00 3 * File: main.cpp
worstcase_ffm 0:202fb7a03a00 4 * Author: Steve Baumann
worstcase_ffm 0:202fb7a03a00 5 * Created: December, 2015
worstcase_ffm 0:202fb7a03a00 6 * Revised:
worstcase_ffm 0:202fb7a03a00 7 * Description:
worstcase_ffm 0:202fb7a03a00 8 * --------------
worstcase_ffm 0:202fb7a03a00 9 * The time read from tinyRTC (DS1307) will be displayed on a 96x96 Sharp Memory LCD
worstcase_ffm 0:202fb7a03a00 10 * LS013B4DN04 (bought from Adafruit http://www.adafruit.com/products/1393).
worstcase_ffm 0:202fb7a03a00 11 * The RTC-lib created by Henry Leinen is used and the algorithm for calculation the
worstcase_ffm 0:202fb7a03a00 12 * clock-hands is based on input from http://arduino-project.net
worstcase_ffm 0:202fb7a03a00 13 * The RTC itself was bought via Amazon http://www.amazon.de/gp/product/B00CWX6UXY/
worstcase_ffm 0:202fb7a03a00 14 */
worstcase_ffm 0:202fb7a03a00 15
worstcase_ffm 0:202fb7a03a00 16 #include <algorithm>
worstcase_ffm 0:202fb7a03a00 17 #include "mbed.h"
worstcase_ffm 0:202fb7a03a00 18 #include "Adafruit_SharpMem.h"
worstcase_ffm 0:202fb7a03a00 19 #include "Rtc_Ds1307.h"
worstcase_ffm 0:202fb7a03a00 20
worstcase_ffm 0:202fb7a03a00 21 #ifdef TARGET_LPC176X
worstcase_ffm 0:202fb7a03a00 22 // Display LS013B4DN04
worstcase_ffm 0:202fb7a03a00 23 #define ENABLE p15 // DISP
worstcase_ffm 0:202fb7a03a00 24 #define CS p8 // CS
worstcase_ffm 0:202fb7a03a00 25 #define MOSI p5 // DI
worstcase_ffm 0:202fb7a03a00 26 #define MISO p6 // not used (no wiring necessary)
worstcase_ffm 0:202fb7a03a00 27 #define SCLK p7 // CLK
worstcase_ffm 0:202fb7a03a00 28 // RTC 1307
worstcase_ffm 0:202fb7a03a00 29 #define SDA p28 // SDA for RTC
worstcase_ffm 0:202fb7a03a00 30 #define SCL p27 // SCL for RTC
worstcase_ffm 0:202fb7a03a00 31 #endif
worstcase_ffm 0:202fb7a03a00 32
worstcase_ffm 0:202fb7a03a00 33 #ifdef TARGET_LPC11XX
worstcase_ffm 0:202fb7a03a00 34 // Display LS013B4DN04
worstcase_ffm 0:202fb7a03a00 35 #define ENABLE dp9 // DISP
worstcase_ffm 0:202fb7a03a00 36 #define CS dp10 // CS
worstcase_ffm 0:202fb7a03a00 37 #define MOSI dp2 // DI
worstcase_ffm 0:202fb7a03a00 38 #define MISO dp1 // not used (no wiring necessary)
worstcase_ffm 0:202fb7a03a00 39 #define SCLK dp6 // CLK
worstcase_ffm 0:202fb7a03a00 40 // RTC 1307
worstcase_ffm 0:202fb7a03a00 41 #define SDA dp5 // SDA for RTC
worstcase_ffm 0:202fb7a03a00 42 #define SCL dp27 // SCL for RTC
worstcase_ffm 0:202fb7a03a00 43 #endif
worstcase_ffm 0:202fb7a03a00 44
worstcase_ffm 0:202fb7a03a00 45 Rtc_Ds1307 rtc(SDA, SCL);
worstcase_ffm 0:202fb7a03a00 46 Adafruit_SharpMem display(ENABLE, CS, MOSI, MISO, SCLK);
worstcase_ffm 0:202fb7a03a00 47
worstcase_ffm 0:202fb7a03a00 48 void drawClockMark(int hour)
worstcase_ffm 0:202fb7a03a00 49 {
worstcase_ffm 0:202fb7a03a00 50 float x1, y1, x2, y2;
worstcase_ffm 0:202fb7a03a00 51
worstcase_ffm 0:202fb7a03a00 52 hour = hour*30;
worstcase_ffm 0:202fb7a03a00 53 hour = hour+270;
worstcase_ffm 0:202fb7a03a00 54
worstcase_ffm 0:202fb7a03a00 55 x1 = 48*cos(hour*0.0175);
worstcase_ffm 0:202fb7a03a00 56 y1 = 48*sin(hour*0.0175);
worstcase_ffm 0:202fb7a03a00 57 x2 = 43*cos(hour*0.0175);
worstcase_ffm 0:202fb7a03a00 58 y2 = 43*sin(hour*0.0175);
worstcase_ffm 0:202fb7a03a00 59
worstcase_ffm 0:202fb7a03a00 60 display.drawLine(x1+48, y1+48, x2+48, y2+48, BLACK);
worstcase_ffm 0:202fb7a03a00 61 }
worstcase_ffm 0:202fb7a03a00 62
worstcase_ffm 0:202fb7a03a00 63 void drawClock()
worstcase_ffm 0:202fb7a03a00 64 {
worstcase_ffm 0:202fb7a03a00 65 // Values for radius and positioning of labels need to be adapt for other displays
worstcase_ffm 0:202fb7a03a00 66 display.setTextSize(1);
worstcase_ffm 0:202fb7a03a00 67 display.setTextColor(BLACK);
worstcase_ffm 0:202fb7a03a00 68
worstcase_ffm 0:202fb7a03a00 69 display.drawCircle(display.width()/2, display.height()/2, 48, BLACK);
worstcase_ffm 0:202fb7a03a00 70 display.setCursor(88,44);
worstcase_ffm 0:202fb7a03a00 71 display.printf("3");
worstcase_ffm 0:202fb7a03a00 72 display.setCursor(46,88);
worstcase_ffm 0:202fb7a03a00 73 display.printf("6");
worstcase_ffm 0:202fb7a03a00 74 display.setCursor(3,44);
worstcase_ffm 0:202fb7a03a00 75 display.printf("9");
worstcase_ffm 0:202fb7a03a00 76 display.setCursor(43,3);
worstcase_ffm 0:202fb7a03a00 77 display.printf("12");
worstcase_ffm 0:202fb7a03a00 78
worstcase_ffm 0:202fb7a03a00 79 for (int i=0; i<12; i++)
worstcase_ffm 0:202fb7a03a00 80 {
worstcase_ffm 0:202fb7a03a00 81 if((i%3) != 0) drawClockMark(i);
worstcase_ffm 0:202fb7a03a00 82 }
worstcase_ffm 0:202fb7a03a00 83 }
worstcase_ffm 0:202fb7a03a00 84
worstcase_ffm 0:202fb7a03a00 85 void drawSecond(int s)
worstcase_ffm 0:202fb7a03a00 86 {
worstcase_ffm 0:202fb7a03a00 87 float x1, y1, x2, y2;
worstcase_ffm 0:202fb7a03a00 88
worstcase_ffm 0:202fb7a03a00 89 s = s*6;
worstcase_ffm 0:202fb7a03a00 90 s = s+270;
worstcase_ffm 0:202fb7a03a00 91
worstcase_ffm 0:202fb7a03a00 92 x1 = 45*cos(s*0.0175);
worstcase_ffm 0:202fb7a03a00 93 y1 = 45*sin(s*0.0175);
worstcase_ffm 0:202fb7a03a00 94 x2 = 4*cos(s*0.0175);
worstcase_ffm 0:202fb7a03a00 95 y2 = 4*sin(s*0.0175);
worstcase_ffm 0:202fb7a03a00 96
worstcase_ffm 0:202fb7a03a00 97 display.drawLine(x1+48, y1+48, x2+48, y2+48, BLACK);
worstcase_ffm 0:202fb7a03a00 98 }
worstcase_ffm 0:202fb7a03a00 99
worstcase_ffm 0:202fb7a03a00 100 void drawMinute(int m)
worstcase_ffm 0:202fb7a03a00 101 {
worstcase_ffm 0:202fb7a03a00 102 float x1, y1, x2, y2;
worstcase_ffm 0:202fb7a03a00 103
worstcase_ffm 0:202fb7a03a00 104 m = m*6;
worstcase_ffm 0:202fb7a03a00 105 m = m+270;
worstcase_ffm 0:202fb7a03a00 106
worstcase_ffm 0:202fb7a03a00 107 x1 = 45*cos(m*0.0175);
worstcase_ffm 0:202fb7a03a00 108 y1 = 45*sin(m*0.0175);
worstcase_ffm 0:202fb7a03a00 109 x2 = 4*cos(m*0.0175);
worstcase_ffm 0:202fb7a03a00 110 y2 = 4*sin(m*0.0175);
worstcase_ffm 0:202fb7a03a00 111
worstcase_ffm 0:202fb7a03a00 112 display.drawLine(x1+48, y1+48, x2+48, y2+48, BLACK);
worstcase_ffm 0:202fb7a03a00 113 }
worstcase_ffm 0:202fb7a03a00 114
worstcase_ffm 0:202fb7a03a00 115 void drawHour(int h, int m)
worstcase_ffm 0:202fb7a03a00 116 {
worstcase_ffm 0:202fb7a03a00 117 float x1, y1, x2, y2;
worstcase_ffm 0:202fb7a03a00 118
worstcase_ffm 0:202fb7a03a00 119 h = (h*30)+(m/2);
worstcase_ffm 0:202fb7a03a00 120 h = h+270;
worstcase_ffm 0:202fb7a03a00 121
worstcase_ffm 0:202fb7a03a00 122 x1 = 30*cos(h*0.0175);
worstcase_ffm 0:202fb7a03a00 123 y1 = 30*sin(h*0.0175);
worstcase_ffm 0:202fb7a03a00 124 x2 = 4*cos(h*0.0175);
worstcase_ffm 0:202fb7a03a00 125 y2 = 4*sin(h*0.0175);
worstcase_ffm 0:202fb7a03a00 126
worstcase_ffm 0:202fb7a03a00 127 display.drawLine(x1+48, y1+48, x2+48, y2+48, BLACK);
worstcase_ffm 0:202fb7a03a00 128 }
worstcase_ffm 0:202fb7a03a00 129
worstcase_ffm 0:202fb7a03a00 130 int main()
worstcase_ffm 0:202fb7a03a00 131 {
worstcase_ffm 0:202fb7a03a00 132 Rtc_Ds1307::Time_rtc tm = {};
worstcase_ffm 0:202fb7a03a00 133
worstcase_ffm 0:202fb7a03a00 134 // start & clear the display
worstcase_ffm 0:202fb7a03a00 135 display.begin();
worstcase_ffm 0:202fb7a03a00 136 display.setRotation(0);
worstcase_ffm 0:202fb7a03a00 137 display.enableDisplay();
worstcase_ffm 0:202fb7a03a00 138 display.clearDisplay();
worstcase_ffm 0:202fb7a03a00 139
worstcase_ffm 0:202fb7a03a00 140 while(1) {
worstcase_ffm 0:202fb7a03a00 141
worstcase_ffm 0:202fb7a03a00 142 drawClock();
worstcase_ffm 0:202fb7a03a00 143
worstcase_ffm 0:202fb7a03a00 144 if (rtc.getTime(tm)) {
worstcase_ffm 0:202fb7a03a00 145 drawSecond(tm.sec);
worstcase_ffm 0:202fb7a03a00 146 drawMinute(tm.min);
worstcase_ffm 0:202fb7a03a00 147 drawHour(tm.hour, tm.min);
worstcase_ffm 0:202fb7a03a00 148 display.refresh();
worstcase_ffm 0:202fb7a03a00 149 } else {
worstcase_ffm 0:202fb7a03a00 150 tm.date = 6; tm.mon = 12; tm.year = 2015;
worstcase_ffm 0:202fb7a03a00 151 tm.hour = 13; tm.min = 50; tm.sec = 0;
worstcase_ffm 0:202fb7a03a00 152 rtc.setTime(tm, false, false);
worstcase_ffm 0:202fb7a03a00 153 rtc.startClock();
worstcase_ffm 0:202fb7a03a00 154 }
worstcase_ffm 0:202fb7a03a00 155 wait(0.5);
worstcase_ffm 0:202fb7a03a00 156 display.clearDisplay();
worstcase_ffm 0:202fb7a03a00 157 }
worstcase_ffm 0:202fb7a03a00 158 }