Paso 8: Sketch de Arduino para Reloj Digital
Biblioteca digital en modo ' Timer 2 ' frecuencia de uso. Puede descargar biblioteca desde aquí.
#include <FrequencyTimer2.h> #include "Wire.h" #define DS3231_I2C_ADDRESS 0x68 // Convert normal decimal numbers to binary coded decimal byte decToBcd(byte val) { return( (val/10*16) + (val%10) ); } // Convert binary coded decimal to normal decimal numbers byte bcdToDec(byte val) { return( (val/16*10) + (val%16) ); } byte second_unit, second_tens, minute_unit, minute_tens, hour_unit, hour_tens, date_unit, date_tens, month_unit, month_tens, year_unit, year_tens, year_remain; byte _second = 0, _minute = 10, _hour = 12, _day = 1, _date = 1, _month = 1, _year = 15; byte tMSB, tLSB; float temp3231; long lastPressTime; #define space { \ {0, 0, 0, 0, 0, 0, 0, 0}, \ {0, 0, 0, 0, 0, 0, 0, 0}, \ {0, 0, 0, 0, 0, 0, 0, 0}, \ {0, 0, 0, 0, 0, 0, 0, 0}, \ {0, 0, 0, 0, 0, 0, 0, 0}, \ {0, 0, 0, 0, 0, 0, 0, 0}, \ {0, 0, 0, 0, 0, 0, 0, 0}, \ {0, 0, 0, 0, 0, 0, 0, 0} \ } #define slash { \ {0, 0, 0, 0, 0, 0, 0, 0}, \ {0, 0, 0, 0, 0, 1, 0, 0}, \ {0, 0, 0, 0, 1, 0, 0, 0}, \ {0, 0, 0, 1, 0, 0, 0, 0}, \ {0, 0, 1, 0, 0, 0, 0, 0}, \ {0, 1, 0, 0, 0, 0, 0, 0}, \ {0, 0, 0, 0, 0, 0, 0, 0}, \ {0, 0, 0, 0, 0, 0, 0, 0} \ } #define A { \ {0, 0, 1, 1, 1, 0, 0, 0}, \ {0, 1, 0, 0, 0, 1, 0, 0}, \ {0, 1, 0, 0, 0, 1, 0, 0}, \ {0, 1, 0, 0, 0, 1, 0, 0}, \ {0, 1, 1, 1, 1, 1, 0, 0}, \ {0, 1, 0, 0, 0, 1, 0, 0}, \ {0, 1, 0, 0, 0, 1, 0, 0}, \ {0, 0, 0, 0, 0, 0, 0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 0, 0, 0, 1, 1, 1, 0}, \ {0, 0, 0, 0, 0, 0, 0, 0} \ } #define T { \ {0, 1, 1, 1, 1, 1, 0, 0}, \ {0, 0, 0, 1, 0, 0, 0, 0}, \ {0, 0, 0, 1, 0, 0, 0, 0}, \ {0, 0, 0, 1, 0, 0, 0, 0}, \ {0, 0, 0, 1, 0, 0, 0, 0}, \ {0, 0, 0, 1, 0, 0, 0, 0}, \ {0, 0, 0, 1, 0, 0, 0, 0}, \ {0, 0, 0, 0, 0, 0, 0, 0} \ } #define smallm { \ {0, 0, 0, 0, 0, 0, 0, 0}, \ {0, 0, 0, 0, 0, 0, 0, 0}, \ {0, 1, 1, 0, 1, 0, 0, 0}, \ {0, 1, 0, 1, 0, 1, 0, 0}, \ {0, 1, 0, 1, 0, 1, 0, 0}, \ {0, 1, 0, 0, 0, 1, 0, 0}, \ {0, 1, 0, 0, 0, 1, 0, 0}, \ {0, 0, 0, 0, 0, 0, 0, 0} \ } #define smallp { \ {0, 0, 0, 0, 0, 0, 0, 0}, \ {0, 0, 0, 0, 0, 0, 0, 0}, \ {0, 1, 1, 1, 1, 0, 0, 0}, \ {0, 1, 0, 0, 0, 1, 0, 0}, \ {0, 1, 1, 1, 1, 0, 0, 0}, \ {0, 1, 0, 0, 0, 0, 0, 0}, \ {0, 1, 0, 0, 0, 0, 0, 0}, \ {0, 0, 0, 0, 0, 0, 0, 0} \ } byte col = 0; byte leds[8][8]; byte rows[8] = {0, 3, 6, 12, 7, 14, 15, 4}; byte cols[8] = {8, 1, 10, 5, 17, 11, 16, 13}; const byte numPatterns = 14; byte patterns[numPatterns][8][8] = { zero, one, two, three, four, five, six, seven, eight, nine, colon, space, slash, DEGC }; int pattern = 0; void setup() { Wire.begin(); for (byte i = 3; i <= 17; i++) { if(i==9) continue; pinMode(i, OUTPUT); } pinMode(0, OUTPUT); pinMode(1, OUTPUT); // set up cols and rows for (byte i = 1; i <= 8; i++) { digitalWrite(rows[i - 1], LOW); } for (byte i = 1; i <= 8; i++) { digitalWrite(cols[i - 1], LOW); } clearLeds(); // Turn off toggling of pin 11 FrequencyTimer2::disable(); // Set refresh rate (interrupt timeout period) FrequencyTimer2::setPeriod(2000); // Set interrupt routine to be called FrequencyTimer2::setOnOverflow(display); setPattern(pattern); } void loop() { calculateDateTime(); digitalClock(); } void clearLeds() { // Clear display array for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { leds[i][j] = 0; } } } void setPattern(int pattern) { for (int i = 0; i < 8; i++) { for (int j = 0; j < 8; j++) { leds[i][j] = patterns[pattern][i][j]; } } } void slidePattern(int pattern, int del) { for (int l = 0; l < 8; l++) { for (int i = 0; i < 7; i++) { for (int j = 0; j < 8; j++) { leds[j][i] = leds[j][i+1]; } } for (int j = 0; j < 8; j++) { leds[j][7] = patterns[pattern][j][0 + l]; } delay(del); } } // Interrupt routine void display() { digitalWrite(cols[col], LOW); // Turn whole previous column off col++; //digitalWrite(rows[row], LOW); // Turn whole previous column off //row++; if (col == 8) { col = 0; } for (int row = 0; row < 8; row++) { if (leds[col][7 - row] == 1) { digitalWrite(rows[row], LOW); // Turn on this led } else { digitalWrite(rows[row], HIGH); // Turn off this led } } digitalWrite(cols[col], HIGH); // Turn whole column on at once (for equal lighting times) } void setDS3231time(byte second, byte minute, byte hour, byte dayOfWeek, byte dayOfMonth, byte month, byte year) { // sets time and date data to DS3231 Wire.beginTransmission(DS3231_I2C_ADDRESS); Wire.write(0); // set next input to start at the seconds register Wire.write(decToBcd(second)); // set seconds Wire.write(decToBcd(minute)); // set minutes Wire.write(decToBcd(hour | 0x40)); // set hours Wire.write(decToBcd(dayOfWeek)); // set day of week (1=Sunday, 7=Saturday) Wire.write(decToBcd(dayOfMonth)); // set date (1 to 31) Wire.write(decToBcd(month)); // set month Wire.write(decToBcd(year)); // set year (0 to 99) Wire.endTransmission(); } void readDS3231time(byte *second, byte *minute, byte *hour, byte *dayOfWeek, byte *dayOfMonth, byte *month, byte *year) { Wire.beginTransmission(DS3231_I2C_ADDRESS); Wire.write(0); // set DS3231 register pointer to 00h Wire.endTransmission(); Wire.requestFrom(DS3231_I2C_ADDRESS, 7); // request seven bytes of data from DS3231 starting from register 00h *second = bcdToDec(Wire.read() & 0x7f); *minute = bcdToDec(Wire.read()); *hour = bcdToDec(Wire.read() & 0x1f); *dayOfWeek = bcdToDec(Wire.read()); *dayOfMonth = bcdToDec(Wire.read()); *month = bcdToDec(Wire.read()); *year = bcdToDec(Wire.read()); } void calculateDateTime(){ byte second, minute, hour, dayOfWeek, dayOfMonth, month, year; // retrieve data from DS3231 readDS3231time(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year); if(hour > 12){ hour = hour - 24; } second_unit = second % 10; second_tens = second / 10; minute_unit = minute % 10; minute_tens = minute / 10; hour_unit = hour % 10; hour_tens = hour / 10; date_unit = dayOfMonth % 10; date_tens = dayOfMonth / 10; month_unit = month % 10; month_tens = month / 10; year_unit = year % 10; year_tens = year / 10; } void digitalClock(){ FrequencyTimer2::setOnOverflow(display); slidePattern(hour_tens, 80); slidePattern(hour_unit, 80); slidePattern(10, 80); slidePattern(minute_tens, 80); slidePattern(minute_unit, 80); slidePattern(10, 80); slidePattern(second_tens, 80); slidePattern(second_unit, 80); //slidePattern(11, 80); slidePattern(11, 80); slidePattern(date_tens, 80); slidePattern(date_unit, 80); slidePattern(12, 80); slidePattern(month_tens, 80); slidePattern(month_unit, 80); slidePattern(12, 80); slidePattern(2, 80); slidePattern(0, 80); slidePattern(year_tens, 80); slidePattern(year_unit, 80); slidePattern(11, 80); int tempC = get3231Temp(); int tempC_unit = tempC % 10; int tempC_tens = tempC / 10; slidePattern(tempC_tens, 80); slidePattern(tempC_unit, 80); slidePattern(13, 80); slidePattern(11, 80); slidePattern(11, 80); } int get3231Temp() { //temp registers (11h-12h) get updated automatically every 64s Wire.beginTransmission(DS3231_I2C_ADDRESS); Wire.write(0x11); Wire.endTransmission(); Wire.requestFrom(DS3231_I2C_ADDRESS, 2); if(Wire.available()) { tMSB = Wire.read(); //2's complement int portion tLSB = Wire.read(); //fraction portion temp3231 = (tMSB & B01111111); //do 2's math on Tmsb } else { //oh noes, no data! } return temp3231; }