void NU32_DisableCache(void) { // Set the CP0 CONFIG register to make kseg0 uncacheable // (See NU32_EnableCache()) __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210582); }
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; } } } }
void main(){ // *************** Initialize pins and chips **************** //SYSTEMConfigPerformance(48000000); __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 TRISA = 0xFFEF; // set pin 4 as output TRISB = 0b0001111001110011; // set outputs/inputs // turn off analog pins ANSELBbits.ANSB2 = 0; ANSELBbits.ANSB3 = 0; // setup i2c i2c_master_setup(); __builtin_enable_interrupts(); // setup accelerometer i2c_write(ADDR, 0x10, 0b10000000); i2c_write(ADDR, 0x11, 0b10000000); i2c_write(ADDR, 0x12, 0b00000100); // initialize PWM RPB7Rbits.RPB7R = 0b0101; //OC1 on pin 16 RPB8Rbits.RPB8R = 0b0101; //OC2 on pin 17 T2CONbits.TCKPS = 4; // Timer2 prescaler N=16 (1:16) PR2 = 4999; // period = (PR2+1) * N * 12.5 ns = 1 ms, 1 kHz TMR2 = 0; // initial TMR2 count is 0 OC1RS = 2500; // duty cycle = OC1RS/(PR2+1) = 50% OC1R = 2500; // initialize before turning OC1 on; afterward it is read-only OC2RS = 2500; OC2R = 2500; OC1CONbits.OCTSEL = 0; // select timer2 OC2CONbits.OCTSEL = 0; OC1CONbits.OCM = 0b110; // PWM mode without fault pin; other OC1CON bits are defaults OC2CONbits.OCM = 0b110; T2CONbits.ON = 1; // turn on Timer2 OC1CONbits.ON = 1; // turn on OC1 OC2CONbits.ON = 1; // RPB13Rbits.RPB13R = 0b0011; //SDO // CS = 1; // initialize SPI SPI_init(); LCD_init(); LCD_clearScreen(RED); //*********************************************************** //****************** constants *************************** char length = 14; unsigned char IMUdata[length]; short temp; unsigned short temperature; short gx; short gy; short gz; unsigned short gyrox; unsigned short gyroy; unsigned short gyroz; short ax; short ay; short az; unsigned short accelx; unsigned short accely; unsigned short accelz; char text[100]; //********************************************************** while(1){ int var = 1337; sprintf(text,"Hello World %d!",var); LCD_print(28,32,text,WHITE); //************ TEST IF IMU WORKS *********************** // unsigned char data = 0; // data = i2c_read(ADDR,0x0F); // // // read output from SPI // CS=0; // setVoltage(0,data); // CS=1; //************* SHOULD GET 01101001 ******************* //********** READ MULTI REGISTERS FROM IMU *************** // i2c_multiread(ADDR,0x20,IMUdata,length); // // temp = (IMUdata[1]<<8) | IMUdata[0]; // temperature = temp + 32768; // // gx = (IMUdata[3]<<8) | IMUdata[2]; // gyrox = gx + 32768; // // gy = (IMUdata[5]<<8) | IMUdata[4]; // gyroy = gy + 32768; // // gz = (IMUdata[7]<<8) | IMUdata[6]; // gyroz = gz + 32768; // // ax = (IMUdata[9]<<8) | IMUdata[8]; // accelx = ax + 32768; // // ay = (IMUdata[11]<<8) | IMUdata[10]; // accely = ay + 32768; // // az = (IMUdata[13]<<8) | IMUdata[12]; // accelz = az + 32768; // // //******************************************************** // // // // //*********************** PWM **************************** // // Calculate OC1RS and OC2RS // OC1RS = floor(accelx/6.5535); // OC2RS = floor(accely/6.5535); // // // Set OC1 limits // if (OC1RS > 7500){ // OC1RS = 5000; // } // else if (OC1RS < 2500){ // OC1RS = 0; // } // else { // OC1RS = OC1RS-2500; // } // // // Set OC2 limits // if (OC2RS > 7500){ // OC2RS = 5000; // } // else if (OC2RS < 2500){ // OC2RS = 0; // } // else { // OC2RS = OC2RS-2500; // } //****************************************************** delay(24000); // Delay 1ms } }
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 ( 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() { // 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; TRISBbits.TRISB13 = 1; INT4Rbits.INT4R = 0b0100; // set up LED1 pin as a digital output ANSELBbits.ANSB15 = 0; TRISBbits.TRISB15 = 0; LATBbits.LATB15 = 1; int d = 0; while(d < 2000000){ d = d+1; } // initializing variables // lookup table for all of the ascii characters static const char ASCII[96][5] = { {0x00, 0x00, 0x00, 0x00, 0x00} // 20 (space) ,{0x00, 0x00, 0x5f, 0x00, 0x00} // 21 ! ,{0x00, 0x07, 0x00, 0x07, 0x00} // 22 " ,{0x14, 0x7f, 0x14, 0x7f, 0x14} // 23 # ,{0x24, 0x2a, 0x7f, 0x2a, 0x12} // 24 $ ,{0x23, 0x13, 0x08, 0x64, 0x62} // 25 % ,{0x36, 0x49, 0x55, 0x22, 0x50} // 26 & ,{0x00, 0x05, 0x03, 0x00, 0x00} // 27 ' ,{0x00, 0x1c, 0x22, 0x41, 0x00} // 28 ( ,{0x00, 0x41, 0x22, 0x1c, 0x00} // 29 ) ,{0x14, 0x08, 0x3e, 0x08, 0x14} // 2a * ,{0x08, 0x08, 0x3e, 0x08, 0x08} // 2b + ,{0x00, 0x50, 0x30, 0x00, 0x00} // 2c , ,{0x08, 0x08, 0x08, 0x08, 0x08} // 2d - ,{0x00, 0x60, 0x60, 0x00, 0x00} // 2e . ,{0x20, 0x10, 0x08, 0x04, 0x02} // 2f / ,{0x3e, 0x51, 0x49, 0x45, 0x3e} // 30 0 ,{0x00, 0x42, 0x7f, 0x40, 0x00} // 31 1 ,{0x42, 0x61, 0x51, 0x49, 0x46} // 32 2 ,{0x21, 0x41, 0x45, 0x4b, 0x31} // 33 3 ,{0x18, 0x14, 0x12, 0x7f, 0x10} // 34 4 ,{0x27, 0x45, 0x45, 0x45, 0x39} // 35 5 ,{0x3c, 0x4a, 0x49, 0x49, 0x30} // 36 6 ,{0x01, 0x71, 0x09, 0x05, 0x03} // 37 7 ,{0x36, 0x49, 0x49, 0x49, 0x36} // 38 8 ,{0x06, 0x49, 0x49, 0x29, 0x1e} // 39 9 ,{0x00, 0x36, 0x36, 0x00, 0x00} // 3a : ,{0x00, 0x56, 0x36, 0x00, 0x00} // 3b ; ,{0x08, 0x14, 0x22, 0x41, 0x00} // 3c < ,{0x14, 0x14, 0x14, 0x14, 0x14} // 3d = ,{0x00, 0x41, 0x22, 0x14, 0x08} // 3e > ,{0x02, 0x01, 0x51, 0x09, 0x06} // 3f ? ,{0x32, 0x49, 0x79, 0x41, 0x3e} // 40 @ ,{0x7e, 0x11, 0x11, 0x11, 0x7e} // 41 A ,{0x7f, 0x49, 0x49, 0x49, 0x36} // 42 B ,{0x3e, 0x41, 0x41, 0x41, 0x22} // 43 C ,{0x7f, 0x41, 0x41, 0x22, 0x1c} // 44 D ,{0x7f, 0x49, 0x49, 0x49, 0x41} // 45 E ,{0x7f, 0x09, 0x09, 0x09, 0x01} // 46 F ,{0x3e, 0x41, 0x49, 0x49, 0x7a} // 47 G ,{0x7f, 0x08, 0x08, 0x08, 0x7f} // 48 H ,{0x00, 0x41, 0x7f, 0x41, 0x00} // 49 I ,{0x20, 0x40, 0x41, 0x3f, 0x01} // 4a J ,{0x7f, 0x08, 0x14, 0x22, 0x41} // 4b K ,{0x7f, 0x40, 0x40, 0x40, 0x40} // 4c L ,{0x7f, 0x02, 0x0c, 0x02, 0x7f} // 4d M ,{0x7f, 0x04, 0x08, 0x10, 0x7f} // 4e N ,{0x3e, 0x41, 0x41, 0x41, 0x3e} // 4f O ,{0x7f, 0x09, 0x09, 0x09, 0x06} // 50 P ,{0x3e, 0x41, 0x51, 0x21, 0x5e} // 51 Q ,{0x7f, 0x09, 0x19, 0x29, 0x46} // 52 R ,{0x46, 0x49, 0x49, 0x49, 0x31} // 53 S ,{0x01, 0x01, 0x7f, 0x01, 0x01} // 54 T ,{0x3f, 0x40, 0x40, 0x40, 0x3f} // 55 U ,{0x1f, 0x20, 0x40, 0x20, 0x1f} // 56 V ,{0x3f, 0x40, 0x38, 0x40, 0x3f} // 57 W ,{0x63, 0x14, 0x08, 0x14, 0x63} // 58 X ,{0x07, 0x08, 0x70, 0x08, 0x07} // 59 Y ,{0x61, 0x51, 0x49, 0x45, 0x43} // 5a Z ,{0x00, 0x7f, 0x41, 0x41, 0x00} // 5b [ ,{0x02, 0x04, 0x08, 0x10, 0x20} // 5c ¥ ,{0x00, 0x41, 0x41, 0x7f, 0x00} // 5d ] ,{0x04, 0x02, 0x01, 0x02, 0x04} // 5e ^ ,{0x40, 0x40, 0x40, 0x40, 0x40} // 5f _ ,{0x00, 0x01, 0x02, 0x04, 0x00} // 60 ` ,{0x20, 0x54, 0x54, 0x54, 0x78} // 61 a ,{0x7f, 0x48, 0x44, 0x44, 0x38} // 62 b ,{0x38, 0x44, 0x44, 0x44, 0x20} // 63 c ,{0x38, 0x44, 0x44, 0x48, 0x7f} // 64 d ,{0x38, 0x54, 0x54, 0x54, 0x18} // 65 e ,{0x08, 0x7e, 0x09, 0x01, 0x02} // 66 f ,{0x0c, 0x52, 0x52, 0x52, 0x3e} // 67 g ,{0x7f, 0x08, 0x04, 0x04, 0x78} // 68 h ,{0x00, 0x44, 0x7d, 0x40, 0x00} // 69 i ,{0x20, 0x40, 0x44, 0x3d, 0x00} // 6a j ,{0x7f, 0x10, 0x28, 0x44, 0x00} // 6b k ,{0x00, 0x41, 0x7f, 0x40, 0x00} // 6c l ,{0x7c, 0x04, 0x18, 0x04, 0x78} // 6d m ,{0x7c, 0x08, 0x04, 0x04, 0x78} // 6e n ,{0x38, 0x44, 0x44, 0x44, 0x38} // 6f o ,{0x7c, 0x14, 0x14, 0x14, 0x08} // 70 p ,{0x08, 0x14, 0x14, 0x18, 0x7c} // 71 q ,{0x7c, 0x08, 0x04, 0x04, 0x08} // 72 r ,{0x48, 0x54, 0x54, 0x54, 0x20} // 73 s ,{0x04, 0x3f, 0x44, 0x40, 0x20} // 74 t ,{0x3c, 0x40, 0x40, 0x20, 0x7c} // 75 u ,{0x1c, 0x20, 0x40, 0x20, 0x1c} // 76 v ,{0x3c, 0x40, 0x30, 0x40, 0x3c} // 77 w ,{0x44, 0x28, 0x10, 0x28, 0x44} // 78 x ,{0x0c, 0x50, 0x50, 0x50, 0x3c} // 79 y ,{0x44, 0x64, 0x54, 0x4c, 0x44} // 7a z ,{0x00, 0x08, 0x36, 0x41, 0x00} // 7b { ,{0x00, 0x00, 0x7f, 0x00, 0x00} // 7c | ,{0x00, 0x41, 0x36, 0x08, 0x00} // 7d } ,{0x10, 0x08, 0x08, 0x10, 0x08} // 7e ? ,{0x00, 0x06, 0x09, 0x09, 0x06} // 7f ? }; // end char ASCII[96][5] // Initializing Display i2c_master_setup(); display_init(); char a; char message[10]; int m = 0; int i; int j; int row; int col; // main loop while (1) { row = 32; col = 28; sprintf(message,"Hello world 1337!"); while(message[m]){ for(j = 0; j<= 4; j = j+1,col = col + 1){ a = ASCII[message[m] - 0x20][j]; for(i = 0; i <= 7; i = i+1) { display_pixel_set(row -i,col,(a << i)&0x80); } } m = m+1; } display_draw(); m = 0; } }
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; } } } }
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 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(); }
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 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 } }
void PIC32startup(void){ //set up DEVCFGs // 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 by turning sosc off #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 = PS1048576 // 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 FPLLODIV = DIV_2 // divide clock after FPLLMUL to get 40MHz #pragma config UPLLIDIV = DIV_2 // divide 8MHz input clock, then multiply by 12 to get 48MHz for USB #pragma config UPLLEN = ON // USB clock on // DEVCFG3 #pragma config USERID = 12345 // some 16bit userid, doesn't matter what #pragma config PMDL1WAY = ON // allow only one reconfiguration #pragma config IOL1WAY = ON // allow only one reconfiguration #pragma config FUSBIDIO = ON // USB pins controlled by USB module #pragma config FVBUSONIO = ON // controlled by USB module __builtin_disable_interrupts(); //beginning of startup __builtin_mtc0(_CP0_CONFIG, _CP0_CONFIG_SELECT, 0xa4210583); BMXCONbits.BMXWSDRM=0x0; INTCONbits.MVEC=0x1; DDPCONbits.JTAGEN=0; __builtin_enable_interrupts(); //end of startup ANSELBbits.ANSB13=0; //sets up USER pin as input U1RXRbits.U1RXR=0b0011; TRISBbits.TRISB4=0; //sets up B7 as digital output //RPB3Rbits.RPB3R=0b0001; //sets up LED1 pin as a digital output //TRISBbits.TRISB3=0; ANSELBbits.ANSB15=0; //sets up LED2 pin as digital PWM output //TRISBbits.TRISB4=0; RPB7Rbits.RPB7R=0b0101; RPB3Rbits.RPB3R=0b0101; OC1CONbits.OCM=0b110; //enable OC1 OC1CONbits.OCTSEL=0; OC1RS=0; OC1R=0; OC1CONbits.ON=1; OC2CONbits.OCM=0b110; //enable OC2 OC2CONbits.OCTSEL=0; OC2RS=0; OC2R=0; OC2CONbits.ON=1; PR2=3125-1; //sets up timer 2 to run at 50Hz TMR2=0; T2CONbits.TCKPS=0b111; T2CONbits.TGATE=0; T2CONbits.TCS=0; T2CONbits.ON=1; ANSELAbits.ANSA0 = 1; //sets up A0 as AN0 AD1CON3bits.ADCS = 3; AD1CHSbits.CH0SA = 0; AD1CON1bits.ADON = 1; }
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(); // set up USER pin as input ANSELBbits.ANSB13 = 0; TRISBbits.TRISB13 = 1; INT4Rbits.INT4R = 0b0100; // set up LED1 pin as a digital output ANSELBbits.ANSB14 = 0; TRISBbits.TRISB14 = 0; //RPB14Rbits.RPB14R = 0b0001; LATBbits.LATB14 = 1; // set up LED2 as OC1 using Timer2 at 1kHz ANSELBbits.ANSB15 = 0; TRISBbits.TRISB15 = 0; RPB15Rbits.RPB15R = 0b0101; T2CONbits.T32 = 0; T2CONbits.TCS = 0; T2CONbits.TGATE = 0; T2CONbits.TCKPS = 0b100; PR2 = 4999; TMR2 = 0; T2CONbits.ON = 1; OC1CONbits.OCM = 0b110; OC1RS = 2500; // 50% duty cycle OC1R = 2500; OC1CONbits.ON = 1; // set up A0 as AN0 ANSELAbits.ANSA0 = 1; AD1CON3bits.ADCS = 3; AD1CHSbits.CH0SA = 0; AD1CON1bits.ADON = 1; while (1) { _CP0_SET_COUNT(0); // set core timer to 0, remember it counts at half the CPU clock LATBINV = 0b100000000000000; // 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*(5000/1024); if (PORTBbits.RB13 == 1) { // nothing } else { LATBINV = 1<<14; } } } }
void APP_Initialize ( void ) { /* Device Layer Handle */ appData.deviceHandle = USB_DEVICE_HANDLE_INVALID; /* USART Driver Handle */ appData.usartHandle = DRV_HANDLE_INVALID; /* CDC Instance index for this app object00*/ appData.cdcInstance = USB_DEVICE_CDC_INDEX_0; /* app state */ appData.state = APP_STATE_INIT ; /* device configured status */ appData.isConfigured = false; /* Initial get line coding state */ appData.getLineCodingData.dwDTERate = 9600; appData.getLineCodingData.bDataBits = 8; appData.getLineCodingData.bParityType = 0; appData.getLineCodingData.bCharFormat = 0; /* Read Transfer Handle */ appData.readTransferHandle = USB_DEVICE_CDC_TRANSFER_HANDLE_INVALID; /* Write Transfer Handle */ appData.writeTransferHandle = USB_DEVICE_CDC_TRANSFER_HANDLE_INVALID; /* Intialize the read complete flag */ appData.isReadComplete = true; /*Initialize the write complete flag*/ appData.isWriteComplete = true; /*Initialize the buffer pointers */ appData.readBuffer = &readBuffer[0]; appData.uartReceivedData = &uartReceivedData; __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; TRISA = 0xFFCF; TRISB = 0b0001111001110011; __builtin_enable_interrupts(); RPB7Rbits.RPB7R = 0b0101; //OC1 RPB8Rbits.RPB8R = 0b0101; //OC2 RPA4Rbits.RPA4R = 0b0101; //OC4 T2CONbits.TCKPS = 2; //timer 2 prescale = 1:4 PR2 = 2999;//1999; //period = (PR2+1) * N * 12.5 ns = 100 us, 10 kHz TMR2 = 0; OC1RS = 1000; OC1R = 1000; OC2RS = 1000; OC2R = 1000; OC4RS = 1000; OC4R = 1000; OC1CONbits.OCTSEL = 0; //select timer2 OC2CONbits.OCTSEL = 0; OC4CONbits.OCTSEL = 0; OC1CONbits.OCM = 0b110; //set pwm mode OC2CONbits.OCM = 0b110; OC4CONbits.OCM = 0b110; T2CONbits.ON = 1; OC1CONbits.ON = 1; OC2CONbits.ON = 1; OC4CONbits.ON = 1; PORTBbits.RB15 = 1; //led init PORTBbits.RB14 = 1; //led init //PORTAbits.RA4 = 1; }
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(); // set up USER pin as input ANSELBbits.ANSB13 = 0; TRISBbits.TRISB13 = 1; // set up LED1 pin as a digital output TRISBbits.TRISB7 = 0; LATBbits.LATB7 = 1; // set up LED2 as OC1 using Timer2 at 1kHz ANSELBbits.ANSB15 = 0; RPB15Rbits.RPB15R = 0b0101; // set up A0 as AN0 ANSELAbits.ANSA0 = 1; AD1CON3bits.ADCS = 3; AD1CHSbits.CH0SA = 0; AD1CON1bits.ADON = 1; //pwm T2CONbits.TCKPS = 1; // Timer2 prescaler N=2 PR2 = 19999; // period = (PR2+1) * N * 25 ns => 1000 Hz TMR2 = 0; // initial TMR4 count is 0 //OC1 OC1CONbits.OCM = 0b110; // PWM mode without fault pin; other OC1CON bits are defaults OC1CONbits.OCTSEL = 0; // set timer select bit to 0 for using Timer2 OC1RS = 20000; // duty cycle = OC1RS/(PR3+1) = 75% // OC1R = 19999; // initialize before turning OC1 on; afterward it is read-only T2CONbits.ON = 1; //Turn Timer4 on OC1CONbits.ON = 1; // turn on OC1 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 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 * 20000/1023; if (PORTBbits.RB13 == 1) { // nothing //EVERYTHING } else { LATBINV = 0b10000000; } } } }
int main ( void ) { /* Initialize all MPLAB Harmony modules, including application(s). */ SYS_Initialize ( NULL ); //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(); volatile short accels[3]; // accelerations for the 3 axes volatile short mags[3]; // magnetometer readings for the 3 axes volatile short temp; // Display display_init(); while (1){ // invert pin every 0.5s, set PWM duty cycle % to the pot voltage output % SYS_Tasks ( ); /* 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); } } 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(); * */ } /* Execution should not come here during normal operation */ return ( EXIT_FAILURE ); }