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); }
int main() { // 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(); ANSELAbits.ANSA0 = 0; // set up USER pin as input ANSELBbits.ANSB13 = 0; TRISBbits.TRISB13 = 1; // set up LED1 pin as a digital output RPB7Rbits.RPB7R = 0b0001; TRISBbits.TRISB7 = 0; LATBbits.LATB7 = 1; // set up LED2 as OC1 using Timer2 at 1kHz ANSELBbits.ANSB15 = 0; // 0 for digital RPB15Rbits.RPB15R = 0b0101; //set B15 as output compare 1 // __builtin_disable_interrupts(); T2CONbits.TCKPS = 0; // Timer2 prescaler N=1 (1:4) PR2 = 39999; // period = (PR2+1) * N * 12.5 ns = 1 ms, 1 kHz TMR2 = 0; // initial TMR2 count is 0 OC1CONbits.OCM = 0b110; // PWM mode without fault pin; other OC1CON bits are defaults T2CONbits.ON = 1; // turn on Timer2 OC1CONbits.ON = 1; // turn on OC1 OC1RS = 0; OC1R = 0; // set up A0 as AN0 ANSELAbits.ANSA0 = 1; AD1CON3bits.ADCS = 3; AD1CHSbits.CH0SA = 0; AD1CON1bits.ADON = 1; while (1) { // invert pin every 0.5s, set PWM duty cycle % to the pot voltage output //Use the core timer to double check your CPU clock settings _CP0_SET_COUNT(0); // set core timer to 0, remember it counts at half the CPU clock LATBINV = 0x0080; // invert a pin // wait for half a second, setting LED brightness to pot angle while waiting while (_CP0_GET_COUNT() < 10000000) { int val = readADC(); OC1RS = (val * PR2)/1024; if (PORTBbits.RB13 == 1) { // nothing } else { LATBINV = 0x0080; } } } }
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; display_init(); int number = 1337; sprintf(buffer,"Hello World %d!", number); display_write(28,32,buffer); /*int ii; // Draw a line from position (x,y) = (0,15) to (127,15) for (ii = 0; ii < 128; ii++){ 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; } } } }
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; }
void setup(){ // DEVCFGs here // DEVCFG0 #pragma config DEBUG = OFF // no debugging #pragma config JTAGEN = OFF // no jtag #pragma config ICESEL = ICS_PGx1 // use PGED1 and PGEC1 #pragma config PWP = OFF // no write protect #pragma config BWP = OFF // not boot write protect #pragma config CP = OFF // no code protect // DEVCFG1 #pragma config FNOSC = PRIPLL // use primary oscillator with pll #pragma config FSOSCEN = OFF // turn off secondary oscillator #pragma config IESO = OFF // no switching clocks #pragma config POSCMOD = HS // high speed crystal mode #pragma config OSCIOFNC = OFF // free up secondary osc pins #pragma config FPBDIV = DIV_1 // divide CPU freq by 1 for peripheral bus clock #pragma config FCKSM = CSDCMD // do not enable clock switch #pragma config WDTPS = PS1 // slowest wdt #pragma config WINDIS = OFF // no wdt window #pragma config FWDTEN = OFF // wdt off by default #pragma config FWDTWINSZ = WINSZ_25 // wdt window at 25% // DEVCFG2 - get the CPU clock to 40MHz #pragma config FPLLIDIV = DIV_2 // divide input clock to be in range 4-5MHz #pragma config FPLLMUL = MUL_20 // multiply clock after FPLLIDIV #pragma config UPLLIDIV = DIV_1 // divide clock after FPLLMUL #pragma config UPLLEN = ON // USB clock on #pragma config FPLLODIV = DIV_2 // divide clock by 1 to output on pin // DEVCFG3 #pragma config USERID = 0 // some 16bit userid #pragma config PMDL1WAY = ON // not multiple reconfiguration, check this #pragma config IOL1WAY = ON // not multimple reconfiguration, check this #pragma config FUSBIDIO = ON // USB pins controlled by USB module #pragma config FVBUSONIO = ON // controlled by USB module // 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(); // set up USER pin as input ANSELBbits.ANSB13 = 0; // set up LED1 pin as a digital output - ANSELBbits.ANSB15 = 0; TRISBbits.TRISB15 = 0; _CP0_SET_COUNT(0); while(_CP0_GET_COUNT()<4000000); LATBbits.LATB15 = 1; // set up A0 as AN0 ANSELAbits.ANSA0 = 1; AD1CON3bits.ADCS = 3; AD1CHSbits.CH0SA = 0; AD1CON1bits.ADON = 1; display_init(); acc_setup(); }
uint32_t cpu_microsecond_count(void) { return _CP0_GET_COUNT(); }
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 flashOperation(uint32 nvmop, uint32 addr, uint32 data) ** ** Synopsis: ** Performs either a page erase, word write, or row write ** ** Parameters: ** nvmop either NVMOP_PAGE_ERASE, NVMOP_WORD_PGM, or NVMOP_ROW_PGM ** addr the uint32_t flash address of: the page to erase, word location to write, or row location to write ** data a uint32_t of data to write, or the uint32_t of the SRAM address containing the array of data to write to the row ** ** Return Values: ** True if successful, false if failed ** ** Errors: ** None ** ** Notes: ** data has no meaning when page erase is specified and should be set to 0ul ** */ uint32_t FlashOperation(uint32_t nvmop, uint32_t addr, uint32_t data) { unsigned long t0; unsigned int status; #if defined(_PCACHE) unsigned long K0; unsigned long PFEN = CHECON & _CHECON_PREFEN_MASK; #endif // Convert Address to Physical Address NVMADDR = KVA_2_PA(addr); NVMDATA = data; NVMSRCADDR = KVA_2_PA(data); // Suspend or Disable all Interrupts SuspendINT(status); #if defined(_PCACHE) // disable predictive prefetching, see errata CHECONCLR = _CHECON_PREFEN_MASK; // turn off caching, see errata ReadK0(K0); WriteK0((K0 & ~0x07) | K0_UNCACHED); #endif // Enable Flash Write/Erase Operations NVMCON = NVMCON_WREN | nvmop; // this is a poorly documented yet very important // required delay on newer silicon. // If you do not delay, on some silicon it will // completely latch up the flash to where you need // to cycle power, so wait for at least // 6us for LVD start-up, see errata t0 = _CP0_GET_COUNT(); while (_CP0_GET_COUNT() - t0 < ((6 * F_CPU) / 2 / 1000000UL)); // magic unlock sequence NVMKEY = 0xAA996655; NVMKEY = 0x556699AA; NVMCONSET = NVMCON_WR; // Wait for WR bit to clear while (NVMCON & NVMCON_WR); // see errata, wait 500ns before writing to any NVM register t0 = _CP0_GET_COUNT(); while (_CP0_GET_COUNT() - t0 < ((F_CPU / 2 / 2000000UL))); // Disable Flash Write/Erase operations NVMCONCLR = NVMCON_WREN; #if defined(_PCACHE) // restore predictive prefetching and caching, see errata WriteK0(K0); CHECONSET = PFEN; #endif // Restore Interrupts RestoreINT(status); // assert no errors return(! (NVMCON & (_NVMCON_WRERR_MASK | _NVMCON_LVDERR_MASK))); }
int main() { // startup FROM HW1 __builtin_disable_interrupts(); __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583); BMXCONbits.BMXWSDRM = 0x0; INTCONbits.MVEC = 0x1; DDPCONbits.JTAGEN = 0; __builtin_enable_interrupts(); ANSELBbits.ANSB13 = 0; //Make B13 Digital TRISBbits.TRISB7 = 0; RPB15Rbits.RPB15R = 0b0101; //Set B15 as OC1 T2CONbits.TCKPS = 0; //Prescalar = 1 PR2 = 39999; //Max counter value TMR2 = 0; // Initialize timer 2 OC1CONbits.OCM = 0b110; //Without failsafe OC1RS = 2000; //Duts cycle is 5% OC1R = 2000; //Initial duty cycle is 5% T2CONbits.ON = 1; //turns timer on OC1CONbits.ON = 1; //Turns output control on ANSELAbits.ANSA0 = 1; AD1CON3bits.ADCS = 3; AD1CHSbits.CH0SA = 0; AD1CON1bits.ADON = 1; display_init(); while (1) { _CP0_SET_COUNT(0); // set core timer to 0, remember it counts at half the CPU clock LATBINV = 0b10000000; // invert a pin B7 (LED1) while (_CP0_GET_COUNT() < 10000000) { int val; val = readADC(); OC1RS = val * 39; //comes out to about 39 if (PORTBbits.RB13 == 1) { } else { LATBINV = 0b10000000; //Invert LED1 } } //END STARTUP FROM HW1 display_clear(); char MessageLetters[17]; //= 'Hello world 1337!'; sprintf(MessageLetters, "Hello world 1337!"); int ii = 0; int currentVal; int message[17]; while (MessageLetters[ii]) { currentVal = MessageLetters[ii]; if (currentVal - 32 >= 0) { message[ii] = currentVal - 32; } ii++; } int StringCount; int colLCD; //Create column location on LCD int rowLCD; //Create row location on LCD for (StringCount = 0; StringCount < 20; StringCount++) { for (colLCD = 1; colLCD < 6; colLCD++) { //Cycle through column of array for (rowLCD = 1; rowLCD < 9; rowLCD++) { //Cycle through row of array display_pixel_set(rowLCD + 32, colLCD + 6 * StringCount + 28, getBit(message[StringCount], rowLCD - 1, colLCD - 1)); //At this specific (row, col) of screen, turn LCD on (1) or off (0) based on array value } } } display_draw(); //Commit to LCD screen once all positions defined } }
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 ) { 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; }
void lcd_start() { LCD_CS_TRIS = 0; LCD_OP_TRIS = 0; LCD_CS = 1; spi_init(); unsigned int time = 0; lcd_command(LCD_CMD_SWRESET);//software reset time = timer_start(); while (!timer_timeout(time,500 * TIMER_MILLISECOND)) {} //delay(500); lcd_command(LCD_CMD_SLPOUT);//exit sleep time = timer_start(); while (!timer_timeout(time,5 * TIMER_MILLISECOND)) {} //delay(5); lcd_command(LCD_CMD_PIXFMT);//Set Color Format 16bit lcd_data(0x05); time = timer_start(); while (!timer_timeout(time,5 * TIMER_MILLISECOND)) {} //delay(5); lcd_command(LCD_CMD_GAMMASET);//default gamma curve 3 lcd_data(0x04);//0x04; time = timer_start(); while (!timer_timeout(time,TIMER_MILLISECOND)) {} //delay(1); lcd_command(LCD_CMD_GAMRSEL);//Enable Gamma adj lcd_data(0x01); time = timer_start(); while (!timer_timeout(time,TIMER_MILLISECOND)) {} //delay(1); lcd_command(LCD_CMD_NORML); lcd_command(LCD_CMD_DFUNCTR); lcd_data(0b11111111); lcd_data(0b00000110); int i = 0; lcd_command(LCD_CMD_PGAMMAC);//Positive Gamma Correction Setting for (i=0;i<15;i++){ lcd_data(pGammaSet[i]); } lcd_command(LCD_CMD_NGAMMAC);//Negative Gamma Correction Setting for (i=0;i<15;i++){ lcd_data(nGammaSet[i]); } lcd_command(LCD_CMD_FRMCTR1);//Frame Rate Control (In normal mode/Full colors) lcd_data(0x08);//0x0C//0x08 lcd_data(0x02);//0x14//0x08 time = timer_start(); while (!timer_timeout(time,TIMER_MILLISECOND)) {} //delay(1); lcd_command(LCD_CMD_DINVCTR); //display inversion lcd_data(0x07); time = _CP0_GET_COUNT(); while (!timer_timeout(time,TIMER_MILLISECOND)) {} //delay(1); lcd_command(LCD_CMD_PWCTR1);//Set VRH1[4:0] & VC[2:0] for VCI1 & GVDD lcd_data(0x0A);//4.30 - 0x0A lcd_data(0x02);//0x05 time = timer_start(); while (!timer_timeout(time,TIMER_MILLISECOND)) {} //delay(1); lcd_command(LCD_CMD_PWCTR2);//Set BT[2:0] for AVDD & VCL & VGH & VGL lcd_data(0x02); time = timer_start(); while (!timer_timeout(time,TIMER_MILLISECOND)) {} //delay(1); lcd_command(LCD_CMD_VCOMCTR1);//Set VMH[6:0] & VML[6:0] for VOMH & VCOML lcd_data(0x50);//0x50 lcd_data(99);//0x5b time = timer_start(); while (!timer_timeout(time,TIMER_MILLISECOND)) {} //delay(1); lcd_command(LCD_CMD_VCOMOFFS); lcd_data(0);//0x40 time = timer_start(); while (!timer_timeout(time,TIMER_MILLISECOND)) {} //delay(1); lcd_command(LCD_CMD_CLMADRS);//Set Column Address lcd_data16(0x00); lcd_data16(LCD_GRAMWIDTH); lcd_command(LCD_CMD_PGEADRS);//Set Page Address lcd_data16(0x00); lcd_data16(LCD_GRAMHEIGH); lcd_command(LCD_CMD_VSCLLDEF); lcd_data16(0); // __OFFSET lcd_data16(LCD_GRAMHEIGH); // _GRAMHEIGH - __OFFSET lcd_data16(0); lcd_command(LCD_CMD_MADCTL); // rotation lcd_data(0b00001000); // bit 3 0 for RGB, 1 for GBR, rotation: 0b00001000, 0b01101000, 0b11001000, 0b10101000 lcd_command(LCD_CMD_DISPON);//display ON time = timer_start(); while (!timer_timeout(time,TIMER_MILLISECOND)) {} //delay(1); lcd_command(LCD_CMD_RAMWR);//Memory Write lcd_clearScreen(LCD_COLOR_BLACK); }
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; } }
void APP_Tasks ( void ) { static int8_t vector = 0; static uint8_t movement_length = 0; static bool sent_dont_move = false; unsigned char data[14]; short output[7]; /* 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) { _CP0_SET_COUNT(0); i2cMasterReadAll(IMU_ADDR,OUT_TEMP_L,14,data); char2short(data,output,14); LATAbits.LATA4 = !LATAbits.LATA4; while(_CP0_GET_COUNT()<UPDATER){;} appData.mouseButton[0] = MOUSE_BUTTON_STATE_RELEASED; appData.mouseButton[1] = MOUSE_BUTTON_STATE_RELEASED; appData.xCoordinate =(int8_t)(output[4]/500);//dir_table[vector & 0x07] ; appData.yCoordinate =(int8_t)(output[5]/500);//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 ); __builtin_disable_interrupts(); // set the CP0 CONFIG register to indicate that kseg0 is cacheable (0x3) __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583); // 0 data RAM access wait states BMXCONbits.BMXWSDRM = 0x0; // enable multi vector interrupts INTCONbits.MVEC = 0x1; // disable JTAG to get pins back DDPCONbits.JTAGEN = 0; // initialize I2C initI2C2(); // initialize IMU initIMU(); // initialize SPI, LCD // SPI1_init(); // LCD_init(); __builtin_enable_interrupts(); // LCD_clearScreen(BLACK); // char message[MAX_LENGTH]; unsigned char data[14]; // float accX, accY; _CP0_SET_COUNT(0); while ( true ) { if (_CP0_GET_COUNT() > 480000) { // 50 Hz i2c_read_multiple(IMU_ADDRESS, OUT_TEMP_L, data, 14); temp_raw = data[1] << 8 | data[0]; gyroX_raw = data[3] << 8 | data[2]; gyroY_raw = data[5] << 8 | data[4]; gyroZ_raw = data[7] << 8 | data[6]; accX_raw = data[9] << 8 | data[8]; accY_raw = data[11] << 8 | data[10]; accZ_raw = data[13] << 8 | data[12]; // accX = accX_raw * 2.0 / 32768; // accel in g // accY = accY_raw * 2.0 / 32768; // accel in g // accZ = accZ_raw * 2.0 / 32768; // accel in g // sprintf(message, "temp raw: %x ", temp_raw); // LCD_drawString(10, 10, message, WHITE); // // sprintf(message, "accX: %f g ", accX); // LCD_drawString(10, 20, message, WHITE); // // sprintf(message, "accY: %f g ", accY); // LCD_drawString(10, 30, message, WHITE); // // sprintf(message, "accZ: %f g ", accZ); // LCD_drawString(10, 40, message, WHITE); // // sprintf(message, "gyroX raw: %i ", gyroX_raw); // LCD_drawString(10, 50, message, WHITE); // // sprintf(message, "gyroY raw: %i ", gyroY_raw); // LCD_drawString(10, 60, message, WHITE); // // sprintf(message, "gyroZ raw: %i ", gyroZ_raw); // LCD_drawString(10, 70, message, WHITE); _CP0_SET_COUNT(0); } /* Maintain state machines of all polled MPLAB Harmony modules. */ SYS_Tasks ( ); } /* Execution should not come here during normal operation */ return ( EXIT_FAILURE ); }
int main() { //int value = 0; char r; makewave(); int count1 = 0; int count2 = 0; __builtin_disable_interrupts(); // set the CP0 CONFIG register to indicate that kseg0 is cacheable (0x3) __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583); // 0 data RAM access wait states BMXCONbits.BMXWSDRM = 0x0; // enable multi vector interrupts INTCONbits.MVEC = 0x1; // disable JTAG to get pins back DDPCONbits.JTAGEN = 0; // do your TRIS and LAT commands here TRISAbits.TRISA4 = 0; // ouput TRISBbits.TRISB4 = 1; // input LATAbits.LATA4 = 1; // intialize LED on initSPI1(); initI2C2(); init_ctrl1(); init_ctrl2(); init_ctrl3(); init_OC(); __builtin_enable_interrupts(); while(1) { // use _CP0_SET_COUNT(0) and _CP0_GET_COUNT() to test the PIC timing // remember the core timer runs at half the CPU speed _CP0_SET_COUNT(0); // set core timer to 0 while(_CP0_GET_COUNT() < 480000){ // wait 1ms / 0.001s ; } while(_CP0_GET_COUNT() < 480000){ // wait 1ms / 0.001s ; } while(_CP0_GET_COUNT() < 480000){ // wait 1ms / 0.001s ; } while(_CP0_GET_COUNT() < 480000){ // wait 1ms / 0.001s ; } //setVoltage(0,sinewave[count1]); //setVoltage(1,triangle_wave[count2]); //count1++; //count2++; //if(count1 == 100){ //count1 = 0; //} //if(count2 == 200){ //count2 = 0; //} I2C_read_multiple(0x6B, 0x20, data_array, 14); }// while loop //CS = 0; // listen to me //SPI1_IO(0x38); // most significant byte of address //SPI1_IO(0x00); // the least significant address byte //CS = 1; }
int main() { // Startup code to run as fast as possible and get pins back from bad defaults __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(); // set up USER pin as digital input ANSELBbits.ANSB13 = 0; // 0 = digital TRISBbits.TRISB13 = 1; // 1 = input // set up LED1 pin as a digital output TRISBbits.TRISB7 = 0; //digital output LATBbits.LATB7 = 1; //output on=0 initially // set up LED2 as OC1 using Timer2 at 1kHz ANSELBbits.ANSB15 = 0; // 0 = digital RPB15Rbits.RPB15R = 0b0101; //Set B15 as OC1 // pwm OC1CONbits.OCTSEL = 0; // Timer 2 is used for output compare OC1CONbits.OCM = 0b110; // PWM mode without fault pin; other OC1CON bits are defaults OC1R = 20000; // duty cycle = OC1RS/(PR2+1) = 100 // initialize before turning OC1 on; afterward it is read-only PR2 = 19999; // period = (PR2+1)*N=2*25ns = 1 ms, or 1 kHz TMR2 = 0; // initalize counter to 0 T2CONbits.TCKPS = 1; // set prescaler to 1:2 T2CONbits.ON = 1; // turn on Timer2 OC1CONbits.ON = 1; // turn on OC1 // set up A0 as AN0 ANSELAbits.ANSA0 = 1; AD1CON3bits.ADCS = 3; AD1CHSbits.CH0SA = 0; AD1CON1bits.ADON = 1; while (1) { // invert pin every 0.5s, set PWM duty cycle % to the pot voltage output % _CP0_SET_COUNT(0); // set core timer to 0, remember it counts at half the CPU clock LATBINV = 0b10000000;// invert pin RB7 // wait for half a second, setting LED brightness to pot angle while waiting while (_CP0_GET_COUNT() < 10000000) { int val = readADC(); //max = 1023 OC1RS = val * 20000/1023; if (PORTBbits.RB13 == 1) { //nothing } else { LATBINV = 0b10000000; } } } }
/* This function overrides the normal _weak_ _generic_exception_handler which is defined in the C32 User's Guide. The _weak_ _generic_exception_handler just does an infinite loop. */ void _general_exception_handler(void) { unsigned long t0 = _CP0_GET_COUNT(); /* Used for NVMOP 6 us Delay */ /* Mask off Mask of the ExcCode Field from the Cause Register Refer to the MIPs M4K Software User's manual */ _excep_code=_CP0_GET_CAUSE() & 0x0000007C >> 2; _excep_addr=_CP0_GET_EPC(); _CP0_SET_STATUS(_CP0_GET_STATUS()&0xFFFFFFE); /* Disable Interrupts */ #ifdef WRITE_EXCEPTION_CAUSE_TO_FLASH /* Store the exception causes in program memory in case the part exhibited the problem in release mode. Gives user a place to start debugging the problem. */ NVMCON = 0x4001; /* set WREN and Word Programing mode */ NVMADDR = EXCEPTION_CAUSE; /* PM Address at which we'll store the */ /* cause register */ NVMDATA = _excep_code; /* wait at least 6 us for LVD start-up assume we're running at max frequency (80 MHz) so we're always safe */ { while (_CP0_GET_COUNT() - t0 < (80/2)*6); } NVMKEY = 0xAA996655; NVMKEY = 0x556699AA; /* unlock sequence */ NVMCONSET = NVMCON_WR; while(NVMCON & NVMCON_WR); /* wait on write to finish */ NVMCON = 0x4001; /* set WREN and Word Programing mode */ NVMADDR = EXCEPTION_ADDR; /* PM Address at which we'll store the */ /* exception address register */ NVMDATA = _excep_addr; /* wait at least 6 us for LVD start-up assume we're running at max frequency (80 MHz) so we're always safe */ { while (_CP0_GET_COUNT() - t0 < (80/2)*6); } NVMKEY = 0xAA996655; NVMKEY = 0x556699AA; /* unlock sequence */ NVMCONSET = NVMCON_WR; while(NVMCON & NVMCON_WR); /* Write the exception cause and address to the part can be read and the cause determined. */ NVMWriteWord((void*)EXCEPTION_CAUSE, _excep_code); NVMWriteWord((void*)EXCEPTION_ADDR, _excep_addr); #endif while (1) { /* Examine _excep_code to identify the type of exception */ /* Examine _excep_addr to find the address that caused the exception */ } }