HELP!! mikroC code...

Discussion in 'Programmer's Corner' started by edski, Dec 12, 2010.

  1. edski

    Thread Starter Member

    Sep 22, 2010
    65
    0
    hi, anyone can help me with my C code..my project is not working. what's wrong
    with my code or my diagram. plsssssss. tnx. i really appreciate it..my code and schematic diagram are attached.
     
  2. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    What works/doesn't work in simulation? What happens with the assembled circuit?

    Here is the source code, it is better to attach it inline using the code tags:

    Code ( (Unknown Language)):
    1.  
    2.  
    3. // initialize function prototypes
    4. void initializePorts();
    5. void initializeLCD();
    6. void startGame();
    7. int checkForHit();
    8. void updateScore(int playerFlag, int score);
    9. void showWinner();
    10. int showMenu();
    11.  
    12. // function main
    13. void main() {
    14.  
    15.   // initialize components
    16.     initializePorts();
    17.     initializeLCD();
    18.  
    19.   // start program proper
    20.   do {
    21.       startGame();
    22.    } while( showMenu() );
    23. } // end main
    24.  
    25. // start game proper
    26. void startGame() {
    27.   int player1Score = 0;
    28.     int player2Score = 0;
    29.     int playerFlag = 1;        // kun hino an natira
    30.     /*
    31.         1 -> player 1
    32.         2 -> player 2
    33.     */
    34.  
    35.     int yellowCtr = 0, greenCtr = 0, blueCtr = 0, redCtr = 0;
    36.     int hitFlag;
    37.     /*
    38.         1 -> yellow
    39.         2 -> green
    40.         3 -> blue
    41.         4 -> red
    42.         5 -> switch player
    43.     */
    44.  
    45.   LCD_Cmd(LCD_CLEAR);
    46.  
    47.     do {
    48.         yellowCtr = 0;
    49.         greenCtr = 0;
    50.         blueCtr = 0;
    51.         redCtr = 0;
    52.  
    53.         hitFlag = checkForHit(); // kitaon kun ano nga color an naigo
    54.  
    55.         switch( hitFlag ) {
    56.             case 1:                    // yellow
    57.                 yellowCtr++;
    58.                 break;
    59.             case 2:                    // green
    60.                 greenCtr++;
    61.                 break;
    62.             case 3:                    // blue
    63.                 blueCtr++;
    64.                 break;
    65.             case 4:                    // red
    66.                 redCtr++;
    67.                 break;
    68.             case 5:
    69.         playerFlag == 1? 2: 1;  // switch player
    70.                 continue;                      // balik ha start han do while loop
    71.         }
    72.  
    73.         if( yellowCtr >= 2 || greenCtr >= 2 || blueCtr >= 2 || redCtr >= 2 ) {
    74.             switch( playerFlag ) {
    75.                 case 1:
    76.                     player1Score += 10;
    77.                     break;
    78.                 case 2:
    79.                     player2Score += 10;
    80.                     break;
    81.             }
    82.  
    83.             updateScore( playerFlag, playerFlag == 1? player1Score: player2Score ); // update display ha LCD
    84.  
    85.         }
    86.  
    87.     } while( player1Score == 50 || player2Score == 50 ); // check for winner
    88.  
    89.     showWinner();
    90. }
    91.  
    92. void showWinner() {
    93.  
    94. }
    95.  
    96. int showMenu() {
    97.      int hit = 0;
    98.  
    99.      LCD_Cmd(LCD_CLEAR);
    100.      LCD_Out(1,1, " Press Any Key");
    101.      LCD_Out(2,1, " To Start Game");
    102.      
    103.      hit = checkForHit();
    104.      
    105.      if( hit ) {
    106.          LCD_Cmd(LCD_CLEAR);
    107.          return 1;
    108.      }
    109.      
    110.      return 0;
    111. }
    112.  
    113. void updateScore( int playerFlag, int score ) {
    114.     char *line1Text = "";
    115.     char *line2Text = "";
    116.     char *points = "";            // string value han score
    117.     char *append = " points";    // igdudugtong ha line2Text
    118.  
    119.     IntToStr( score, points );
    120.     strcpy( line2Text, strcat( points, append ) );
    121.    
    122.     LCD_Cmd(LCD_CLEAR);
    123.  
    124.     switch( playerFlag ) {
    125.         case 1:
    126.             strcpy(line1Text, "Player 1 Score");
    127.             break;
    128.         case 2:
    129.             strcpy(line1Text, "Player 2 Score");
    130.             break;
    131.     }
    132.  
    133.     LCD_Out(1,1, line1Text);
    134.     LCD_Out(2,4, line2Text);
    135. }
    136.  
    137. int checkForHit() {
    138.     int hit = 0;
    139.     int oldState = 1;
    140.  
    141.     do {
    142.         // for red
    143.         if ( Button(&PORTD, 0, 1, 0) )                // detect logical one on RD0 pin
    144.             oldState = 0;
    145.         if ( oldState && Button(&PORTD, 0, 1, 1) ) {  // detect one-to-zero transition on RD0 pin
    146.             hit = 4;
    147.             oldState = 1;
    148.         }
    149.  
    150.         //for green
    151.         if ( Button(&PORTD, 1, 1, 0) )                // detect logical one on RD1 pin
    152.             oldState = 0;
    153.         if ( oldState && Button(&PORTD, 1, 1, 1) ) {  // detect one-to-zero transition on RD1 pin
    154.             hit = 2;
    155.             oldState = 1;
    156.         }
    157.  
    158.         //for blue
    159.         if ( Button(&PORTD, 2, 1, 0) )                // detect logical one on RD2 pin
    160.             oldState = 0;
    161.         if ( oldState && Button(&PORTD, 2, 1, 1) ) {  // detect one-to-zero transition on RD2 pin
    162.             hit = 3;
    163.             oldState = 1;
    164.         }
    165.  
    166.         //for yellow
    167.         if ( Button(&PORTD, 3, 1, 0) )                // detect logical one on RD3 pin
    168.             oldState = 0;
    169.         if ( oldState && Button(&PORTD, 3, 1, 1) ) {  // detect one-to-zero transition on RD3 pin
    170.             hit = 1;
    171.             oldState = 1;
    172.         }
    173.  
    174.         //for Switch player button
    175.         if ( Button(&PORTC, 1, 1, 0) )                // detect logical one on RC1 pin
    176.             oldState = 0;
    177.         if ( oldState && Button(&PORTC, 1, 1, 1) ) {  // detect one-to-zero transition on RC1 pin
    178.             hit = 5;
    179.             oldState = 1;
    180.         }
    181.  
    182.     } while( !hit ); // ma-exit la kun non-zero it hit
    183.  
    184.     return hit; // return kun ano an napidlit
    185. }
    186.  
    187. void initializePorts() {
    188.     TRISB = 0;            // output
    189.                    // initialize PORTB
    190.  
    191.     TRISD = 1;            // input
    192.     PORTD = 0xFF;        // initialize PORTD
    193.  
    194.     TRISC = 1;            // input
    195.     PORTC = 0xFF;        // initialize PORTC
    196. }
    197.  
    198. void initializeLCD() {
    199.     Lcd_Init(&PORTB);             // Lcd_Init_EP4, see Autocomplete
    200.  
    201.     LCD_Cmd(LCD_CLEAR);           // Clear display
    202.     LCD_Cmd(LCD_CURSOR_OFF);      // Turn cursor off
    203.   Lcd_Out(1, 4, "Let's Play");
    204.   Delay_ms(1000);
    205.   LCD_Cmd(LCD_CLEAR);
    206. }
    207.  
     
  3. edski

    Thread Starter Member

    Sep 22, 2010
    65
    0
    there is no display in lcd....
     
  4. thatoneguy

    AAC Fanatic!

    Feb 19, 2009
    6,357
    718
    Does it display when you are stepping through the simulation in the IDE?
     
  5. retched

    AAC Fanatic!

    Dec 5, 2009
    5,201
    312
    I dont see a power source.
     
  6. t06afre

    AAC Fanatic!

    May 11, 2009
    5,939
    1,222
    I do not know MicroC. But how do you set the configuration word?
     
  7. edski

    Thread Starter Member

    Sep 22, 2010
    65
    0
    there is no display.. when any of the 4 switches will be pressed twice, there should be a display of 10 points...@retched, you can simulate in proteus even w/o a source...
     
  8. retched

    AAC Fanatic!

    Dec 5, 2009
    5,201
    312
    ok.

    How does the simulation simulate the current with no defined voltage?

    I also dont see you setting ports as inputs or outputs or which osc to use etc...
     
Loading...