Code for my numitron clock. Based on the STM32F042 and using a TLC5916 per numitron to drive them.

Dependencies:   SWSPI mbed-dev

Files at this revision

API Documentation at this revision

Comitter:
riktw
Date:
Sun Dec 25 09:52:54 2016 +0000
Parent:
0:d1558f6f88bf
Child:
2:e1d75e2ab77d
Commit message:
Rev 0.2 PCB, added external XTAL that is used for the RTC

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-dev.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show diff for this revision Revisions of this file
--- a/main.cpp	Sat Nov 12 08:25:50 2016 +0000
+++ b/main.cpp	Sun Dec 25 09:52:54 2016 +0000
@@ -1,18 +1,19 @@
-#include "mbed.h"
+ #include "mbed.h"
 #include "SWSPI.h"
 
-SWSPI spi( PA_2, PA_1, PA_3 );                          // create the fake SPI port
-DigitalOut OE(PA_0);                                    // Output Enable
-DigitalOut LE(PA_4);                                    // The LE pin and the number of connected chips
-BusIn buttons(PA_5, PA_6, PA_7, PB_1);                  // The 4 user buttons
-// All the digits places in an array, 0-9.
+SWSPI spi( PA_2, PA_1, PA_3 );              // create the SPI port
+DigitalOut OE(PA_0);
+DigitalOut LE(PA_4);                                    // the LE pin and the number of connected chips
+DigitalIn B1(PA_5), B2(PA_6), B3(PA_7), B4(PB_1);       //B1 is the most left button
+BusIn buttons(PA_5, PA_6, PA_7, PB_1);
 const uint8_t numbers[10] = {0x7B, 0x60, 0x57, 0x76, 0x6C, 0x3E, 0x3F, 0x70, 0x7F, 0x7E};
+uint8_t flip = 1;
 
-// A flag to control the blinking dot.
-uint8_t flipflag = 1;
+RTC_HandleTypeDef hrtc;
+HAL_StatusTypeDef status;
+void SystemClock_Config(void);
+static void MX_RTC_Init(void);
 
-// Struct that contains all the numitron values. 
-// Even if this clock only uses 4 numitrons, the struct is already prepared for 6.
 typedef struct
     {
         uint8_t second_right;
@@ -22,25 +23,22 @@
         uint8_t hour_right;
         uint8_t hour_left;
     }clock_typ; 
-clock_typ numiclock;
 
-// Variables for getting time
 struct tm t;
 time_t rawtime;
-
-// Ticker to update the clock.
+clock_typ numiclock;
 Ticker secondticker; 
+void secondtick(void); 
 void updatetime(void);
