00001 /* 00002 graphic LCD backpack test code 00003 00004 set up for the big LCD, 160x128 00005 00006 10/16/08, everything works. Pixel, line, circle, set x, set y, print_char, clear_screen. All good. 00007 00008 11/12/08 code jacked for VU meter 00009 00010 11/18/08 reverse display 00011 */ 00012 00013 00014 #include <avr/io.h> 00015 #include "rprintf.h" 00016 #include <math.h> 00017 #include <avr/interrupt.h> 00018 00019 #define FOSC 16000000// Clock Speed 00020 #define BAUD 57600 00021 #define MYUBRR FOSC/16/BAUD-1 00022 00023 #define WR 0 //PC0 00024 #define RD 1 //PC1 00025 #define CE 2 //PC2 00026 #define CD 3 //PC3 00027 #define HALT 4 //PC4 00028 #define RST 5 //PC5 00029 00030 #define PEAK 1 00031 00032 #define BL_EN 2 //PB2 00033 00034 #define X_ENDPOINT 159 00035 #define Y_ENDPOINT 127 00036 00037 //#define CS20 0 00038 //#define CS21 1 00039 //#define CS22 2 00040 00041 00042 //Define functions 00043 //====================== 00044 void ioinit(void); //Initializes IO 00045 void delay_ms(uint16_t x); //General purpose delay 00046 void delay(void); 00047 void delay_us(uint8_t x); 00048 void USART_Init( unsigned int ubrr); 00049 void put_char(char byte); 00050 void print_num(short num); 00051 int rnd(float number); 00052 00053 void set_data(char data); 00054 char read(char D_S);//reads data (D_S = 1) or status (D_S = anything else) 00055 void write(char D_C, char byte);//writes data or command 00056 void display_init(void); 00057 00058 00059 void clear_screen(void); 00060 00061 void print_char(char S_R, char txt); 00062 void del_char(char endpoint); 00063 void pixel(char S_R, char x, char y); 00064 void line(char S_R, char x1, char y1, char x2, char y2); 00065 void circle(char S_R, int x, int y, int r); 00066 void demo(void); 00067 00068 void erase_block(char x1, char y1, char x2, char y2); 00069 void box(char x1, char y1, char x2, char y2); 00070 00071 int get_adc(void); 00072 00073 //====================== 00074 char x_offset = 0; 00075 char y_offset = 127; 00076 char reverse = 1; 00077 00078 unsigned char RX_array[256]; 00079 volatile unsigned short RX_in = 0; 00080 unsigned short RX_read = 0; 00081 00082 static char logo[30] = {0x01,0xC0,0x03,0x80,0x03,0x80,0x01,0xD0, 00083 0x01,0xF8,0x0C,0xF8,0x18,0xF8,0x1F,0xF8, 00084 0x1F,0xF8,0x1F,0xF0,0x1F,0xE0,0x1F,0xC0, 00085 0x1C,0x00,0x18,0x00,0x10,0x00}; 00086 00087 //Jacked from Sinister 7 code 00088 static char text_array[130] = {0x00,0x00,0x00,0x00,0x00,/*space*/ 00089 0x00,0xF6,0xF6,0x00,0x00,/*!*/ 00090 0x00,0xE0,0x00,0xE0,0x00,/*"*/ 00091 0x28,0xFE,0x28,0xFE,0x28,/*#*/ 00092 0x00,0x64,0xD6,0x54,0x08,/*$*/ 00093 0xC2,0xCC,0x10,0x26,0xC6,/*%*/ 00094 0x4C,0xB2,0x92,0x6C,0x0A,/*&*/ 00095 0x00,0x00,0xE0,0x00,0x00,/*'*/ 00096 0x00,0x38,0x44,0x82,0x00,/*(*/ 00097 0x00,0x82,0x44,0x38,0x00,/*)*/ 00098 0x88,0x50,0xF8,0x50,0x88,/***/ 00099 0x08,0x08,0x3E,0x08,0x08,/*+*/ 00100 0x00,0x00,0x05,0x06,0x00,/*,*/ 00101 0x08,0x08,0x08,0x08,0x08,/*-*/ 00102 0x00,0x00,0x06,0x06,0x00,/*.*/ 00103 0x02,0x0C,0x10,0x60,0x80,/*/*/ 00104 0x7C,0x8A,0x92,0xA2,0x7C,/*0*/ 00105 0x00,0x42,0xFE,0x02,0x00,/*1*/ 00106 0x42,0x86,0x8A,0x92,0x62,/*2*/ 00107 0x44,0x82,0x92,0x92,0x6C,/*3*/ 00108 0x10,0x30,0x50,0xFE,0x10,/*4*/ 00109 0xE4,0xA2,0xA2,0xA2,0x9C,/*5*/ 00110 0x3C,0x52,0x92,0x92,0x0C,/*6*/ 00111 0x80,0x86,0x98,0xE0,0x80,/*7*/ 00112 0x6C,0x92,0x92,0x92,0x6C,/*8*/ 00113 0x60,0x92,0x92,0x94,0x78};/*9*/ 00114 00115 00116 static char line_array[284] = {10,80,40,37, 00117 12,81,41,38, 00118 14,82,42,38, 00119 16,83,43,39, 00120 18,84,45,40, 00121 20,85,46,40, 00122 22,86,47,41, 00123 24,87,48,41, 00124 26,88,49,42, 00125 28,89,50,42, 00126 30,90,51,42, 00127 32,91,53,43, 00128 34,92,54,44, 00129 36,92,55,44, 00130 38,93,56,44, 00131 40,94,57,45, 00132 42,94,58,45, 00133 44,95,59,45, 00134 46,95,61,46, 00135 48,96,62,46, 00136 50,96,63,46, 00137 52,97,64,46, 00138 54,97,65,47, 00139 56,98,66,47, 00140 58,98,67,47, 00141 60,98,69,47, 00142 62,99,70,47, 00143 64,99,71,47, 00144 66,99,72,48, 00145 68,99,73,48, 00146 70,100,74,48, 00147 72,100,75,48, 00148 74,100,77,48, 00149 76,100,78,48, 00150 78,100,79,48, 00151 80,100,80,48, 00152 82,100,81,48, 00153 84,100,82,48, 00154 86,100,83,48, 00155 88,100,85,48, 00156 90,100,86,48, 00157 92,99,87,48, 00158 94,99,88,48, 00159 96,99,89,47, 00160 98,99,90,47, 00161 100,98,91,47, 00162 102,98,93,47, 00163 104,98,94,47, 00164 106,97,95,47, 00165 108,97,96,46, 00166 110,96,97,46, 00167 112,96,98,46, 00168 114,95,99,46, 00169 116,95,101,45, 00170 118,94,102,45, 00171 120,94,103,45, 00172 122,93,104,44, 00173 124,92,105,44, 00174 126,92,106,44, 00175 128,91,107,43, 00176 130,90,109,42, 00177 132,89,110,42, 00178 134,88,111,42, 00179 136,87,112,41, 00180 138,86,113,41, 00181 140,85,114,40, 00182 142,84,115,40, 00183 144,83,117,39, 00184 146,82,118,38, 00185 148,81,119,38, 00186 150,80,120,37}; 00187 00188 00189 00190 00191 00192 00193 00194 00195 00196 ISR (SIG_UART_RECV) //USART Receive Interrupt 00197 { 00198 cli();//Disable Interrupts 00199 RX_array[RX_in] = UDR0; 00200 00201 RX_in++; 00202 00203 if (RX_in >= 256) RX_in = 0; 00204 00205 sei();//Enable Interrupts 00206 00207 } 00208 00209 00210 int main (void) 00211 { 00212 char x, y, a, temp; 00213 //short a, b; 00214 float temp_y, temp_y2, temp_x; 00215 int q; 00216 00217 //char a = 0; 00218 ioinit(); //Setup IO pins and defaults 00219 //USART_Init( MYUBRR); 00220 //rprintf_devopen(put_char); /* init rrprintf */ 00221 00222 //reset the display 00223 delay_ms(1); 00224 PORTC |= (1<<RST); 00225 00226 //initialize the display 00227 display_init(); 00228 00229 clear_screen(); 00230 00231 //Backlight on 00232 PORTB &= (~(1<<BL_EN)); 00233 00234 //circle(1,80,-20,142); 00235 //circle(1,80,-20,70); 00236 00237 for (x = 0; x < 160; x++) 00238 { 00239 //temp_y = ((sqrt((r^2) - ((x - 80)*(x - 80)))) - 20) 00240 temp_y = ((sqrt((23716) - ((x - 80)*(x - 80)))) - 30); 00241 temp_y2 = ((sqrt((23409) - ((x - 80)*(x - 80)))) - 30); 00242 00243 //temp_y *= (-1); 00244 pixel(1, x, (char)(temp_y)); 00245 pixel(1, x, (char)(temp_y2)); 00246 } 00247 00248 for (x = 0; x < 160; x++) 00249 { 00250 //temp_y = ((sqrt((r^2) - ((x - 80)*(x - 80)))) - 20) 00251 temp_y = ((sqrt((20164) - ((x - 80)*(x - 80)))) - 30); 00252 temp_y2 = ((sqrt((19881) - ((x - 80)*(x - 80)))) - 30); 00253 00254 //temp_y *= (-1); 00255 pixel(1, x, (char)(temp_y)); 00256 pixel(1, x, (char)(temp_y2)); 00257 } 00258 00259 for (x = 40; x < 120; x++) 00260 { 00261 temp_y = ((sqrt((4900) - ((x - 80)*(x - 80)))) - 30); 00262 temp_y2 = ((sqrt((4761) - ((x - 80)*(x - 80)))) - 30); 00263 00264 //temp_y *= (-1); 00265 pixel(1, x, (char)(temp_y)); 00266 pixel(1, x, (char)(temp_y2)); 00267 } 00268 00269 //======================================================================= 00270 /* 00271 for (x = 109; x < 158; x++) 00272 { 00273 temp_y = ((sqrt((19600) - ((x - 80)*(x - 80)))) - 30); 00274 temp_y2 = ((sqrt((19321) - ((x - 80)*(x - 80)))) - 30); 00275 00276 //temp_y *= (-1); 00277 pixel(1, x, (char)(temp_y)); 00278 pixel(1, x, (char)(temp_y2)); 00279 } 00280 */ 00281 00282 for (x = 108; x < 156; x++) 00283 { 00284 //temp_y = ((sqrt((19044) - ((x - 80)*(x - 80)))) - 30); 00285 temp_y2 = ((sqrt((18225) - ((x - 80)*(x - 80)))) - 30); 00286 00287 //temp_y *= (-1); 00288 //pixel(1, x, (char)(temp_y)); 00289 pixel(1, x, (char)(temp_y2)); 00290 } 00291 00292 line(1,0,87,40,27); 00293 line(1,1,87,41,27); 00294 line(1,120,27,159,87); 00295 line(1,119,27,158,87); 00296 00297 //cleanup================================================================ 00298 pixel(1,80,122); 00299 pixel(0,80,124); 00300 00301 pixel(1,80,110); 00302 pixel(0,80,112); 00303 00304 pixel(1,80,38); 00305 pixel(0,80,40); 00306 00307 //numbers================================================================ 00308 x_offset = 1, y_offset = 99; 00309 print_char(1,'2'); 00310 y_offset = 101; 00311 print_char(1,'0'); 00312 00313 line(1, 9, 92, 12, 86); 00314 line(1, 8, 92, 11, 86); 00315 00316 x_offset = 24, y_offset = 111; 00317 print_char(1,'1'); 00318 y_offset = 112; 00319 print_char(1,'0'); 00320 00321 line(1,30, 101, 32, 95); 00322 line(1, 31, 101, 33, 95); 00323 00324 x_offset = 48, y_offset = 117; 00325 print_char(1,'7'); 00326 00327 line(1,50, 108, 51, 101); 00328 line(1, 51, 108, 52, 101); 00329 00330 x_offset = 66, y_offset = 119; 00331 print_char(1,'5'); 00332 00333 line(1,68, 109, 69, 103); 00334 line(1, 69, 109, 70, 103); 00335 00336 x_offset = 84, y_offset = 119; 00337 print_char(1,'3'); 00338 00339 line(1,86, 109, 86, 103); 00340 line(1, 87, 109, 87, 103); 00341 00342 x_offset = 110, y_offset = 116; 00343 print_char(1,'0'); 00344 00345 line(1,108, 102, 109, 106); 00346 line(1,109, 102, 110, 106); 00347 00348 x_offset = 135, y_offset = 108; 00349 print_char(1,'3'); 00350 00351 line(1,132, 93, 135, 100); 00352 line(1,133, 93, 136, 100); 00353 00354 //logo================================================================== 00355 y = 28; 00356 00357 q = 0; 00358 while(q < 30) 00359 { 00360 temp = logo[q]; 00361 for (x = 72; x < 80; x++) 00362 { 00363 if (temp & 0x80) pixel(1,x,y); 00364 00365 temp <<= 1; 00366 } 00367 q++; 00368 temp = logo[q]; 00369 for (x = 80; x < 88; x++) 00370 { 00371 if (temp & 0x80) pixel(1,x,y); 00372 00373 temp <<= 1; 00374 } 00375 y--; 00376 q++; 00377 } 00378 00379 x = 0; 00380 line(1, line_array[x*4], line_array[x*4+1], line_array[x*4+2], line_array[x*4+3]); 00381 00382 while(1) 00383 { 00384 q = get_adc(); 00385 00386 y = (char)(rnd(q / 13.5)-10); 00387 00388 delay_ms(25); 00389 00390 if (y != x) 00391 { 00392 line(0, line_array[x*4], line_array[x*4+1], line_array[x*4+2], line_array[x*4+3]); 00393 line(1, line_array[y*4], line_array[y*4+1], line_array[y*4+2], line_array[y*4+3]); 00394 x = y; 00395 00396 if (y >= 50) PORTD |= (1<<PEAK); 00397 else PORTD &= (~(1<<PEAK)); 00398 } 00399 00400 00401 00402 00403 00404 //rprintf("%d\r\n",q); 00405 00406 00407 /* 00408 for (x = 10; x <= 80; x += 4) 00409 { 00410 00411 00412 //draw new line 00413 line(1, line_array[(x-10)*4], line_array[(x-10)*4+1], line_array[(x-10)*4+2], line_array[(x-10)*4+3]); 00414 line(1, line_array[(x-10)*4]+1, line_array[(x-10)*4+1], line_array[(x-10)*4+2]+1, line_array[(x-10)*4+3]); 00415 delay_ms(3); 00416 line(0, line_array[(x-10)*4], line_array[(x-10)*4+1], line_array[(x-10)*4+2], line_array[(x-10)*4+3]); 00417 line(0, line_array[(x-10)*4]+1, line_array[(x-10)*4+1], line_array[(x-10)*4+2]+1, line_array[(x-10)*4+3]); 00418 00419 } 00420 00421 00422 00423 for (x = 80; x >= 10; x -= 4) 00424 { 00425 00426 //draw new line 00427 line(1, line_array[(x-10)*4], line_array[(x-10)*4+1], line_array[(x-10)*4+2], line_array[(x-10)*4+3]); 00428 line(1, line_array[(x-10)*4]+1, line_array[(x-10)*4+1], line_array[(x-10)*4+2]+1, line_array[(x-10)*4+3]); 00429 delay_ms(3); 00430 line(0, line_array[(x-10)*4], line_array[(x-10)*4+1], line_array[(x-10)*4+2], line_array[(x-10)*4+3]); 00431 line(0, line_array[(x-10)*4]+1, line_array[(x-10)*4+1], line_array[(x-10)*4+2]+1, line_array[(x-10)*4+3]); 00432 } 00433 */ 00434 00435 } 00436 00437 while(1); 00438 //{ 00439 00440 00441 00442 00443 00444 00445 /* 00446 if(RX_in != RX_read) 00447 { 00448 x = RX_array[RX_read]; 00449 RX_read++; 00450 if(RX_read >= 256) RX_read = 0; 00451 00452 //Backspace=================================================== 00453 if(x == 8) del_char(0); 00454 00455 //Special commands 00456 else if (x == 124) 00457 { 00458 //make sure the next byte is there 00459 while(RX_in == RX_read); 00460 00461 //0, clear screen====================================================== 00462 if(RX_array[RX_read] == 0) 00463 { 00464 clear_screen(); 00465 RX_read++; 00466 if(RX_read >= 256) RX_read = 0; 00467 } 00468 00469 00470 //Backlight on/off 00471 else if(RX_array[RX_read] == 2) 00472 { 00473 y = PINB; 00474 if (y & (1<<BL_EN)) PORTB &= (~(1<<BL_EN)); 00475 else PORTB |= (1<<BL_EN); 00476 RX_read++; 00477 } 00478 00479 //demo mode 00480 else if(RX_array[RX_read] == 4) 00481 { 00482 RX_in = 0, RX_read = 0; 00483 demo(); 00484 clear_screen(); 00485 RX_in = 0; 00486 } 00487 00488 else 00489 { 00490 //set x or y========================================================= 00491 if((RX_array[RX_read] == 24) | (RX_array[RX_read] == 25)) 00492 { 00493 RX_read++; 00494 if(RX_read >= 256) RX_read = 0; 00495 while(RX_in == RX_read);//wait for byte 00496 if (RX_array[RX_read-1] == 24) x_offset = RX_array[RX_read]; 00497 else if (RX_array[RX_read-1] == 25) y_offset = RX_array[RX_read]; 00498 00499 RX_read++; 00500 if(RX_read >= 256) RX_read = 0; 00501 00502 if (x_offset > 159) x_offset = 159; 00503 if (y_offset > 127) y_offset = 127; 00504 00505 } 00506 00507 //set pixel========================================================= 00508 if (RX_array[RX_read] == 16) 00509 { 00510 //need 3 bytes 00511 for (y = 0; y < 3; y++) 00512 { 00513 RX_read++; 00514 if(RX_read >= 256) RX_read = 0; 00515 while(RX_in == RX_read);//wait for byte 00516 } 00517 00518 pixel(RX_array[RX_read], RX_array[RX_read-2], RX_array[RX_read-1]); 00519 00520 RX_read++; 00521 if(RX_read >= 256) RX_read = 0; 00522 00523 } 00524 00525 //<ctrl>c, circle====================================================== 00526 if(RX_array[RX_read] == 3) 00527 { 00528 //need 4 bytes 00529 for (y = 0; y < 4; y++) 00530 { 00531 RX_read++; 00532 if(RX_read >= 256) RX_read = 0; 00533 while(RX_in == RX_read);//wait for byte 00534 } 00535 00536 circle(RX_array[RX_read], RX_array[RX_read-3], RX_array[RX_read-2], RX_array[RX_read-1]); 00537 00538 RX_read++; 00539 if(RX_read >= 256) RX_read = 0; 00540 } 00541 00542 00543 //<ctrl>e, erase block====================================================== 00544 if(RX_array[RX_read] == 5) 00545 { 00546 //need 4 bytes 00547 for (y = 0; y < 4; y++) 00548 { 00549 RX_read++; 00550 if(RX_read >= 256) RX_read = 0; 00551 while(RX_in == RX_read);//wait for byte 00552 } 00553 00554 erase_block(RX_array[RX_read-3], RX_array[RX_read-2], RX_array[RX_read-1], RX_array[RX_read]); 00555 00556 RX_read++; 00557 if(RX_read >= 256) RX_read = 0; 00558 } 00559 00560 00561 //<ctrl>o, box, running out of meaningful letters====================================================== 00562 if(RX_array[RX_read] == 15) 00563 { 00564 //need 4 bytes 00565 for (y = 0; y < 4; y++) 00566 { 00567 RX_read++; 00568 if(RX_read >= 256) RX_read = 0; 00569 while(RX_in == RX_read);//wait for byte 00570 } 00571 00572 box(RX_array[RX_read-3], RX_array[RX_read-2], RX_array[RX_read-1], RX_array[RX_read]); 00573 00574 RX_read++; 00575 if(RX_read >= 256) RX_read = 0; 00576 } 00577 00578 00579 //<ctrl>L, line======================================================== 00580 else if (RX_array[RX_read] == 12) 00581 { 00582 //need 5 bytes 00583 for (y = 0; y < 5; y++) 00584 { 00585 RX_read++; 00586 if(RX_read >= 256) RX_read = 0; 00587 while(RX_in == RX_read);//wait for byte 00588 } 00589 00590 line(RX_array[RX_read], RX_array[RX_read-4], RX_array[RX_read-3], RX_array[RX_read-2], RX_array[RX_read+-1]); 00591 RX_read++; 00592 if(RX_read >= 256) RX_read = 0; 00593 } 00594 00595 00596 } 00597 00598 } 00599 00600 //print character to the screen=============================================== 00601 else 00602 { 00603 del_char(1); 00604 print_char(1, x); 00605 } 00606 } 00607 */ 00608 00609 //} 00610 00611 00612 00613 } 00614 00615 void ioinit (void) 00616 { 00617 00618 //1 = output, 0 = input 00619 00620 /* 00621 WR //PC0 00622 RD //PC1 00623 CE //PC2 00624 C_D //PC3 00625 HALT //PC4 00626 RST //PC5 00627 */ 00628 00629 PORTB |= (1<<BL_EN);//Backlight off 00630 DDRB |= (1<<BL_EN);//set PB2 as output 00631 00632 //set these in the read/write functions instead of here 00633 //DDRB = 0b00000011; //PB0 and PB1 are outs 00634 //DDRD = 0b11111100; //PD2-PD7 are also outs. Ports B and D are the data bus. 00635 00636 PORTC = ((1<<WR) | (1<<RD) | (1<<CE) | (1<<CD) | (1<<HALT)); 00637 PORTC &= (~(1<<RST));//set the reset line low at power up 00638 DDRC = ((1<<WR) | (1<<RD) | (1<<CE) | (1<<CD) | (1<<HALT) | (1<<RST)); 00639 00640 DDRD = (1<<PEAK); 00641 PORTD &= (~(1<<PEAK)); 00642 00643 //Init timer 2 00644 //8,000,000 / 8 = 1,000,000 00645 TCCR2B = (1<<CS21); //Set Prescaler to 8. CS21=1 00646 //TCCR2 = ((1<<CS20) | (1<<CS22) | (1<<CS22)); 00647 00648 } 00649 00650 //General short delays 00651 void delay_ms(uint16_t x) 00652 { 00653 for (; x > 0 ; x--) 00654 { 00655 delay_us(250); 00656 delay_us(250); 00657 delay_us(250); 00658 delay_us(250); 00659 } 00660 00661 } 00662 00663 //General short delays 00664 void delay_us(uint8_t x) 00665 { 00666 char temp; 00667 00668 if (x == 0) temp = 1; 00669 else temp = x; 00670 //TIFR = 0x01; //Clear any interrupt flags on Timer2 00671 TIFR2 |= 0x01; 00672 00673 TCNT2 = 256 - temp; //256 - 125 = 131 : Preload timer 2 for x clicks. Should be 1us per click 00674 00675 //while( (TIFR & (1<<TOV2)) == 0); 00676 while(!(TIFR2 & 0x01)); 00677 00678 if (x == 0) return;//this is for display timing 00679 00680 00681 //The prescaler doesn't allow for a setting of 16, just 8 or 32. So, we do this twice. 00682 TIFR2 |= 0x01; 00683 00684 TCNT2 = 256 - temp; //256 - 125 = 131 : Preload timer 2 for x clicks. Should be 1us per click 00685 00686 //while( (TIFR & (1<<TOV2)) == 0); 00687 while(!(TIFR2 & 0x01)); 00688 00689 } 00690 00691 00692 void USART_Init( unsigned int ubrr) 00693 { 00694 // Set baud rate 00695 UBRR0H = (unsigned char)(ubrr>>8); 00696 UBRR0L = (unsigned char)ubrr; 00697 // Enable receiver and transmitter 00698 //UCSRB = (1<<RXEN)|(1<<TXEN); 00699 UCSR0B = (1<<RXCIE0)|(1<<RXEN0)|(1<<TXEN0); //Enable Interrupts on receive character 00700 // Set frame format: 8data, 2stop bit 00701 //UCSRC = (1<<URSEL)|(1<<USBS)|(3<<UCSZ0); 00702 UCSR0C = (1<<UMSEL00)|(1<<UCSZ00)|(1<<UCSZ01); 00703 sei(); 00704 } 00705 00706 00707 00708 void put_char(char byte) 00709 { 00710 /* Wait for empty transmit buffer */ 00711 while ( !( UCSR0A & (1<<UDRE0)) ); 00712 /* Put data into buffer, sends the data */ 00713 UDR0 = byte; 00714 } 00715 00716 //delay for display timing 00717 void delay(void) 00718 { 00719 char y; 00720 00721 for(y = 0; y < 20; y++) 00722 { 00723 asm volatile ("nop"); 00724 00725 } 00726 00727 /* 00728 asm volatile ("nop"); 00729 asm volatile ("nop"); 00730 asm volatile ("nop"); 00731 asm volatile ("nop"); 00732 asm volatile ("nop"); 00733 */ 00734 00735 } 00736 00737 //set data port 00738 void set_data(char data) 00739 { 00740 //PORTB 00741 //DB0 = PB0 00742 //DB1 = PB1 00743 00744 PORTB &= 0xFC; 00745 00746 //PORTD 00747 //DB2 = PD2 00748 //DB3 = PD3 00749 //DB4 = PD4 00750 //DB5 = PD5 00751 //DB6 = PD6 00752 //DB7 = PD7 00753 00754 PORTD &= 0x03; 00755 00756 PORTB |= (data & 0x03); 00757 PORTD |= (data & 0xFC); 00758 00759 } 00760 00761 00762 //Reads data or status 00763 //for data D_S = 1, for status D_S = 0 00764 //returns the value of the data bus 00765 char read(char D_S) 00766 { 00767 char data1 = 0, data2 = 0; 00768 00769 DDRB &= 0xFC;//PB0 and PB1 inputs 00770 DDRD &= 0x02;//everything but PD1 as input 00771 00772 PORTC &= ~((1 << RD) | (1 << CE));//CD high for status 00773 if (D_S == 1) PORTC &= ~(1 << CD);//CD down for data 00774 00775 delay_us(0); 00776 00777 data1 = PINB; 00778 data1 &= 0x03; 00779 00780 data2 = PIND; 00781 data2 &= 0xFC; 00782 00783 data1 |= data2; 00784 00785 PORTC |= ((1 << CD) | (1 << RD) | (1 << CE));//all up 00786 00787 delay_us(0); 00788 00789 return data1; 00790 00791 } 00792 00793 00794 //Writes data (D_C = 1) or command (D_C = anything else) 00795 void write(char D_C, char byte) 00796 { 00797 DDRB |= 0x03; //PB0 and PB1 are outs 00798 DDRD |= 0xFC; //PD2-PD7 are also outs. Ports B and D are the data bus 00799 00800 set_data(byte); 00801 00802 if (D_C == 1) PORTC &= ~((1 << WR) | (1 << CE) | (1 << CD));//down 00803 else PORTC &= ~((1 << WR) | (1 << CE));//down 00804 00805 delay_us(0); 00806 PORTC |= ((1 << CD) | (1 << WR) | (1 << CE));//all up 00807 delay(); 00808 DDRB &= 0xFC;//PB0 and PB1 inputs 00809 DDRD &= 0x02;//everything but PD1 as input 00810 00811 delay_us(0); 00812 00813 } 00814 00815 00816 00817 void display_init(void) 00818 { 00819 //set graphics home address to 0 00820 while(!(read(0) & 3));//read status 00821 write(1, 0); 00822 while(!(read(0) & 3));//read status 00823 write(1, 0); 00824 while(!(read(0) & 3));//read status 00825 write(0, 0x42); 00826 00827 //set graphics area 00828 while(!(read(0) & 3));//read status 00829 write(1, 20);//20 bytes, 160/8 00830 while(!(read(0) & 3));//read status 00831 write(1, 0); 00832 while(!(read(0) & 3));//read status 00833 write(0, 0x43); 00834 00835 //set mode 00836 while(!(read(0) & 3));//read status 00837 write(0, 0x80);//Or, with internal character generator 00838 00839 //set display mode 00840 while(!(read(0) & 3));//read status 00841 write(0, 0x98);//Graphics on 00842 00843 } 00844 00845 00846 00847 00848 00849 void clear_screen(void) 00850 { 00851 int x; 00852 00853 //set address pointer to 0, start of graphics 00854 while(!(read(0) & 3));//read status 00855 write(1, 0); 00856 while(!(read(0) & 3));//read status 00857 write(1, 0); 00858 while(!(read(0) & 3));//read status 00859 write(0, 0x24); 00860 00861 for(x = 0; x < 0xA00; x++) 00862 { 00863 while(!(read(0) & 3));//read status 00864 if (reverse == 1) write(1,0xFF); 00865 else if (reverse == 0) write(1, 0); 00866 while(!(read(0) & 3));//read status 00867 write(0, 0xC0); 00868 } 00869 00870 x_offset = 0; 00871 y_offset = 127; 00872 } 00873 00874 /* 00875 void set_cursor(char x_spot, char y_spot) 00876 { 00877 //set address pointer to 0, start of graphics 00878 while(!(read(0) & 3));//read status 00879 write(1, x_spot); 00880 while(!(read(0) & 3));//read status 00881 write(1, y_spot); 00882 while(!(read(0) & 3));//read status 00883 write(0, 0x21); 00884 00885 } 00886 */ 00887 00888 void pixel(char S_R, char x, char y) 00889 { 00890 short address = 0; 00891 char byte = 0; 00892 00893 if (reverse == 1) S_R ^= 1; 00894 00895 //don't try to print something outside of our range 00896 if ((x > 159) | (y > 127)) return; 00897 00898 address = ((127-y) * 20) + (x / 8); 00899 00900 //set address pointer 00901 while(!(read(0) & 3));//read status 00902 byte = (char)(address & 0xFF); 00903 00904 write(1, byte);//20 bytes, 160/8 00905 00906 while(!(read(0) & 3));//read status 00907 byte = (char)((address & 0xFF00) >> 8); 00908 00909 write(1, byte); 00910 00911 while(!(read(0) & 3));//read status 00912 write(0, 0x24); 00913 00914 byte = ~(x % 8); 00915 00916 byte |= 0xF8; 00917 if (S_R == 0) byte &= 0xF7; 00918 00919 //set-reset bit 00920 while(!(read(0) & 3));//read status 00921 write(0, byte); 00922 00923 } 00924 00925 00926 00927 void line(char S_R, char x1, char y1, char x2, char y2) 00928 { 00929 float m, q; 00930 int x_dif, y_dif; 00931 int a, b, c; 00932 00933 //if ((x1 > X_ENDPOINT) | (x2 > X_ENDPOINT)) return; 00934 //if ((x1 < 0) | (x2 < 0)) return; 00935 //if ((y1 > Y_ENDPOINT) | (y2 > Y_ENDPOINT)) return; 00936 //if ((y1 < 0) | (y2 < 0)) return; 00937 00938 x_dif = x2 - x1; 00939 y_dif = y2 - y1; 00940 if (y_dif < 0) y_dif *= (-1); 00941 00942 00943 m = (float)(y2 - y1) / (float)(x2 - x1); 00944 00945 b = y1-(m*x1); 00946 00947 if(x_dif >= y_dif) 00948 { 00949 for (a = x1; a <= x2; a++) 00950 { 00951 pixel(S_R, (char)a, (char)((m*a)+b)); 00952 //delay_ms(25); 00953 } 00954 } 00955 00956 else 00957 { 00958 if (y2 > y1) 00959 { 00960 for (a = y1; a <= y2; a++) 00961 { 00962 if (x_dif == 0) c = x1; 00963 else 00964 { 00965 q = (((float)(a-b))/m); 00966 c = rnd(q); 00967 } 00968 00969 pixel(S_R, (char)c, (char)a); 00970 //delay_ms(25); 00971 } 00972 } 00973 00974 else if (y1 > y2) 00975 { 00976 for (a = y1; a >= y2; a--) 00977 { 00978 if (x_dif == 0) c = x1; 00979 else 00980 { 00981 q = (((float)(a-b))/m); 00982 c = rnd(q); 00983 } 00984 00985 pixel(S_R, (char)c, (char)a); 00986 //delay_ms(25); 00987 } 00988 } 00989 } 00990 00991 } 00992 00993 00994 00995 void circle(char S_R, int x, int y, int r) 00996 { 00997 int x1 = 0, x2 = 0; 00998 int x_line = 0, y_line = 0; 00999 int temp_y; 01000 int temp_x; 01001 //float x_float, y_float, r_float; 01002 01003 x1 = x - r; 01004 x2 = x + r; 01005 01006 for (temp_x = x1; temp_x <= x2; temp_x++) 01007 { 01008 temp_y = ((sqrt((r*r) - ((temp_x - x)*(temp_x - x)))) - y); 01009 01010 temp_y *= (-1); 01011 01012 /* 01013 print_num((short)temp_x); 01014 put_char(9); 01015 print_num((short)temp_y); 01016 put_char(10); 01017 put_char(13); 01018 */ 01019 if (temp_x > x1) 01020 { 01021 //line(S_R, (char)x_line, (char)y_line, (char)temp_x, (char)temp_y); 01022 line(S_R, (char)x_line, (char)(2*y - y_line), (char)temp_x, (char)(2*y - temp_y)); 01023 } 01024 01025 else 01026 { 01027 //pixel(S_R, (char)temp_x, (char)temp_y); 01028 pixel(S_R, (char)temp_x, (char)(y + y - temp_y)); 01029 } 01030 01031 x_line = temp_x; 01032 y_line = temp_y; 01033 01034 // 01035 01036 01037 } 01038 01039 01040 } 01041 01042 01043 int rnd(float number) 01044 { 01045 int a; 01046 float b; 01047 01048 a = number / 1; 01049 b = number - a; 01050 01051 if (b >= 0.5) a++; 01052 01053 return a; 01054 01055 } 01056 01057 01058 void print_char(char S_R, char txt) 01059 { 01060 short text_array_offset = (txt - 32)*5, j; 01061 char x, k; 01062 01063 01064 for (j = text_array_offset; j < text_array_offset+5; j++) 01065 { 01066 01067 01068 k = text_array[j]; 01069 01070 for (x = 0; x < 8; x++) 01071 { 01072 if(k & 0x80) pixel(S_R, x_offset, y_offset - x); 01073 k <<= 1; 01074 } 01075 01076 //if (j == text_array_offset+5) x_offset++;//blank byte for letter spacing 01077 01078 01079 x_offset++; 01080 01081 } 01082 01083 x_offset++; 01084 01085 if ((x_offset + 6) > 159) 01086 { 01087 x_offset = 0; 01088 if (y_offset <= 7) y_offset = 127; 01089 else y_offset -= 8; 01090 01091 } 01092 01093 } 01094 01095 01096 01097 void print_num(short num) 01098 { 01099 short a; 01100 char b, c; 01101 a = num; 01102 01103 //print hex 01104 for (c = 0; c < 4; c++) 01105 { 01106 b = (char)((a & 0xF000) >> 12); 01107 if (b < 10) put_char(b+48); 01108 else put_char(b+55); 01109 a <<= 4; 01110 } 01111 01112 //decimal 01113 /* 01114 b = a/100; 01115 putchr(b+48); 01116 a -= b*100; 01117 b = a/10; 01118 putchr(b+48); 01119 a -= b*10; 01120 putchr(a+48); 01121 */ 01122 01123 } 01124 01125 01126 void demo(void) 01127 { 01128 char x, y, temp; 01129 int q = 0; 01130 01131 while(1) 01132 { 01133 x_offset = 0; 01134 y_offset = 127; 01135 01136 for (y = 0; y < 5; y++) 01137 { 01138 for (x = 32; x < 123; x++) 01139 { 01140 del_char(1); 01141 print_char(1, x); 01142 if (RX_in > 0) return; 01143 } 01144 } 01145 01146 clear_screen(); 01147 01148 for (y = 0; y < 5; y++) 01149 { 01150 for (x = 32; x < 123; x++) 01151 { 01152 //x_offset += 4; 01153 y_offset -= 6; 01154 if (y_offset <= 8) y_offset = 127; 01155 del_char(1); 01156 print_char(1, x); 01157 if (RX_in > 0) return; 01158 } 01159 } 01160 01161 clear_screen(); 01162 01163 //draw circles================================ 01164 for (x = 5; x < 120; x += 5) 01165 { 01166 circle(1,80,64,x); 01167 if (RX_in > 0) return; 01168 } 01169 01170 01171 //draw lines=================================== 01172 y = Y_ENDPOINT; 01173 01174 for (x = 0; x < X_ENDPOINT; x += 20) 01175 { 01176 line(1,0,y,x,0); 01177 y -= 16; 01178 } 01179 01180 y = 0; 01181 01182 for (x = 0; x < X_ENDPOINT; x += 20) 01183 { 01184 line(1,x,0,X_ENDPOINT,y); 01185 y += 16; 01186 } 01187 01188 y = Y_ENDPOINT; 01189 01190 for (x = 0; x < X_ENDPOINT; x += 20) 01191 { 01192 line(1,x,Y_ENDPOINT,X_ENDPOINT,y); 01193 y -= 16; 01194 } 01195 01196 y = 0; 01197 01198 for (x = 0; x < X_ENDPOINT; x += 20) 01199 { 01200 line(1,0,y,x,Y_ENDPOINT); 01201 y += 16; 01202 } 01203 01204 01205 //erase circles================================ 01206 for (x = 5; x < 120; x += 5) 01207 { 01208 circle(0,80,64,x); 01209 if (RX_in > 0) return; 01210 } 01211 01212 //erase lines=================================== 01213 y = Y_ENDPOINT; 01214 01215 for (x = 0; x < X_ENDPOINT; x += 20) 01216 { 01217 line(0,0,y,x,0); 01218 y -= 16; 01219 } 01220 01221 y = 0; 01222 01223 for (x = 0; x < X_ENDPOINT; x += 20) 01224 { 01225 line(0,x,0,X_ENDPOINT,y); 01226 y += 16; 01227 } 01228 01229 y = Y_ENDPOINT; 01230 01231 for (x = 0; x < X_ENDPOINT; x += 20) 01232 { 01233 line(0,x,Y_ENDPOINT,X_ENDPOINT,y); 01234 y -= 16; 01235 } 01236 01237 y = 0; 01238 01239 for (x = 0; x < X_ENDPOINT; x += 20) 01240 { 01241 line(0,0,y,x,Y_ENDPOINT); 01242 y += 16; 01243 } 01244 01245 if (RX_in > 0) return; 01246 01247 //Boxes================================================================= 01248 y = 111; 01249 for (x = 0; x <= 140; x += 10) 01250 { 01251 erase_block(x, y, x+16, y+16); 01252 box(x, y, x+16, y+16); 01253 y -= 7; 01254 } 01255 01256 01257 //x = 110; 01258 y = 28; 01259 //Logo================================================================= 01260 q = 0; 01261 while(q < 30) 01262 { 01263 temp = logo[q]; 01264 for (x = 140; x < 148; x++) 01265 { 01266 if (temp & 0x80) pixel(1,x,y); 01267 01268 temp <<= 1; 01269 } 01270 q++; 01271 temp = logo[q]; 01272 for (x = 148; x < 156; x++) 01273 { 01274 if (temp & 0x80) pixel(1,x,y); 01275 01276 temp <<= 1; 01277 } 01278 y--; 01279 q++; 01280 } 01281 01282 delay_ms(3000); 01283 clear_screen(); 01284 01285 } 01286 01287 01288 } 01289 01290 //Deletes a character. Endpoint == 0 for a backwards delete, 01291 //Endpoint != 0 to erase spot for a new character write 01292 void del_char(char endpoint) 01293 { 01294 char a, y; 01295 01296 if (endpoint == 0)//Backwards delete 01297 { 01298 if (x_offset <= 5) 01299 { 01300 //x_offset = 150; 01301 //if (y_offset >= 120) y_offset = 7; 01302 //else y_offset += 8; 01303 01304 x_offset += 152; 01305 y_offset += 8; 01306 01307 if (y_offset > 127) y_offset -= 128; 01308 } 01309 01310 else x_offset -= 6; 01311 } 01312 /* 01313 for (a = 0; a < 6; a++) 01314 { 01315 for (y = 0; y < 8; y++) 01316 { 01317 pixel(0, x_offset + a, y_offset - y); 01318 01319 } 01320 } 01321 */ 01322 01323 for (a = x_offset; a < x_offset + 6; a++) 01324 { 01325 for (y = y_offset - 7; y <= y_offset; y++) 01326 { 01327 pixel(0, a, y); 01328 //put_char('L'); 01329 } 01330 } 01331 01332 } 01333 01334 01335 void erase_block(char x1, char y1, char x2, char y2) 01336 { 01337 static char temp_x = 0, temp_y = 0; 01338 01339 for (temp_y = y2; temp_y >= y1; temp_y--) 01340 { 01341 for (temp_x = x1; temp_x <= x2; temp_x++) 01342 { 01343 pixel(0, temp_x, temp_y); 01344 01345 //rprintf("%d ",temp_x); 01346 //rprintf("%d \r\n",temp_y); 01347 //delay_ms(1000); 01348 01349 } 01350 } 01351 01352 01353 01354 } 01355 01356 void box(char x1, char y1, char x2, char y2) 01357 { 01358 //static char temp_x = 0, temp_y = 0; 01359 01360 line(1, x1, y1, x1, y2); 01361 line(1, x1, y1, x2, y1); 01362 line(1, x2, y1, x2, y2); 01363 line(1, x1, y2, x2, y2); 01364 01365 } 01366 01367 01368 int get_adc(void) 01369 { 01370 //ADC result vars 01371 int l;//x low register 01372 //int h;//x high register 01373 int l2;//temp low 01374 int h2;//temp high 01375 01376 //int Vref = 502;//reference V in mV 01377 01378 //adc conversion 01379 ADMUX = ((1 << MUX1)|(1 << MUX2) | (0x40));//ADC6, AVcc as Vref 01380 ADCSRA = (1 << ADEN)|(1 << ADSC)|(1<<ADPS2)|(1<<ADPS1); 01381 01382 while(ADCSRA & (1 << ADSC)); 01383 l2 = (ADCL & 0xFF); 01384 h2 = (ADCH & 0x03); 01385 //rprintf("%d %d\r\n", h2, l2); 01386 01387 //h2 = h2 << 8; 01388 //h2 = ((h2 + l2)*Vref)/1024; 01389 h2 <<= 8; 01390 h2 |= l2; 01391 //l = (ADCH & 0x03) << 8; 01392 //l |= ADCL; 01393 01394 return (int)h2; 01395 //return 512; 01396 01397 //l = ADCL; 01398 //h = ADCH & 0x03; 01399 //h = h << 8; 01400 //h = ((h + l)*Vref)/512; 01401 01402 01403 } 01404 01405 01406 01407 01408