7 years, 8 months ago.

How to make a alarm clock and real time clock for Nucleo-F429ZI

1. I've no idea how to write an alarm clock code. If the alarm clock is work, the question 2 can skip. thanks.

2. I've a timer using a button but how to start countdown the timer?

3. I'm trying to get a real time clock on the internet but the year always start from 1970. How can I synchronise with the real time?

this is my code, please have a look /media/uploads/jackypun/timernucleo_f429zi_-4-.bin

please give some advice. Thank you very much!!

  1. include "mbed.h"
  2. include "EthernetInterface.h"
  3. include "TextLCD.h"
  4. include "NTPClient.h"
  5. include "string.h"
  6. include "stdlib.h"

EthernetInterface eth; NTPClient ntp; TextLCD lcd(D4,D5,D6,D7,D8,D9); InterruptIn switch1(D14); InterruptIn switch2(D15); InterruptIn switch3(D13); DigitalOut led(LED1); Ticker sw_check;

float frequency[]=

time_t ctTime; int start = 0; int dh = 0; int h = 0; int m = 0; int s = 0; int start_time_h =0; int start_time_m =0; int start_time_s = 0; int old_time_h = 0; int old_time_m = 0; int old_time_s =0; int stop_time_h =0; int stop_time_m = 0; int stop_time_s = 0; int timer_h = 0; int timer_m =0; int timer_s =0; int time_diff =0; char buffer [80]; char hour [2]; char mins [4]; char sec [6]; struct tm * current_time;

void start_stop() { if (start == 1){ start = 0; } else start = 1; start_time_h = h; start_time_m = m; start_time_s = s; }

void reset() { timer_h = 0; timer_m = 0; timer_s = 0; old_time_h = 0; old_time_m = 0; old_time_s = 0; }

int SW0,SW1,SW2,BUTTON;

void check_sw() called every 5 msec { int work;

SW0 = SW1; shift previous status SW1 = SW2; shift previous status get new status SW2 = switch1.read()+(switch2.read()<<1); following sequence detects switch press edge HIGH(SW0) -> LOW(SW1) -> LOW(SW2) work = (SW0&SW1)&SW2; if (work) { BUTTON |= work; update button status } if }

int main() { int hours, minutes, seconds, position;

hours=12; minutes=50; seconds=31; position=0;

switch1.mode(PullUp); switch2.mode(PullUp); switch3.mode(PullUp); SW0 = 0x0f; SW1 = 0x0f; SW2 = 0x0f; BUTTON = 0;

sw_check.attach_us(&check_sw, 5000); status check every 5 msec

lcd.cls(); lcd.writeCommand(0x0D); blink cursor switch1.rise(&start_stop); switch2.rise(&reset);

Setup NTP connection printf("STARTING......\n\r"); eth.init(); Use DHCP

eth.connect();

printf("Trying to update time...\r\n"); if (ntp.setTime("time-b.nist.gov", 123) == 0) { "0.pool.ntp.org" "176.58.127.165" time()=12345; printf("Set time successfully\r\n"); time_t ctTime; ctTime = time(NULL); printf("Time is set to (UTC): %s\r\n", ctime(&ctTime)); } else { printf("Error\r\n"); }

eth.disconnect();

while(1) { ctTime = time(NULL); current_time = localtime(&ctTime); change due to daylight saving time

strcpy (hour,""); strftime (hour,2,"%H",current_time); h = atoi(hour);

strcpy (mins,""); strftime(mins,4,"%M",current_time); m = atoi(mins);

strcpy (sec,""); strftime (sec,6,"%S",current_time); s = atoi(sec); if (start == 1) { stop_time_s = s - start_time_s; if (s == 0) old_time_s += 1; } if timer is reset else { old_time_s += stop_time_s; stop_time_s = 0; } add to actual timer timer_h = stop_time_h + old_time_h; timer_m = stop_time_m + old_time_m; timer_s = stop_time_s + old_time_s;

reset count if timer and add if (timer_s == 59) { old_time_m += 1; old_time_s = 0; start_time_s = s; } if (timer_m == 59) { old_time_h += 1; old_time_m = 0; start_time_m = m; } reset start timer to 0 if actual time is 0 if (s == 59) { start_time_s = 0; old_time_s += stop_time_s; } if (m == 59) { start_time_m = 0; old_time_m += stop_time_m; }

timezone change dh = h;

if (dh > 12 - time_diff) dh+=-12 + time_diff; else if (h <= -time_diff) dh += 12 + time_diff; else dh += time_diff;

rest of clock strftime (buffer,80,":%M:%S",current_time); Print lcd.locate(0,0); lcd.printf("%d:%d%s ",time_diff,dh,buffer,ctime(&ctTime));

wait(0.1); lcd.locate(0,1); lcd.printf("%02d:%02d:%02d", hours, minutes, seconds); lcd.locate(position*3+1,0); set cursor position if ( BUTTON&0x01 ) { button1 pressed BUTTON &= 0xfe; clear button status switch (position) { case 0: hours++; if (hours == 24 ) hours = 0; break; case 1: minutes++; if (minutes == 60) minutes = 0; break; case 2: seconds++; if (seconds == 60) seconds = 0; break; } switch } if

if ( BUTTON&0x02 ) { button2 pressed BUTTON &= 0xfd; clear button status position++; if (position == 3) position = 0; } if wait(0.1); } while } main

Be the first to answer this question.