Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 5th, 2012  |  syntax: C++  |  size: 9.57 KB  |  hits: 14  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
This paste has a previous version, view the difference. Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <ST7565.h>
  2. #include <SPI.h>
  3. #define outC 2
  4. #define outB 3
  5. #define outA 4
  6. #define in 12
  7. #define button 10
  8. #define SW1 11
  9. byte KMAP/*2][4]*/;
  10. String BOOL = "";
  11. String Old = "";
  12. byte t;
  13. byte f;
  14. ST7565 glcd(9, 8, 7, 6, 5);
  15.  
  16. void setup()  
  17. /* This section sets up the intial conditions required to execute
  18.  /  all subsequent functions, mostly setting the pins as input
  19.  /  and output and intializing the LCD libraries.
  20.  */
  21. {
  22.  
  23.   //pinMode(outC, OUTPUT);              //Set these pins to output
  24.   //pinMode(outB, OUTPUT);
  25.   //pinMode(outA, OUTPUT);
  26.   //pinMode(in, INPUT);                 //Set these pins to input
  27.   digitalWrite(in, HIGH);
  28.   pinMode(button, INPUT);
  29.   digitalWrite(button, HIGH);         //Turn on internal pullup resistor
  30.   Serial.begin(9600);                 //Start serial communication
  31.   Serial.println("Serial begin.");    //Print the test line
  32.   Serial.println(freeRam());
  33.   glcd.begin(0x18);
  34.   glcd.st7565_init();
  35.   glcd.st7565_command(CMD_DISPLAY_ON);
  36.   glcd.st7565_command(CMD_SET_ALLPTS_NORMAL);
  37.   DDRC |= B111;
  38.  
  39. }
  40. void loop()  
  41. {
  42.   if (digitalRead(button) == LOW) {
  43.     digitalWrite(13, HIGH);
  44.     READ();
  45.     CONV();
  46.   }
  47.   else {
  48.   digitalWrite(13, LOW);
  49.   }
  50.   //DISP();
  51.   glcd.display();
  52. }
  53.    
  54.   /*digitalWrite(13, LOW);
  55.   if (digitalRead(SW1) == LOW)  {
  56.     DISPT();
  57.   }
  58.   else if (digitalRead(SW1) == HIGH)  {
  59.     DISPB();
  60.   }*/
  61.  
  62.  
  63.  
  64. void READ()  
  65. /* This section is the read and store section; it interacts with
  66.  /  the outside world via the designated output and input pins.
  67.  /  This section will write to each output, counting up in binary
  68.  /  with the MSB called A, and the LSB called C.
  69.  */
  70. {
  71.  
  72.  
  73.   byte KMAP = 0;
  74.   byte x;
  75.   byte y;
  76.  
  77.   for (x = 0; x < 8; x++)  {
  78.     PORTC = x;
  79.     delay(2);
  80.     bitWrite(KMAP, x, digitalRead(in));
  81.   }
  82.  
  83. }
  84.  
  85. void CONV()  
  86. /* This is the Karnaugh mapping section, which checks for contiguous
  87.  /  sections of true outputs on a grid created in the array KMAP.
  88.  /  This simplifies the expression by eliminating variables whose level
  89.  /  is irrelevant because the output is true when it is true or false.
  90.  /  Simplified expressions are added to string BOOL.
  91.  */
  92. {
  93.   BOOL = "";
  94.  
  95.   if (bitRead(KMAP, 4) && bitRead(KMAP, 5) && bitRead(KMAP, 6) && bitRead(KMAP, 7))  {
  96.     BOOL += "A + ";
  97.   }  
  98.   else if (bitRead(KMAP, 4) || bitRead(KMAP, 5))  {
  99.     if (bitRead(KMAP, 4) && bitRead(KMAP, 5) && !(bitRead(KMAP, 2) && bitRead(KMAP, 3)))  {
  100.       BOOL += "AB + ";
  101.     }
  102.     else if (bitRead(KMAP, 4) && !(bitRead(KMAP, 2) || bitRead(KMAP, 6)))  {
  103.       BOOL += "ABC' + ";
  104.     }
  105.     else if (!(bitRead(KMAP, 3) || bitRead(KMAP, 7)))  {
  106.       BOOL += "ABC + ";
  107.     }
  108.   }
  109.   else if (bitRead(KMAP, 6) || bitRead(KMAP, 7))  {
  110.     if (bitRead(KMAP, 6) && bitRead(KMAP, 7) && !(bitRead(KMAP, 0) && bitRead(KMAP, 1)))  {
  111.       BOOL += "AB' + ";
  112.     }
  113.     else if (bitRead(KMAP, 6) && !(bitRead(KMAP, 2) || bitRead(KMAP, 6)))  {
  114.       BOOL += "AB'C' + ";
  115.     }
  116.     else if (!(bitRead(KMAP, 3) || bitRead(KMAP, 7)))  {
  117.       BOOL += "AB'C + ";
  118.     }
  119.   }
  120.  
  121.   /*-----------------------------------------------------------------------------------------------*/
  122.  
  123.   if (bitRead(KMAP, 0) && bitRead(KMAP, 1) && bitRead(KMAP, 2) && bitRead(KMAP, 3))  {
  124.     BOOL += "A' + ";
  125.   }
  126.   else if (bitRead(KMAP, 0) || bitRead(KMAP, 1))  {
  127.     if (bitRead(KMAP, 0) && bitRead(KMAP, 1) && !(bitRead(KMAP, 6) && bitRead(KMAP, 7)))  {
  128.       BOOL += "A'B' + ";
  129.     }
  130.     else if (bitRead(KMAP, 0) && !(bitRead(KMAP, 2) || bitRead(KMAP, 6)))  {
  131.       BOOL += "A'B'C' + ";
  132.     }
  133.     else if (!(bitRead(KMAP, 3) || bitRead(KMAP, 7)))  {
  134.       BOOL += "A'B'C + ";
  135.     }
  136.   }
  137.   else if (bitRead(KMAP, 2) || bitRead(KMAP, 3))  {
  138.     if (bitRead(KMAP, 2) && bitRead(KMAP, 3) && !(bitRead(KMAP, 4) && bitRead(KMAP, 5)))  {
  139.       BOOL += "A'B + ";
  140.     }
  141.     else if (bitRead(KMAP, 2) && !bitRead(KMAP, 4) && !bitRead(KMAP, 0))  {
  142.       BOOL += "A'BC' + ";
  143.     }
  144.     else if (!(bitRead(KMAP, 5) || bitRead(KMAP, 1)))  {
  145.       BOOL += "A'BC + ";
  146.     }
  147.   }
  148.  
  149.   /*-----------------------------------------------------------------------------------------------*/
  150.  
  151.   if (bitRead(KMAP, 2) && bitRead(KMAP, 3) && bitRead(KMAP, 4) && bitRead(KMAP, 5))  {
  152.     BOOL += "B + ";
  153.   }  
  154.   else if (bitRead(KMAP, 2) || bitRead(KMAP, 3))  {
  155.     if (bitRead(KMAP, 2) && bitRead(KMAP, 3) && !(bitRead(KMAP, 0) && bitRead(KMAP, 1)))  {
  156.       BOOL += "A'B + ";
  157.     }
  158.     else if (bitRead(KMAP, 2) && !(bitRead(KMAP, 0) || bitRead(KMAP, 4)))  {
  159.       BOOL += "A'BC' + ";
  160.     }
  161.     else if (!(bitRead(KMAP, 1) || bitRead(KMAP, 5)))  {
  162.       BOOL += "A'BC + ";
  163.     }
  164.   }
  165.   else if (bitRead(KMAP, 4) || bitRead(KMAP, 5))  {
  166.     if (bitRead(KMAP, 4) && bitRead(KMAP, 5) && !(bitRead(KMAP, 6) && bitRead(KMAP, 7)))  {
  167.       BOOL += "AB + ";
  168.     }
  169.     else if (bitRead(KMAP, 4) && !(bitRead(KMAP, 2) || bitRead(KMAP, 6)))  {
  170.       BOOL += "ABC' + ";
  171.     }
  172.     else if (!(bitRead(KMAP, 3) || bitRead(KMAP, 7)))  {
  173.       BOOL += "ABC + ";
  174.     }
  175.   }
  176.  
  177.   /*-----------------------------------------------------------------------------------------------*/
  178.  
  179.   if (bitRead(KMAP, 0) && bitRead(KMAP, 1) && bitRead(KMAP, 6) && bitRead(KMAP, 7))  {
  180.     BOOL += "B' + ";
  181.   }
  182.   else if (bitRead(KMAP, 0) || bitRead(KMAP, 1))  {
  183.     if (bitRead(KMAP, 0) && bitRead(KMAP, 6) && !(bitRead(KMAP, 1) || bitRead(KMAP, 7)) && !(bitRead(KMAP, 2) && bitRead(KMAP, 4)))  {
  184.       BOOL += "B'C' + ";
  185.     }
  186.     else if (bitRead(KMAP, 1) && bitRead(KMAP, 7) && !(bitRead(KMAP, 3) && bitRead(KMAP, 5)))  {
  187.       BOOL += "B'C + ";
  188.     }
  189.   }
  190.  
  191.   /*-----------------------------------------------------------------------------------------------*/
  192.  
  193.   if (bitRead(KMAP, 1) && bitRead(KMAP, 3) && bitRead(KMAP, 5) && bitRead(KMAP, 7))  {
  194.     BOOL += "C + ";
  195.   }
  196.   else if (bitRead(KMAP, 1) || bitRead(KMAP, 3))  {
  197.     if (bitRead(KMAP, 1) && bitRead(KMAP, 3) && !(bitRead(KMAP, 0) && bitRead(KMAP, 2)))  {
  198.       BOOL += "A'C + ";
  199.     }
  200.     else if (bitRead(KMAP, 3) && bitRead(KMAP, 5) && !(bitRead(KMAP, 2) && bitRead(KMAP, 4)))  {
  201.       BOOL += "BC + ";
  202.     }
  203.   }
  204.   else if (bitRead(KMAP, 5) || bitRead(KMAP, 7))  {
  205.     if (bitRead(KMAP, 5) && bitRead(KMAP, 7) && !(bitRead(KMAP, 4) && bitRead(KMAP, 6)))  {
  206.       BOOL += "AC + ";
  207.     }
  208.     else if (bitRead(KMAP, 1) && bitRead(KMAP, 7) && !(bitRead(KMAP, 0) && bitRead(KMAP, 6)))  {
  209.       BOOL += "B'C + ";
  210.     }
  211.   }
  212.  
  213.   /*-----------------------------------------------------------------------------------------------*/
  214.  
  215.   if (bitRead(KMAP, 0) && bitRead(KMAP, 2) && bitRead(KMAP, 4) && bitRead(KMAP, 6))  {
  216.     BOOL += "C' + ";
  217.   }
  218.   else if (bitRead(KMAP, 0) || bitRead(KMAP, 2))  {
  219.     if (bitRead(KMAP, 0) && bitRead(KMAP, 2) && !(bitRead(KMAP, 1) && bitRead(KMAP, 3)))  {
  220.       BOOL += "A'C' + ";
  221.     }
  222.     else if (bitRead(KMAP, 0) && bitRead(KMAP, 6) && !(bitRead(KMAP, 1) && bitRead(KMAP, 7)))  {
  223.       BOOL += "B'C' + ";
  224.     }
  225.   }
  226.   else if (bitRead(KMAP, 4) || bitRead(KMAP, 6))  {
  227.     if (bitRead(KMAP, 4) && bitRead(KMAP, 6) && !(bitRead(KMAP, 5) && bitRead(KMAP, 7)))  {
  228.       BOOL += "AC' + ";
  229.     }
  230.     else if (bitRead(KMAP, 2) && bitRead(KMAP, 4) && !(bitRead(KMAP, 3) && bitRead(KMAP, 5)))  {
  231.       BOOL += "BC' + ";
  232.     }
  233.   }
  234.  
  235.  
  236.   /*-----------------------------------------------------------------------------------------------*/
  237.  
  238.   if (bitRead(KMAP, 0) && bitRead(KMAP, 2) && bitRead(KMAP, 4) && bitRead(KMAP, 6) && bitRead(KMAP, 1) && bitRead(KMAP, 3) && bitRead(KMAP, 5) && bitRead(KMAP, 7))  {
  239.     BOOL = "All conditions are true.";
  240.     t = 1;
  241.   }
  242.   if (!(bitRead(KMAP, 0) || bitRead(KMAP, 2) || bitRead(KMAP, 4) || bitRead(KMAP, 6) || bitRead(KMAP, 1) || bitRead(KMAP, 3) || bitRead(KMAP, 5) || bitRead(KMAP, 7)))  {
  243.     BOOL = "All conditions are false.";
  244.     f = 1;
  245.   }
  246.  
  247.   /*-----------------------------------------------------------------------------------------------*/
  248. }
  249.  
  250. /*void DISP()
  251. {
  252.   if (!t && !f)  {
  253.     BOOL[BOOL.length() - 2] = NULL;
  254.   }
  255.  
  256.   if (BOOL != Old)  {
  257.     Serial.println(BOOL);
  258.     Old = BOOL;
  259.     byte x;
  260.     byte y;
  261.     for (x = 0; x < 2; x++)
  262.     {
  263.             for (y = 0; y < 3; y++)
  264.             {
  265.                     Serial.print(KMAP[x][y]);
  266.                     Serial.print(" ");
  267.             }
  268.             Serial.print(KMAP[x][y]);
  269.             Serial.println("");
  270.     }
  271.   }
  272.  
  273. }*/
  274.  
  275. void DISPT()  {
  276.  
  277.   // Displays the truth table for the logic circuit.
  278.   //glcd.drawchar(
  279.   glcd.drawstring(0, 0, "A'B'C'");
  280.   glcd.drawstring(1, 0, "A'B'C");
  281.   glcd.drawstring(2, 0, "A'BC'");
  282.   glcd.drawstring(3, 0, "A'BC");
  283.   glcd.drawstring(4, 0, "AB'C'");
  284.   glcd.drawstring(5, 0, "AB'C");
  285.   glcd.drawstring(6, 0, "ABC'");
  286.   glcd.drawstring(7, 0, "ABC");
  287.  
  288.   byte y;
  289.  
  290.   for (y = 0; y < 8; y++)  {
  291.     glcd.drawchar(y, 8, bitRead(KMAP, y));
  292.   }
  293.   /*
  294.  
  295.   byte x;
  296.   byte y;
  297.   for (x = 0; x < 2; x++)  {
  298.     for (y = 0; y < 3; y++)  {
  299.       glcd.drawchar(y, x, KMAP[x][y]);
  300.     }
  301.     glcd.drawchar(y, x, KMAP[x][y]);
  302.   }*/
  303. }
  304.  
  305. void DISPB()  {
  306.   // Displays the boolean expression for the logic cicuit.
  307.   int L = BOOL.length() - 2;
  308.   byte l;
  309.   byte m;
  310.   for (l = 0; l < L; l++)  {
  311.     for (m = 0; m < 122; m++)  {
  312.       glcd.drawchar(m, 0, BOOL[l]);
  313.       Serial.print(BOOL[l]);
  314.       Serial.print(" ");
  315.       Serial.println(m);
  316.     }
  317.   }
  318. }
  319.  
  320. int freeRam()  {
  321.  
  322.   extern int  __bss_end;
  323.   extern int  *__brkval;
  324.   int free_memory;
  325.   if((int)__brkval == 0) {
  326.     free_memory = ((int)&free_memory) - ((int)&__bss_end);
  327.   }
  328.   else {
  329.     free_memory = ((int)&free_memory) - ((int)__brkval);
  330.   }
  331.   return free_memory;
  332. }