Beispiel #1
0
int main(void) {
  CFGCONbits.JTAGEN = 0; // turn off JTAG, get back those pins for IO use

  SYSTEMConfigPerformance(SYS_FREQ);
  INTDisableInterrupts();
  
  PIC32MX250_setup_pins();
  
  //Setup SPI1
  SPI1CON = 0;              // turn off the spi module and reset it
  SPI1BUF;                  // clear the rx buffer by reading from it
  SPI1BRG = 0x3;            // baud rate to 5MHz [SPI1BRG = (40000000/(2*desired))-1]
  SPI1STATbits.SPIROV = 0;  // clear the overflow bit
  SPI1CONbits.CKE = 1;      // data changes when clock goes from active to inactive
                            //    (high to low since CKP is 0)
  SPI1CONbits.MSTEN = 1;    // master operation
  SPI1CONbits.ON = 1;       // turn on spi
  
  TRISBbits.TRISB7=0;   //set pins as outputs--0=output
  TRISAbits.TRISA4=0;
  TRISAbits.TRISA3=0;
  TRISAbits.TRISA2=1; //make RA2 an input to trigger ADC collecting

  //Initialize pin values
  LATAbits.LATA3=1; //make this pin be 3.3v
  LATAbits.LATA4=1;
  LATBbits.LATB7=0;  //make this pin go low
  
  
  unsigned int sample=0;
  while(1){
      if (PORTAbits.RA2){
          //if pin 9 goes high, read from the adc
          sample=adc_grab(1);
      }
  }
  return 0;
}
int main() {
    CFGCONbits.JTAGEN = 0; // turn off JTAG, get back those pins for IO use
    // set PIC32to max computing power
    DEBUGLED = 0;
    // enable multi-vector interrupts
    INTEnableSystemMultiVectoredInt();
    INTEnableInterrupts();
    PIC32MX250_setup_pins();
    SYSTEMConfigPerformance(SYS_FREQ);

    setTimer2(OUTPUT_FREQ); //
    setupTimer3(BUFFER_FREQ);
    initPWM(); //Initializing PWM on OC3.

    // Initialize texture buffer and index to zero

    int i;
    tBuff.Index = 0; //init to zero
    tBuff.On = 0; //turn off
    tBuff.Length0 = 0; //start with zero length
    tBuff.Length1 = 0; //start with zero length
    tBuff.Front = 0;
    for (i = 0; i < MAX_BUFFER_LENGTH; i++) {
        tBuff.Buff0[i] = 0; //init entire buffer to zero
        tBuff.Buff1[i] = 0; //init entire buffer to zero
    }


    // Initialize the USB host
    ConnectionInit();
    DEBUGLED = 1;

    //Main USB State Machine
    while (1) {
        // Keep the USB connection running;
        // Handle incoming data and manage outgoing data.
        ConnectionTasks();
        // Main state machine
        switch (state) {
            case STATE_INIT:

                state = STATE_WAITING;
                h = INVALID_CHANNEL_HANDLE;
                break;

            case STATE_WAITING:
                DEBUGLED = 0;
                if (ADBAttached()) {
                    state = STATE_CONNECTING;
                }
                break;

            case STATE_CONNECTING:

                if (ADBConnected()) {
                    // Open a channel to the Android device
                    // See "adb.h" in libadb for more details
                    // (I don't think the name tcp:4545 matters)
                    h = ADBOpen("tcp:4545", &ADBCallback);

                    if (h != INVALID_CHANNEL_HANDLE) {
                        state = STATE_CONNECTED;
                        WriteCoreTimer(0);
                        // Send plaintext and let the recipient do formatting
                        ADBWrite(h & 0xFF, "Hello from TPAD!", 17);
                    }
                }
                break;

            case STATE_CONNECTED:
                DEBUGLED = 1;

                if (!ADBAttached()) {
                    state = STATE_INIT;
                }
                if (ADBChannelReady(h)) {

                    // Execute tasks that rely on the Android-PIC32 connection
                    // Here we will just wait for messages to come in and be handled below
                }
                // Timeout timer. If the coretimer is not reset by a keepalive command from the Android, the PIC will reset the USB communications
                if (ReadCoreTimer() > 200000000) {
                    state = STATE_INIT;
                }

                break;
        } // end state machine
    } // end while loop

    return 0;
}