Advertisement
Guest User

Untitled

a guest
Jan 20th, 2016
175
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.65 KB | None | 0 0
  1. #include <htc.h>        //PIC hardware mapping
  2. #include <delay.c>
  3. #include <I2CMaster.h>
  4. #include <stdlib.h>
  5. //config bits
  6.  
  7.     __CONFIG(HS & PWRTDIS & WDTDIS & LVPDIS);
  8.  
  9. /////////// Constants ///////////
  10. #define LEDPLADE8PIN 1                  // Defines how many light plates with 8pins that's attached (Not ever actually used, was supposed to be able to handle more than one)
  11.  
  12. /////////// Bit manipulation functions ///////////
  13.  
  14. void clear_bit(unsigned char *x, char bitNum){
  15.     *x &= ~(1 << bitNum);
  16. }
  17. void set_bit(unsigned char *x, char bitNum) {
  18.     *x |= (1 << bitNum);
  19. }
  20. unsigned char get_bit(unsigned char x, char bitNum) {
  21.     return x & (1 << bitNum);
  22. }
  23.  
  24. /////////////// Function prototypes for patterns //////////////////
  25.  
  26. void glider(void);
  27. void lwss(void);
  28. void toad(void);
  29. void kasse(void);
  30. void blinker(void);
  31. void randompat(void);
  32.  
  33. /////////// Variables ///////////
  34.  
  35. ////////// GAME OF LIFE ///////////
  36.  
  37.     char LifeGameBuf[2][7];
  38.     char LiveCount;
  39.     char FirstEvol;
  40.     char Pattern;
  41.     char NewPattern;
  42.  
  43. ///////////////////////////////////
  44.  
  45.     char i;                             // Indexes
  46.     char j;
  47.    
  48.     char I2C_Address;                   // Defines the i2c address we want to write to
  49.     char I2C_i;                         // Defines what type of byte we want to send (I don't remember what this is, and it is apparently unused)
  50.  
  51. /////////// Main program ///////////
  52. void main(void){                        // Start
  53.  
  54.     srand(70);
  55.  
  56. /////////// Opsætning ///////////
  57.  
  58. /////////// Opsætning af SSP modul ///////////
  59.  
  60.     TRISC = 0b00011000;                 // Sets port C pin 3 and 4 to be inputs
  61.     SSPADD = 39;                        // Defines the baudrate of the i2c communications
  62.     SSPCON = 0b00101000;                // Configuration and start of SSP module
  63.     SSPCON2 = 0b00000000;
  64.     SSPSTAT = 0b11000000;
  65.     SSPIE = 1;                          // Enable interrupt on i2c communication start
  66.     SSPIF = 0;
  67.  
  68. /////////// Setup of interrupts ///////////
  69.  
  70.     PEIE = 1;
  71.     GIE = 1;                            // Global interrupt enable
  72.  
  73. /////////// GAME OF LIFE ///////////
  74.    
  75.     LiveCount = 0;
  76.     Pattern = 0;
  77.     NewPattern = 0;
  78.    
  79.     glider();
  80.  
  81. /////////// Runs in a loop from here (Obviously not) ///////////
  82.  
  83. /////////// Pattern change ///////////
  84.     TRISD = 0b00000010;             // Set pins on port D as input and output
  85.     while(!0){
  86.     PORTD = 0b00001000;             // Drive current to RD3 pin
  87.     if(RD3 == 0){                   // I HAVE NO IDEA WHY THIS WORKS (Basically, the code here sets a pin, then checks if it is set. It worked, I still have no idea why)
  88.     Pattern = Pattern + 1;
  89.     NewPattern = 1;
  90.     DelayMs(255);
  91.     }
  92.     if(NewPattern){
  93.         if(Pattern > 5) Pattern = 0;
  94.         if(Pattern == 0) glider();
  95.         else if(Pattern == 1) lwss();
  96.         else if(Pattern == 2) toad();
  97.         else if(Pattern == 3) kasse();
  98.         else if(Pattern == 4) blinker();
  99.         else if(Pattern == 5) randompat();
  100.         NewPattern = 0;
  101.     }
  102.  
  103. //////////////// Blink light to indicate when the next generations of game of life is calculated /////////////////
  104.     PORTD = 0;
  105.     DelayMs(50);
  106.     PORTD = 1;
  107.  
  108. //////////////// Game of Life /////////////////
  109.         if(!FirstEvol){
  110.             for(i=0; i<7; i++){
  111.                 for(j=0; j<8; j++){
  112.                     if(i == 0){
  113.                         if(j == 0){
  114.                             if(get_bit(LifeGameBuf[0][i],j+1)) LiveCount = LiveCount + 1;
  115.                             if(get_bit(LifeGameBuf[0][i],7)) LiveCount = LiveCount + 1;
  116.                             if(get_bit(LifeGameBuf[0][6],j+1)) LiveCount = LiveCount + 1;
  117.                             if(get_bit(LifeGameBuf[0][6],j)) LiveCount = LiveCount + 1;
  118.                             if(get_bit(LifeGameBuf[0][6],7)) LiveCount = LiveCount + 1;
  119.                             if(get_bit(LifeGameBuf[0][i+1],j+1)) LiveCount = LiveCount + 1;
  120.                             if(get_bit(LifeGameBuf[0][i+1],j)) LiveCount = LiveCount + 1;
  121.                             if(get_bit(LifeGameBuf[0][i+1],7)) LiveCount = LiveCount + 1;
  122.                         }
  123.                         else if (j == 7){
  124.                             if(get_bit(LifeGameBuf[0][i],0)) LiveCount = LiveCount + 1;
  125.                             if(get_bit(LifeGameBuf[0][i],j-1)) LiveCount = LiveCount + 1;
  126.                             if(get_bit(LifeGameBuf[0][6],0)) LiveCount = LiveCount + 1;
  127.                             if(get_bit(LifeGameBuf[0][6],j)) LiveCount = LiveCount + 1;
  128.                             if(get_bit(LifeGameBuf[0][6],j-1)) LiveCount = LiveCount + 1;
  129.                             if(get_bit(LifeGameBuf[0][i+1],0)) LiveCount = LiveCount + 1;
  130.                             if(get_bit(LifeGameBuf[0][i+1],j)) LiveCount = LiveCount + 1;
  131.                             if(get_bit(LifeGameBuf[0][i+1],j-1)) LiveCount = LiveCount + 1;
  132.                         }
  133.                         else{
  134.                             if(get_bit(LifeGameBuf[0][i],j+1)) LiveCount = LiveCount + 1;
  135.                             if(get_bit(LifeGameBuf[0][i],j-1)) LiveCount = LiveCount + 1;
  136.                             if(get_bit(LifeGameBuf[0][6],j+1)) LiveCount = LiveCount + 1;
  137.                             if(get_bit(LifeGameBuf[0][6],j)) LiveCount = LiveCount + 1;
  138.                             if(get_bit(LifeGameBuf[0][6],j-1)) LiveCount = LiveCount + 1;
  139.                             if(get_bit(LifeGameBuf[0][i+1],j+1)) LiveCount = LiveCount + 1;
  140.                             if(get_bit(LifeGameBuf[0][i+1],j)) LiveCount = LiveCount + 1;
  141.                             if(get_bit(LifeGameBuf[0][i+1],j-1)) LiveCount = LiveCount + 1;
  142.                         }
  143.                     }
  144.                     else if(i == 6){
  145.                         if(j == 0){
  146.                             if(get_bit(LifeGameBuf[0][i],j+1)) LiveCount = LiveCount + 1;
  147.                             if(get_bit(LifeGameBuf[0][i],7)) LiveCount = LiveCount + 1;
  148.                             if(get_bit(LifeGameBuf[0][i-1],j+1)) LiveCount = LiveCount + 1;
  149.                             if(get_bit(LifeGameBuf[0][i-1],j)) LiveCount = LiveCount + 1;
  150.                             if(get_bit(LifeGameBuf[0][i-1],7)) LiveCount = LiveCount + 1;
  151.                             if(get_bit(LifeGameBuf[0][0],j+1)) LiveCount = LiveCount + 1;
  152.                             if(get_bit(LifeGameBuf[0][0],j)) LiveCount = LiveCount + 1;
  153.                             if(get_bit(LifeGameBuf[0][0],7)) LiveCount = LiveCount + 1;
  154.                         }
  155.                         else if (j == 7){
  156.                             if(get_bit(LifeGameBuf[0][i],0)) LiveCount = LiveCount + 1;
  157.                             if(get_bit(LifeGameBuf[0][i],j-1)) LiveCount = LiveCount + 1;
  158.                             if(get_bit(LifeGameBuf[0][i-1],0)) LiveCount = LiveCount + 1;
  159.                             if(get_bit(LifeGameBuf[0][i-1],j)) LiveCount = LiveCount + 1;
  160.                             if(get_bit(LifeGameBuf[0][i-1],j-1)) LiveCount = LiveCount + 1;
  161.                             if(get_bit(LifeGameBuf[0][0],0)) LiveCount = LiveCount + 1;
  162.                             if(get_bit(LifeGameBuf[0][0],j)) LiveCount = LiveCount + 1;
  163.                             if(get_bit(LifeGameBuf[0][0],j-1)) LiveCount = LiveCount + 1;
  164.                         }
  165.                         else{
  166.                             if(get_bit(LifeGameBuf[0][i],j+1)) LiveCount = LiveCount + 1;
  167.                             if(get_bit(LifeGameBuf[0][i],j-1)) LiveCount = LiveCount + 1;
  168.                             if(get_bit(LifeGameBuf[0][i-1],j+1)) LiveCount = LiveCount + 1;
  169.                             if(get_bit(LifeGameBuf[0][i-1],j)) LiveCount = LiveCount + 1;
  170.                             if(get_bit(LifeGameBuf[0][i-1],j-1)) LiveCount = LiveCount + 1;
  171.                             if(get_bit(LifeGameBuf[0][0],j+1)) LiveCount = LiveCount + 1;
  172.                             if(get_bit(LifeGameBuf[0][0],j)) LiveCount = LiveCount + 1;
  173.                             if(get_bit(LifeGameBuf[0][0],j-1)) LiveCount = LiveCount + 1;
  174.                         }
  175.                     }
  176.                     else{
  177.                         if(j == 0){
  178.                             if(get_bit(LifeGameBuf[0][i],j+1)) LiveCount = LiveCount + 1;
  179.                             if(get_bit(LifeGameBuf[0][i],7)) LiveCount = LiveCount + 1;
  180.                             if(get_bit(LifeGameBuf[0][i-1],j+1)) LiveCount = LiveCount + 1;
  181.                             if(get_bit(LifeGameBuf[0][i-1],j)) LiveCount = LiveCount + 1;
  182.                             if(get_bit(LifeGameBuf[0][i-1],7)) LiveCount = LiveCount + 1;
  183.                             if(get_bit(LifeGameBuf[0][i+1],j+1)) LiveCount = LiveCount + 1;
  184.                             if(get_bit(LifeGameBuf[0][i+1],j)) LiveCount = LiveCount + 1;
  185.                             if(get_bit(LifeGameBuf[0][i+1],7)) LiveCount = LiveCount + 1;
  186.                         }
  187.                         else if (j == 7){
  188.                             if(get_bit(LifeGameBuf[0][i],0)) LiveCount = LiveCount + 1;
  189.                             if(get_bit(LifeGameBuf[0][i],j-1)) LiveCount = LiveCount + 1;
  190.                             if(get_bit(LifeGameBuf[0][i-1],0)) LiveCount = LiveCount + 1;
  191.                             if(get_bit(LifeGameBuf[0][i-1],j)) LiveCount = LiveCount + 1;
  192.                             if(get_bit(LifeGameBuf[0][i-1],j-1)) LiveCount = LiveCount + 1;
  193.                             if(get_bit(LifeGameBuf[0][i+1],0)) LiveCount = LiveCount + 1;
  194.                             if(get_bit(LifeGameBuf[0][i+1],j)) LiveCount = LiveCount + 1;
  195.                             if(get_bit(LifeGameBuf[0][i+1],j-1)) LiveCount = LiveCount + 1;
  196.                         }
  197.                         else{
  198.                             if(get_bit(LifeGameBuf[0][i],j+1)) LiveCount = LiveCount + 1;
  199.                             if(get_bit(LifeGameBuf[0][i],j-1)) LiveCount = LiveCount + 1;
  200.                             if(get_bit(LifeGameBuf[0][i-1],j+1)) LiveCount = LiveCount + 1;
  201.                             if(get_bit(LifeGameBuf[0][i-1],j)) LiveCount = LiveCount + 1;
  202.                             if(get_bit(LifeGameBuf[0][i-1],j-1)) LiveCount = LiveCount + 1;
  203.                             if(get_bit(LifeGameBuf[0][i+1],j+1)) LiveCount = LiveCount + 1;
  204.                             if(get_bit(LifeGameBuf[0][i+1],j)) LiveCount = LiveCount + 1;
  205.                             if(get_bit(LifeGameBuf[0][i+1],j-1)) LiveCount = LiveCount + 1;
  206.                         }
  207.                     }
  208.                    
  209.                 if(LiveCount < 2){clear_bit(&LifeGameBuf[1][i],j);}
  210.                 else if(LiveCount == 2 && get_bit(LifeGameBuf[1][i],j) == 1){set_bit(&LifeGameBuf[1][i],j);}
  211.                 else if(LiveCount == 2 && get_bit(LifeGameBuf[1][i],j) == 0){clear_bit(&LifeGameBuf[1][i],j);}
  212.                 else if(LiveCount == 3){set_bit(&LifeGameBuf[1][i],j);}
  213.                 else if(LiveCount > 3){clear_bit(&LifeGameBuf[1][i],j);}
  214.                 LiveCount = 0;
  215.                
  216.                 }
  217.             }
  218.             for(i=0; i<7; i++){
  219.             LifeGameBuf[0][i] = LifeGameBuf[1][i];
  220.             }
  221.         }
  222.         FirstEvol = 0;              // No longer first generation
  223.  
  224. ////////////// SEND TO DISPLAY /////////////////
  225.  
  226.     i2c_Start();
  227.     i2c_Address(15,0);
  228.     i2c_Write(LifeGameBuf[0][0]);
  229.     i2c_Write(LifeGameBuf[0][1]);
  230.     i2c_Write(LifeGameBuf[0][2]);
  231.     i2c_Write(LifeGameBuf[0][3]);
  232.     i2c_Write(LifeGameBuf[0][4]);
  233.     i2c_Write(LifeGameBuf[0][5]);
  234.     i2c_Write(LifeGameBuf[0][6]);
  235.     i2c_Stop();
  236.     }
  237. }                                                   // End
  238.  
  239. static void interrupt isr(void)
  240. {
  241.  
  242.     if(SSPIF){                                      // Was it a i2c interrupt?
  243.     SSPIF = 0;                                      // Clear SSP interrupt flag
  244.     }
  245. }
  246.  
  247. /////////// Game of Life pattern funftions ///////////
  248.  
  249. void glider() {
  250.  
  251.     LifeGameBuf[0][0] = 0b00100000;
  252.     LifeGameBuf[0][1] = 0b10100000;
  253.     LifeGameBuf[0][2] = 0b01100000;
  254.     LifeGameBuf[0][3] = 0b00000000;
  255.     LifeGameBuf[0][4] = 0b00000000;
  256.     LifeGameBuf[0][5] = 0b00000000;
  257.     LifeGameBuf[0][6] = 0b00000000;
  258.  
  259.     LifeGameBuf[1][0] = LifeGameBuf[0][0];
  260.     LifeGameBuf[1][1] = LifeGameBuf[0][1];
  261.     LifeGameBuf[1][2] = LifeGameBuf[0][2];
  262.     LifeGameBuf[1][3] = LifeGameBuf[0][3];
  263.     LifeGameBuf[1][4] = LifeGameBuf[0][4];
  264.     LifeGameBuf[1][5] = LifeGameBuf[0][5];
  265.     LifeGameBuf[1][6] = LifeGameBuf[0][6];
  266.  
  267.     FirstEvol = 1;
  268. }
  269.  
  270.  
  271. void lwss() {
  272.  
  273.     LifeGameBuf[0][0] = 0b00000000;
  274.     LifeGameBuf[0][1] = 0b00000000;
  275.     LifeGameBuf[0][2] = 0b00110000;
  276.     LifeGameBuf[0][3] = 0b11011000;
  277.     LifeGameBuf[0][4] = 0b11110000;
  278.     LifeGameBuf[0][5] = 0b01100000;
  279.     LifeGameBuf[0][6] = 0b00000000;
  280.  
  281.     LifeGameBuf[1][0] = LifeGameBuf[0][0];
  282.     LifeGameBuf[1][1] = LifeGameBuf[0][1];
  283.     LifeGameBuf[1][2] = LifeGameBuf[0][2];
  284.     LifeGameBuf[1][3] = LifeGameBuf[0][3];
  285.     LifeGameBuf[1][4] = LifeGameBuf[0][4];
  286.     LifeGameBuf[1][5] = LifeGameBuf[0][5];
  287.     LifeGameBuf[1][6] = LifeGameBuf[0][6];
  288.  
  289.     FirstEvol = 1;
  290. }
  291.  
  292. void toad() {
  293.  
  294.     LifeGameBuf[0][0] = 0b00000000;
  295.     LifeGameBuf[0][1] = 0b00000000;
  296.     LifeGameBuf[0][2] = 0b00000000;
  297.     LifeGameBuf[0][3] = 0b00111000;
  298.     LifeGameBuf[0][4] = 0b00011100;
  299.     LifeGameBuf[0][5] = 0b00000000;
  300.     LifeGameBuf[0][6] = 0b00000000;
  301.  
  302.     LifeGameBuf[1][0] = LifeGameBuf[0][0];
  303.     LifeGameBuf[1][1] = LifeGameBuf[0][1];
  304.     LifeGameBuf[1][2] = LifeGameBuf[0][2];
  305.     LifeGameBuf[1][3] = LifeGameBuf[0][3];
  306.     LifeGameBuf[1][4] = LifeGameBuf[0][4];
  307.     LifeGameBuf[1][5] = LifeGameBuf[0][5];
  308.     LifeGameBuf[1][6] = LifeGameBuf[0][6];
  309.  
  310.     FirstEvol = 1;
  311. }
  312.  
  313. void kasse() {
  314.  
  315.     LifeGameBuf[0][0] = 0b00000000;
  316.     LifeGameBuf[0][1] = 0b00110000;
  317.     LifeGameBuf[0][2] = 0b01001000;
  318.     LifeGameBuf[0][3] = 0b01001000;
  319.     LifeGameBuf[0][4] = 0b00110000;
  320.     LifeGameBuf[0][5] = 0b00000000;
  321.     LifeGameBuf[0][6] = 0b00000000;
  322.  
  323.     LifeGameBuf[1][0] = LifeGameBuf[0][0];
  324.     LifeGameBuf[1][1] = LifeGameBuf[0][1];
  325.     LifeGameBuf[1][2] = LifeGameBuf[0][2];
  326.     LifeGameBuf[1][3] = LifeGameBuf[0][3];
  327.     LifeGameBuf[1][4] = LifeGameBuf[0][4];
  328.     LifeGameBuf[1][5] = LifeGameBuf[0][5];
  329.     LifeGameBuf[1][6] = LifeGameBuf[0][6];
  330.  
  331.     FirstEvol = 1;
  332. }
  333.  
  334. void blinker() {
  335.  
  336.     LifeGameBuf[0][0] = 0b01000000;
  337.     LifeGameBuf[0][1] = 0b01000000;
  338.     LifeGameBuf[0][2] = 0b01000000;
  339.     LifeGameBuf[0][3] = 0b00001000;
  340.     LifeGameBuf[0][4] = 0b00001000;
  341.     LifeGameBuf[0][5] = 0b00001000;
  342.     LifeGameBuf[0][6] = 0b00000000;
  343.  
  344.     LifeGameBuf[1][0] = LifeGameBuf[0][0];
  345.     LifeGameBuf[1][1] = LifeGameBuf[0][1];
  346.     LifeGameBuf[1][2] = LifeGameBuf[0][2];
  347.     LifeGameBuf[1][3] = LifeGameBuf[0][3];
  348.     LifeGameBuf[1][4] = LifeGameBuf[0][4];
  349.     LifeGameBuf[1][5] = LifeGameBuf[0][5];
  350.     LifeGameBuf[1][6] = LifeGameBuf[0][6];
  351.  
  352.     FirstEvol = 1;
  353. }
  354.  
  355. void randompat() {
  356.  
  357.     LifeGameBuf[0][0] = (char)rand();
  358.     LifeGameBuf[0][1] = (char)rand();
  359.     LifeGameBuf[0][2] = (char)rand();
  360.     LifeGameBuf[0][3] = (char)rand();
  361.     LifeGameBuf[0][4] = (char)rand();
  362.     LifeGameBuf[0][5] = (char)rand();
  363.     LifeGameBuf[0][6] = (char)rand();
  364.  
  365.     LifeGameBuf[1][0] = LifeGameBuf[0][0];
  366.     LifeGameBuf[1][1] = LifeGameBuf[0][1];
  367.     LifeGameBuf[1][2] = LifeGameBuf[0][2];
  368.     LifeGameBuf[1][3] = LifeGameBuf[0][3];
  369.     LifeGameBuf[1][4] = LifeGameBuf[0][4];
  370.     LifeGameBuf[1][5] = LifeGameBuf[0][5];
  371.     LifeGameBuf[1][6] = LifeGameBuf[0][6];
  372.  
  373.     FirstEvol = 1;
  374. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement