void accelerometer_test(void){ unsigned char who,message[5]; acc_read_register(WHO_AM_I,&who,1); sprintf(message,"%d",who); write_OLED_message(message,0,0); display_draw(); }
void OnGetACCReg(u_char param_len, const char * parametres, u_char * rep_len, char * reponse) { if(acc_read_register(*parametres,reponse)) { *rep_len = strlen(reponse); } else { *rep_len = sprintf(reponse,"%c",203); } }
void accelerometer_pixel_bars(void){ short accels[3]; int xpix,ypix; char x_acc_dir,y_acc_dir; unsigned char x_dir[20],y_dir[20]; acc_read_register(OUT_X_L_A,(unsigned char *) accels, 6); xpix = accel_to_pixels(accels[0]); ypix = accel_to_pixels(accels[1]); x_acc_dir = accel_pixel_direction(xpix); y_acc_dir = accel_pixel_direction(ypix); sprintf(x_dir,"X-Acc = %c",x_acc_dir); sprintf(y_dir,"Y-Acc = %c",y_acc_dir); display_clear(); write_OLED_message(x_dir,0,0); write_OLED_message(y_dir,10,0); accel_pixels(xpix,'x'); accel_pixels(ypix,'y'); display_draw(); }
void APP_Tasks ( void ) { //static int8_t vector = 0; static uint8_t movement_length = 0; static bool sent_dont_move = false; short accels[3]; double xvel; double yvel; //int8_t dir_table[] ={-4,-4,-4, 0, 4, 4, 4, 0}; /* Check the application's current state. */ switch ( appData.state ) { /* Application's initial state. */ case APP_STATE_INIT: { /* Open the device layer */ appData.deviceHandle = USB_DEVICE_Open( USB_DEVICE_INDEX_0, DRV_IO_INTENT_READWRITE ); if(appData.deviceHandle != USB_DEVICE_HANDLE_INVALID) { /* Register a callback with device layer to get event notification (for end point 0) */ USB_DEVICE_EventHandlerSet(appData.deviceHandle, APP_USBDeviceEventHandler, 0); appData.state = APP_STATE_WAIT_FOR_CONFIGURATION; } else { /* The Device Layer is not ready to be opened. We should try * again later. */ } break; } case APP_STATE_WAIT_FOR_CONFIGURATION: /* Check if the device is configured. The * isConfigured flag is updated in the * Device Event Handler */ if(appData.isConfigured) { appData.state = APP_STATE_MOUSE_EMULATE; } break; case APP_STATE_MOUSE_EMULATE: APP_ProcessSwitchPress(); /* The following logic rotates the mouse icon when * a switch is pressed */ if(appData.isSwitchPressed) { /* Toggle the mouse emulation with each switch press */ appData.emulateMouse ^= 1; appData.isSwitchPressed = false; } if(appData.emulateMouse) { sent_dont_move = false; if(movement_length > 50) { acc_read_register(OUT_X_L_A, (unsigned char *) accels, 6); xvel = accels[0]/10000; yvel = accels[1]/10000; appData.xCoordinate = (double) xvel; appData.yCoordinate = (double) yvel; appData.mouseButton[0] = MOUSE_BUTTON_STATE_RELEASED; appData.mouseButton[1] = MOUSE_BUTTON_STATE_RELEASED; //appData.xCoordinate =(int8_t)dir_table[vector & 0x07] ; //appData.yCoordinate =(int8_t)dir_table[(vector+2) & 0x07]; //vector ++; movement_length = 0; } } else { appData.mouseButton[0] = MOUSE_BUTTON_STATE_RELEASED; appData.mouseButton[1] = MOUSE_BUTTON_STATE_RELEASED; appData.xCoordinate = 0; appData.yCoordinate = 0; } if(!appData.isMouseReportSendBusy) { if(((sent_dont_move == false) && (!appData.emulateMouse)) || (appData.emulateMouse)) { /* This means we can send the mouse report. The isMouseReportBusy flag is updated in the HID Event Handler. */ appData.isMouseReportSendBusy = true; /* Create the mouse report */ MOUSE_ReportCreate(appData.xCoordinate, appData.yCoordinate, appData.mouseButton, &mouseReport); if(memcmp((const void *)&mouseReportPrevious, (const void *)&mouseReport, (size_t)sizeof(mouseReport)) == 0) { /* Reports are same as previous report. However mouse reports * can be same as previous report as the co-ordinate positions are relative. * In that case it needs to be send */ if((appData.xCoordinate == 0) && (appData.yCoordinate == 0)) { /* If the coordinate positions are 0, that means there * is no relative change */ if(appData.idleRate == 0) { appData.isMouseReportSendBusy = false; } else { /* Check the idle rate here. If idle rate time elapsed * then the data will be sent. Idle rate resolution is * 4 msec as per HID specification; possible range is * between 4msec >= idlerate <= 1020 msec. */ if(appData.setIdleTimer * APP_USB_CONVERT_TO_MILLISECOND >= appData.idleRate * 4) { /* Send REPORT as idle time has elapsed */ appData.isMouseReportSendBusy = true; } else { /* Do not send REPORT as idle time has not elapsed */ appData.isMouseReportSendBusy = false; } } } } if(appData.isMouseReportSendBusy == true) { /* Copy the report sent to previous */ memcpy((void *)&mouseReportPrevious, (const void *)&mouseReport, (size_t)sizeof(mouseReport)); /* Send the mouse report. */ USB_DEVICE_HID_ReportSend(appData.hidInstance, &appData.reportTransferHandle, (uint8_t*)&mouseReport, sizeof(MOUSE_REPORT)); appData.setIdleTimer = 0; } movement_length ++; sent_dont_move = true; } } break; case APP_STATE_ERROR: break; /* The default state should never be executed. */ default: { /* TODO: Handle error in application's state machine. */ break; } } }
int main ( void ) { /* Initialize all MPLAB Harmony modules, including application(s). */ SYS_Initialize ( NULL ); // string used for OLED char str[200]; ANSELBbits.ANSB13 = 0; // make analog input digital U1RXRbits.U1RXR = 0b0000; // set U1RX to pin A2 RPB15Rbits.RPB15R = 0b0101; // set B15 to U1TX TRISBbits.TRISB13 = 1; // set up USER pin as input TRISBbits.TRISB7 = 0; // set up LED1 pin as a digital output APP_USBDeviceEventHandler(); __builtin_disable_interrupts(); // set the CP0 CONFIG register to indicate that // kseg0 is cacheable (0x3) or uncacheable (0x2) // see Chapter 2 "CPU for Devices with M4K Core" // of the PIC32 reference manual __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583); // no cache on this chip! // 0 data RAM access wait states BMXCONbits.BMXWSDRM = 0x0; // enable multi vector interrupts INTCONbits.MVEC = 0x1; // disable JTAG to be able to use TDI, TDO, TCK, TMS as digital DDPCONbits.JTAGEN = 0; // set up accelerometer acc_setup(); __builtin_enable_interrupts(); short accels[3]; // accelerations for the 3 axes short mags[3]; // magnetometer readings for the 3 axes short temp; // temperature display_init(); while ( true ) { /* Maintain state machines of all polled MPLAB Harmony modules. */ // read the accelerometer from all three axes // the accelerometer and the pic32 are both little endian by default (the lowest address has the LSB) // the accelerations are 16-bit twos compliment numbers, the same as a short acc_read_register(OUT_X_L_A, (unsigned char *) accels, 6); // need to read all 6 bytes in one transaction to get an update. acc_read_register(OUT_X_L_M, (unsigned char *) mags, 6); // read the temperature data. Its a right justified 12 bit two's compliment number acc_read_register(TEMP_OUT_L, (unsigned char *) &temp, 2); display_clear(); // sprintf(str, "Hello world %d!", accels); int x_acc = accels[0]*64/16000; int y_acc = accels[1]*32/16000; int xxx,yyy; if (x_acc>=0){ if(x_acc>64) x_acc=64; for (xxx=0;xxx<x_acc;xxx++){ display_pixel_set(31,64-xxx,1); display_pixel_set(32,64-xxx,1); display_pixel_set(33,64-xxx,1); } } else{ if(x_acc<-64) x_acc=-64; for (xxx=0;xxx>x_acc;xxx--){ display_pixel_set(31,64-xxx,1); display_pixel_set(32,64-xxx,1); display_pixel_set(33,64-xxx,1); } } if (y_acc>=0){ if(y_acc>32) y_acc=32; for (yyy=0;yyy<y_acc;yyy++){ display_pixel_set(32-yyy,63,1); display_pixel_set(32-yyy,64,1); display_pixel_set(32-yyy,65,1); } } else{ if(y_acc<-32) y_acc=-32; for (yyy=0;yyy>y_acc;yyy--){ display_pixel_set(32-yyy,63,1); display_pixel_set(32-yyy,64,1); display_pixel_set(32-yyy,65,1); } } display_draw(); SYS_Tasks ( ); } /* Execution should not come here during normal operation */ return ( EXIT_FAILURE ); }
int main () { __builtin_disable_interrupts(); // set the CP0 CONFIG register to indicate that // kseg0 is cacheable (0x3) or uncacheable (0x2) // see Chapter 2 "CPU for Devices with M4K Core" // of the PIC32 reference manual __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583); // no cache on this chip! // 0 data RAM access wait states BMXCONbits.BMXWSDRM = 0x0; // enable multi vector interrupts INTCONbits.MVEC = 0x1; // disable JTAG to be able to use TDI, TDO, TCK, TMS as digital DDPCONbits.JTAGEN = 0; __builtin_enable_interrupts(); TRISBbits.TRISB7 = 0; // set up LED1 pin as a digital output // int i,a; // display_init(); // for (i = 0; i<10; ++i) // { // a = 30+i; // display_pixel_set(15,a,1); // display_draw(); // } char input[100]; int i=0; short accels[3]; // accelerations for the 3 axes short mags[3]; // magnetometer readings for the 3 axes short temp; acc_setup(); //initialize accelerometer display_init(); //initialize LED screen float xg, yg, zg; while(1) { start_position[0] = 0; start_position[1] = 0; center_position[0] = 32; center_position[1] = 64; acc_read_register(OUT_X_L_A, (unsigned char *) accels, 6); acc_read_register(OUT_X_L_M, (unsigned char *) mags, 6); acc_read_register(TEMP_OUT_L, (unsigned char *) &temp, 2); xg = (float) accels[0]/16000; yg = (float) accels[1]/16000; zg = (float) accels[2]/16000; sprintf(input,"x: %.2f y: %.2f z: %.2f ", xg,yg,zg); display_reset(); display_draw(); display_ggraph(xg,yg); while(input[i]) { display_message(input[i]); i++; start_position[1] = start_position[1]+5; // if(start_position[1]+5>128) // {start_position[0]+1;} // else // {start_position[1] = start_position[1]+5;} } i = 0; display_draw(); _CP0_SET_COUNT(0); while(_CP0_GET_COUNT()<5000000) {;} } return (0); }
void APP_Tasks (void ) { char message[25]; char inputfrompc[25]; int i; short accels[3]; // accelerations for the 3 axes short accelsMAF; short accelsFIR; int buf1=0; int buf2=0; int buf3=0; int buf4=0; int buf5=0; //bn*1000 for FIR float b1=.0088; float b2=.0479; float b3=.1640; float b4=.2793; float b5=.2793; float b6=.1640; float b7=.0479; float b8=.0088; int FIRbuf1=0; int FIRbuf2=0; int FIRbuf3=0; int FIRbuf4=0; int FIRbuf5=0; int FIRbuf6=0; int FIRbuf7=0; int FIRbuf8=0; //sprintf(message,"Hello!"); //use_display(20,20,message); //display_draw(); /* Check if device is configured. See if it is configured with correct * configuration value */ switch(appData.state) { case APP_STATE_INIT: /* Open the device layer */ appData.usbDevHandle = USB_DEVICE_Open( USB_DEVICE_INDEX_0, DRV_IO_INTENT_READWRITE ); if(appData.usbDevHandle != USB_DEVICE_HANDLE_INVALID) { /* Register a callback with device layer to get event notification (for end point 0) */ USB_DEVICE_EventHandlerSet(appData.usbDevHandle, APP_USBDeviceEventHandler, 0); appData.state = APP_STATE_WAIT_FOR_CONFIGURATION; } else { /* The Device Layer is not ready to be opened. We should try * again later. */ } break; case APP_STATE_WAIT_FOR_CONFIGURATION: if(appData.deviceConfigured == true) { /* Device is ready to run the main task */ appData.hidDataReceived = false; appData.hidDataTransmitted = true; appData.state = APP_STATE_MAIN_TASK; /* Place a new read request. */ USB_DEVICE_HID_ReportReceive (USB_DEVICE_HID_INDEX_0, &appData.rxTransferHandle, appData.receiveDataBuffer, 64); } break; case APP_STATE_MAIN_TASK: if(!appData.deviceConfigured) { /* Device is not configured */ appData.state = APP_STATE_WAIT_FOR_CONFIGURATION; } else if( appData.hidDataReceived ) { /* Look at the data the host sent, to see what * kind of application specific command it sent. */ switch(appData.receiveDataBuffer[0]) { case 0x80: /* Toggle on board LED1 to LED2. */ BSP_LEDToggle( APP_USB_LED_1 ); BSP_LEDToggle( APP_USB_LED_2 ); for (i=0; i<8; i++){ inputfrompc[i] = appData.receiveDataBuffer[i+1]; } use_display(20,20,inputfrompc); display_draw(); appData.hidDataReceived = false; /* Place a new read request. */ USB_DEVICE_HID_ReportReceive (USB_DEVICE_HID_INDEX_0, &appData.rxTransferHandle, appData.receiveDataBuffer, 64 ); break; case 0x81: if(appData.hidDataTransmitted) { /* Echo back to the host PC the command we are fulfilling in * the first byte. In this case, the Get Push-button State * command. */ appData.transmitDataBuffer[0] = 0x81; acc_read_register(OUT_X_L_A, (unsigned char *) accels, 6); sprintf(message,"Z: %d",accels[0]); use_display(10,10,message); display_draw(); if(_CP0_GET_COUNT()>800000){ appData.transmitDataBuffer[1] = 1; appData.transmitDataBuffer[2] = accels[0]>>8; appData.transmitDataBuffer[3] = accels[0]&0xFF; //use MAF buffer values to calculate accelsMAF accelsMAF = ((buf1+buf2+buf3+buf4+buf5+accels[0])/6); //change MAF buffer values buf5=buf4; buf4=buf3; buf3=buf2; buf2=buf1; buf1=accels[0]; //change FIR buffer values FIRbuf8=FIRbuf7; FIRbuf7=FIRbuf6; FIRbuf6=FIRbuf5; FIRbuf5=FIRbuf4; FIRbuf4=FIRbuf3; FIRbuf3=FIRbuf2; FIRbuf2=FIRbuf1; FIRbuf1=accels[0]; //FIR Filtering calculations accelsFIR = (b1*FIRbuf1)+(b2*FIRbuf2)+(b3*FIRbuf3)+(b4*FIRbuf4)+(b5*FIRbuf5)+(b6*FIRbuf6)+(b7*FIRbuf7)+(b8*FIRbuf8); appData.transmitDataBuffer[4] = accelsMAF>>8; appData.transmitDataBuffer[5] = accelsMAF&0xFF; appData.transmitDataBuffer[6] = accelsFIR>>8; appData.transmitDataBuffer[7] = accelsFIR&0xFF; _CP0_SET_COUNT(0); } else{appData.transmitDataBuffer[1]=0;} appData.hidDataTransmitted = false; /* Prepare the USB module to send the data packet to the host */ USB_DEVICE_HID_ReportSend (USB_DEVICE_HID_INDEX_0, &appData.txTransferHandle, appData.transmitDataBuffer, 64 ); appData.hidDataReceived = false; /* Place a new read request. */ USB_DEVICE_HID_ReportReceive (USB_DEVICE_HID_INDEX_0, &appData.rxTransferHandle, appData.receiveDataBuffer, 64 ); } break; default: appData.hidDataReceived = false; /* Place a new read request. */ USB_DEVICE_HID_ReportReceive (USB_DEVICE_HID_INDEX_0, &appData.rxTransferHandle, appData.receiveDataBuffer, 64 ); break; }
int main(void) { //Startup __builtin_disable_interrupts(); // set the CP0 CONFIG register to indicate that // kseg0 is cacheable (0x3) or uncacheable (0x2) // see Chapter 2 "CPU for Devices with M4K Core" // of the PIC32 reference manual __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583); // no cache on this chip! // 0 data RAM access wait states BMXCONbits.BMXWSDRM = 0x0; // enable multi vector interrupts INTCONbits.MVEC = 0x1; // disable JTAG to be able to use TDI, TDO, TCK, TMS as digital DDPCONbits.JTAGEN = 0; __builtin_enable_interrupts(); ANSELBbits.ANSB13 = 0; // 0 for digital, 1 for analog ANSELBbits.ANSB15 = 0; // 0 for digital, 1 for analog T2CONbits.TCKPS = 0; //Setting prescaler to 1 (0 corresponds to 1) PR2 = 39999; //Setting PR for timer 2 to 39999 TMR2 = 0; //Setting Timer 2 to 0 OC1CONbits.OCTSEL = 0; //Telling OC1 to use timer 2 OC1CONbits.OCM = 0b110; //Telling OC1 to use PWM without the fault OC1RS = 20000; //Setting initial duty cycle to 20000/(39999+1)*100% = 50% OC1R = 20000; //Updating duty cycles to 20000/(39999+1)*100% = 50% T2CONbits.ON = 1; //turn on timer OC1CONbits.ON = 1; //turn on OC code // set up USER pin as input TRISBbits.TRISB13 = 1; // set pin B13 to be digital INPUT // U1RXRbits.U1RXR = 0b0011; // set U1RX to pin B13 (Input pin from User button) // set up LED1 pin as a digital output TRISBbits.TRISB7 = 0; // set pin B7 to be digital OUTPUT // LATBbits.LATB7 = 1; // RPB7Rbits.RPB7R = 0b0001; //set B7 to U1TX (Output pin for LED1) // set up LED2 as OC1 using Timer2 at 1kHz // TRISBbits.TRISB15 = 0; // set B15 to digital OUTPUT RPB15Rbits.RPB15R = 0b0101; // set B15 to U1TX (Output pin for OC1) // set up A0 as AN0 ANSELAbits.ANSA0 = 1; AD1CON3bits.ADCS = 3; AD1CHSbits.CH0SA = 0; AD1CON1bits.ADON = 1; // Accelerometer acc_setup(); short accels[3]; // accelerations for the 3 axes short mags[3]; // magnetometer readings for the 3 axes short temp; // Display display_init(); /*int number = 1337; sprintf(buffer,"Hello World %d!", number); display_write(28,32,buffer); */ /* for (ii = 0; ii < 128; ii++){ // Draw a line from position (x,y) = (0,15) to (127,15) display_pixel_set(15,ii,1); display_draw(); }*/ while (1){ // invert pin every 0.5s, set PWM duty cycle % to the pot voltage output % _CP0_SET_COUNT(0); LATBINV = 0b10000000; while(_CP0_GET_COUNT()<10000000){ OC1RS = readADC()*(PR2+1)/1023; // delay for 10M core ticks, 0.5s if (PORTBbits.RB13 == 1){ ;// nothing } else{ LATBINV = 0b10000000; } } // read the accelerometer from all three axes // the accelerometer and the pic32 are both little endian by default (the lowest address has the LSB) // the accelerations are 16-bit twos compliment numbers, the same as a short acc_read_register(OUT_X_L_A, (unsigned char *) accels, 6); // need to read all 6 bytes in one transaction to get an update. acc_read_register(OUT_X_L_M, (unsigned char *) mags, 6); // read the temperature data. Its a right justified 12 bit two's compliment number acc_read_register(TEMP_OUT_L, (unsigned char *) &temp, 2); display_clear(); sprintf(buffer1, "(ax, ay, az)"); display_write(0, 0, buffer1); sprintf(buffer2, "(%d, %d, %d)", accels[0], accels[1], accels[2]); display_write(0, 48, buffer2); sprintf(buffer, "Derek Oung"); display_write(0, 56, buffer); if (accels[0]>0 && accels[1]>0){ x_line_point = (float)((float)accels[0]/16000*64) + 64; y_line_point = (float)((float)accels[1]/16000*32) + 32; /* while (i<x_line_point){ display_pixel_set(32,i,1); i++; } */ for (i=64;i<x_line_point;i++){ display_pixel_set(31, i, 1); display_pixel_set(32, i, 1); display_pixel_set(33, i, 1); } for (j=32;j<y_line_point;j++){ display_pixel_set(j, 63, 1); display_pixel_set(j, 64, 1); display_pixel_set(j, 65, 1); } } else if (accels[0]>0 && accels[1]<0){ x_line_point = (float)((float)accels[0]/16000*64) + 64; y_line_point = (float)((float)accels[1]/16000*32) + 32; for (i=64;i<x_line_point;i++){ display_pixel_set(31, i, 1); display_pixel_set(32, i, 1); display_pixel_set(33, i, 1); } for (j=32;j>y_line_point;j--){ display_pixel_set(j, 63, 1); display_pixel_set(j, 64, 1); display_pixel_set(j, 65, 1); } } else if (accels[0]<0 && accels[1]>0){ x_line_point = (float)((float)accels[0]/16000*64) + 64; y_line_point = (float)((float)accels[1]/16000*32) + 32; for (i=64;i>x_line_point;i--){ display_pixel_set(31, i, 1); display_pixel_set(32, i, 1); display_pixel_set(33, i, 1); } for (j=32;j<y_line_point;j++){ display_pixel_set(j, 63, 1); display_pixel_set(j, 64, 1); display_pixel_set(j, 65, 1); } } else if (accels[0]<0 && accels[1]<0){ x_line_point = (float)((float)accels[0]/16000*64) + 64; y_line_point = (float)((float)accels[1]/16000*32) + 32; for (i=64;i>x_line_point;i--){ display_pixel_set(31, i, 1); display_pixel_set(32, i, 1); display_pixel_set(33, i, 1); } for (j=32;j>y_line_point;j--){ display_pixel_set(j, 63, 1); display_pixel_set(j, 64, 1); display_pixel_set(j, 65, 1); } } display_pixel_set(31,63,1); display_pixel_set(31,64,1); display_pixel_set(31,65,1); display_pixel_set(32,63,1); display_pixel_set(32,64,1); display_pixel_set(32,65,1); display_pixel_set(33,63,1); display_pixel_set(33,64,1); display_pixel_set(33,65,1); display_draw(); } }
void APP_Tasks (void ) { unsigned char print[26]; int jj = 0,row; short accels[3]; /* Check if device is configured. See if it is configured with correct * configuration value */ switch(appData.state) { case APP_STATE_INIT: /* Open the device layer */ appData.usbDevHandle = USB_DEVICE_Open( USB_DEVICE_INDEX_0, DRV_IO_INTENT_READWRITE ); if(appData.usbDevHandle != USB_DEVICE_HANDLE_INVALID) { /* Register a callback with device layer to get event notification (for end point 0) */ USB_DEVICE_EventHandlerSet(appData.usbDevHandle, APP_USBDeviceEventHandler, 0); appData.state = APP_STATE_WAIT_FOR_CONFIGURATION; } else { /* The Device Layer is not ready to be opened. We should try * again later. */ } break; case APP_STATE_WAIT_FOR_CONFIGURATION: if(appData.deviceConfigured == true) { /* Device is ready to run the main task */ appData.hidDataReceived = false; appData.hidDataTransmitted = true; appData.state = APP_STATE_MAIN_TASK; /* Place a new read request. */ USB_DEVICE_HID_ReportReceive (USB_DEVICE_HID_INDEX_0, &appData.rxTransferHandle, appData.receiveDataBuffer, 64); } break; case APP_STATE_MAIN_TASK: if(!appData.deviceConfigured) { /* Device is not configured */ appData.state = APP_STATE_WAIT_FOR_CONFIGURATION; } else if( appData.hidDataReceived ) { /* Look at the data the host sent, to see what * kind of application specific command it sent. */ switch(appData.receiveDataBuffer[0]) { case 0x1: /* Toggle on board LED1 to LED2. */ BSP_LEDToggle( APP_USB_LED_1 ); BSP_LEDToggle( APP_USB_LED_2 ); memcpy(print,&appData.receiveDataBuffer[2],25); print[26] = '\0'; display_clear(); write_OLED_message(print,appData.receiveDataBuffer[1],0); display_draw(); appData.hidDataReceived = false; /* Place a new read request. */ USB_DEVICE_HID_ReportReceive (USB_DEVICE_HID_INDEX_0, &appData.rxTransferHandle, appData.receiveDataBuffer, 64 ); _CP0_SET_COUNT(0); break; case 0x2: if(appData.hidDataTransmitted) { /* Echo back to the host PC the command we are fulfilling in * the first byte. In this case, the Get Push-button State * command. */ BSP_LEDToggle( APP_USB_LED_1 ); BSP_LEDToggle( APP_USB_LED_2 ); if(_CP0_GET_COUNT() > 200000){ appData.transmitDataBuffer[0] = 1; acc_read_register(OUT_X_L_A,(unsigned char *) accels, 6); appData.transmitDataBuffer[1] = accels[0] >> 8; appData.transmitDataBuffer[2] = accels[0]; appData.transmitDataBuffer[3] = accels[1] >> 8; appData.transmitDataBuffer[4] = accels[1]; appData.transmitDataBuffer[5] = accels[2] >> 8; appData.transmitDataBuffer[6] = accels[2]; _CP0_SET_COUNT(0); } else{ appData.transmitDataBuffer[0] = 0; } // appData.transmitDataBuffer[0] = 0x81; // // if( BSP_SwitchStateGet(APP_USB_SWITCH_1) == BSP_SWITCH_STATE_PRESSED ) // { // appData.transmitDataBuffer[1] = 0x00; // } // else // { // appData.transmitDataBuffer[1] = 0x01; // } appData.hidDataTransmitted = false; /* Prepare the USB module to send the data packet to the host */ USB_DEVICE_HID_ReportSend (USB_DEVICE_HID_INDEX_0, &appData.txTransferHandle, appData.transmitDataBuffer, 64 ); appData.hidDataReceived = false; /* Place a new read request. */ USB_DEVICE_HID_ReportReceive (USB_DEVICE_HID_INDEX_0, &appData.rxTransferHandle, appData.receiveDataBuffer, 64 ); } break; default: appData.hidDataReceived = false; /* Place a new read request. */ USB_DEVICE_HID_ReportReceive (USB_DEVICE_HID_INDEX_0, &appData.rxTransferHandle, appData.receiveDataBuffer, 64 ); break; }
int main() { int potValue; int timerResets=0; // startup startup(); T2CONbits.TCKPS = 2; // Timer2 prescaler N=4 (1:4) PR2 = 19999; // period = (PR2+1) * N * 12.5 ns = 100 us, 10 kHz TMR2 = 0; // initial TMR2 count is 0 OC1CONbits.OCM = 0b110; // PWM mode without fault pin; other OC1CON bits are defaults OC1RS = 5000; // duty cycle = OC1RS/(PR2+1) = 25% OC1R = 5000; // initialize before turning OC1 on; afterward it is read-only T2CONbits.ON = 1; // turn on Timer2 OC1CONbits.ON = 1; // turn on OC1 // set up USER pin as input ANSELBbits.ANSB13 = 0; // 0 for digital, 1 for analog // set up LED1 pin as a digital output ANSELBbits.ANSB15 = 0; // 0 for digital, 1 for analog TRISBbits.TRISB15 = 0; // set up LED2 as OC1 using Timer2 at 1kHz //RPB7Rbits.RPB7R = 0b0101; // set B15 to OC1 TRISBbits.TRISB7 = 0; LATBbits.LATB7=0; //LATBbits.LATB15=1; // set up A0 as AN0 ANSELAbits.ANSA0 = 1; AD1CON3bits.ADCS = 3; AD1CHSbits.CH0SA = 0; AD1CON1bits.ADON = 1; int counter = 0; int user = 1; /* /////////Testing section, old code/////////// display_init(); drawChar(72-0x20,45,50); drawChar(73-0x20,50,50); display_draw(); */ /* /////// Write to LCD display_init(); int k=0; int L=0; int charCurrent; int xIndex = 28; int yIndex = 32; char message[50]; sprintf(message,"Hello world 1337!"); while(L==0){ charCurrent = (int)message[k]; if(charCurrent==0){ L=1; } else{ drawChar(charCurrent - 0x20,xIndex, yIndex); xIndex+=6; k+=1; } } display_draw(); */ int a; int b; int aa; int bb; int c; int d; // set up power supply to LCD as digital output _CP0_SET_COUNT(0); while(_CP0_GET_COUNT()<4000000){ } LATBbits.LATB15=1; display_init(); int charCurrent; acc_setup(); while (1) { display_clear(); int xIndex = 10; int yIndex = 32; int k=0; int L=0; char message[500]; short accels[3]; // accelerations for the 3 axes short mags[3]; // magnetometer readings for the 3 axes short temp; // read the accelerometer from all three axes // the accelerometer and the pic32 are both little endian by default (the lowest address has the LSB) // the accelerations are 16-bit twos compliment numbers, the same as a short acc_read_register(OUT_X_L_A, (unsigned char *) accels, 6); // need to read all 6 bytes in one transaction to get an update. acc_read_register(OUT_X_L_M, (unsigned char *) mags, 6); // read the temperature data. Its a right justified 12 bit two's compliment number acc_read_register(TEMP_OUT_L, (unsigned char *) &temp, 2); /* /////// Write to LCD sprintf(message,"%d %d %d ",accels[0], accels[1],accels[2]); L=0; while(message[k]){ drawChar(message[k] - 0x20,xIndex, yIndex); xIndex+=6; k+=1; //} } */ a=accels[0]/500; b=accels[1]/1000; display_pixel_set(31,64,1); display_pixel_set(32,64,1); display_pixel_set(33,64,1); display_pixel_set(32,63,1); display_pixel_set(32,65,1); if(a<0){ c=-a; for(aa=0;aa<c;aa++){ display_pixel_set(31,64-aa,1); display_pixel_set(32,64-aa,1); display_pixel_set(33,64-aa,1); } } if(a>=0){ for(aa=0;aa<a;aa++){ display_pixel_set(31,aa+64,1); display_pixel_set(32,aa+64,1); display_pixel_set(33,aa+64,1); } } if(b<0){ d=-b; for(bb=0;bb<d;bb++){ display_pixel_set(32-bb,63,1); display_pixel_set(32-bb,64,1); display_pixel_set(32-bb,65,1); } } if(b>=0){ for(bb=0;bb<b;bb++){ display_pixel_set(32+bb,63,1); display_pixel_set(32+bb,64,1); display_pixel_set(32+bb,65,1); } } display_draw(); } }
void APP_Tasks(void) { /* Check if device is configured. See if it is configured with correct * configuration value */ switch (appData.state) { case APP_STATE_INIT: /* Open the device layer */ appData.usbDevHandle = USB_DEVICE_Open(USB_DEVICE_INDEX_0, DRV_IO_INTENT_READWRITE); if (appData.usbDevHandle != USB_DEVICE_HANDLE_INVALID) { /* Register a callback with device layer to get event notification (for end point 0) */ USB_DEVICE_EventHandlerSet(appData.usbDevHandle, APP_USBDeviceEventHandler, 0); appData.state = APP_STATE_WAIT_FOR_CONFIGURATION; } else { /* The Device Layer is not ready to be opened. We should try * again later. */ } break; case APP_STATE_WAIT_FOR_CONFIGURATION: if (appData.deviceConfigured == true) { /* Device is ready to run the main task */ appData.hidDataReceived = false; appData.hidDataTransmitted = true; appData.state = APP_STATE_MAIN_TASK; /* Place a new read request. */ USB_DEVICE_HID_ReportReceive(USB_DEVICE_HID_INDEX_0, &appData.rxTransferHandle, appData.receiveDataBuffer, 64); } break; case APP_STATE_MAIN_TASK: if (!appData.deviceConfigured) { /* Device is not configured */ appData.state = APP_STATE_WAIT_FOR_CONFIGURATION; } else if (appData.hidDataReceived) { /* Look at the data the host sent, to see what * kind of application specific command it sent. */ switch (appData.receiveDataBuffer[0]) { case 0x80: /* Toggle on board LED1 to LED2. */ BSP_LEDToggle(APP_USB_LED_1); BSP_LEDToggle(APP_USB_LED_2); setRTR(); break; case 0x81: if (appData.hidDataTransmitted) { /* Echo back to the host PC the command we are fulfilling in * the first byte. In this case, the Get Push-button State * command. */ appData.transmitDataBuffer[0] = 0x81; appData.transmitDataBuffer[1] = 0b1 & BSP_SwitchStateGet(APP_USB_SWITCH_1); appData.transmitDataBuffer[2] = 111; setRTS(); setRTR(); } break; case 0x82: if (!appData.numTX || _CP0_GET_COUNT() > 200000) { //prepare new data to send acc_read_register(OUT_X_L_A, (unsigned char *) appData.accels, 6); appData.transmitDataBuffer[0] = 1; //we have data to send appData.transmitDataBuffer[1] = appData.accels[0] >> 8; //x high byte appData.transmitDataBuffer[2] = appData.accels[0] & 0xFF; //x low byte appData.transmitDataBuffer[3] = appData.accels[1] >> 8; //y high byte appData.transmitDataBuffer[4] = appData.accels[1] & 0xFF; //y low byte appData.transmitDataBuffer[5] = appData.accels[2] >> 8; //z high byte appData.transmitDataBuffer[6] = appData.accels[2] & 0xFF; //z low byte // reset core timer for 100 hz _CP0_SET_COUNT(0); appData.numTX++; } else { appData.transmitDataBuffer[0] = 0; // we don't have new data } setRTS(); setRTR(); break; case 0x83: // prepare for a bout of sending accel data //parse incoming data to screen oled_clear_buffer(); int row = appData.receiveDataBuffer[1]; char * msg; msg = &appData.receiveDataBuffer[2]; oled_draw_string(0, row, msg, 1); oled_update(); // clear buffered accel data so we read new data to send acc_read_register(OUT_X_L_A, (unsigned char *) appData.accels, 6); appData.numTX = 0; //we're starting over setRTR(); break; case 0x84: // done asking for data oled_draw_string(0, 55, "Done!", 1); oled_update(); setRTR(); break; default: setRTR(); break; } }