/*void textSpeed(void) { while (1) { _CP0_SET_COUNT(0); // set core timer to 0, remember it counts at half the CPU clock LATBINV = some_pin; // invert a pin // wait for half a second, setting LED brightness to pot angle while waiting while (_CP0_GET_COUNT() < 10000000) { val = readADC(); OC1RS = val * normalize_to_pr; if (push_button_pin == 1) { // nothing } else { LATBINV = some_pin; } } } }*/ void drawChar(int tableIndex, int xPos, int yPos){ //int check; int i; int j; char number; int check1; int check2; check1 = checkPos(xPos,yPos); //see if this position works check2 = checkPos(xPos+4,yPos+7); if(check1&&check2){ for(i=0;i<5;i++){ number = ASCII[tableIndex][i]; for(j=0;j<=7;j++){ //if((((int)number)&(1<<j))>>j){ if(((int)number>>j)&1==1){ display_pixel_set(yPos+j,xPos+i,1); } else{ display_pixel_set(yPos+j,xPos+i,0); } } } }
void accel_pixels(int accelpixels,char axis){ int i,j; if (axis == 'x'){ if (accelpixels > 0){ for (i=0;i<=accelpixels;i++){ for (j=0;j<6;j++){ display_pixel_set(29+j,64+i,1); } } } else if (accelpixels < 0){ for (i=0;i<=-accelpixels;i++){ for (j=0;j<6;j++){ display_pixel_set(29+j,63-i,1); } } } } if (axis == 'y'){ if (accelpixels > 0){ for (i=0;i<=accelpixels;i++){ for (j=0;j<6;j++){ display_pixel_set(32+i,61+j,1); } } } else if (accelpixels < 0){ for (i=0;i<=-accelpixels;i++){ for (j=0;j<6;j++){ display_pixel_set(31-i,61+j,1); } } } } }
void display_message(char input) { char value; int i, m; for (m = 0; m< 5; ++m) { int temp; char binary[8]; //initialize binary to be 0b00000000 value = read_ascii(input,m); //retrieve the hex number from the ascii table //hex[m]= value; //assign the read value to a temp hex number holder for (i=0;i<8;++i) { temp = value; temp = temp >> 7-i; temp = hextobinary(temp); binary[7-i]=temp; } for (i = 0; i < 8; ++i) { display_pixel_set(start_position[0]+i,start_position[1]+m,binary[i]); } } }
void display_sprite_put_P(coord_t x, coord_t y, coord_t w, coord_t h, color_t c, const uint8_t *sprite) { if(!h) { return; } do { const coord_t dh = ((h-1) & 007)+1; for(coord_t dx = 0; dx < w; dx++, sprite++) { color_t dc = pgm_read_byte(sprite); for(coord_t dy = 0; dy < dh; dy++) { if(dc & 1) { display_pixel_set(x + dx, y + dy, c); } dc >>= 1; } } y += 8; h = (h >= 8) ? h - 8 : 0; } while(h); }
void display_text(char *message, int row_cur, int col_cur){ int i = 0, j, k; while(message[i]){ //reads through characters in message int ch_row = ascii_row(message[i]); //gets row of character in ascii table //checking if pixel is out of bounds if(col_cur > 125){ col_cur = 0; row_cur = row_cur + 8; } if(row_cur > 56){ row_cur = 0; col_cur = 0; } int row = row_cur; int col = col_cur; //loops through columns and rows of each charcter pixel values for(j = 0; j < 5; j++){ for(k = 0; k < 8; k++){ int ch_hex = ASCII[ch_row][j]; //gets the pixel values from each column int ch_bin = (ch_hex & (1 << k)) >> k; //bitmask the value row = row_cur + k; col = col_cur + j; display_pixel_set(row, col, ch_bin); //set the pixel value }; }; i++; col_cur = col_cur + 5; }; display_draw(); //draws the image }
void display_message(int row, int col, char *message){ int i = 0; int k; int j; int new_row = row; int column_ctr = 0; while(message[i]){ char askey_index = message[i] - 0x20; for (k=0; k<5; k++){ char askey_col = ASCII[askey_index][k]; for(j = 7; j>=0; j--){ display_pixel_set(new_row, (column_ctr + col), (askey_col&(1<<j))); new_row = new_row - 1; } new_row = row; column_ctr++; } i++; } display_draw(); }
void display_write(int x_start, int y_start, char buffer1[]){ int ASCII_string_row_index = 0; int ascii_byte; int rowindex; int columnindex; int x = x_start; int y = y_start; int number_of_rows = 1; while(buffer1[ASCII_string_row_index]){ for(columnindex = 0; columnindex < 5; columnindex++){ ascii_byte = ASCII[buffer1[ASCII_string_row_index]-0x20][columnindex]; // Extract desired byte from ASCII y = y_start; for(rowindex = 0; rowindex < 8; rowindex++){ if(x < 128 && y < 64){ display_pixel_set(y,x,ascii_byte&1); // Isolate Least Significant Bit ascii_byte = ascii_byte>>1; // Shift bits to the right by 1 } y++; if(rowindex == 7){ y = y_start; // When y = 8, set y = y_start } } x++; if(x>127){ // Start a new row when x limit is reached y_start = y_start+8; x = x_start; number_of_rows++; } } ASCII_string_row_index++; }
void display_arrow(int x_val, int y_val){ display_clear(); //starts at center of board int row = 32; int col = 64; // scales the values to draw a line proportional to the values from accelerometer and still be able to fit on the screen int x_draw, y_draw; x_draw = x_val*SCALE; y_draw = y_val*SCALE; //determines direction to draw the line - up, down, left, or right int x_direction = 1, y_direction = 1; if (x_draw > 0){ x_direction = -1; }; if (y_draw > 0){ y_direction = -1; }; //take absolute value so that value will get smaller in while loop x_draw = abs(x_draw); y_draw = abs(y_draw); //will draw a line of pixels at a time, to a length proportional to the value from accelerometer int w; while(x_draw > 0){ row = row + 1*x_direction; //multiply by x_direction for line to be in the right direction for(w = 0; w < 5; w++){ // draws a thin line 5 pixels wide display_pixel_set(row, col+w, 1); //set the pixel value }; x_draw = x_draw - 1; } //set the row back to the center of screen row = 32; int h; while(y_draw > 0){ col = col + 1*y_direction; for(h = 0; h < 5; h++){ display_pixel_set(row + h, col, 1); //set the pixel value }; y_draw = y_draw - 1; } display_draw(); //draws the image }
void display_ggraph (float x, float y) { int lengthx, lengthy,i,m; lengthx = x*20; lengthy = y*20; for (i = -2; i<3; ++i) //set center position always be lit up { for (m = -2; m<3; ++m) {display_pixel_set(center_position[0]+i,center_position[1]+m,1);} } for(i = -2; i<3; ++i) { if (lengthx > 0) { for(m = 0; m<=lengthx; ++m) { display_pixel_set(center_position[0]+i,center_position[1]+1+m,1); } } if(lengthx <= 0) { for(m = 0; m>=lengthx; --m) { display_pixel_set(center_position[0]+i,center_position[1]-1+m,1); } } if (lengthy > 0) { for(m = 0; m<=lengthy; ++m) { display_pixel_set(center_position[0]+1+m,center_position[1]+i,1); } } if (lengthy <= 0) { for(m = 0; m>=lengthy; --m) { display_pixel_set(center_position[0]-1+m,center_position[1]+i,1); } } } }
void display_reset (void) { int m,n; for (m=0;m<64;++m) { for (n=0;n<128;++n) { display_pixel_set(m,n,0); } } }
void display_bars(int len, int dir){ //0 for x dir, 1 for y dir int i; if (dir == 0){ if (len > 0){ if (len > 60){ len = 60; } for(i = 0; i < abs(len); i++){ display_pixel_set(32, (64+i), 1); } } else if (len < 0){ if (len < -60){ len = -60; } for(i = 0; i < abs(len); i++){ display_pixel_set(32, (64-i), 1); } } } if(dir == 1){ if (len > 0){ if (len > 25){ len = 25; } for(i = 0; i < abs(len); i++){ display_pixel_set((32+i), 64, 1); } } else if (len < 0){ if (len < -25){ len = -25; } for(i = 0; i < abs(len); i++){ display_pixel_set((32-i), 64, 1); } } } display_draw(); }
void display_write_byte_column(char col_byte) // function to write a column byte { int num = (int)col_byte; // transform byte into a DEC int bin[8] = {0,0,0,0,0,0,0,0}; // array for the BIN number getBin8(num, bin); invertBin8(bin); int i; for(i = 0; i<8; i++) { display_pixel_set(row_current+i,column_current,bin[i]); } column_current++; }
void heart() { display_buffer_write_set(0); display_clear_black(); display_buffer_active_set(0); display_buffer_write_set(1); for(coord_t x = 0; x < DISPLAY_WIDTH; x++) { for(coord_t y = 0; y < DISPLAY_HEIGHT; y++) { display_pixel_set(x, y, display_color_from_rgb( 128 + 127 * y / (DISPLAY_HEIGHT - 1), 255 - 255 * (DISPLAY_HEIGHT - y - 1) / (DISPLAY_HEIGHT - 1), 128 - 128 * (DISPLAY_WIDTH - x - 1) / (DISPLAY_WIDTH - 1) ) ); } } time_sync(); for (uint16_t counter = 256; counter; --counter) { display_buffer_write_set(0); display_buffer_copy(1, 0); coord_t p = counter % DISPLAY_WIDTH; display_sprite_put_P(p, 0, 9, 8, display_color_from_rgb( 255, 0, 128 ), heart_data); if( DISPLAY_WIDTH - p < 9 ) { display_sprite_put_P(p - DISPLAY_WIDTH, 0, 9, 8, display_color_from_rgb( 255, 0, 128 ), heart_data); } if(counter & 1) { for(coord_t y = 0; y < DISPLAY_HEIGHT; y++) { color_t tmp = display[1][0][y]; for(coord_t x = 0; x < DISPLAY_WIDTH - 1; x++) { display[1][x][y] = display[1][x + 1][y]; } display[1][DISPLAY_WIDTH - 1][y] = tmp; } } delay_ms(50); } }
void oled_write(int currentr, int currentc, char *message){ //Initialize the iteration variables int i,j,k; i = 0; //Go through each character of the message until the message is over while(message[i]){ //Find the row of hex values associated with a certain character int hex_array = get_hex_array(message[i]); //Check if the value where you're trying to write is out of bounds in terms of either the rows or columns //Add 5 to the current column value, because passing this check will make sure that the message will fit in the bounds of the display if(currentc+5 > 128){ //reset column value for carriage return currentc = 0; //move down one row currentr = currentr + 8; } //Add 8 to the current row value, because passing this check will make sure that the message will fit in the bounds of the display if(currentr+8 > 64){ //reset column and row values currentr = 0; currentc = 0; } int row = currentr; int col = currentc; //Go through each row and column to set the appropriate pixel values for(j = 0; j < 5; j++){ for(k = 0; k < 8; k++){ //go through each hex value in the hex array and set the binary values associated with it using a bitmask int hex_val = ASCII[hex_array][j]; int bin_val = (hex_val & (1 << k)) >> k; //go to the next row and column row = currentr + k; col = currentc + j; //set each of the pixels display_pixel_set(row, col, bin_val); }; }; i++; currentc = currentc + 5; }; }
int oledprint(int printcol, int printrow, char asciimessage[]) { int i = 0; int asciicol; int asciirow; int bitnum; while (asciimessage[i]) { asciirow = asciimessage[i] - 0x20; for (asciicol=0; asciicol<5; asciicol++) { int asciibyte = ASCII[asciirow][asciicol]; for (bitnum=0; bitnum<8; bitnum++) { int asciibit = asciibyte >> bitnum; int onoff = asciibit & 0b1; display_pixel_set(printrow+bitnum, printcol, onoff); } printcol = printcol+1; } i++; } }
void matrix_code () { short pause = 100; byte toppoint[9] = {1, 2, 3, 3, 2, 4, 5, 1, 0}; byte frickl[7] = {9, 10, 4, 11, 6, 8, 0}; byte kiel[5] = {6, 4, 7, 8, 0}; byte *woerter[3] = {toppoint, frickl, kiel}; short lauf = 0; // randomSeed (micros() ); byte rgb[10][3] = {{0, 255, 0}, {0, 159, 0}, {0, 127, 0}, {0, 95, 0}, {0, 63, 0}, {0, 63, 0}, {0, 63, 0}, {0, 63, 0}, {0, 63, 0}, {0, 63, 0}}; struct zeichenkette { signed short x; byte *font; byte fontzahl; signed short zyklus; }; short anzahl = DISPLAY_WIDTH / 8; struct zeichenkette zeichen[anzahl]; short tmp = 0; for (short i = 0; i < anzahl; i++) { //init zeichen zeichen[i].zyklus = random_range (0, 6) - 5; zeichen[i].x = i * 8 + random_range (0, 3); tmp = random_range (0, sizeof (woerter) / sizeof (byte*) ); zeichen[i].font = woerter[tmp]; tmp = 0; while (zeichen[i].font[tmp] != 0) { tmp++; } zeichen[i].fontzahl = tmp; } while (lauf < 100) { // anzahl schritte display_buffer_swap (1); for (short i = 0; i < anzahl; i++) { if ( (zeichen[i].zyklus >= 0) && (zeichen[i].zyklus < zeichen[i].fontzahl * 5 + DISPLAY_HEIGHT) ) { for (short j = 0; j < zeichen[i].fontzahl; j++) { //einzelne buchstaben durchlaufen uint8_t charid = zeichen[i].font[zeichen[i].fontzahl - j - 1]; uint16_t charbmp = pgm_read_word ( &fontmap[charid] ); for (byte ix = 0; ix < 4; ix++) { for (byte iy = 0; iy < 4; iy++) { if ( (charbmp & 1) != 0 ) { display_pixel_set (ix + zeichen[i].x, iy + j * 5 + zeichen[i].zyklus - zeichen[i].fontzahl * 5, display_color_from_rgb ( rgb[zeichen[i].fontzahl - j - 1][0], rgb[zeichen[i].fontzahl - j - 1][1], rgb[zeichen[i].fontzahl - j - 1][2] ) ); } charbmp >>= 1; } } } zeichen[i].zyklus++; } else if (zeichen[i].zyklus < 0) { //warte, bis die zeit reif ist zeichen[i].zyklus++; } else { //neu initialisieren zeichen[i].zyklus = random_range (0, 10) - 9; zeichen[i].x = i * 8 + random_range (0, 3); tmp = random_range (0, sizeof (woerter) / sizeof (byte*) ); zeichen[i].font = woerter[tmp]; tmp = 0; while (zeichen[i].font[tmp] != 0) { tmp++; } zeichen[i].fontzahl = tmp; } } delay_ms (pause); lauf++; }
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() { 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(); } }
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 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(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 PlayPong(void) { display_buffer_write_set(0); display_clear_black(); display_buffer_active_set(0); display_buffer_write_set(1); display_clear_black(); for(coord_t y = 1; y < DISPLAY_HEIGHT; y+=2) { display_pixel_set(DISPLAY_WIDTH / 2, y, display_color_from_rgb(128, 128, 128)); display_pixel_set(DISPLAY_WIDTH - DISPLAY_WIDTH / 2 - 1, y, display_color_from_rgb(128, 128, 128)); } display_pixel_set(DISPLAY_WIDTH / 2, 0, 0377); display_pixel_set(DISPLAY_WIDTH - DISPLAY_WIDTH / 2 - 1, 0, 0377); int pts_player1 = 0, pts_player2 = 0; int pts_max = (DISPLAY_WIDTH - 1) / 2; int ball_dx = random_range(0, 2) ? 1 : -1; while( (pts_player1 < pts_max) && (pts_player2 < pts_max) ) { display_buffer_copy(1, 2); display_buffer_write_set(2); for(coord_t x = 0; x < pts_player1; x++) { display_pixel_set(DISPLAY_WIDTH - DISPLAY_WIDTH / 2 - x - 2, 0, ~x&1 ? 0007 : 0002); } for(coord_t x = 0; x < pts_player2; x++) { display_pixel_set(DISPLAY_WIDTH / 2 + x + 1, 0, ~x&1 ? 0070 : 0020); } int ball_x = DISPLAY_WIDTH / 2; if (ball_dx < 1) ball_x = DISPLAY_WIDTH - DISPLAY_WIDTH / 2 - 1; int ball_y = DISPLAY_HEIGHT / 2; int ball_dy = 1; int pad_player1 = DISPLAY_HEIGHT / 2; int pad_player2 = DISPLAY_HEIGHT / 2; int step = 0; while( (ball_x >= 0) && (ball_x < DISPLAY_WIDTH) ) { display_buffer_copy(2, 0); display_buffer_write_set(0); //Unser Ball display_pixel_set(ball_x, ball_y, 0377); //Die Schläger for(int i = -1; i <= 1; i++) { display_pixel_set(0, pad_player1 + i, 007); display_pixel_set(DISPLAY_WIDTH - 1, pad_player2 + i, 070); } //Reflektion am Schläger if(0 == ball_x) { if(abs (pad_player1 - ball_y) < 2) { ball_dx = 1; ball_dy = random_range(-2, 2); if(0 <= ball_dy) ball_dy++; } } else if(DISPLAY_WIDTH - 1 == ball_x) { if(abs (pad_player2 - ball_y) < 2) { ball_dx = -1; ball_dy = random_range(-2, 2); if(0 <= ball_dy) ball_dy++; } } //Ballbewegung simulieren ball_x += ball_dx; ball_y += ball_dy; step++; if(ball_y < 1) { ball_y = (1 - ball_y)/*delta am Spiegel*/ + 1/*Offset des Spiegels*/; ball_dy = abs(ball_dy); } else if(ball_y > DISPLAY_HEIGHT - 1) { ball_y = (DISPLAY_HEIGHT - 1)/*Offset des Spiegels*/ - (ball_y - DISPLAY_HEIGHT + 1)/*delta am Spiegel*/; ball_dy = -abs(ball_dy); } if( random_range(0, 3) ) { if( (ball_y < pad_player1) && (pad_player1 > 2) ) { pad_player1--; } if( (ball_y < pad_player2) && (pad_player2 > 2) ) { pad_player2--; } if( (ball_y > pad_player1) && (pad_player1 < DISPLAY_HEIGHT - 2) ) { pad_player1++; } if( (ball_y > pad_player2) && (pad_player2 < DISPLAY_HEIGHT - 2) ) { pad_player2++; } } delay_ms(25); } if(ball_x < 0) { pts_player2+=2; } else { pts_player1+=2; } delay_ms(500); } }