HELP!! mikroC code...

Thread Starter

edski

Joined Sep 22, 2010
65
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.
 

Attachments

thatoneguy

Joined Feb 19, 2009
6,359
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:

Rich (BB code):
// initialize function prototypes
void initializePorts();
void initializeLCD();
void startGame();
int checkForHit();
void updateScore(int playerFlag, int score);
void showWinner();
int showMenu();

// function main
void main() {

  // initialize components
    initializePorts();
    initializeLCD();

  // start program proper
  do {
      startGame();
   } while( showMenu() );
} // end main

// start game proper
void startGame() {
  int player1Score = 0;
    int player2Score = 0;
    int playerFlag = 1;        // kun hino an natira
    /*
        1 -> player 1
        2 -> player 2
    */

    int yellowCtr = 0, greenCtr = 0, blueCtr = 0, redCtr = 0;
    int hitFlag;
    /*
        1 -> yellow
        2 -> green
        3 -> blue
        4 -> red
        5 -> switch player
    */

  LCD_Cmd(LCD_CLEAR);

    do {
        yellowCtr = 0;
        greenCtr = 0;
        blueCtr = 0;
        redCtr = 0;

        hitFlag = checkForHit(); // kitaon kun ano nga color an naigo

        switch( hitFlag ) {
            case 1:                    // yellow
                yellowCtr++;
                break;
            case 2:                    // green
                greenCtr++;
                break;
            case 3:                    // blue
                blueCtr++;
                break;
            case 4:                    // red
                redCtr++;
                break;
            case 5:
        playerFlag == 1? 2: 1;  // switch player
                continue;                      // balik ha start han do while loop
        }

        if( yellowCtr >= 2 || greenCtr >= 2 || blueCtr >= 2 || redCtr >= 2 ) {
            switch( playerFlag ) {
                case 1:
                    player1Score += 10;
                    break;
                case 2:
                    player2Score += 10;
                    break;
            }

            updateScore( playerFlag, playerFlag == 1? player1Score: player2Score ); // update display ha LCD

        }

    } while( player1Score == 50 || player2Score == 50 ); // check for winner

    showWinner();
}

void showWinner() {

}

int showMenu() {
     int hit = 0;

     LCD_Cmd(LCD_CLEAR);
     LCD_Out(1,1, " Press Any Key");
     LCD_Out(2,1, " To Start Game");
     
     hit = checkForHit();
     
     if( hit ) {
         LCD_Cmd(LCD_CLEAR);
         return 1;
     }
     
     return 0;
}

void updateScore( int playerFlag, int score ) {
    char *line1Text = "";
    char *line2Text = "";
    char *points = "";            // string value han score
    char *append = " points";    // igdudugtong ha line2Text

    IntToStr( score, points );
    strcpy( line2Text, strcat( points, append ) );
    
    LCD_Cmd(LCD_CLEAR);

    switch( playerFlag ) {
        case 1:
            strcpy(line1Text, "Player 1 Score");
            break;
        case 2:
            strcpy(line1Text, "Player 2 Score");
            break;
    }

    LCD_Out(1,1, line1Text);
    LCD_Out(2,4, line2Text);
}

int checkForHit() {
    int hit = 0;
    int oldState = 1;

    do {
        // for red
        if ( Button(&PORTD, 0, 1, 0) )                // detect logical one on RD0 pin
            oldState = 0;
        if ( oldState && Button(&PORTD, 0, 1, 1) ) {  // detect one-to-zero transition on RD0 pin
            hit = 4;
            oldState = 1;
        }

        //for green
        if ( Button(&PORTD, 1, 1, 0) )                // detect logical one on RD1 pin
            oldState = 0;
        if ( oldState && Button(&PORTD, 1, 1, 1) ) {  // detect one-to-zero transition on RD1 pin
            hit = 2;
            oldState = 1;
        }

        //for blue
        if ( Button(&PORTD, 2, 1, 0) )                // detect logical one on RD2 pin
            oldState = 0;
        if ( oldState && Button(&PORTD, 2, 1, 1) ) {  // detect one-to-zero transition on RD2 pin
            hit = 3;
            oldState = 1;
        }

        //for yellow
        if ( Button(&PORTD, 3, 1, 0) )                // detect logical one on RD3 pin
            oldState = 0;
        if ( oldState && Button(&PORTD, 3, 1, 1) ) {  // detect one-to-zero transition on RD3 pin
            hit = 1;
            oldState = 1;
        }

        //for Switch player button
        if ( Button(&PORTC, 1, 1, 0) )                // detect logical one on RC1 pin
            oldState = 0;
        if ( oldState && Button(&PORTC, 1, 1, 1) ) {  // detect one-to-zero transition on RC1 pin
            hit = 5;
            oldState = 1;
        }

    } while( !hit ); // ma-exit la kun non-zero it hit

    return hit; // return kun ano an napidlit
}

void initializePorts() {
    TRISB = 0;            // output
                   // initialize PORTB

    TRISD = 1;            // input
    PORTD = 0xFF;        // initialize PORTD

    TRISC = 1;            // input
    PORTC = 0xFF;        // initialize PORTC
}

void initializeLCD() {
    Lcd_Init(&PORTB);             // Lcd_Init_EP4, see Autocomplete

    LCD_Cmd(LCD_CLEAR);           // Clear display
    LCD_Cmd(LCD_CURSOR_OFF);      // Turn cursor off
  Lcd_Out(1, 4, "Let's Play");
  Delay_ms(1000);
  LCD_Cmd(LCD_CLEAR);
}
 

Thread Starter

edski

Joined Sep 22, 2010
65
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...
 

retched

Joined Dec 5, 2009
5,207
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...
 
Top