Étape 3: Partie logicielle
Télécharger le programme sur votre Microdino-Core + par le biais de Microduino-FT232R.
----------------------------------
#include « U8glib.h »
U8GLIB_SSD1306_128X64 u8g(U8G_I2C_OPT_NONE) ; HW SPI Com: CS = 10, A0 = 9 (matériel broches sont SCK = 13 et MOSI = 11)
---les paramètre de taille de police, big|medium|small
#define setFont_L u8g.setFont(u8g_font_7x13)
#define setFont_M u8g.setFont(u8g_font_fixed_v0r)
#define setFont_S u8g.setFont(u8g_font_chikitar)
/*
polices :
u8g.SetFont(u8g_font_7x13)
u8g.SetFont(u8g_font_fixed_v0r) ;
u8g.SetFont(u8g_font_chikitar) ;
u8g.SetFont(u8g_font_osb21) ;
*/
#include < Wire.h >
#include < Rtc_Pcf8563.h >
Rtc_Pcf8563 CCF ;
int time_nian, time_yue, time_ri, time_shi, time_fen, time_miao, time_zhou ;
#include < EtherCard.h >
#define SECONDS_IN_DAY 86400
#define START_YEAR 1900
#define TIME_ZONE + 8
public static int days_in_month [] = {}
31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31} ;
public static byte MONMAC [] = {}
0 x 00, 0x1A, 0x4B, 0 x 38, 0x0C, 0x5C} ;
octet Ethernet::buffer [700] ;
public static byte ntpServer [] = {}
193,204,114,232} ;
static ubyte srcPort = 0 ;
uint32_t timeStamp ;
Boolean NET_TIME_reply ;
#define NET_TIME_INTERVAL 1000
#define TIME_OUT 10000
#define intervalle 100000
unsigned long lastTime = 0, TIME_cache = 0 ;
annuler le programme d’installation () {}
Serial.Begin(57600) ;
volcdsetup ("charge réseau", 0, 15) ;
Delay(1000) ;
Si (! ether.begin (Ethernet::buffer, MONMAC sizeof))
Serial.println ("Failed to accéder contrôleur Ethernet") ;
d’autre
Serial.println (« contrôleur Ethernet initialisée") ;
Si (! ether.dhcpSetup())
{
Serial.println ("Impossible d’obtenir la configuration du serveur DHCP") ;
volcdsetup ("erreur DHCP", 0, 15) ;
Delay(1000) ;
}
d’autre
{
Serial.println ("configuration DHCP faite ») ;
volcdsetup ("DHCP à réussir", 0, 15) ;
Delay(1000) ;
}
ether.printIp (« IP adresse : \t », ether.myip) ;
ether.printIp ("Netmask:\t", ether.mymask) ;
ether.printIp ("Gateway : \t", ether.gwip) ;
//-------------------------------------------------------
volcdsetup ("charge Net Time", 0, 15) ;
Delay(1000) ;
NET_TIME_reply = true ;
vonettime() ;
if(NET_TIME_reply)
{
volcdsetup "(Time Sync. Erreur", 0, 15) ;
Delay(1000) ;
}
d’autre
{
vosettime() ;
volcdsetup "(Time Sync. Réussir", 0, 15) ;
Delay(1000) ;
}
//-------------------------------------------------------
Serial.println() ;
}
void loop() {}
if(Millis() - lastTime > intervalle)
{
NET_TIME_reply = true ;
vonettime() ;
if(!NET_TIME_reply)
{
vosettime() ;
}
lastTime = millis() ;
}
volcd() ;
}
Sub volcd()
{
u8g.FirstPage() ;
{}
setFont_M ;
u8g.setPrintPos (0, 9) ;
u8g.Print("Time:") ;
u8g.Print(RTC.FormatDate()) ;
u8g.setPrintPos (0, 20) ;
u8g.Print(RTC.formatTime()) ;
//-----------------------------------------
u8g.setPrintPos (0, 34) ;
u8g.Print("Time:") ;
u8g.Print("20") ;
u8g.Print(RTC.GetYear()) ;
u8g.Print("/") ;
u8g.Print(RTC.GetMonth()) ;
u8g.Print("/") ;
u8g.Print(RTC.getDay()) ;
u8g.setPrintPos (0, 45) ;
u8g.Print(RTC.getHour()) ;
u8g.Print(":") ;
u8g.Print(RTC.getMinute()) ;
u8g.Print(":") ;
u8g.Print(RTC.getSecond()) ;
Dim semaine = "" ;
Switch(RTC.getWeekday())
{
cas 1 :
SEMAINE = « Lundi » ;
rupture ;
cas 2 :
SEMAINE = « Mardi » ;
rupture ;
cas 3 :
SEMAINE = « Mercredi » ;
rupture ;
cas 4 :
SEMAINE = « Jeudi » ;
rupture ;
cas no 5 :
SEMAINE = « Vendredi » ;
rupture ;
cas 6 :
SEMAINE = « Samedi » ;
rupture ;
cas 7 :
SEMAINE = « Dimanche » ;
rupture ;
}
u8g.setPrintPos (60, 45) ;
u8g.Print(week) ;
setFont_S ;
u8g.drawFrame (0, 54,128,10) ;
u8g.setPrintPos (3, 62) ;
u8g.Print ("Microduino-CCF v1.0 201307") ;
}
tandis que (u8g.nextPage()) ;
}
Sub vosettime()
{
rtc.initClock() ; fixer une durée pour commencer avec.
rtc.setDate (time_ri, time_zhou, time_yue, 0, 100 % de time_nian) ; jour, semaine, mois, siècle (1 = 1900, 0 = 2000), year(0-99)
rtc.setTime (time_shi, time_fen, time_miao) ; ressources humaines, min, sec
}
void volcdsetup (char * zi, unsigned int, unsigned int XY)
{
u8g.FirstPage() ;
{}
setFont_L ;
u8g.setPrintPos (x, y) ;
u8g.Print(ZI) ;
}
tandis que (u8g.nextPage()) ;
}
Sub vonettime()
{
uint32_t timeout = millis() ; //
même si (NET_TIME_reply == true & & millis() - timeout < TIME_OUT) / /
{
ether.packetLoop(ether.packetReceive()) ;
if(Millis() - TIME_cache > NET_TIME_INTERVAL) / /
{
ether.ntpRequest (ntpServer, srcPort) ;
TIME_cache = millis() ;
NET_TIME_reply = true ; 标记发送
}
Si (NET_TIME_reply & & ether.ntpProcessAnswer (& timeStamp, srcPort)) / /
{
printDate (horodatage + 3600 * TIME_ZONE) ; //
TIME_cache = millis() ;
NET_TIME_reply = false ; //
}
}
}
void printDate (uint32_t timeStamp) {}
unsigned longue semaine = (((timeStamp/3600/24)+1)%7) ;
Serial.Print ("date et heure actuelles:") ;
Serial.println(timestamp) ;
unsigned int année = START_YEAR ;
{while(1)}
uint32_t secondes ;
if(IsLeapYear(Year)) secondes = SECONDS_IN_DAY * 366 ;
d’autre secondes = SECONDS_IN_DAY * 365 ;
Si (timeStamp > = secondes) {}
timeStamp = secondes ;
année ++ ;
}
saut de l’autre ;
}
unsigned int mois = 0 ;
{while(1)}
uint32_t secondes = SECONDS_IN_DAY * days_in_month [mois] ;
if(IsLeapYear(Year) & & mois == 1) secondes = SECONDS_IN_DAY * 29 ;
Si (timeStamp > = secondes) {}
timeStamp = secondes ;
mois ++ ;
}
saut de l’autre ;
}
mois ++ ;
unsigned int jour = 1 ;
{while(1)}
Si (timeStamp > = SECONDS_IN_DAY) {}
Horodatage-= SECONDS_IN_DAY ;
jour ++ ;
}
saut de l’autre ;
}
unsigned int heure = timeStamp / 3600 ;
unsigned int minute = (horodatage - heure (uint32_t) * 3600) / 60 ;
unsigned int deuxième = (horodatage - heure (uint32_t) * 3600) - minute * 60 ;
Serial.Print (« semaine: ") ;
Serial.println(week) ;
if(Day < 10) Serial.print("0") ;
Serial.Print(Day) ;
Serial.Print("/") ;
if(month < 10) Serial.print("0") ;
Serial.Print(month) ;
Serial.Print("/") ;
Serial.println(Year) ;
if(Hour < 10) Serial.print("0") ;
Serial.Print(Hour) ;
Serial.Print(":") ;
if(minute < 10) Serial.print("0") ;
Serial.Print(minute) ;
Serial.Print(":") ;
if(second < 10) Serial.print("0") ;
Serial.println(second) ;
Serial.println() ;
time_nian = année ;
time_yue = mois ;
time_ri = jour ;
time_shi = heure ;
time_fen = minutes ;
time_miao = seconde ;
time_zhou = semaine ;
}
isLeapYear booléenne (an unsigned int) {}
retour (année % 4 == 0 & & (100 % année! = 0 || année % 400 == 0)) ;
}
---