This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

dansku

By: a guest on Aug 1st, 2009  |  syntax: C  |  size: 4.60 KB  |  views: 23  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1.  
  2.  
  3. #include "Wire.h"
  4. #define DS1307_I2C_ADDRESS 0x68
  5.  
  6. int* ano,i;
  7. char* myStrings[]={"Segunda-Feira", "Terca-Feira", "Quarta-Feira","Quinta-Feira", "Sexta-Feira","Sabado","Domingo"};
  8.  
  9. int tempc = 0,tempf=0; // temperature variables
  10. int samples[8]; // variables to make a better precision
  11. int maxi = -100,mini = 100; // to start max/min temperature
  12.  
  13. // Convert normal decimal numbers to binary coded decimal
  14. byte decToBcd(byte val)
  15. {
  16.   return ( (val/10*16) + (val%10) );
  17. }
  18.  
  19. // Convert binary coded decimal to normal decimal numbers
  20. byte bcdToDec(byte val)
  21. {
  22.   return ( (val/16*10) + (val%16) );
  23. }
  24.  
  25. // Stops the DS1307, but it has the side effect of setting seconds to 0
  26. // Probably only want to use this for testing
  27. /*void stopDs1307()
  28. {
  29.   Wire.beginTransmission(DS1307_I2C_ADDRESS);
  30.   Wire.send(0);
  31.   Wire.send(0x80);
  32.   Wire.endTransmission();
  33. }*/
  34.  
  35. // 1) Sets the date and time on the ds1307
  36. // 2) Starts the clock
  37. // 3) Sets hour mode to 24 hour clock
  38. // Assumes you're passing in valid numbers
  39. void setDateDs1307(byte second,        // 0-59
  40.                    byte minute,        // 0-59
  41.                    byte hour,          // 1-23
  42.                    byte dayOfWeek,     // 1-7
  43.                    byte dayOfMonth,    // 1-28/29/30/31
  44.                    byte month,         // 1-12
  45.                    byte year)          // 0-99
  46. {
  47.    Wire.beginTransmission(DS1307_I2C_ADDRESS);
  48.    Wire.send(0);
  49.    Wire.send(decToBcd(second));    // 0 to bit 7 starts the clock
  50.    Wire.send(decToBcd(minute));
  51.    Wire.send(decToBcd(hour));      // If you want 12 hour am/pm you need to set
  52.                                    // bit 6 (also need to change readDateDs1307)
  53.    Wire.send(decToBcd(dayOfWeek));
  54.    Wire.send(decToBcd(dayOfMonth));
  55.    Wire.send(decToBcd(month));
  56.    Wire.send(decToBcd(year));
  57.    Wire.endTransmission();
  58. }
  59.  
  60. // Gets the date and time from the ds1307
  61. void getDateDs1307(byte *second,
  62.           byte *minute,
  63.           byte *hour,
  64.           byte *dayOfWeek,
  65.           byte *dayOfMonth,
  66.           byte *month,
  67.           byte *year)
  68. {
  69.   // Reset the register pointer
  70.   Wire.beginTransmission(DS1307_I2C_ADDRESS);
  71.   Wire.send(0);
  72.   Wire.endTransmission();
  73.  
  74.   Wire.requestFrom(DS1307_I2C_ADDRESS, 7);
  75.  
  76.   // A few of these need masks because certain bits are control bits
  77.   *second     = bcdToDec(Wire.receive() & 0x7f);
  78.   *minute     = bcdToDec(Wire.receive());
  79.   *hour       = bcdToDec(Wire.receive() & 0x3f);  // Need to change this if 12 hour am/pm
  80.   *dayOfWeek  = bcdToDec(Wire.receive());
  81.   *dayOfMonth = bcdToDec(Wire.receive());
  82.   *month      = bcdToDec(Wire.receive());
  83.   *year       = bcdToDec(Wire.receive());
  84. }
  85.  
  86. void setup()
  87. {
  88.   byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  89.   Wire.begin();
  90.   Serial.begin(9600);
  91.  
  92.   // Change these values to what you want to set your clock to.
  93.   // You probably only want to set your clock once and then remove
  94.   // the setDateDs1307 call.
  95.   second = 40;
  96.   minute = 45;
  97.   hour = 10;
  98.   dayOfWeek = 6;
  99.   dayOfMonth = 1;
  100.   month = 8;
  101.   year = 9;
  102.   setDateDs1307(second, minute, hour, dayOfWeek, dayOfMonth, month, year);
  103. }
  104.  
  105. void loop()
  106. {
  107.  
  108.  
  109.   byte second, minute, hour, dayOfWeek, dayOfMonth, month, year;
  110.  
  111.   getDateDs1307(&second, &minute, &hour, &dayOfWeek, &dayOfMonth, &month, &year);
  112.  
  113.   ano = 2000 + year; // Changed to a int so it can show the year in 20XX style
  114.  
  115.     for(i = 0;i< =7;i++){ // gets 8 samples of temperature
  116.  
  117.     samples[i] = ( 5 * analogRead(0) * 100) / 1024;
  118.     tempc = tempc + samples[i];
  119.     delay(50);
  120.     }
  121.    
  122.     tempc = tempc/8; // better precision
  123.     tempf = (tempc * 9)/ 5 + 32; // converts to fahrenheit
  124.    
  125.     if(tempc > maxi) {maxi = tempc;} // set max temperature
  126.     if(tempc < mini) {mini = tempc;} // set min temperature
  127.  
  128.   if(hour<10) {Serial.print("0");}
  129.   Serial.print(hour, DEC);
  130.   Serial.print(":");
  131.   if(minute<10) {Serial.print("0");}
  132.   Serial.print(minute, DEC);
  133.   Serial.print(":");
  134.   if(second<10) {Serial.print("0");}
  135.   Serial.print(second, DEC);
  136.   Serial.print("  ");
  137.   if(dayOfMonth<10) {Serial.print("0");}
  138.   Serial.print(dayOfMonth, DEC);
  139.   Serial.print("/");
  140.   if(month<10) {Serial.print("0");}
  141.   Serial.print(month, DEC);
  142.   Serial.print("/");
  143.   Serial.print(ano, DEC);
  144.   Serial.print("  Dia da semana: ");
  145.   Serial.print(myStrings[dayOfWeek-1]);
  146.    Serial.print(" - Temperatura:a: ");
  147.   Serial.print(tempc,DEC);
  148.   Serial.print("C ,  ");
  149.   Serial.print(tempf,DEC);
  150.   Serial.print("F - ");
  151.   Serial.print(maxi,DEC);
  152.   Serial.print(" Max, ");
  153.   Serial.print(mini,DEC);
  154.   Serial.println(" Min");
  155.  
  156. tempc = 0;
  157.  
  158.   delay(600);
  159. }
clone this paste RAW Paste Data