Paso 7: Programa de Arduino para reloj binario
Todo el programa está escrito en el entorno de arduino y subido a ATMega328 usando arduino uno. Protocolo de programación ISP se utiliza para la programación. Yo he dividido el programa en dos partes y cada una puede ser utilizada independientemente. Un reloj binario y otro para modo digital del reloj.
Siguiente programa es solo para modo binario del reloj y utiliza la biblioteca de botón:
#include "Wire.h" #define DS3231_I2C_ADDRESS 0x68 #include <Button.h> //https://github.com/JChristensen/Button #define BUTTON_PIN_1 2 //Connect a tactile button switch (or something similar) #define BUTTON_PIN_2 9 //from Arduino pin 2 to ground. #define PULLUP true //To keep things simple, we use the Arduino's internal pullup resistor. #define INVERT true //Since the pullup resistor will keep the pin high unless the //switch is closed, this is negative logic, i.e. a high state //means the button is NOT pressed. (Assuming a normally open switch.) #define DEBOUNCE_MS 20 //A debounce time of 20 milliseconds usually works well for tactile button switches. #define LONG_PRESS 1000 //We define a "long press" to be 1000 milliseconds. #define msec 1 //for brightness control of LED Button myBtn1(BUTTON_PIN_1, PULLUP, INVERT, DEBOUNCE_MS); //Declare the button Button myBtn2(BUTTON_PIN_2, PULLUP, INVERT, DEBOUNCE_MS); // 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; long lastPressTime; // you have to adjust pin number according to your connection byte colPin[8] = {4, 15, 14, 7, 12, 6, 3, 0}; //-ve pin byte rowPin[8] = {8, 1, 10, 5, 17, 11, 16, 13}; //+ve byte buttonStateOne = 0, buttonLongStateOne = 0, buttonStateTwo = 0, buttonLongStateTwo = 0; byte clockMode = 0; void setup() { Wire.begin(); for (byte i = 3; i <= 17; i++) { if(i==9) continue; //ignore pin 9 pinMode(i, OUTPUT); } pinMode(0, OUTPUT); pinMode(1, OUTPUT); pinMode(2, INPUT); // button one pinMode(9, INPUT); // button two digitalWrite(2, HIGH); // enable internal pullup digitalWrite(9, HIGH); // set up cols and rows for (byte i = 1; i <= 8; i++) { digitalWrite(colPin[i - 1], HIGH); } for (byte i = 1; i <= 8; i++) { digitalWrite(rowPin[i - 1], LOW); } } void loop() { calculateDateTime(); if(buttonLongStateTwo == 0) binaryClock(); else editDisplay(); } 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 time_zero(int pos){ delay(msec); } void time_one(int pos){ digitalWrite(colPin[pos], LOW); digitalWrite(rowPin[7], HIGH); delay(msec); digitalWrite(colPin[pos], HIGH); digitalWrite(rowPin[7], LOW); } void time_two(int pos){ digitalWrite(colPin[pos], LOW); digitalWrite(rowPin[6], HIGH); delay(msec); digitalWrite(colPin[pos], HIGH); digitalWrite(rowPin[6], LOW); } void time_three(int pos){ digitalWrite(colPin[pos], LOW); digitalWrite(rowPin[6], HIGH); digitalWrite(rowPin[7], HIGH); delay(msec); digitalWrite(colPin[pos], HIGH); digitalWrite(rowPin[6], LOW); digitalWrite(rowPin[7], LOW); } void time_four(int pos){ digitalWrite(colPin[pos], LOW); digitalWrite(rowPin[5], HIGH); delay(msec); digitalWrite(colPin[pos], HIGH); digitalWrite(rowPin[5], LOW); } void time_five(int pos){ digitalWrite(colPin[pos], LOW); digitalWrite(rowPin[5], HIGH); digitalWrite(rowPin[7], HIGH); delay(msec); digitalWrite(colPin[pos], HIGH); digitalWrite(rowPin[5], LOW); digitalWrite(rowPin[7], LOW); } void time_six(int pos){ digitalWrite(colPin[pos], LOW); digitalWrite(rowPin[5], HIGH); digitalWrite(rowPin[6], HIGH); delay(msec); digitalWrite(colPin[pos], HIGH); digitalWrite(rowPin[5], LOW); digitalWrite(rowPin[6], LOW); } void time_seven(int pos){ digitalWrite(colPin[pos], LOW); digitalWrite(rowPin[5], HIGH); digitalWrite(rowPin[6], HIGH); digitalWrite(rowPin[7], HIGH); delay(msec); digitalWrite(colPin[pos], HIGH); digitalWrite(rowPin[5], LOW); digitalWrite(rowPin[6], LOW); digitalWrite(rowPin[7], LOW); } void time_eight(int pos){ digitalWrite(colPin[pos], LOW); digitalWrite(rowPin[4], HIGH); delay(msec); digitalWrite(colPin[pos], HIGH); digitalWrite(rowPin[4], LOW); } void time_nine(int pos){ digitalWrite(colPin[pos], LOW); digitalWrite(rowPin[4], HIGH); digitalWrite(rowPin[7], HIGH); delay(msec); digitalWrite(colPin[pos], HIGH); digitalWrite(rowPin[4], LOW); digitalWrite(rowPin[7], LOW); } void date_zero(int pos){ delay(msec); } void date_one(int pos){ digitalWrite(colPin[pos], LOW); digitalWrite(rowPin[7-4], HIGH); delay(msec); digitalWrite(colPin[pos], HIGH); digitalWrite(rowPin[7-4], LOW); } void date_two(int pos){ digitalWrite(colPin[pos], LOW); digitalWrite(rowPin[6-4], HIGH); delay(msec); digitalWrite(colPin[pos], HIGH); digitalWrite(rowPin[6-4], LOW); } void date_three(int pos){ digitalWrite(colPin[pos], LOW); digitalWrite(rowPin[6-4], HIGH); digitalWrite(rowPin[7-4], HIGH); delay(msec); digitalWrite(colPin[pos], HIGH); digitalWrite(rowPin[6-4], LOW); digitalWrite(rowPin[7-4], LOW); } void date_four(int pos){ digitalWrite(colPin[pos], LOW); digitalWrite(rowPin[5-4], HIGH); delay(msec); digitalWrite(colPin[pos], HIGH); digitalWrite(rowPin[5-4], LOW); } void date_five(int pos){ digitalWrite(colPin[pos], LOW); digitalWrite(rowPin[5-4], HIGH); digitalWrite(rowPin[7-4], HIGH); delay(msec); digitalWrite(colPin[pos], HIGH); digitalWrite(rowPin[5-4], LOW); digitalWrite(rowPin[7-4], LOW); } void date_six(int pos){ digitalWrite(colPin[pos], LOW); digitalWrite(rowPin[5-4], HIGH); digitalWrite(rowPin[6-4], HIGH); delay(msec); digitalWrite(colPin[pos], HIGH); digitalWrite(rowPin[5-4], LOW); digitalWrite(rowPin[6-4], LOW); } void date_seven(int pos){ digitalWrite(colPin[pos], LOW); digitalWrite(rowPin[5-4], HIGH); digitalWrite(rowPin[6-4], HIGH); digitalWrite(rowPin[7-4], HIGH); delay(msec); digitalWrite(colPin[pos], HIGH); digitalWrite(rowPin[5-4], LOW); digitalWrite(rowPin[6-4], LOW); digitalWrite(rowPin[7-4], LOW); } void date_eight(int pos){ digitalWrite(colPin[pos], LOW); digitalWrite(rowPin[4-4], HIGH); delay(msec); digitalWrite(colPin[pos], HIGH); digitalWrite(rowPin[4-4], LOW); } void date_nine(int pos){ digitalWrite(colPin[pos], LOW); digitalWrite(rowPin[4-4], HIGH); digitalWrite(rowPin[7-4], HIGH); delay(msec); digitalWrite(colPin[pos], HIGH); digitalWrite(rowPin[4-4], LOW); digitalWrite(rowPin[7-4], LOW); } void show(int digit, int pos){ switch(digit){ case 0: time_zero(pos); break; case 1: time_one(pos); break; case 2: time_two(pos); break; case 3: time_three(pos); break; case 4: time_four(pos); break; case 5: time_five(pos); break; case 6: time_six(pos); break; case 7: time_seven(pos); break; case 8: time_eight(pos); break; case 9: time_nine(pos); break; } } void date_show(int digit, int pos){ switch(digit){ case 0: date_zero(pos); break; case 1: date_one(pos); break; case 2: date_two(pos); break; case 3: date_three(pos); break; case 4: date_four(pos); break; case 5: date_five(pos); break; case 6: date_six(pos); break; case 7: date_seven(pos); break; case 8: date_eight(pos); break; case 9: date_nine(pos); break; } } void binaryClock(){ FrequencyTimer2::setOnOverflow(0); show(second_unit, 7); show(second_tens, 6); show(minute_unit, 4); show(minute_tens, 3); show(hour_unit, 1); show(hour_tens, 0); date_show(date_unit, 7); date_show(date_tens, 6); date_show(month_unit, 4); date_show(month_tens, 3); date_show(year_unit, 1); date_show(year_tens, 0); } void showMinute(){ int _minute_unit = _minute % 10; int _minute_tens = _minute / 10; show(_minute_unit, 4); show(_minute_tens, 3); } void showHour(){ int _hour_unit = _hour % 10; int _hour_tens = _hour / 10; show(_hour_unit, 1); show(_hour_tens, 0); } void showDate(){ int _date_unit = _date % 10; int _date_tens = _date / 10; date_show(_date_unit, 7); date_show(_date_tens, 6); } void showMonth(){ int _month_unit = _month % 10; int _month_tens = _month / 10; date_show(_month_unit, 4); date_show(_month_tens, 3); } void showYear(){ int _year_unit = _year % 10; int _year_tens = _year / 10; date_show(_year_unit, 1); date_show(_year_tens, 0); } void button(){ myBtn1.read(); //Read the button myBtn2.read(); //Read the button if (myBtn1.wasReleased()){ lastPressTime = millis(); if(buttonLongStateTwo == 0){} else if((buttonLongStateTwo == 1) && (buttonStateTwo == 1)) { showMinute(); _minute++; delay(20); if(_minute > 59) _minute = 0; //Serial.println(minute); } else if((buttonLongStateTwo == 1) && (buttonStateTwo == 2)) { showHour(); _hour++; delay(20); if(_hour > 12) _hour = 1; //Serial.println(hour); } else if((buttonLongStateTwo == 1) && (buttonStateTwo == 3)) { showDate(); _date++; delay(20); if(_date > 31) _date = 1; //Serial.println(date); } else if((buttonLongStateTwo == 1) && (buttonStateTwo == 4)) { showMonth(); _month++; delay(20); if(_month > 12) _month = 1; // Serial.println(month); } else if((buttonLongStateTwo == 1) && (buttonStateTwo == 5)) { showYear(); _year++; if(_year > 99) _year = 15; delay(20); //Serial.println(year); } } else if (myBtn1.pressedFor(LONG_PRESS)){ lastPressTime = millis(); setDS3231time(_second,_minute,_hour,1,_date,_month,_year); //Serial.println("date set"); buttonLongStateTwo = 0; //Serial.println(buttonLongStateTwo); delay(100); } else if (myBtn2.wasReleased()){ lastPressTime = millis(); if(buttonLongStateTwo == 0){ } else if(buttonLongStateTwo == 1){ buttonStateTwo++; if(buttonStateTwo > 5) buttonStateTwo = 1; } if((buttonStateTwo == 1)&&(buttonLongStateTwo == 1)){ showMinute(); delay(20); //Serial.println("display minute"); } if((buttonStateTwo == 2)&&(buttonLongStateTwo == 1)){ //Serial.println("display hour"); showHour(); delay(20); } if((buttonStateTwo == 3)&&(buttonLongStateTwo == 1)){ //Serial.println("dispaly date"); showDate(); delay(20); } if((buttonStateTwo == 4)&&(buttonLongStateTwo == 1)){ //Serial.println("display month"); showMonth(); delay(20); } if((buttonStateTwo == 5)&&(buttonLongStateTwo == 1)){ //Serial.println("display year"); showYear(); delay(20); } } else if (myBtn2.pressedFor(LONG_PRESS)){ lastPressTime = millis(); readDS3231time(&_second, &_minute, &_hour, &_day, &_date, &_month, &_year); buttonLongStateTwo = 1; //Serial.println("editing mode"); delay(10); } } void editDisplay(){ if((buttonStateTwo == 1)&&(buttonLongStateTwo == 1)){ showMinute(); //Serial.println("display minute"); } if((buttonStateTwo == 2)&&(buttonLongStateTwo == 1)){ //Serial.println("display hour"); showHour(); } if((buttonStateTwo == 3)&&(buttonLongStateTwo == 1)){ //Serial.println("dispaly date"); showDate(); } if((buttonStateTwo == 4)&&(buttonLongStateTwo == 1)){ //Serial.println("display month"); showMonth(); } if((buttonStateTwo == 5)&&(buttonLongStateTwo == 1)){ //Serial.println("display year"); showYear(); } }