Advertisement
orneto

Untitled

May 30th, 2023
823
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 5.33 KB | None | 0 0
  1. //#include "DS1307RTC.h"
  2. #include <Wire.h>
  3.  
  4. #define DS1307_ADD 0x68
  5. #define SDA PB_3
  6. #define SCL PB_2
  7.  
  8. unsigned int SETTED = false;
  9. typedef struct {
  10.   uint8_t s;
  11.   uint8_t m;
  12.   uint8_t h;
  13.   uint8_t DW;
  14.   uint8_t D;
  15.   uint8_t M;
  16.   uint8_t Y;
  17.  
  18. }_ctime;
  19.  
  20.  
  21. uint8_t bcd2dec(uint8_t num);
  22. uint8_t dec2bcd(uint8_t num);
  23.  
  24. uint8_t DIASMES(uint8_t MES){
  25.   uint8_t meses[12] = {31 , 28, 31 , 30 , 31 , 30 , 31 , 31 , 30 , 31 , 30 ,31};
  26.   return meses[MES%12];
  27. }
  28.  
  29. static void(*proc)();
  30.  
  31. typedef union {
  32.   uint8_t state = 0;
  33.   struct{
  34.     uint8_t isBerlim:1;
  35.     uint8_t isAmPm:1;
  36.   };
  37.  
  38.   void operator=(int in){
  39.     (*this).state = in;
  40.   }
  41. }programStates_t;
  42.  
  43. programStates_t Programstate;
  44.  
  45. _ctime DSgetTime();
  46.  
  47.  
  48. void nullf(){
  49.  
  50. }
  51.  
  52.  
  53. void setData(){
  54.   if(Serial.available()){
  55.     String inputD  = Serial.readString();
  56.     uint8_t buff;
  57.     unsigned int vals = 8;
  58.     for (int cont = 0 ; cont < a.length() ; cont++){
  59.         switch(a[cont]){
  60.             case '0':case '1':case '2':case '3':case '4':case '5':case '6':case '7':case '8':case '9':
  61.             buff = buff*10 +  a[cont];
  62.             break;
  63.             default:
  64.             vals += (vals<<(vals%0xFF));
  65.             vals += 8;
  66.         }
  67.         buff+= a[cont];
  68.     }
  69.     proc = Menu;
  70.   }
  71. }
  72.  
  73. void readmenu(){
  74.   char c = '\0';
  75.   if(Serial.available()){
  76.     c = Serial.read();
  77.   }
  78.  
  79.   switch(c){
  80.     case 'A':
  81.     break;
  82.     case 'C':
  83.     break;
  84.     case 'D':
  85.     proc = setData;
  86.     break;
  87.     case 'F':
  88.       Programstate.isBerlim = !Programstate.isBerlim;
  89.     break;
  90.     case 'W':case 'w':
  91.       Programstate.isAmPm = !Programstate.isAmPm;
  92.     break;
  93.   }
  94. }
  95.  
  96.  
  97.  
  98. void Menu(){
  99.   Serial.println("A - Seta um Alarme");
  100.   Serial.println("C - Cronômetro");
  101.   Serial.println("D - Ajusta a Data");
  102.   Serial.println("F - Fuso horário");
  103.   Serial.println("H - Ajusta a hora");
  104.   Serial.println("W - Ajusta o formato da hora");
  105.   proc = readmenu;
  106. }
  107.  
  108.  
  109. void printdate(_ctime currentTime){
  110.   if(Programstate.isBerlim && ((currentTime.h + 4) > 24)){
  111.     currentTime.D = (currentTime.D + 1)%DIASMES(currentTime.M);
  112.     currentTime.M = (currentTime.M +  ((currentTime.D + 1) >DIASMES(currentTime.M)))%12 + 1;
  113.     currentTime.Y = currentTime.Y +   ((currentTime.D == 1)&&(currentTime.M == 1));
  114.   }
  115.   currentTime.h = (currentTime.h +  Programstate.isBerlim*4)%24;
  116.  
  117.   if(Programstate.isAmPm){
  118.     Serial.print("Time: ");
  119.     Serial.print(currentTime.D);
  120.     Serial.print("/");
  121.     Serial.print(currentTime.M);
  122.     Serial.print("/");
  123.     Serial.print(1982+currentTime.Y);
  124.     Serial.print("   ");
  125.     Serial.print((currentTime.h)%12 == 0 ? 12 : (currentTime.h)%12);
  126.     Serial.print(":");
  127.     Serial.print(currentTime.m);
  128.     Serial.print(":");
  129.     Serial.print(currentTime.s);
  130.     Serial.print("  ");
  131.     if(currentTime.h > 12){
  132.       Serial.println("Pm");
  133.     }else{
  134.       Serial.println("Am");
  135.     }
  136.   }else{
  137.     Serial.print("Time: ");
  138.     Serial.print(currentTime.D);
  139.     Serial.print("/");
  140.     Serial.print(currentTime.M);
  141.     Serial.print("/");
  142.     Serial.print(1982+currentTime.Y);
  143.     Serial.print("   ");
  144.     Serial.print(currentTime.h);
  145.     Serial.print(":");
  146.     Serial.print(currentTime.m);
  147.     Serial.print(":");
  148.     Serial.println(currentTime.s);
  149.   }
  150. }
  151.  
  152. void setup() {
  153.   Programstate = 0;
  154.   Serial.begin(115200);
  155.   Wire.begin();
  156.   Serial.println("DS1307 Code");
  157.   _ctime thistime;
  158.   thistime.s = 0;
  159.   thistime.m = 29;
  160.   thistime.h = 21;
  161.   thistime.DW = 2;
  162.   thistime.D = 29;
  163.   thistime.M = 5;
  164.   thistime.Y = 23;
  165.   //DSwriteTime(thistime);
  166.   delay(1000);
  167.   proc = Menu;
  168. }
  169.  
  170. void loop() {
  171.   static _ctime currentTime;
  172.   proc();
  173.   if(!(millis()%1000)){
  174.     currentTime = DSgetTime();
  175.     printdate(currentTime);
  176.   }
  177.   delay(2);
  178. }
  179.  
  180.  
  181. _ctime DSgetTime(){
  182.   _ctime rtn;
  183.   Wire.beginTransmission(DS1307_ADD);
  184.   Wire.write((uint8_t)0x00);
  185.   if(Wire.endTransmission() != 0){
  186.     Serial.println("Not found");
  187.     return rtn;
  188.   }
  189.   Wire.requestFrom(DS1307_ADD, 7);
  190.   uint8_t avl = Wire.available();
  191.   if (avl < 7){
  192.     Serial.print("Avl: ");
  193.     Serial.println(avl);
  194.     return rtn;
  195.   }
  196.   rtn.s  =  bcd2dec(Wire.read()&0x7F);
  197.   rtn.m  =  bcd2dec(Wire.read());
  198.   rtn.h  =  bcd2dec(Wire.read()&0x3F);
  199.   rtn.DW =  bcd2dec(Wire.read());
  200.   rtn.D  =  bcd2dec(Wire.read());
  201.   rtn.M  =  bcd2dec(Wire.read());
  202.   rtn.Y  =  bcd2dec(Wire.read());
  203.  
  204.   return rtn;
  205. }
  206.  
  207. uint8_t DSwriteTime(_ctime tm){
  208.   Wire.beginTransmission(DS1307_ADD);
  209.    Wire.write((uint8_t)0x00); // reset register pointer  
  210.   Wire.write((uint8_t)0x80); // Stop the clock. The seconds will be written last
  211.   Wire.write(dec2bcd(tm.m));
  212.   Wire.write(dec2bcd(tm.h));      // sets 24 hour format
  213.   Wire.write(dec2bcd(tm.DW));  
  214.   Wire.write(dec2bcd(tm.D));
  215.   Wire.write(dec2bcd(tm.M));
  216.   Wire.write(dec2bcd(tm.Y)+30);
  217.  
  218.   if(Wire.endTransmission() != 0){
  219.     Serial.println("Not found");
  220.     return 0;
  221.   }
  222.  
  223.   Wire.beginTransmission(DS1307_ADD);
  224.   Wire.write((uint8_t)0x00); // reset register pointer  
  225.   Wire.write(dec2bcd(tm.s));
  226.  
  227.   if(Wire.endTransmission() != 0){
  228.     Serial.println("Not found");
  229.     return 0;
  230.   }
  231.   return 1;
  232. }
  233.  
  234.  
  235.  
  236. uint8_t dec2bcd(uint8_t num){
  237.   return ((num/10 * 16) + (num % 10));
  238. }
  239.  
  240. // Convert Binary Coded Decimal (BCD) to Decimal
  241. uint8_t bcd2dec(uint8_t num){
  242.   return ((num/16 * 10) + (num % 16));
  243. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement