void display_init() { i2c_master_setup(); // goes through the reset procedure display_command(0xAE); // turn off display display_command(0xA8); // set the multiplex ratio (how many rows are updated per oled driver clock) to the number of rows in the display display_command(HEIGHT-1); // the multiplex ratio set is the value sent + 1, so subtract 1 // we will always write the full display on a single update. display_command(0x20); // set address mode display_command(0x00); // horizontal address mode display_command(0x21); // set column address display_command(0x00); // start at 0 display_command(0xFF); // end at 127 // with this address mode, the address will go through all the pixels and then return to the start, // hence we never need to set the address again display_command(0x8d); // charge pump display_command(0x14); // turn charge pump on (creates the ~7 Volts needed to light pixels) display_command(0xAF); // turn on the display video_buffer[0] = 0x40; // co = 0, dc =1, allows us to send data directly from video buffer, //0x40 is the "next bytes have data" byte }
void i2c_init(void){ ANSELBbits.ANSB2 = 0; ANSELBbits.ANSB3 = 0; i2c_master_setup(); }
int main() { __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; __builtin_enable_interrupts(); initSPI1(); initI2C2(); i2c_master_setup(); initExpander(); //sine wave int sine[1000]; int i; for(i = 0; i < 1000; i++){ sine[i] = 128 + 127*sin(2*3.14*10*i/1000); } int triangle[1000]; i = 0; for(i = 0; i < 1000; i++){ triangle[i] = .256*i; } i = 0; while(1) { _CP0_SET_COUNT(0); if(_CP0_GET_COUNT() > 24000){ i++; setVoltage(0, sine[i]); setVoltage(1, triangle[i]); _CP0_SET_COUNT(0); } if(i > 1000){ i = 0; } char status = getExpander(); //read the expander char g7 = (status & 0x80) >> 7; //get level of pin g7 setExpander(0, g7); //set pin 0 to level of g7 } }
void main() { __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 = 0xFFCF; TRISB = 0b0001111001110011; i2c_master_setup(); ANSELBbits.ANSB2 = 0; //SDA2 set to digital ANSELBbits.ANSB3 = 0; //SCL2 set to digital __builtin_enable_interrupts(); // SYSTEMConfigPerformance(48000000); SPI1_init(); LCD_init(); LCD_clearScreen(0); // RPB13Rbits.RPB13R = 0b0011; //SDO // SDI1Rbits.SDI1R = 0b0000; //A1 RPB7Rbits.RPB7R = 0b0101; //OC1 RPB8Rbits.RPB8R = 0b0101; //OC2 PORTAbits.RA4 = 1; //led init T2CONbits.TCKPS = 2; //timer 2 prescale = 1:4 PR2 = 1999; //period = (PR2+1) * N * 12.5 ns = 100 us, 10 kHz TMR2 = 0; OC1RS = 1000; OC1R = 1000; OC2RS = 1000; OC2R = 1000; OC1CONbits.OCTSEL = 0; //select timer2 OC2CONbits.OCTSEL = 0; OC1CONbits.OCM = 0b110; //set pwm mode OC2CONbits.OCM = 0b110; T2CONbits.ON = 1; OC1CONbits.ON = 1; OC2CONbits.ON = 1; unsigned char x = 0; //sine counter unsigned char y = 0; //triangle counter char pressed = 0; //for tracking button logic char counter = 0; char m = 100; //(triangle wave frequency is 1000/2m) unsigned char voltage = 0; unsigned char channel = 0; int bytes = 14; unsigned char i2cdata[bytes]; unsigned char i2cdatatest; unsigned char i2cwhoami; short temp = 0; short accel_x = 0; short accel_y = 0; short accel_z = 0; short gyro_x = 0; short gyro_y = 0; short gyro_z = 0; char i2cdatacount = 0; char textbuffer[20]; char length = 0; i2cwhoami = i2c_master_read(GYRO,WHOAMI,0,0); i2c_master_write(GYRO,CTRL1_XL,0b10000000,0); i2c_master_write(GYRO,CTRL2_G,0b10000000,0); i2c_master_write(GYRO,CTRL3_C,0b00000100); //i2cdatatest = i2c_master_read(GYRO,CTRL1_XL,0,0); // i2c_master_write(GYRO,CTRL1_XL,0b10000000,1); // i2c_master_send(0b10000000); // i2c_master_send(0b00000100); // i2c_master_stop(); CS = 1; int leet = 1337; sprintf(textbuffer,"Hello world %d!",leet); char text[2] = {'H','6'}; //int text[5] = {40,30,50,30,20}; LCD_clearScreen(0); while(1) { length = sizeof(textbuffer);//size must be taken here otherwise pointer size is taken instead of LCD_type(28,32,textbuffer,length,0b1111100000000000); //LCD_char(28,32,30,0b1111100000000000); i2c_master_multiread(GYRO,OUT_TEMP_L,bytes,i2cdata); // //i2cdatatest = i2c_master_read(GYRO,0x28,0,0); temp = i2cdata[1]; temp = (temp<<8)|i2cdata[0]; temp = (unsigned short)temp; gyro_x = i2cdata[3]; gyro_x = (gyro_x<<8)|i2cdata[2]; gyro_x = (unsigned short)gyro_x; gyro_y = i2cdata[5]; gyro_y = (gyro_y<<8)|i2cdata[4]; gyro_y = (unsigned short)gyro_y; gyro_z = i2cdata[7]; gyro_z = (gyro_z<<8)|i2cdata[6]; gyro_z = (unsigned short)gyro_z; accel_x = i2cdata[9]; accel_x = (i2cdata[9]<<8)|i2cdata[8]; accel_x = (unsigned short)accel_x; accel_y = i2cdata[11]; accel_y = (i2cdata[11]<<8)|i2cdata[10]; accel_y = (unsigned short)accel_y; accel_z = i2cdata[13]; accel_z = (accel_z<<8)|i2cdata[12]; accel_z = (unsigned short)accel_z; OC1RS = floor((accel_x/16.768)); OC2RS = floor((accel_y/16.768)); if(OC1RS>2999){ OC1RS = 2000; } else if(OC1RS<1000){ OC1RS = 0; } else{ OC1RS = OC1RS - 1000; } if(OC2RS>2999){ OC2RS = 2000; } else if(OC2RS<1000){ OC2RS = 0; } else{ OC2RS = OC2RS - 1000; } // OC1R = floor((gyro_x/3.2768 + 10000)); // OC2R = floor((gyro_y/3.2768 + 10000)); //-------------SPI debugging for IMU------------------ // if (!PORTBbits.RB4){ // CS = 0; // channel = counter; // //voltage = floor(100*sin((x*2*pi)/100)+100); // voltage = i2cwhoami; // //char voltage = 0b10101001; // spi1_set(channel,voltage); // //delay(6000); // CS = 1; // } // else { // CS = 0; // channel = counter; // //voltage = floor(100*sin((x*2*pi)/100)+100); // voltage = i2cdata[0]; // //char voltage = 0b10101001; // spi1_set(channel,voltage); // //delay(6000); // CS = 1; // //++i2cdatacount %14; // } //------------------------------------------------------- delay(960000); } }
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 } }
// Initialize I2C2 void initI2C2() { // turn off analog on I2C pins ANSELBbits.ANSB2 = 0; ANSELBbits.ANSB3 = 0; i2c_master_setup(); }
void initI2C() { ANSELBbits.ANSB2 = 0; // turn off SDA2 pin's analog functionality ANSELBbits.ANSB3 = 0; // turn off SCL2 pin's analog functionality i2c_master_setup(); }
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; } }