/*************** Prints out a string of a given length ***************/ void display_printl(const char *str, uint8_t length, uint8_t clearrestofline) { for(uint8_t i=0; i<length; i++) display_write(str[i]); if(clearrestofline){ for(uint8_t j=0;j<_numcolumns-length;j++){ display_write(' '); } } }
void native_nibo_textdisplay_init(void) { display_init(); //clear display display_wait(0x00); display_write(0x00, 0x01); //display on, blink cursor display_wait(0x00); display_write(0x00, 0x0f); // 8 bit mode, 2-line, 5x7 font display_wait(0x00); display_write(0x00, 0x38); }
void print_char(char c) { if ((cursor_x>15)||(cursor_y>1)) return; display_wait(0x00); display_write(0x02, c); cursor_x++; if ((cursor_x==16)&&(cursor_y==0)) { cursor_x=0; cursor_y=1; display_wait(0x00); display_write(0x00, 0x80+0x40); } }
/*************** Writes out to a line, and fills the rest of it with blanks ***************/ uint8_t display_println(const char *str) { uint8_t i=0; while(1){ if(str[i]==0) break; display_write(str[i]); i++; } if(i<_numcolumns) for(uint8_t j=0;j<_numcolumns-i;j++){ display_write(' '); } return i; }
// Allows us to fill the first 8 CGRAM locations // with custom characters void display_createChar(uint8_t location, uint8_t charmap[]) { location &= 0x7; // we only have 8 locations 0-7 display_command(LCD_SETCGRAMADDR | (location << 3)); for (int i=0; i<8; i++) { display_write(charmap[i]); } }
// Displays a string of characters on the LCD display // // c: string to be displayed void display_string(char* c) { int i = 0; while (c[i] != '\0') { display_write(c[i]); i++; } }
/* Clear the entire display, page by page */ void display_clear() { register byte x __asm("r16"); register byte y __asm("r17"); /* Clear each page of data */ for (y = 0; y < 8; y++) for (x = 0; x < 128; x++) display_write(x, y, 0, 0); }
/*************** Writes out to a null terminated string ***************/ uint8_t display_prints(const char *str) { uint8_t i=0; while(1){ if(str[i]==0) break; display_write(str[i]); i++; } return i; }
void native_nibo_textdisplay_invoke(u08_t mref) { // JAVA: void clear() if(mref == NATIVE_METHOD_clear) { display_wait(0x00); display_write(0x00, 0x01); // JAVA: void gotoXY(int x, int y) } else if(mref == NATIVE_METHOD_gotoXY) { cursor_y = stack_pop(); cursor_x = stack_pop(); if ((cursor_x>15)||(cursor_y>1)) return; display_wait(0x00); if (cursor_y==0) display_write(0x00, 0x80+0x00+cursor_x); else display_write(0x00, 0x80+0x40+cursor_x); // JAVA: void print(String str) } else if(mref == NATIVE_METHOD_print) { char * str = stack_pop_addr(); #ifdef NVM_USE_EEPROM if(NVMFILE_ISSET(str)) { char chr; while((chr = nvmfile_read08(str++))) print_char(chr); } else #endif while(*str) print_char(*str++); // JAVA: void setCursorMode(int mode) } else if(mref == NATIVE_METHOD_setCursorMode) { uint8_t mode = stack_pop(); display_wait(0x00); if (mode==0) display_write(0x00, 0x0c); else if (mode==1) display_write(0x00, 0x0e); else if (mode==2) display_write(0x00, 0x0f); } else error(ERROR_NATIVE_UNKNOWN_METHOD); }
void native_nibo_graphicdisplay_init(void) { display_init(); display_wait(0x00); display_write(0x00, 0x3f); display_wait(0x00); display_write(0x00, 0xc0); display_wait(0x00); display_write(0x00, 0x40); display_wait(0x00); display_write(0x00, 0xb8); display_wait(0x01); display_write(0x01, 0x3f); display_wait(0x01); display_write(0x01, 0xc0); display_wait(0x01); display_write(0x01, 0x40); display_wait(0x01); display_write(0x01, 0xb8); gfx_fill(0x00); gfx_move(40, 0); gfx_print_text("nibo 1.0", 1); gfx_move(5, 12); gfx_print_text("(c) 2007 by nicai-systems", 1); uint8_t prop = 0; // gfx_move(15, 16); // gfx_print_text(" !\"#$%&\\()*+,-./", prop); gfx_move(15, 24); gfx_print_text("0123456789:;<=>?", prop); gfx_move(15, 32); gfx_print_text("@ABCDEFGHIJKLMNO", prop); gfx_move(15, 40); gfx_print_text("PQRSTUVWXYZ[\\]^_", prop); gfx_move(15, 48); gfx_print_text("`abcdefghijklmno", prop); gfx_move(15, 56); gfx_print_text("pqrstuvwxyz{|}~", prop); gfx_print_char(127, prop); /* for (int16_t i=0; i<64; ++i) { display_wait(0x02); display_write(0x02, i); //display_wait(0x03); //display_write(0x03, i); } display_wait(0x00); display_write(0x00, 0xb9); for (int16_t i=0; i<64; ++i) { display_wait(0x02); display_write(0x02, i); //display_wait(0x03); //display_write(0x03, i); } */ }
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(); } }