Advertisement
RuiViana

Duplo_Set_Seg_Anod_C

Jul 15th, 2016
282
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.11 KB | None | 0 0
  1. //Doble Didplay siete - segmentos LED con botones
  2. //
  3. // Basado en un trabajo de Natalia Fargasch Norman
  4. //
  5. //
  6. // Common Catode digit 1 pin 10
  7. // Common Catode digit 2 pin 5
  8. //
  9. // Modificado y mejorado 11.08.11
  10. // V. García
  11. //
  12. // Utiliza 2202 bytes con Arduino v0013
  13.  
  14. // CA1 G F A B
  15. // | | | | | | -> pines y segmentos de control
  16. // --------- ---------
  17. // | A | | A |
  18. // F| |B F| |B
  19. // |---G---| |---G---|
  20. // E| |C E| |C
  21. // | D | | D |
  22. // --------- ---------
  23. // | | | | | | -> pines y segmentos de control
  24. // D DP E C CA2
  25.  
  26. // Composición de los digitos en Segmentos
  27. // 0 => -FEDCBA
  28. // 1 => ----BC-
  29. // 2 => G-ED-BA
  30. // 3 => G--DCBA
  31. // 4 => GF--CB-
  32. // 5 => GF-DC-A
  33. // 6 => GFEDC-A
  34. // 7 => ----CBA
  35. // 8 => GFEDCBA
  36. // 9 => GF-DCBA
  37.  
  38. // Pines digitales usados Arduino para encender
  39. // los correspondientes segmentos LED del display
  40. #define A 2
  41. #define B 3
  42. #define C 4
  43. #define D 5
  44. #define E 6
  45. #define F 7
  46. #define G 8
  47.  
  48. // Pulsadoes boton conectados a pines 9 y 10
  49. #define BTN1 14
  50. #define BTN2 15
  51. #define led 13
  52. // Pines comunes de cada display de anodo comun
  53. #define CA1 9
  54. #define CA2 10
  55.  
  56. // Pines para A B C D E F G, en secuencia
  57. // se pueden usar los que más interesen
  58. const int segs[7] = { 2, 3, 4, 5, 6, 7, 8 };
  59.  
  60. // Segmentos que componen cada número
  61. // Para CC.
  62. const byte numbers[10] = { 0b0111111, 0b0000110, 0b1011011, 0b1001111, 0b1100110,
  63.                            0b1101101, 0b1111101, 0b0000111, 0b1111111, 0b1101111
  64.                          };
  65. // Para CA. descomentar las 2 líneas que siguen.
  66. // const byte numbers[10] = { 0b1000000, 0b1111001, 0b0100100, 0b0110000, 0b0011001, 0b0010010,
  67. // 0b0000010, 0b1111000, 0b0000000, 0b0010000};
  68.  
  69. int digit1 = 0;
  70. int digit2 = 0;
  71.  
  72. void setup()
  73. {
  74.   pinMode(A, OUTPUT);
  75.   pinMode(B, OUTPUT);
  76.   pinMode(C, OUTPUT);
  77.   pinMode(D, OUTPUT);
  78.   pinMode(E, OUTPUT);
  79.   pinMode(F, OUTPUT);
  80.   pinMode(G, OUTPUT);
  81.   pinMode(BTN1, INPUT);
  82.   pinMode(BTN2, INPUT);
  83.   digitalWrite(BTN1, HIGH); // activa RPA
  84.   digitalWrite(BTN2, HIGH); // activa RPA
  85.   pinMode(CA1, OUTPUT);
  86.   pinMode(CA2, OUTPUT);
  87.   pinMode(led, OUTPUT);
  88.  
  89.   // digit1 = 9; digit2 = 9;
  90.  
  91. }
  92. //-------------------------------------
  93. void loop()
  94. {
  95.   // chequea boton1. Incrementa
  96.   int val1 = digitalRead(BTN1);
  97.   if (val1 == LOW)
  98.   {
  99.     if (digit2 >= 9 && digit1 >= 9)
  100.     {
  101.       digit2 = 0;
  102.       digit1++;
  103.       digit1 %= 10;
  104.       if (digit1 >= 9)
  105.       {
  106.         digit2++;
  107.       }
  108.     } else if (digit1 >= 9)
  109.     {
  110.       digit2++;
  111.       digit2 %= 10;
  112.     }
  113.     digit1++;
  114.     digit1 %= 10;
  115.     delay(10);
  116.   }
  117.  
  118.   // cheque boton2. Decrementa
  119.   int val2 = digitalRead(BTN2);
  120.   if (val2 == LOW)
  121.   {
  122.     if (digit1 >= 0)
  123.     {
  124.       if (digit1 < 0)
  125.       {
  126.         digit1 = 0;
  127.       }
  128.       digit1--;
  129.     }
  130.     if (digit1 < 0)
  131.     {
  132.       digit1 = 9;
  133.       if (digit2 < 0)
  134.       {
  135.         digit2 = 0;
  136.       }
  137.       digit2--;
  138.     }
  139.     if (digit2 < 0)
  140.     {
  141.       digit2 = 9;
  142.     }
  143.   }
  144.  
  145.   // display numero
  146.   unsigned long startTime = millis();
  147.   for (unsigned long elapsed = 0; elapsed < 300; elapsed = millis() - startTime)
  148.   {
  149.     lightDigit1(numbers[digit1]);
  150.     delay(5);
  151.     lightDigit2(numbers[digit2]);
  152.     delay(5);
  153.   }
  154. }
  155. //-----------------------------------
  156. void lightDigit1(byte number)
  157. {
  158.  // digitalWrite(CA1, LOW);             // Catodo comum
  159.  // digitalWrite(CA2, HIGH);            // Catodo comum
  160.  
  161.   digitalWrite(CA1, HIGH);              // Anodo comum
  162.   digitalWrite(CA2, LOW);               // Anodo comum
  163.  
  164.   lightSegments(number);
  165. }
  166. //-----------------------------------
  167. void lightDigit2(byte number)
  168. {
  169.  // digitalWrite(CA1, HIGH);            // Catodo comum
  170.  // digitalWrite(CA2, LOW);             // Catodo comum
  171.  
  172.   digitalWrite(CA1, LOW);               // Anodo comum
  173.   digitalWrite(CA2, HIGH);              // Anodo comum
  174.  
  175.   lightSegments(number);
  176. }
  177. //-----------------------------------
  178. void lightSegments(byte number)
  179. {
  180.   for (int i = 0; i < 7; i++)
  181.   {
  182.     // int bit = !bitRead(number, i);     // Catodo comum
  183.     int bit = !bitRead(number, i);        // Anodo comum
  184.     digitalWrite(segs[i], bit);
  185.   }
  186. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement