#include #define DS3231_I2C_ADDRESS 104 // SCL - pin A5 // SDA - pin A4 // To set the clock, run the sketch and use the serial monitor. // Enter T1124154091014; the code will read this and set the clock. See the code for full details. // byte seconds, minutes, hours, day, date, month, year; char weekDay[4]; byte tMSB, tLSB; float temp3231; const byte Pin_hour_up = 2; const byte Pin_minute_up = 3; // For Arduino Nano or Arduino UNO the PWM pins are 3,5,6,9,10 and 11. // The pins 3, 9, 10 and 11 generates PWM frequency of 490Hz and pins 5 and 6 generates PWM frequency of 980Hz. // The value parameter ranges from 0 to 255 corresponding to 0% and 100% duty cycle. // ============================================================================================================ int Pin_PWM_hour = 9; // Ausgabe-Pin für das PWM-Signal für die Stunden int Pin_PWM_minute = 10; // Ausgabe-Pin für das PWM-Signal für die Minuten int PWM_hour; // On-time des PWM-Signals für Stunden int PWM_minute; // On-time des PWM-Signals für Minuten //============================== //=========== SETUP ============ //============================== void setup() { Wire.begin(); Serial.begin(9600); pinMode(Pin_hour_up, INPUT); pinMode(Pin_minute_up, INPUT); pinMode(Pin_PWM_hour, OUTPUT); pinMode(Pin_PWM_minute, OUTPUT); } //====================================== //=========== HAUPTSCHLEIFE ============ //====================================== void loop() { watchConsole(); get3231Date(); if(digitalRead(Pin_hour_up) == 1) // change hour button pressed { hours = hours + 1; if(hours == 24) { hours = 0; } Wire.beginTransmission(DS3231_I2C_ADDRESS); Wire.write(0x00); Wire.write(decToBcd(seconds)); Wire.write(decToBcd(minutes)); Wire.write(decToBcd(hours)); Wire.write(decToBcd(day)); Wire.write(decToBcd(date)); Wire.write(decToBcd(month)); Wire.write(decToBcd(year)); Wire.endTransmission(); delay(500); } if(digitalRead(Pin_minute_up) == 1) // change minute button pressed { minutes = minutes + 1; if(minutes == 60) { minutes = 0; } Wire.beginTransmission(DS3231_I2C_ADDRESS); Wire.write(0x00); Wire.write(decToBcd(seconds)); Wire.write(decToBcd(minutes)); Wire.write(decToBcd(hours)); Wire.write(decToBcd(day)); Wire.write(decToBcd(date)); Wire.write(decToBcd(month)); Wire.write(decToBcd(year)); Wire.endTransmission(); delay(500); } /* Serial.print(weekDay); Serial.print(", "); Serial.print(date, DEC); Serial.print("/"); Serial.print(month, DEC); Serial.print("/"); Serial.print(year, DEC); Serial.print(" - "); Serial.print(hours, DEC); Serial.print(":"); Serial.print(minutes, DEC); Serial.print(":"); Serial.println(seconds, DEC); */ /* Serial.print(" - Temp: "); Serial.println(get3231Temp()); */ if(hours < 12) { PWM_hour = map(hours, 0, 11, 0, 255); } if(hours >= 12) { PWM_hour = map(hours - 12, 0, 11, 0, 255); } //Serial.print(hours); //Serial.print(":"); //Serial.println(minutes); PWM_minute = map(minutes, 0, 59, 0, 255); analogWrite(Pin_PWM_hour, PWM_hour); analogWrite(Pin_PWM_minute, PWM_minute); //analogWrite(Pin_PWM_hour, 255); //analogWrite(Pin_PWM_minute, 255); delay(100); } // Convert normal decimal numbers to binary coded decimal byte decToBcd(byte val) { return ( (val/10*16) + (val%10) ); } void watchConsole() { if (Serial.available()) { // Look for char in serial queue and process if found if (Serial.read() == 84) { //If command = "T" Set Date set3231Date(); get3231Date(); Serial.println(" "); } } } void set3231Date() { //T(sec)(min)(hour)(dayOfWeek)(dayOfMonth)(month)(year) //T(00-59)(00-59)(00-23)(1-7)(01-31)(01-12)(00-99) //Example: 02-Okt-15 @ 19:57:11 for the 3rd day of the week -> T1157193021015 seconds = (byte) ((Serial.read() - 48) * 10 + (Serial.read() - 48)); // Use of (byte) type casting and ascii math to achieve result. minutes = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48)); hours = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48)); day = (byte) (Serial.read() - 48); date = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48)); month = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48)); year = (byte) ((Serial.read() - 48) *10 + (Serial.read() - 48)); Wire.beginTransmission(DS3231_I2C_ADDRESS); Wire.write(0x00); Wire.write(decToBcd(seconds)); Wire.write(decToBcd(minutes)); Wire.write(decToBcd(hours)); Wire.write(decToBcd(day)); Wire.write(decToBcd(date)); Wire.write(decToBcd(month)); Wire.write(decToBcd(year)); Wire.endTransmission(); } void get3231Date() { // send request to receive data starting at register 0 Wire.beginTransmission(DS3231_I2C_ADDRESS); // 104 is DS3231 device address Wire.write(0x00); // start at register 0 Wire.endTransmission(); Wire.requestFrom(DS3231_I2C_ADDRESS, 7); // request seven bytes if(Wire.available()) { seconds = Wire.read(); // get seconds minutes = Wire.read(); // get minutes hours = Wire.read(); // get hours day = Wire.read(); date = Wire.read(); month = Wire.read(); //temp month year = Wire.read(); seconds = (((seconds & B11110000)>>4)*10 + (seconds & B00001111)); // convert BCD to decimal minutes = (((minutes & B11110000)>>4)*10 + (minutes & B00001111)); // convert BCD to decimal hours = (((hours & B00110000)>>4)*10 + (hours & B00001111)); // convert BCD to decimal (assume 24 hour mode) day = (day & B00000111); // 1-7 date = (((date & B00110000)>>4)*10 + (date & B00001111)); // 1-31 month = (((month & B00010000)>>4)*10 + (month & B00001111)); //msb7 is century overflow year = (((year & B11110000)>>4)*10 + (year & B00001111)); } else { //oh noes, no data! } switch (day) { case 1: strcpy(weekDay, "Mo"); break; case 2: strcpy(weekDay, "Di"); break; case 3: strcpy(weekDay, "Mi"); break; case 4: strcpy(weekDay, "Do"); break; case 5: strcpy(weekDay, "Fr"); break; case 6: strcpy(weekDay, "Sa"); break; case 7: strcpy(weekDay, "So"); break; } } float 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 temp3231 += ( (tLSB >> 6) * 0.25 ); //only care about bits 7 & 8 } else { //oh noes, no data! } return temp3231; }