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