-
 int main() 
 {
+    MX_RTC_Init();
     static struct tm *t3;
     buttons.mode(PullUp);
 
     LE = 0;
     OE = 1;
 
-    // Set the time on 00:00:00 @ 1 jan 2016
     t.tm_sec = 0;
     t.tm_min = 0;
     t.tm_hour = 0;
@@ -49,36 +47,34 @@
     t.tm_year = 2016-1900;
     set_time(mktime(&t));
 
-    // Clear the displays
+    rawtime = time(NULL);
+
     spi.write(0x00);
     spi.write(0x00);
     spi.write(0x00);
     spi.write(0x00);
 
-    // Toggle LE to latch in the values in the TLC5916
     LE = 1;
     wait_us(10);
     LE = 0;
     wait_us(10);
 
-    OE = 0;     // Enable the displays
-    secondticker.attach(&updatetime, 1);
-    
+    OE = 0;
+    secondticker.attach(&updatetime, 2);
     while( 1 )
-    {   // If any button is pressed and after 50ms still is pressed, count it as a press.
+    {   
         uint8_t bpressed = buttons;
         wait_ms(50);
         if((bpressed == buttons) && (bpressed != 0xf))
         {
-            // Get the current time, see what button is pressed, update the time and use this as the new time.
             rawtime = time(NULL);
             t3 = localtime(&rawtime);
-            bpressed = (~bpressed)&0x0f;    // The buttons are high when unpressed, invert it to make the code a bit easier.
+            bpressed = (~bpressed)&0x0f;
             switch(bpressed)
             {
-                case 0x01:                  // Button most left pressed, add 10 hours to the current time
+                case 0x01:
                     t3->tm_hour += 10;
-                    if(t3->tm_hour >= 30)   // Keep in mind that the maximum for hours is 23.
+                    if(t3->tm_hour >= 30)
                     {
                         t3->tm_hour -= 30;
                     }
@@ -87,7 +83,7 @@
                         t3->tm_hour -= 20;
                     }
                 break;
-                case 0x02:                  // Button second left, add 1 hour
+                case 0x02:
                     t3->tm_hour += 1;
                     if((t3->tm_hour%10) == 0)
                     {
@@ -98,14 +94,14 @@
                         t3->tm_hour -= 4;
                     }                       
                 break;
-                case 0x04:                  // Button second right, add 10 minutes
+                case 0x04:
                     t3->tm_min += 10;
                     if(t3->tm_min >= 60)
                     {
                         t3->tm_min -= 60;
                     }
-                break;  
-                case 0x08:                  // Button most right, add 1 minute.
+                break;
+                case 0x08:
                     t3->tm_min += 1;
                     if((t3->tm_min%10) == 0)
                     {
@@ -114,17 +110,14 @@
                 break;
             }
             set_time(mktime(t3));
-            flipflag = 0;                   // Update the display without the blinking dot.
-            updatetime();               
-            flipflag = 1;
-            wait_ms(150);                   // extra delay so a press is only seen once.
+            flip = 0;
+            updatetime();
+            flip = 1;
+            wait_ms(150);
         }
     }
 }
 
-// When called this function retrieves the current time
-// and places this in the numiclock struct.
-// It then sends the data to the SPI bus to display the change
 void updatetime(void)
 {
     static uint8_t flipbit;
@@ -140,7 +133,7 @@
     numiclock.hour_left = (t2->tm_hour-numiclock.hour_right)/10;
 
     spi.write(numbers[numiclock.hour_left]);
-    if(flipflag)
+    if(flip)
     {
         if(flipbit == 1)
         {
@@ -165,3 +158,38 @@
     LE = 0;
     wait_us(10); 
 }    
+ 
+/* RTC init function */
+void MX_RTC_Init(void)
+{
+ 
+  RTC_TimeTypeDef sTime;
+  RTC_DateTypeDef sDate;
+ 
+    /**Initialize RTC and set the Time and Date
+    */
+  hrtc.Instance = RTC;
+  hrtc.Init.HourFormat = RTC_HOURFORMAT_24;
+  hrtc.Init.AsynchPrediv = 124;
+  hrtc.Init.SynchPrediv = 1999;
+  hrtc.Init.OutPut = RTC_OUTPUT_DISABLE;
+  hrtc.Init.OutPutPolarity = RTC_OUTPUT_POLARITY_HIGH;
+  hrtc.Init.OutPutType = RTC_OUTPUT_TYPE_OPENDRAIN;
+  status = HAL_RTC_Init(&hrtc);
+ 
+  sTime.Hours = 0x0;
+  sTime.Minutes = 0x0;
+  sTime.Seconds = 0x0;
+  sTime.DayLightSaving = RTC_DAYLIGHTSAVING_NONE;
+  sTime.StoreOperation = RTC_STOREOPERATION_RESET;
+  HAL_RTC_SetTime(&hrtc, &sTime, RTC_FORMAT_BCD);
+ 
+  sDate.WeekDay = RTC_WEEKDAY_MONDAY;
+  sDate.Month = RTC_MONTH_JANUARY;
+  sDate.Date = 0x1;
+  sDate.Year = 0x0;
+ 
+  HAL_RTC_SetDate(&hrtc, &sDate, RTC_FORMAT_BCD);
+ 
+}
+
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-dev.lib	Sun Dec 25 09:52:54 2016 +0000
@@ -0,0 +1,1 @@
+http://developer.mbed.org/users/mbed_official/code/mbed-dev/#4ad7ad749c0c
--- a/mbed.bld	Sat Nov 12 08:25:50 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/25aea2a3f4e3
\ No newline at end of file