Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //#include "DS1307RTC.h"
- #include <Wire.h>
- #define DS1307_ADD 0x68
- #define SDA PB_3
- #define SCL PB_2
- unsigned int SETTED = false;
- typedef struct {
- uint8_t s;
- uint8_t m;
- uint8_t h;
- uint8_t DW;
- uint8_t D;
- uint8_t M;
- uint8_t Y;
- }_ctime;
- uint8_t bcd2dec(uint8_t num);
- uint8_t dec2bcd(uint8_t num);
- uint8_t DIASMES(uint8_t MES){
- uint8_t meses[12] = {31 , 28, 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 ,31};
- return meses[MES%12];
- }
- static void(*proc)();
- typedef union {
- uint8_t state = 0;
- struct{
- uint8_t isBerlim:1;
- uint8_t isAmPm:1;
- };
- void operator=(int in){
- (*this).state = in;
- }
- }programStates_t;
- programStates_t Programstate;
- _ctime DSgetTime();
- void nullf(){
- }
- void setData(){
- if(Serial.available()){
- String inputD = Serial.readString();
- uint8_t buff;
- unsigned int vals = 8;
- for (int cont = 0 ; cont < a.length() ; cont++){
- switch(a[cont]){
- case '0':case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8':case '9':
- buff = buff*10 + a[cont];
- break;
- default:
- vals += (vals<<(vals%0xFF));
- vals += 8;
- }
- buff+= a[cont];
- }
- proc = Menu;
- }
- }
- void readmenu(){
- char c = '\0';
- if(Serial.available()){
- c = Serial.read();
- }
- switch(c){
- case 'A':
- break;
- case 'C':
- break;
- case 'D':
- proc = setData;
- break;
- case 'F':
- Programstate.isBerlim = !Programstate.isBerlim;
- break;
- case 'W':case 'w':
- Programstate.isAmPm = !Programstate.isAmPm;
- break;
- }
- }
- void Menu(){
- Serial.println("A - Seta um Alarme");
- Serial.println("C - Cronômetro");
- Serial.println("D - Ajusta a Data");
- Serial.println("F - Fuso horário");
- Serial.println("H - Ajusta a hora");
- Serial.println("W - Ajusta o formato da hora");
- proc = readmenu;
- }
- void printdate(_ctime currentTime){
- if(Programstate.isBerlim && ((currentTime.h + 4) > 24)){
- currentTime.D = (currentTime.D + 1)%DIASMES(currentTime.M);
- currentTime.M = (currentTime.M + ((currentTime.D + 1) >DIASMES(currentTime.M)))%12 + 1;
- currentTime.Y = currentTime.Y + ((currentTime.D == 1)&&(currentTime.M == 1));
- }
- currentTime.h = (currentTime.h + Programstate.isBerlim*4)%24;
- if(Programstate.isAmPm){
- Serial.print("Time: ");
- Serial.print(currentTime.D);
- Serial.print("/");
- Serial.print(currentTime.M);
- Serial.print("/");
- Serial.print(1982+currentTime.Y);
- Serial.print(" ");
- Serial.print((currentTime.h)%12 == 0 ? 12 : (currentTime.h)%12);
- Serial.print(":");
- Serial.print(currentTime.m);
- Serial.print(":");
- Serial.print(currentTime.s);
- Serial.print(" ");
- if(currentTime.h > 12){
- Serial.println("Pm");
- }else{
- Serial.println("Am");
- }
- }else{
- Serial.print("Time: ");
- Serial.print(currentTime.D);
- Serial.print("/");
- Serial.print(currentTime.M);
- Serial.print("/");
- Serial.print(1982+currentTime.Y);
- Serial.print(" ");
- Serial.print(currentTime.h);
- Serial.print(":");
- Serial.print(currentTime.m);
- Serial.print(":");
- Serial.println(currentTime.s);
- }
- }
- void setup() {
- Programstate = 0;
- Serial.begin(115200);
- Wire.begin();
- Serial.println("DS1307 Code");
- _ctime thistime;
- thistime.s = 0;
- thistime.m = 29;
- thistime.h = 21;
- thistime.DW = 2;
- thistime.D = 29;
- thistime.M = 5;
- thistime.Y = 23;
- //DSwriteTime(thistime);
- delay(1000);
- proc = Menu;
- }
- void loop() {
- static _ctime currentTime;
- proc();
- if(!(millis()%1000)){
- currentTime = DSgetTime();
- printdate(currentTime);
- }
- delay(2);
- }
- _ctime DSgetTime(){
- _ctime rtn;
- Wire.beginTransmission(DS1307_ADD);
- Wire.write((uint8_t)0x00);
- if(Wire.endTransmission() != 0){
- Serial.println("Not found");
- return rtn;
- }
- Wire.requestFrom(DS1307_ADD, 7);
- uint8_t avl = Wire.available();
- if (avl < 7){
- Serial.print("Avl: ");
- Serial.println(avl);
- return rtn;
- }
- rtn.s = bcd2dec(Wire.read()&0x7F);
- rtn.m = bcd2dec(Wire.read());
- rtn.h = bcd2dec(Wire.read()&0x3F);
- rtn.DW = bcd2dec(Wire.read());
- rtn.D = bcd2dec(Wire.read());
- rtn.M = bcd2dec(Wire.read());
- rtn.Y = bcd2dec(Wire.read());
- return rtn;
- }
- uint8_t DSwriteTime(_ctime tm){
- Wire.beginTransmission(DS1307_ADD);
- Wire.write((uint8_t)0x00); // reset register pointer
- Wire.write((uint8_t)0x80); // Stop the clock. The seconds will be written last
- Wire.write(dec2bcd(tm.m));
- Wire.write(dec2bcd(tm.h)); // sets 24 hour format
- Wire.write(dec2bcd(tm.DW));
- Wire.write(dec2bcd(tm.D));
- Wire.write(dec2bcd(tm.M));
- Wire.write(dec2bcd(tm.Y)+30);
- if(Wire.endTransmission() != 0){
- Serial.println("Not found");
- return 0;
- }
- Wire.beginTransmission(DS1307_ADD);
- Wire.write((uint8_t)0x00); // reset register pointer
- Wire.write(dec2bcd(tm.s));
- if(Wire.endTransmission() != 0){
- Serial.println("Not found");
- return 0;
- }
- return 1;
- }
- uint8_t dec2bcd(uint8_t num){
- return ((num/10 * 16) + (num % 10));
- }
- // Convert Binary Coded Decimal (BCD) to Decimal
- uint8_t bcd2dec(uint8_t num){
- return ((num/16 * 10) + (num % 16));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement