#include <avr/pgmspace.h> // allows use of PROGMEM to store patterns in flash
#include "CheckCube4_4.h"
Le fichier CheckCube4_4.h contient toutes les configurations d'allumage des LED.
#define CUBESIZE 4
#define PLANESIZE CUBESIZE * CUBESIZE
#define TIMECONST 15 // Constante de temps pour l'affichage d'une figure
#define NB_CYCLE 3 // Nombre de fois, ou l'on joue un scénario
#define NB_SCENARIO 9 // Nombre de scenario différent de lumière
#define PLANETIME 3333 // time each plane is displayed in us -> 100 Hz refresh
char LEDPin[] = {0, 1, 2, 3, 4, 5, 6 , 7, 8, 9 , 10, 11, 12, 13, 14, 15};
char PlanePin[] = {16, 17, 18, 19};
unsigned char MonTableau [PLANESIZE] ;
int NmrCycle = 0 ;
char NbScenario = 0 ;
int PatternIdx, PatternIdx_Old ;
void setup ()
{
int pin; // loop counter
// set up LED pins as output (active HIGH)
for (pin=0; pin<PLANESIZE; pin++)
{
pinMode( LEDPin[pin], OUTPUT );
}
// set up plane pins as outputs (active LOW)
for (pin=0; pin<CUBESIZE; pin++)
{
pinMode( PlanePin[pin], OUTPUT );
}
// Serial.begin (9600) ;
// Serial.println ("Hello la Communication Serie est initialisee...") ;
// Initialise PatternIdx au Début du Tableau
PatternIdx = PatternIdx_Old = 0 ;
}
void loop ()
{
int i ;
byte DisplayTime ;
char Compteur ;
unsigned long TimeFin ;
int plane; // loop counter for cube refresh
int ledrow; // counts LEDs in refresh loop
int ledcol; // counts LEDs in refresh loop
int ledpin; // counts LEDs in refresh loop
// Initialise PatternIdx au Début du Tableau
// PatternIdx = 0 ;
// Copie de la SRAM vers la mémoire normale
// strcpy_P ( MonTableau, LeTableau) ;
do
{
memcpy_P (MonTableau, LeTableau+PatternIdx, PLANESIZE) ;
PatternIdx += PLANESIZE ;
DisplayTime = pgm_read_byte_near (LeTableau+PatternIdx) ;
TimeFin = millis () + ((unsigned long) DisplayTime) * TIMECONST ;
PatternIdx++ ;
// loop while DisplayTime>0 and current time < EndTime
while (millis () < TimeFin)
{
Compteur = 0; // reset index counter to beginning of buffer
// loop over planes
for (plane=0; plane<CUBESIZE; plane++)
{
// turn previous plane off
if (plane==0)
{
digitalWrite( PlanePin[CUBESIZE-1], HIGH );
}
else
{
digitalWrite( PlanePin[plane-1], HIGH );
}
// load current plane pattern data into ports
ledpin = 0;
for (ledrow=0; ledrow<CUBESIZE; ledrow++)
{
for (ledcol=0; ledcol<CUBESIZE; ledcol++)
{
digitalWrite( LEDPin[ledpin++],MonTableau[Compteur] & (1 << ledcol));
}
Compteur++;
}
// turn current plane on
digitalWrite( PlanePin[plane], LOW );
// delay PLANETIME us
delayMicroseconds( PLANETIME );
} // for plane
} ;
} while (DisplayTime > 0) ;
// Incrémentation du NmrCycle
// On reste sur un scénario de 10 Cycles
NmrCycle++ ;
if (NmrCycle < NB_CYCLE)
{
PatternIdx = PatternIdx_Old;
}
else
{
// Serial.println (NbScenario, DEC) ;
PatternIdx_Old = PatternIdx ;
NmrCycle = 0 ;
NbScenario++ ;
if (NbScenario >= NB_SCENARIO)
{
PatternIdx = PatternIdx_Old = 0 ;
NbScenario = 0 ;
}
}
}
#include "HorlogeRTC_PA.h"
class RtcDve MonRtcDve ;
void setup()
{
pinMode (BIT_0, OUTPUT) ;
pinMode (BIT_1, OUTPUT) ;
pinMode (BIT_2, OUTPUT) ;
pinMode (BIT_3, OUTPUT) ;
pinMode (LATCH_USEC, OUTPUT) ;
pinMode (LATCH_DSEC, OUTPUT) ;
pinMode (LATCH_UMN, OUTPUT) ;
pinMode (LATCH_DMN, OUTPUT) ;
pinMode (LATCH_UH, OUTPUT) ;
pinMode (LATCH_DH, OUTPUT) ;
digitalWrite (BIT_0 , LOW) ;
digitalWrite (BIT_1 , LOW) ;
digitalWrite (BIT_2 , LOW) ;
digitalWrite (BIT_3 , LOW) ;
digitalWrite (LATCH_USEC, LOW) ;
digitalWrite (LATCH_DSEC, LOW) ;
digitalWrite (LATCH_UMN, LOW) ;
digitalWrite (LATCH_DMN, LOW) ;
digitalWrite (LATCH_UH, LOW) ;
digitalWrite (LATCH_DH, LOW) ;
MonHeure.SEC = 0 ;
MonHeure.MN = 45 ;
MonHeure.H = 11 ;
// Initialisation de la COM série
Serial.begin (9600) ;
Serial.print ('\0') ;
Serial.println ("Salut la liaison serie...") ;
// Initialisation du bus I2C
Wire.begin() ;
// Création de l’objet RtcDve
// MonRtcDve = new RtcDve ;
// Ecriture de la Date à faire une première fois pour lancer le DS1307
// MonRtcDve.Ecrire () ;
// Tentative de synchronisation avec le pc
MonRtcDve.Synchronisation() ;
// On affiche d'abord l'Heure
TypeAffichage = AFFICHAGE_HEURE ;
Time_Old = millis () ;
}
void loop()
{
MonRtcDve.Lire () ;
if (TypeAffichage == AFFICHAGE_HEURE)
{
if (millis () < (Time_Old + 5000))
{
AfficheHeure (MonRtcDve.DateLocale->secondes, MonRtcDve.DateLocale->minutes, MonRtcDve.DateLocale->heures) ;
}
else
{
TypeAffichage = AFFICHAGE_DATE ;
Time_Old = millis () ;
}
}
else
{
if (TypeAffichage == AFFICHAGE_DATE)
{
if (millis () < (Time_Old + 2000))
{
// Date.annee est comptée à partir de 1970
// Donc 1970 - 2000 = - 30
AfficheHeure (MonRtcDve.DateLocale->annee - 30, MonRtcDve.DateLocale->mois, MonRtcDve.DateLocale->jour) ;
}
else
{
TypeAffichage = AFFICHAGE_HEURE ;
Time_Old = millis () ;
}
}
}
}
//---------------------------------------------
// Affiche l'Heure sur les afficheurs
//---------------------------------------------
void AfficheHeure (char S, char MN, char H)
{ ;
AfficheDigit (AFF, H - int (H / 10) * 10 , LATCH_UH) ;
AfficheDigit (AFF, int (H / 10) , LATCH_DH) ; ;
AfficheDigit (AFF, MN - int (MN / 10) * 10 , LATCH_UMN) ;
AfficheDigit (AFF, int (MN / 10) , LATCH_DMN) ; ;
AfficheDigit (AFF, S - int (S / 10) * 10 , LATCH_USEC) ;
AfficheDigit (AFF, int (S / 10) , LATCH_DSEC) ;
}
void AfficheDigit (char * AFF, char LeChiffre, char Latch)
{
// Mise à 0 des Broches AFFICHEUR
digitalWrite (AFF [0], LOW) ;
digitalWrite (AFF [1], LOW) ;
digitalWrite (AFF [2], LOW) ;
digitalWrite (AFF [3], LOW) ;
// Mise à 1 ou à 0 des Broches selon le Chiffre
if (LeChiffre >= 8)
{
digitalWrite (AFF [3], HIGH) ;
LeChiffre = LeChiffre - 8 ;
}
if (LeChiffre >= 4)
{
digitalWrite (AFF [2], HIGH) ;
LeChiffre = LeChiffre - 4 ;
}
if (LeChiffre >= 2)
{
digitalWrite (AFF [1], HIGH) ;
LeChiffre = LeChiffre - 2 ;
}
if (LeChiffre >=1)
{
digitalWrite (AFF [0], HIGH) ;
LeChiffre = LeChiffre - 1 ;
}
// Latch du Chiffre
digitalWrite (Latch, LOW) ;
delayMicroseconds (500) ;
digitalWrite (Latch, HIGH) ;
}
byte bcd2dec(byte bcd)
{
return ((bcd / 16 * 10) + (bcd % 16));
}
byte dec2bcd(byte dec) {
return ((dec / 10 * 16) + (dec % 10));
}
| Constructeur | RtcDve () |
| Destructeur | ~RtcDve () |
| Méthodes | Lire () |
| Ecrire () | |
| Synchronisation () |
#include "HorlogeRTC_PA.h"
RtcDve::RtcDve () // Constructeur
{
DateLocale = new Date ;
DateLocale->secondes = 0 ;
DateLocale->minutes = 9 ;
DateLocale->heures = 22 ;
DateLocale->jourDeLaSemaine = 1 ;
DateLocale->jour = 6 ;
DateLocale->mois = 9 ;
DateLocale->annee = 46 ;
}
RtcDve::~RtcDve ()
{
Serial.println ("Bye Bye") ;
}
void RtcDve::Ecrire()
{
Wire.beginTransmission(DS1307_ADDRESS); // Début de transaction I2C
Wire.write((byte)0x00); // Arrête l'oscillateur du DS1307
Wire.write(dec2bcd(DateLocale->secondes)); // Envoi des données
Wire.write(dec2bcd(DateLocale->minutes));
Wire.write(dec2bcd(DateLocale->heures));
Wire.write(dec2bcd(DateLocale->jourDeLaSemaine));
Wire.write(dec2bcd(DateLocale->jour));
Wire.write(dec2bcd(DateLocale->mois));
Wire.write(dec2bcd(DateLocale->annee));
Wire.write((byte) 0x00); // Redémarre l'oscillateur du DS1307
Wire.endTransmission(); // Fin de transaction I2C
}
void RtcDve::Lire()
{
Wire.beginTransmission(DS1307_ADDRESS); // Début de transaction I2C
Wire.write((byte) 0x00); // Demande les info à partir de l'adresse 0
Wire.endTransmission(); // Fin de transaction I2C
// Récupère les info (7 octets = 7 valeurs correspondant à l'heure et à la date courante)
Wire.requestFrom(DS1307_ADDRESS, 7);
// stockage et conversion des données reçu
DateLocale->secondes = bcd2dec(Wire.read() & 0x7F);
DateLocale->minutes = bcd2dec(Wire.read());
DateLocale->heures = bcd2dec(Wire.read() & 0x3F);
DateLocale->jourDeLaSemaine = bcd2dec(Wire.read());
DateLocale->jour = bcd2dec(Wire.read());
DateLocale->mois = bcd2dec(Wire.read());
DateLocale->annee = bcd2dec(Wire.read());
}
void RtcDve::Synchronisation()
{
int i;
// Envoi de la commande de synchronisation
Serial.print("SYNC");
// Attente de 3s max
for (i = 0 ; (Serial.available() < 3) && (i < 6) ; i++)
{
delay (TEMPO) ;
}
if (i != 6)
{ // Il n'y as pas eu de timeout
if (Serial.read() == 'A')
if (Serial.read() == 'C')
if (Serial.read() == 'K')
{
// Si le pc à répondu par une commande ACK c'est que la synchronisation peut commencer
// Attente des 14 octets de configuration
while (Serial.available() < 14) ;
DateLocale->secondes = ((Serial.read () - 48) * 10 + Serial.read () - 48) ;
DateLocale ->minutes = ((Serial.read () - 48) * 10 + Serial.read () - 48) ;
DateLocale ->heures = ((Serial.read () - 48) * 10 + Serial.read () - 48) ;
DateLocale ->jourDeLaSemaine = ((Serial.read () - 48) * 10 + Serial.read () - 48) ;
DateLocale ->jour = ((Serial.read () - 48) * 10 + Serial.read () - 48) ;
DateLocale ->mois = ((Serial.read () - 48) * 10 + Serial.read () - 48) ;
DateLocale ->annee = ((Serial.read () - 48) * 10 + Serial.read () - 48) ;
// Stockage dans le DS1307 des éléments de la date lus
Ecrire ();
// Fin de la synchronisation
Serial.println ("Synchro Faite : ") ;
// Affichage des valeurs reçues
Serial.print ("Heure : ") ;
Serial.println (bcd2dec (DateLocale ->heures)) ;
Serial.print ("Minute : ") ;
Serial.println (bcd2dec (DateLocale ->minutes)) ;
Serial.print ("Seconde : ") ;
Serial.println (bcd2dec (DateLocale ->secondes)) ;
Serial.print ("jourDeLaSemaine ") ;
Serial.println (bcd2dec (DateLocale ->jourDeLaSemaine)) ;
Serial.print ("Jour : ") ;
Serial.println (bcd2dec (DateLocale ->jour)) ;
Serial.print ("Mois : ") ;
Serial.println (bcd2dec (DateLocale ->mois)) ;
Serial.print ("Annee : ") ;
Serial.println (bcd2dec (DateLocale ->annee)) ;
}
}
}
//#include <DS1307RTC.h>
#ifndef Horloge_RTC_PA_H
#define Horloge_RTC_PA_H
#include <Time.h>
#include <Wire.h>
#define DS1307_ADDRESS 0x68
unsigned long Time_Old ;
char TypeAffichage ;
#define AFFICHAGE_HEURE 0
#define AFFICHAGE_DATE 1
#define BIT_0 11
#define BIT_1 10
#define BIT_2 9
#define BIT_3 8
#define LATCH_DH 7
#define LATCH_UH 6
#define LATCH_DMN 5
#define LATCH_UMN 4
#define LATCH_DSEC 3
#define LATCH_USEC 2
#define TEMPO 500
char AFF [] = {BIT_0, BIT_1, BIT_2, BIT_3 } ;
struct HMS {char SEC ; char MN ; char H ;} ;
HMS MonHeure ;
// Définition de la structure Date
typedef struct
{
uint8_t secondes ;
uint8_t minutes ;
uint8_t heures; // format 24h
uint8_t jourDeLaSemaine; // 0~7 = lundi, mardi, ...
uint8_t jour;
uint8_t mois;
uint8_t annee; // format yy (ex 2012 -> 12)
} Date;
// Ps uint8_t est un synonyme de byte, byte ne pouvant être utilisé dans un structure contenu dans un .h externe
// Initialisation d’une structure date
Date date = {
0, // Secondes
04, // minutes
07, // heures
3, // jour de la semaine (Dimanche = 1)
24, // jour
8, // Mois
2016-1970 // Année depuis 1970
} ;
class RtcDve
{
public :
// Variable
Date * DateLocale ;
// Constructeur
RtcDve () ;
~RtcDve () ;
// Méthodes
void Lire () ;
void Ecrire () ;
void Synchronisation () ;
} ;
#endif