Gretz13
Gretz13

Reputation: 1

MPLAB X IDE Output Keyboard Press

I am trying to write a program that makes a PIC24FJ64GA002 accept button inputs from 3 players for telltale games, and depending on their input, make the computer press the key 1, 2, 3, or 4. I am not having trouble with button inputs, but I can't figure out how to get the computer to press a key.

First, I tried downloading windows.h off github and putting it in MPLAB's include folder. MPLAB was not able to find most of the files and how they connected to eachother because they were in many different folders. I tried using Visual Studio Code with the windows.h include statement, along with some code from the internet that pressed a key on the computer, and it worked. So, I tried copying all of the include files used by VS Code into the include folder of MPLAB. I no longer had the issue of finding files, but now I am getting this error:

c:\program files\microchip\xc16\v2.00\bin\bin../..\include/_mingw.h:271:2: error: #error Only Win32 target is supported!

Followed by a bunch of errors similar to this:

c:\program files\microchip\xc16\v2.00\bin\bin../..\include/_mingw.h:580:14: error: expected '=', ',', ';', 'asm' or 'attribute' before '__debugbreak' c:\program files\microchip\xc16\v2.00\bin\bin../..\include/_mingw.h:601:38: error: expected ',' or ';' before '__fastfail'

Code:

#include <p24FJ64GA002.h>
#include "xc.h"
#include <stdlib.h>
#include <windows.h>

// CW1: FLASH CONFIGURATION WORD 1 (see PIC24 Family Reference Manual 24.1)
#pragma config ICS = PGx1          // Comm Channel Select (Emulator EMUC1/EMUD1 pins are shared with PGC1/PGD1)
#pragma config FWDTEN = OFF        // Watchdog Timer Enable (Watchdog Timer is disabled)
#pragma config GWRP = OFF          // General Code Segment Write Protect (Writes to program memory are allowed)
#pragma config GCP = OFF           // General Code Segment Code Protect (Code protection is disabled)
#pragma config JTAGEN = OFF        // JTAG Port Enable (JTAG port is disabled)

// CW2: FLASH CONFIGURATION WORD 2 (see PIC24 Family Reference Manual 24.1)
#pragma config I2C1SEL = PRI       // I2C1 Pin Location Select (Use default SCL1/SDA1 pins)
#pragma config IOL1WAY = OFF       // IOLOCK Protection (IOLOCK may be changed via unlocking seq)
#pragma config OSCIOFNC = ON       // Primary Oscillator I/O Function (CLKO/RC15 functions as I/O pin)
#pragma config FCKSM = CSECME      // Clock Switching and Monitor (Clock switching is enabled, 
                                       // Fail-Safe Clock Monitor is enabled)
#pragma config FNOSC = FRCPLL      // Oscillator Select (Fast RC Oscillator with PLL module (FRCPLL))


void setup(void) {
    _RCDIV = 0;
    AD1PCFG = 0xffff;
    
    // Player 1 controls
    TRISAbits.TRISA0 = 1; // Pin 2 A
    TRISAbits.TRISA1 = 1; // Pin 3 X
    TRISBbits.TRISB2 = 1; // Pin 6 Y
    TRISBbits.TRISB3 = 1; // Pin 7 B
    
    // Player 2 controls
    TRISAbits.TRISA2 = 1; // Pin 9 A
    TRISAbits.TRISA3 = 1; // Pin 10 X
    TRISBbits.TRISB4 = 1; // Pin 11 Y
    TRISAbits.TRISA4 = 1; // Pin 12 B
    
    // Player 3 controls
    TRISBbits.TRISB5 = 1; // Pin 14 A
    TRISBbits.TRISB6 = 1; // Pin 15 X
    TRISBbits.TRISB7 = 1; // Pin 16 Y
    TRISBbits.TRISB8 = 1; // Pin 17 B
}

void delay(int val) {
    for(int i=0;i<val;i++) {
        asm("repeat #15998");
        asm("nop");
    }
}



// Player must hold button until everyone is holding a button
int main(void) {
    setup();
    while(1) {
        // Check player 1 input
        if((PORTAbits.RA0 == 1) || (PORTAbits.RA1 == 1) || (PORTBbits.RB2 == 1) || (PORTBbits.RB3 == 1)) {
            // Check player 2 input
            if((PORTAbits.RA2 == 1) || (PORTAbits.RA3 == 1) || (PORTBbits.RB4 == 1) || (PORTAbits.RA4 == 1)) {
                // Check player 3 input
                if((PORTBbits.RB5 == 1) || (PORTBbits.RB6 == 1) || (PORTBbits.RB7 == 1) || (PORTBbits.RB8 == 1)) {
                    int choice1 = 0;
                    int choice2 = 0;
                    int choice3 = 0;
                    int choice4 = 0;
                    choice1 += (PORTAbits.RA0 + PORTAbits.RA2 + PORTBbits.RB5);
                    choice2 += (PORTAbits.RA1 + PORTAbits.RA3 + PORTBbits.RB6);
                    choice3 += (PORTBbits.RB2 + PORTBbits.RB4 + PORTBbits.RB7);
                    choice4 += (PORTBbits.RB3 + PORTAbits.RA4 + PORTBbits.RB8);
                    // If choice 1 has the most votes
                    if((choice1 > choice2) && (choice1 > choice3) && (choice1 > choice4)) {
                        // Keyboard press 1
                    }
                    // If choice 2 has the most votes
                    else if((choice2 > choice1) && (choice2 > choice3) && (choice2 > choice4)) {
                        // Keyboard press 2
                    }
                    // If choice 3 has the most votes
                    else if((choice3 > choice1) && (choice3 > choice2) && (choice3 > choice4)) {
                        // Keyboard press 3
                    }
                    // If choice 4 has the most votes
                    else if((choice4 > choice1) && (choice4 > choice2) && (choice4 > choice3)) {
                        // Keyboard press 4
                    }
                    // If no choice has 2 votes
                    else {
                        int r = 0;
                        if(choice4 != 0) {
                            r = (rand() % 4) + 1;
                        }
                        else {
                            r = (rand() % 3) + 1;
                        }
                        if(r == 1) {
                            // Keyboard press 1
                        }
                        if(r == 2) {
                            // Keyboard press 2
                        }
                        if(r == 3) {
                            // Keyboard press 3
                        }
                        if(r == 4) {
                            // Keyboard press 4
                        }
                    }
                    delay(5000);
                }
            }
        }
    }
    return 0;
}

Upvotes: 0

Views: 37

Answers (0)

Related Questions