Advertisement
RuiViana

Relogio_C_Pisca

Apr 22nd, 2016
228
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.31 KB | None | 0 0
  1. //Programa : Relogio com Arduino, DS1307 e Display 7 Seg
  2. //Autor : FILIPEFLOP
  3.  
  4. #include "Wire.h"
  5. #include "SevSeg.h"
  6.  
  7. #define DS1307_ADDRESS 0x68
  8.  
  9. //Create an instance of the object.
  10. SevSeg display7seg;
  11.  
  12. int valor = 0;
  13.  
  14. byte zero = 0x00;
  15. unsigned long timer, Tempo;
  16. byte Blink = 0;
  17. //-------------------------------------------------
  18. void setup()
  19. {
  20. Serial.begin(9600);
  21. Wire.begin();
  22. //A linha abaixo pode ser retirada apos setar a data e hora
  23. // SelecionaDataeHora();
  24. //Selecao tipo de display anodo comum
  25. int displayType = COMMON_CATHODE;
  26.  
  27. //Definicao dos pinos dos digitos
  28. int digit1 = 10; //Pino Digito1 do display
  29. int digit2 = 11; //Pino Digito2 do display
  30. int digit3 = 12; //Pino Digito3 do display
  31. int digit4 = 13; //Pino Digito4 do display
  32.  
  33. //Pinos ligados aos segmentos A - H
  34. int segA = 2; //Pino segmento A
  35. int segB = 3; //Pino segmento B
  36. int segC = 4; //Pino segmento C
  37. int segD = 5; //Pino segmento D
  38. int segE = 6; //Pino segmento E
  39. int segF = 7; //Pino segmento F
  40. int segG = 8; //Pino segmento G
  41. int segDP= 9; //Pino segmento H
  42.  
  43. //Define o numero de digitos do display
  44. int numberOfDigits = 4;
  45.  
  46. //Inicializa o display
  47. display7seg.Begin(displayType, numberOfDigits, digit1, digit2, digit3, digit4, segA, segB, segC, segD, segE, segF, segG, segDP);
  48.  
  49. //Nivel de brilho do display
  50. display7seg.SetBrightness(50);
  51. timer = millis();
  52. Tempo = millis();
  53. }
  54. //-------------------------------------------------
  55. void loop()
  56. {
  57. if (millis() - Tempo >= 500)
  58. {
  59. if (Blink == 2)
  60. {
  61. Blink = 0;
  62. }
  63. else
  64. {
  65. Blink = 2;
  66. }
  67. Tempo = millis();
  68. }
  69.  
  70. char tempString[10]; //Used for sprintf
  71. Wire.beginTransmission(DS1307_ADDRESS);
  72. Wire.write(zero);
  73. Wire.endTransmission();
  74. Wire.requestFrom(DS1307_ADDRESS, 7);
  75. int segundos = ConverteparaDecimal(Wire.read());
  76. int minutos = ConverteparaDecimal(Wire.read());
  77. int horas = ConverteparaDecimal(Wire.read() & 0b111111);
  78. sprintf(tempString, "%02d%02d", horas, minutos);
  79. display7seg.DisplayString(tempString, Blink);
  80. }
  81. //-------------------------------------------------
  82. void SelecionaDataeHora() //Seta a data e a hora do DS1307
  83. {
  84. byte segundos = 10; //Valores de 0 a 59
  85. byte minutos = 32; //Valores de 0 a 59
  86. byte horas = 11; //Valores de 0 a 23
  87. Wire.beginTransmission(DS1307_ADDRESS);
  88. Wire.write(zero); //Stop no CI para que o mesmo possa receber os dados
  89.  
  90. //As linhas abaixo escrevem no CI os valores de
  91. //data e hora que foram colocados nas variaveis acima
  92. Wire.write(ConverteParaBCD(segundos));
  93. Wire.write(ConverteParaBCD(minutos));
  94. Wire.write(ConverteParaBCD(horas));
  95. Wire.write(zero);
  96. Wire.endTransmission();
  97. }
  98. //-------------------------------------------------
  99. byte ConverteParaBCD(byte val)
  100. {
  101. //Converte o número de decimal para BCD
  102. return ( (val/10*16) + (val%10) );
  103. }
  104. //-------------------------------------------------
  105. byte ConverteparaDecimal(byte val)
  106. {
  107. //Converte de BCD para decimal
  108. return ( (val/16*10) + (val%16) );
  109. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement