00001 /* 00002 graphic LCD backpack test code 00003 00004 10/7/08, text and lines work, need to set up UART and circle functions 00005 00006 */ 00007 00008 #include <avr/io.h> 00009 #include <stdlib.h> 00010 #include <stdio.h> 00011 #include "rprintf.h" 00012 #include <string.h> 00013 #include <math.h> 00014 #include <avr/interrupt.h> 00015 00016 00017 #define FOSC 16000000// Clock Speed 00018 //#define BAUD 9600 00019 //#define MYUBRR FOSC/16/BAUD-1 00020 00021 //128x64 definitions 00022 #define EN 0 //PC0 00023 #define RS 1 //PC1, D_I? 00024 #define R_W 2 //PC2 00025 #define RESET 3 //PC3 00026 #define CS1 4 //PC4 00027 #define CS2 5 //PC5 00028 00029 //160x128 definitions 00030 #define WR 0 //PC0 00031 #define RD 1 //PC1 00032 #define CE 2 //PC2 00033 #define CD 3 //PC3 00034 #define HALT 4 //PC4 00035 #define RST 5 //PC5 00036 00037 #define BL_EN 2 //PB2 00038 00039 #define BPS 0 00040 #define BACKLIGHT 1 00041 #define SPLASH 2 00042 #define REV 3 00043 00044 #define BAUD 6 00045 #define BUFFER 416 00046 00047 /* 00048 if (b == 1) USART_Init( 1000000/2400-1);//4800 00049 else if (b == 2) USART_Init( 1000000/4800-1);//9600 00050 else if (b == 3) USART_Init( 1000000/9600-1);//19200 00051 else if (b == 4) USART_Init( 1000000/19200-1);//38400 00052 else if (b == 5) USART_Init( 1000000/28800-1);//57600 00053 else if (b == 6) USART_Init( 1000000/57600-1);//115200 00054 */ 00055 00056 //Define functions 00057 //====================== 00058 void EEPROM_write(unsigned int uiAddress, unsigned char ucData); 00059 unsigned char EEPROM_read(unsigned int uiAddress); 00060 00061 void ioinit(void); //Initializes IO 00062 void delay_ms(uint16_t x); //General purpose delay 00063 void delay_us(uint8_t x); 00064 void delay(void);//display timing 00065 void USART_Init( unsigned int ubrr); 00066 void put_char(char byte); 00067 00068 void set_data(char data); 00069 void set_x(char x_spot); 00070 void set_page(char page); 00071 void clear_screen(void); 00072 void write_byte(char byte, char side); 00073 char read_byte(char byte, char side); 00074 void display_on(void); 00075 void set_backlight(unsigned char dutycycle); 00076 00077 // 00078 char read(char D_S);//reads data (D_S = 1) or status (D_S = anything else) 00079 void write(char D_C, char byte);//writes data or command 00080 void display_init(void);//initializes the display 00081 00082 //unsigned char print_char(char txt); 00083 void set_baud(char b); 00084 void print_char(char S_R, char txt); 00085 void print_string(char S_R, char str[]); 00086 void print_dec(char S_R,int number); 00087 00088 void del_char(char endpoint); 00089 void pixel(char S_R, char x, char y); 00090 void pixel_h(char S_R, char x, char y); 00091 00092 void line(char S_R, char x1, char y1, char x2, char y2); 00093 int rnd(float number); 00094 void circle(char S_R, int x, int y, int r); 00095 void thick_circle(char S_R, int x, int y, int r,int thickness); 00096 00097 void demo(void); 00098 void erase_block(char x1, char y1, char x2, char y2); 00099 void box(char x1, char y1, char x2, char y2); 00100 void solid_box(char x1, char y1, char x2, char y2);//Add by Kai 00101 void bar(char x1, char y1, char width, char length,char value,char v_h);//Add by Kai 00102 00103 //====================== 00104 void ilab_logo(void); 00105 void small_ilab_logo(void); 00106 void motor_screen(void); 00107 void bar_move(void); 00108 void motor_value(int speed,int turn,int cap,int mode,int estop); 00109 00110 00111 char x_offset = 0; 00112 char y_offset = 63; 00113 char side_mark = 1;//left side default 00114 char page_mark = 0; 00115 char baud_rate = BAUD;//115200 by default 00116 char reverse = 0; 00117 unsigned char display = 0;//display type, 0 = small, 1 = large 00118 unsigned char RX_array[BUFFER]; 00119 volatile unsigned short RX_in = 0; 00120 unsigned short RX_read = 0; 00121 unsigned char BL_dutycycle = 100; 00122 unsigned char splash_screen = 1; 00123 /* 00124 SparkFun logo 00125 static char logo[30] = {0x01,0xC0,0x03,0x80,0x03,0x80,0x01,0xD0, 00126 0x01,0xF8,0x0C,0xF8,0x18,0xF8,0x1F,0xF8, 00127 0x1F,0xF8,0x1F,0xF0,0x1F,0xE0,0x1F,0xC0, 00128 0x1C,0x00,0x18,0x00,0x10,0x00}; 00129 */ 00130 //Small ilab logo 16x15 00131 static char logo[30] ={ 00132 0b00000111,0b11100000, 00133 0b00001000,0b00010000, 00134 0b00110000,0b00001100, 00135 0b00100100,0b00000100, 00136 0b01010100,0b00010010, 00137 0b10101100,0b00010001, 00138 0b10010100,0b11011001, 00139 0b10010101,0b00110101, 00140 0b10010101,0b00110101, 00141 0b10010110,0b11011001, 00142 0b01000000,0b00000010, 00143 0b00100000,0b00000100, 00144 0b00110000,0b00001100, 00145 0b00001000,0b00010000, 00146 0b00000111,0b11100000 00147 00148 00149 00150 00151 00152 00153 00154 00155 00156 00157 00158 00159 00160 00161 }; 00162 00163 00164 00165 00166 //Jacked from Sinister 7 code 00167 static char text_array[475] = {0x00,0x00,0x00,0x00,0x00,/*space*/ 00168 0x00,0xF6,0xF6,0x00,0x00,/*!*/ 00169 0x00,0xE0,0x00,0xE0,0x00,/*"*/ 00170 0x28,0xFE,0x28,0xFE,0x28,/*#*/ 00171 0x00,0x64,0xD6,0x54,0x08,/*$*/ 00172 0xC2,0xCC,0x10,0x26,0xC6,/*%*/ 00173 0x4C,0xB2,0x92,0x6C,0x0A,/*&*/ 00174 0x00,0x00,0xE0,0x00,0x00,/*'*/ 00175 0x00,0x38,0x44,0x82,0x00,/*(*/ 00176 0x00,0x82,0x44,0x38,0x00,/*)*/ 00177 0x88,0x50,0xF8,0x50,0x88,/***/ 00178 0x08,0x08,0x3E,0x08,0x08,/*+*/ 00179 0x00,0x00,0x05,0x06,0x00,/*,*/ 00180 0x08,0x08,0x08,0x08,0x08,/*-*/ 00181 0x00,0x00,0x06,0x06,0x00,/*.*/ 00182 0x02,0x0C,0x10,0x60,0x80,/*/*/ 00183 0x7C,0x8A,0x92,0xA2,0x7C,/*0*/ 00184 0x00,0x42,0xFE,0x02,0x00,/*1*/ 00185 0x42,0x86,0x8A,0x92,0x62,/*2*/ 00186 0x44,0x82,0x92,0x92,0x6C,/*3*/ 00187 0x10,0x30,0x50,0xFE,0x10,/*4*/ 00188 0xE4,0xA2,0xA2,0xA2,0x9C,/*5*/ 00189 0x3C,0x52,0x92,0x92,0x0C,/*6*/ 00190 0x80,0x86,0x98,0xE0,0x80,/*7*/ 00191 0x6C,0x92,0x92,0x92,0x6C,/*8*/ 00192 0x60,0x92,0x92,0x94,0x78,/*9*/ 00193 0x00,0x00,0x36,0x36,0x00,/*:*/ 00194 0x00,0x00,0x35,0x36,0x00,/*;*/ 00195 0x10,0x28,0x44,0x82,0x00,/*<*/ 00196 0x28,0x28,0x28,0x28,0x28,/*=*/ 00197 0x00,0x82,0x44,0x28,0x10,/*>*/ 00198 0x40,0x80,0x8A,0x90,0x60,/*?*/ 00199 0x7C,0x82,0xBA,0xBA,0x62,/*@*/ 00200 0x3E,0x48,0x88,0x48,0x3E,/*A*/ 00201 0xFE,0x92,0x92,0x92,0x6C,/*B*/ 00202 0x7C,0x82,0x82,0x82,0x44,/*C*/ 00203 0xFE,0x82,0x82,0x82,0x7C,/*D*/ 00204 0xFE,0x92,0x92,0x92,0x82,/*E*/ 00205 0xFE,0x90,0x90,0x90,0x80,/*F*/ 00206 0x7C,0x82,0x82,0x8A,0x4E,/*G*/ 00207 0xFE,0x10,0x10,0x10,0xFE,/*H*/ 00208 0x82,0x82,0xFE,0x82,0x82,/*I*/ 00209 0x84,0x82,0xFC,0x80,0x80,/*J*/ 00210 0xFE,0x10,0x28,0x44,0x82,/*K*/ 00211 0xFE,0x02,0x02,0x02,0x02,/*L*/ 00212 0xFE,0x40,0x20,0x40,0xFE,/*M*/ 00213 0xFE,0x60,0x10,0x0C,0xFE,/*N*/ 00214 0x7C,0x82,0x82,0x82,0x7C,/*O*/ 00215 0xFE,0x90,0x90,0x90,0x60,/*P*/ 00216 0x7C,0x82,0x82,0x86,0x7E,/*Q*/ 00217 0xFE,0x90,0x98,0x94,0x62,/*R*/ 00218 0x64,0x92,0x92,0x92,0x4C,/*S*/ 00219 0x80,0x80,0xFE,0x80,0x80,/*T*/ 00220 0xFC,0x02,0x02,0x02,0xFC,/*U*/ 00221 0xF8,0x04,0x02,0x04,0xF8,/*V*/ 00222 0xFC,0x02,0x0C,0x02,0xFC,/*W*/ 00223 0xC6,0x28,0x10,0x28,0xC6,/*X*/ 00224 0xC0,0x20,0x1E,0x20,0xC0,/*Y*/ 00225 0x86,0x8A,0x92,0xA2,0xC2,/*Z*/ 00226 0x00,0x00,0xFE,0x82,0x00,/*[*/ 00227 0x00,0x00,0x00,0x00,0x00,/*this should be / */ 00228 0x80,0x60,0x10,0x0C,0x02,/*]*/ 00229 0x20,0x40,0x80,0x40,0x20,/*^*/ 00230 0x01,0x01,0x01,0x01,0x01,/*_*/ 00231 0x80,0x40,0x20,0x00,0x00,/*`*/ 00232 0x04,0x2A,0x2A,0x2A,0x1E,/*a*/ 00233 0xFE,0x12,0x22,0x22,0x1C,/*b*/ 00234 0x1C,0x22,0x22,0x22,0x14,/*c*/ 00235 0x1C,0x22,0x22,0x12,0xFE,/*d*/ 00236 0x1C,0x2A,0x2A,0x2A,0x18,/*e*/ 00237 0x10,0x7E,0x90,0x80,0x40,/*f*/ 00238 0x18,0x25,0x25,0x25,0x1E,/*g*/ 00239 0xFE,0x10,0x10,0x10,0x0E,/*h*/ 00240 0x00,0x12,0x5E,0x02,0x00,/*i*/ 00241 0x02,0x01,0x01,0x11,0x5E,/*j*/ 00242 0xFE,0x08,0x08,0x14,0x22,/*k*/ 00243 0x00,0x82,0xFE,0x02,0x00,/*l*/ 00244 0x3E,0x20,0x1C,0x20,0x1E,/*m*/ 00245 0x3E,0x20,0x20,0x20,0x1E,/*n*/ 00246 0x1C,0x22,0x22,0x22,0x1C,/*o*/ 00247 0x3F,0x24,0x24,0x24,0x18,/*p*/ 00248 0x18,0x24,0x24,0x3F,0x01,/*q*/ 00249 0x3E,0x10,0x20,0x20,0x10,/*r*/ 00250 0x12,0x2A,0x2A,0x2A,0x04,/*s*/ 00251 0x00,0x10,0x3C,0x12,0x04,/*t*/ 00252 0x3C,0x02,0x02,0x02,0x3E,/*u*/ 00253 0x30,0x0C,0x02,0x0C,0x30,/*v*/ 00254 0x38,0x06,0x18,0x06,0x38,/*w*/ 00255 0x22,0x14,0x08,0x14,0x22,/*x*/ 00256 0x38,0x05,0x05,0x05,0x3E,/*y*/ 00257 0x22,0x26,0x2A,0x32,0x22,/*z*/ 00258 0x00,0x10,0x6C,0x82,0x82,/*{*/ 00259 //0x00,0x00,0xFF,0x00,0x00,/*|*/ 00260 0x04,0x02,0xFF,0x02,0x04,/*|, arrow*/ 00261 0x82,0x82,0x6C,0x10,0x00,/*}*/ 00262 0x08,0x10,0x18,0x08,0x10};/*~*/ 00263 00264 00265 00266 00267 00268 ISR (SIG_USART_RECV)//USART Receive Interrupt 00269 { 00270 cli();//Disable Interrupts 00271 RX_array[RX_in] = UDR0; 00272 00273 RX_in++; 00274 00275 if (RX_in >= BUFFER) RX_in = 0; 00276 00277 sei();//Enable Interrupts 00278 00279 } 00280 00281 ISR (TIMER0_COMPA_vect) 00282 { 00283 unsigned char y; 00284 00285 //cli();//Disable Interrupts 00286 00287 TCCR0B = 0; 00288 TIMSK0 = 0;//disable timer ints 00289 00290 TIFR0 = 0x02;//clear the interrupt 00291 TCNT0 = 0; 00292 00293 //PORTB |= (1<<BL_EN);//off 00294 00295 00296 y = PINB; 00297 if (y & (1<<BL_EN))//on 00298 { 00299 PORTB &= (~(1<<BL_EN)); 00300 OCR0A = BL_dutycycle; 00301 } 00302 00303 else//off 00304 { 00305 PORTB |= (1<<BL_EN); 00306 OCR0A = 100 - BL_dutycycle; 00307 } 00308 00309 //PORTB |= (1<<BL_EN); 00310 00311 TIMSK0 = 0x02;//enable OCR0A 00312 TCCR0B = 0x02; 00313 //sei();//Enable Interrupts 00314 } 00315 00316 00317 int main (void) 00318 { 00319 char x, y, temp; 00320 00321 //check for display type 00322 PORTB |= 0x08;//pullup on PB3 00323 delay(); 00324 x = PINB; 00325 if ((x & 0x08) == 0x08) display = 1;//set up for the big display 00326 else display = 0;//small display 00327 00328 ioinit(); //Setup IO pins and defaults 00329 set_baud(BAUD);//115200 00330 rprintf_devopen(put_char); /* init rrprintf */ 00331 00332 //check for existing preset values============================================================== 00333 temp = EEPROM_read((unsigned int)BPS); 00334 00335 if ((temp < 1) | (temp > 6))//BPS will only be 1-6 00336 { 00337 cli();//Disable Interrupts 00338 00339 EEPROM_write((unsigned int) BPS, 6); 00340 EEPROM_write((unsigned int) BACKLIGHT, 100); 00341 EEPROM_write((unsigned int) SPLASH, 1); 00342 EEPROM_write((unsigned int) REV, 0); 00343 00344 sei();//Enable Interrupts 00345 00346 BL_dutycycle = 100; 00347 baud_rate = BAUD; 00348 splash_screen = 1; 00349 reverse = 0; 00350 } 00351 00352 else 00353 { 00354 baud_rate = temp; 00355 BL_dutycycle = EEPROM_read((unsigned int)BACKLIGHT); 00356 splash_screen = EEPROM_read((unsigned int)SPLASH); 00357 reverse = EEPROM_read((unsigned int)REV); 00358 } 00359 00360 00361 //Reset the display================================================= 00362 if (display == 0) 00363 { 00364 PORTC &= ~(1 << RESET); 00365 delay_ms(50); 00366 PORTC |= (1 << RESET); 00367 00368 clear_screen(); 00369 00370 set_page(0); 00371 00372 set_x(0); 00373 00374 display_on(); 00375 00376 //set display start line to 0 00377 //set control lines 00378 PORTC &= ~((1 << EN) | (1 << R_W) | (1 << RS));//down 00379 00380 set_data(0xC0); 00381 //set_data(0xFF); 00382 delay(); 00383 PORTC |= (1 << EN);//up 00384 delay(); 00385 PORTC &= ~(1 << EN);//down 00386 delay(); 00387 PORTC |= ((1 << EN) | (1 << R_W) | (1 << RS));//all high 00388 00389 delay(); 00390 00391 x_offset = 0; 00392 00393 set_page(0); 00394 00395 } 00396 00397 else if (display == 1) 00398 { 00399 PORTC &= ~(1 << RST); 00400 delay_ms(50); 00401 PORTC |= (1 << RST); 00402 00403 //initialize the display 00404 display_init(); 00405 00406 clear_screen(); 00407 } 00408 00409 00410 set_backlight(BL_dutycycle); 00411 00412 delay_ms(500); 00413 00414 00415 //Logo========================================================== 00416 if (splash_screen == 1) 00417 { 00418 /* 00419 y = 40; 00420 x = 6; 00421 00422 if (display == 1) y += 32, x += 16; 00423 00424 for (q = 0; q < 30; q++) 00425 { 00426 temp = logo[q]; 00427 for (z = x; z < (x+8); z++) 00428 { 00429 if (temp & 0x80) pixel(1,z,y); 00430 00431 temp <<= 1; 00432 } 00433 00434 q++; 00435 00436 temp = logo[q]; 00437 for (z = (x+8); z < (x+16); z++) 00438 { 00439 if (temp & 0x80) pixel(1,z,y); 00440 00441 temp <<= 1; 00442 } 00443 y--; 00444 00445 } 00446 */ 00447 //motor_screen(); 00448 //bar_move(); 00449 //motor_value(22,44,66,2,0); 00450 ilab_logo(); 00451 00452 } 00453 00454 00455 if (display == 0) pixel(0,0,0);//cheat for small display 00456 00457 RX_in = 0; 00458 00459 delay_ms(1000); 00460 clear_screen(); 00461 00462 if (RX_in > 0)//revert to 115200 00463 { 00464 print_char(1,'1'); 00465 print_char(1,'1'); 00466 print_char(1,'5'); 00467 print_char(1,'2'); 00468 print_char(1,'0'); 00469 print_char(1,'0'); 00470 00471 baud_rate = BAUD; 00472 set_baud(BAUD);//115200 00473 00474 cli(); 00475 00476 EEPROM_write((unsigned int) BPS, 6); 00477 00478 sei();//Enable Interrupts 00479 } 00480 00481 else (set_baud(baud_rate)); 00482 00483 delay_ms(1000); 00484 clear_screen(); 00485 00486 00487 motor_screen(); 00488 motor_value(22,44,66,2,1); 00489 //main loop=================================================== 00490 while(1) 00491 { 00492 if(RX_in != RX_read) 00493 { 00494 x = RX_array[RX_read]; 00495 RX_read++; 00496 if(RX_read >= BUFFER) RX_read = 0; 00497 00498 //Backspace=================================================== 00499 if(x == 8) del_char(0); 00500 00501 //Special commands 00502 else if (x == 124) 00503 { 00504 //make sure the next byte is there 00505 while(RX_in == RX_read); 00506 00507 //0, clear screen====================================================== 00508 if(RX_array[RX_read] == 0)//^@ 00509 { 00510 clear_screen(); 00511 RX_read++; 00512 if(RX_read >= BUFFER) RX_read = 0; 00513 } 00514 00515 //demo mode 00516 else if(RX_array[RX_read] == 4)//^d 00517 { 00518 RX_in = 0, RX_read = 0; 00519 demo(); 00520 clear_screen(); 00521 RX_in = 0; 00522 } 00523 00524 00525 //reverse mode 00526 else if(RX_array[RX_read] == 18)//^r 00527 { 00528 reverse ^= 1; 00529 clear_screen(); 00530 RX_read++; 00531 if(RX_read >= BUFFER) RX_read = 0; 00532 00533 cli(); 00534 EEPROM_write((unsigned int) REV, reverse); 00535 sei(); 00536 } 00537 00538 00539 //toggle spasl screen 00540 else if(RX_array[RX_read] == 19)//^s 00541 { 00542 splash_screen ^= 1; 00543 //clear_screen(); 00544 RX_read++; 00545 if(RX_read >= BUFFER) RX_read = 0; 00546 00547 cli(); 00548 EEPROM_write((unsigned int) SPLASH, splash_screen); 00549 sei(); 00550 } 00551 00552 //display spasl screen 00553 else if(RX_array[RX_read] == 20)// 00554 { 00555 00556 00557 RX_in = 0, RX_read = 0; 00558 ilab_logo(); 00559 RX_in = 0; 00560 00561 } 00562 else 00563 { 00564 //set backlight (0 to 100)========================================================= 00565 if(RX_array[RX_read] == 2)//^b 00566 { 00567 RX_read++; 00568 if(RX_read >= BUFFER) RX_read = 0; 00569 while(RX_in == RX_read);//wait for byte 00570 BL_dutycycle = RX_array[RX_read]; 00571 00572 RX_read++; 00573 if(RX_read >= BUFFER) RX_read = 0; 00574 00575 set_backlight(BL_dutycycle); 00576 00577 cli(); 00578 EEPROM_write((unsigned int) BACKLIGHT, BL_dutycycle); 00579 sei(); 00580 00581 00582 00583 } 00584 00585 00586 //change baud rate========================================================= 00587 else if(RX_array[RX_read] == 7)//^g 00588 { 00589 RX_read++; 00590 if(RX_read >= BUFFER) RX_read = 0; 00591 while(RX_in == RX_read);//wait for byte 00592 //if (RX_array[RX_read] == '1') USART_Init( 1000000/2400-1);//4800 00593 //else if (RX_array[RX_read] == '2') USART_Init( 1000000/4800-1);//9600 00594 //else if (RX_array[RX_read] == '3') USART_Init( 1000000/9600-1);//19200 00595 //else if (RX_array[RX_read] == '4') USART_Init( 1000000/19200-1);//38400 00596 //else if (RX_array[RX_read] == '5') USART_Init( 1000000/28800-1);//57600 00597 //else if (RX_array[RX_read] == '6') USART_Init( 1000000/57600-1);//115200 00598 00599 if ((RX_array[RX_read] > '0') * (RX_array[RX_read] < '7')) baud_rate = (RX_array[RX_read]) - 48; 00600 00601 set_baud(baud_rate); 00602 00603 cli(); 00604 EEPROM_write((unsigned int) BPS, baud_rate); 00605 sei(); 00606 00607 RX_read++; 00608 if(RX_read >= BUFFER) RX_read = 0; 00609 00610 } 00611 00612 00613 //set x or y========================================================= 00614 else if((RX_array[RX_read] == 24) | (RX_array[RX_read] == 25))//^x or ^y 00615 { 00616 RX_read++; 00617 if(RX_read >= BUFFER) RX_read = 0; 00618 while(RX_in == RX_read);//wait for byte 00619 if (RX_array[RX_read-1] == 24) x_offset = RX_array[RX_read]; 00620 else if (RX_array[RX_read-1] == 25) y_offset = RX_array[RX_read]; 00621 00622 RX_read++; 00623 if(RX_read >= BUFFER) RX_read = 0; 00624 00625 if (x_offset > 159) x_offset = 159; 00626 if (y_offset > 127) y_offset = 127; 00627 00628 } 00629 00630 //set pixel========================================================= 00631 else if (RX_array[RX_read] == 16)//^p 00632 { 00633 //need 3 bytes 00634 for (y = 0; y < 3; y++) 00635 { 00636 RX_read++; 00637 if(RX_read >= BUFFER) RX_read = 0; 00638 while(RX_in == RX_read);//wait for byte 00639 } 00640 00641 pixel(RX_array[RX_read], RX_array[RX_read-2], RX_array[RX_read-1]); 00642 00643 RX_read++; 00644 if(RX_read >= BUFFER) RX_read = 0; 00645 00646 } 00647 00648 00649 //<ctrl>c, circle====================================================== 00650 else if(RX_array[RX_read] == 3)//^c 00651 { 00652 //need 4 bytes 00653 for (y = 0; y < 4; y++) 00654 { 00655 RX_read++; 00656 if(RX_read >= BUFFER) RX_read = 0; 00657 while(RX_in == RX_read);//wait for byte 00658 } 00659 00660 circle(RX_array[RX_read], RX_array[RX_read-3], RX_array[RX_read-2], RX_array[RX_read-1]); 00661 00662 RX_read++; 00663 if(RX_read >= BUFFER) RX_read = 0; 00664 } 00665 00666 00667 //<ctrl>e, erase block====================================================== 00668 else if(RX_array[RX_read] == 5)//^e 00669 { 00670 //need 4 bytes 00671 for (y = 0; y < 4; y++) 00672 { 00673 RX_read++; 00674 if(RX_read >= BUFFER) RX_read = 0; 00675 while(RX_in == RX_read);//wait for byte 00676 } 00677 00678 erase_block(RX_array[RX_read-3], RX_array[RX_read-2], RX_array[RX_read-1], RX_array[RX_read]); 00679 00680 RX_read++; 00681 if(RX_read >= BUFFER) RX_read = 0; 00682 } 00683 00684 00685 //box====================================================== 00686 else if(RX_array[RX_read] == 15)//^o 00687 { 00688 //need 4 bytes 00689 for (y = 0; y < 4; y++) 00690 { 00691 RX_read++; 00692 if(RX_read >= BUFFER) RX_read = 0; 00693 while(RX_in == RX_read);//wait for byte 00694 } 00695 00696 box(RX_array[RX_read-3], RX_array[RX_read-2], RX_array[RX_read-1], RX_array[RX_read]); 00697 00698 RX_read++; 00699 if(RX_read >= BUFFER) RX_read = 0; 00700 } 00701 00702 00703 //line======================================================== 00704 else if (RX_array[RX_read] == 12)//^l 00705 { 00706 //need 5 bytes 00707 for (y = 0; y < 5; y++) 00708 { 00709 RX_read++; 00710 if(RX_read >= BUFFER) RX_read = 0; 00711 while(RX_in == RX_read);//wait for byte 00712 } 00713 00714 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]); 00715 RX_read++; 00716 if(RX_read >= BUFFER) RX_read = 0; 00717 } 00718 //Used 0,2,3,4,5,7,12,15,16,18,19,24,25 00719 //new Command I add 00720 //20 : show logo 00721 //14 : solid_box 00722 //6 : thick_circle 00723 //8 : bar 00724 //9 : moto_value change 00725 //10 : motor_screen 00726 else //solid box====================================================== 00727 if(RX_array[RX_read] == 14) 00728 { 00729 //need 4 bytes 00730 for (y = 0; y < 4; y++) 00731 { 00732 RX_read++; 00733 if(RX_read >= BUFFER) RX_read = 0; 00734 while(RX_in == RX_read);//wait for byte 00735 } 00736 00737 solid_box(RX_array[RX_read-3], RX_array[RX_read-2], RX_array[RX_read-1], RX_array[RX_read]); 00738 00739 RX_read++; 00740 if(RX_read >= BUFFER) RX_read = 0; 00741 } 00742 //<thick circle====================================================== 00743 else if(RX_array[RX_read] == 6) 00744 { 00745 //need 5 bytes 00746 for (y = 0; y < 5; y++) 00747 { 00748 RX_read++; 00749 if(RX_read >= BUFFER) RX_read = 0; 00750 while(RX_in == RX_read);//wait for byte 00751 } 00752 00753 thick_circle(RX_array[RX_read], RX_array[RX_read-4],RX_array[RX_read-3], RX_array[RX_read-2], RX_array[RX_read-1]); 00754 00755 RX_read++; 00756 if(RX_read >= BUFFER) RX_read = 0; 00757 } 00758 //bar===================================================== 00759 else if(RX_array[RX_read] == 8) 00760 { 00761 //need 6 bytes 00762 for (y = 0; y < 6; y++) 00763 { 00764 RX_read++; 00765 if(RX_read >= BUFFER) RX_read = 0; 00766 while(RX_in == RX_read);//wait for byte 00767 } 00768 //void bar(char x1, char y1, char width, char length,char value,char v_h) 00769 00770 //Checksum 00771 //if(((RX_array[RX_read-6]+RX_array[RX_read-5]+RX_array[RX_read-4]+ 00772 // RX_array[RX_read-3]+RX_array[RX_read-2]+RX_array[RX_read-1])& 0xFF ) == RX_array[RX_read]) 00773 //{ 00774 //if(RX_array[RX_read] == 8) 00775 // bar(RX_array[RX_read-6],RX_array[RX_read-5],RX_array[RX_read-4],RX_array[RX_read-3], RX_array[RX_read-2], RX_array[RX_read-1]); 00776 //} 00777 00778 bar(RX_array[RX_read-5],RX_array[RX_read-4],RX_array[RX_read-3], RX_array[RX_read-2], RX_array[RX_read-1],RX_array[RX_read]); 00779 RX_read++; 00780 if(RX_read >= BUFFER) RX_read = 0; 00781 } 00782 //===== change the motor value 00783 else if(RX_array[RX_read] == 9) 00784 { 00785 //void motor_value(int speed,int turn,int cap,int mode,int estop) 00786 //need 5 bytes 00787 for (y = 0; y < 5; y++) 00788 { 00789 RX_read++; 00790 if(RX_read >= BUFFER) RX_read = 0; 00791 while(RX_in == RX_read);//wait for byte 00792 } 00793 00794 00795 motor_value(RX_array[RX_read-4],RX_array[RX_read-3], RX_array[RX_read-2], RX_array[RX_read-1],RX_array[RX_read]); 00796 RX_read++; 00797 if(RX_read >= BUFFER) RX_read = 0; 00798 } 00799 else if(RX_array[RX_read] == 10) 00800 { 00801 RX_in = 0, RX_read = 0; 00802 motor_screen(); 00803 RX_in = 0; 00804 } 00805 00806 00807 } 00808 00809 } 00810 00811 //print character to the screen=============================================== 00812 else 00813 { 00814 del_char(1); 00815 print_char(1, x); 00816 } 00817 } 00818 00819 } 00820 00821 //demo(); 00822 00823 00824 00825 00826 } 00827 00828 00829 /* 00830 char unzip(char x,char y) 00831 { 00832 char i = 0, j = 0, k = 0; 00833 char count = 0; 00834 char size = 0; 00835 char data = 0; 00836 char dx,dy; 00837 while(size < 512) 00838 { 00839 count = ilablogo[i]; 00840 data = ilablogo[i+1]; 00841 00842 for(j = 0 ; i < count ; j++ ) 00843 { 00844 for(k = 0 ; k < 8 ; k++) 00845 { 00846 dx = x + ((size+j)%8)*8 + k ; 00847 dy = y + 64 - (( size + j ) / 8 ); 00848 00849 if (data & 0x80) pixel(1,dx,dy); 00850 data <<= 1; 00851 } 00852 00853 } 00854 00855 i += 2; 00856 size += count; 00857 00858 } 00859 */ 00860 00861 00862 /* 00863 00864 00865 ====================BitMap Version========================== 00866 } 00867 00868 void ilab_logo(int x,int y) 00869 { 00870 //Logo========================================================== 00871 00872 int i = 0; 00873 int j = 0; 00874 int k = 0; 00875 char temp; 00876 if (display == 1) y += 32, x += 16; 00877 00878 for (i = 0; i < 64; i++) 00879 { 00880 00881 00882 for (j=0; j< 8; j++) 00883 { 00884 temp = ilablogo[i*8+j]; 00885 for (k=0; k< 8; k++) 00886 { 00887 if (temp & 0x80) pixel(1,x+j*8+k,y-i); 00888 00889 temp <<= 1; 00890 } 00891 } 00892 } 00893 00894 } 00895 */ 00896 00897 void ilab_logo(void) 00898 { 00899 00900 circle(1,80,64,55); 00901 circle(1,80,64,56); 00902 circle(1,80,64,57); 00903 circle(1,80,64,58); 00904 circle(1,80,64,59) ; 00905 circle(1,80,64,60); 00906 00907 //Draw i 00908 circle(1,47,80,10); 00909 circle(1,47,80,9) ; 00910 solid_box(44,75,50,40); 00911 00912 //Draw L 00913 solid_box(58,85,65,40); 00914 solid_box(58,47,80,40); 00915 00916 //Draw a 00917 thick_circle(1,85,50,10,2); 00918 //circle(1,85,50,9); 00919 solid_box(93,62,99,40); 00920 00921 ////Draw b 00922 solid_box(103,80,108,40); 00923 circle(1,114,50,10); 00924 circle(1,114,50,9) ; 00925 00926 00927 00928 } 00929 void small_ilab_logo(void) 00930 { 00931 circle(1,135,50,24) ; 00932 circle(1,135,50,22); 00933 00934 //Draw i 00935 circle(1,121,56,4); 00936 solid_box(120,38,122,54); 00937 00938 //Draw L 00939 solid_box(126,38,128,60); 00940 solid_box(126,38,133,40); 00941 00942 //Draw a 00943 circle(1,135,43,4); 00944 solid_box(139,39,141,47); 00945 00946 ////Draw b 00947 solid_box(143,39,145,60); 00948 circle(1,149,43,4); 00949 00950 } 00951 00952 void move(int x,int y) 00953 { 00954 x_offset = x; 00955 y_offset = y; 00956 00957 } 00958 00959 void motor_screen(void) 00960 { 00961 00962 // void bar(char x1, char y1, char width, char length,char value,char v_h) 00963 00964 move(0,68); 00965 print_string(1,"Speed:"); 00966 bar(55,60,50,10,0,1); 00967 00968 move(0,55); 00969 print_string(1,"Turn :"); 00970 bar(55,47,50,10,0,1); 00971 00972 00973 move(0,42); 00974 print_string(1,"CAP :"); 00975 bar(55,34,50,10,0,1); 00976 00977 move(0,30); 00978 print_string(1,"Mode :"); 00979 move(37,30); 00980 print_string(1,"Auto"); 00981 00982 move(20,90); 00983 print_string(1,"Emergency Mode: ON"); 00984 box(20,100,130,120); 00985 00986 00987 small_ilab_logo(); 00988 00989 00990 } 00991 void bar_move(void) 00992 { 00993 int i; 00994 for(i = 0; i< 20 ;i++) 00995 bar(55,60,50,10,100-i*5,1); 00996 00997 for(i = 0; i< 20 ;i++) 00998 bar(55,47,50,10,100-i*5,1); 00999 01000 for(i = 0; i< 20 ;i++) 01001 bar(55,34,50,10,100-i*5,1); 01002 01003 } 01004 01005 void motor_value(int speed,int turn,int cap,int mode,int estop) 01006 { 01007 char manual[] = "Manual"; 01008 erase_block(37,60,37+6*3,68); 01009 move(37,68); 01010 print_dec(1,speed); 01011 bar(55,60,50,10,speed,1); 01012 01013 erase_block(37,47,37+6*3,55); 01014 move(37,55); 01015 print_dec(1,turn); 01016 bar(55,47,50,10,turn,1); 01017 01018 erase_block(37,34,37+6*3,42); 01019 move(37,42); 01020 print_dec(1,cap); 01021 bar(55,34,50,10,cap,1); 01022 move(37,30); 01023 01024 //void erase_block(char x1, char y1, char x2, char y2) 01025 erase_block(37,22,100,30); 01026 01027 move(37,30); 01028 //print_string(1,"Manual"); 01029 if(mode == 1) 01030 print_string(1,manual); 01031 else if (mode ==2) 01032 print_string(1,"Semi-Auto"); 01033 else if (mode ==3) 01034 print_string(1,"Auto"); 01035 01036 01037 erase_block(116,82,116+6*3,90); 01038 01039 move(116,90); 01040 if(estop == 1) 01041 { 01042 print_string(1,"ON "); 01043 solid_box(21,101,129,120); 01044 } 01045 else 01046 { 01047 print_string(1,"OFF"); 01048 erase_block(21,101,129,119); 01049 } 01050 01051 } 01052 01053 01054 void ioinit (void) 01055 { 01056 01057 //1 = output, 0 = input 01058 01059 DDRB = 0b00000011; //PB0 and PB1 are outs 01060 DDRD = 0b11111100; //PD2-PD7 are also outs. Ports B and D are the data bus. 01061 01062 PORTB |= (1<<BL_EN);//Backlight off 01063 DDRB |= (1<<BL_EN);//set PB2 as output 01064 01065 if (display == 0) 01066 { 01067 DDRC = ((1<<EN) | (1<<RS) | (1<<R_W) | (1<<RESET) | (1<<CS1) | (1<<CS2)); 01068 PORTC = ((1<<EN) | (1<<RS) | (1<<R_W) | (1<<RESET) | (1<<CS1) | (1<<CS2)); 01069 } 01070 01071 else if (display == 1) 01072 { 01073 PORTC = ((1<<WR) | (1<<RD) | (1<<CE) | (1<<CD) | (1<<HALT) | (1<<RST)); 01074 DDRC = ((1<<WR) | (1<<RD) | (1<<CE) | (1<<CD) | (1<<HALT) | (1<<RST)); 01075 } 01076 01077 //Init timer 2 01078 TCCR2B = (1<<CS21); //Set Prescaler to 8. CS21=1 01079 01080 //Set up Timer 0 01081 TCCR0A = 0x02;//CTC mode 01082 //TCCR0B = 0x02; 01083 TIMSK0 = 0x02;//enable OCR0A 01084 //OCR0B = 255 - BL_dutycycle; 01085 01086 //OCR0A = 255 - (100 - BL_dutycycle); 01087 OCR0A = BL_dutycycle; 01088 01089 } 01090 01091 //General short delays 01092 void delay_ms(uint16_t x) 01093 { 01094 for (; x > 0 ; x--) 01095 { 01096 delay_us(250); 01097 delay_us(250); 01098 delay_us(250); 01099 delay_us(250); 01100 } 01101 01102 } 01103 01104 //General short delays 01105 void delay_us(uint8_t x) 01106 { 01107 char temp; 01108 01109 if (x == 0) temp = 1; 01110 else temp = x; 01111 01112 TIFR2 |= 0x01;//Clear any interrupt flags on Timer2 01113 01114 TCNT2 = 256 - temp; //256 - 125 = 131 : Preload timer 2 for x clicks. Should be 1us per click 01115 01116 while(!(TIFR2 & 0x01)); 01117 01118 if (x == 0) return;//this is for display timing 01119 01120 //The prescaler doesn't allow for a setting of 16, just 8 or 32. So, we do this twice. 01121 TIFR2 |= 0x01; 01122 01123 TCNT2 = 256 - temp; //256 - 125 = 131 : Preload timer 2 for x clicks. Should be 1us per click 01124 01125 while(!(TIFR2 & 0x01)); 01126 01127 } 01128 01129 void USART_Init( unsigned int ubrr) 01130 { 01131 // Set baud rate 01132 UBRR0H = (unsigned char)(ubrr>>8); 01133 UBRR0L = (unsigned char)ubrr; 01134 01135 // Enable receiver and transmitter 01136 UCSR0A = (1<<U2X0); 01137 UCSR0B = (1<<RXCIE0)|(1<<RXEN0)|(1<<TXEN0); //Enable Interrupts on receive character 01138 01139 UCSR0C = (1<<UCSZ00)|(1<<UCSZ01); 01140 sei(); 01141 } 01142 01143 void put_char(char byte) 01144 { 01145 /* Wait for empty transmit buffer */ 01146 while ( !( UCSR0A & (1<<UDRE0)) ); 01147 /* Put data into buffer, sends the data */ 01148 UDR0 = byte; 01149 } 01150 01151 //delay for display timing 01152 void delay(void) 01153 { 01154 char y; 01155 01156 //for(y = 0; y < 20; y++) 01157 for(y = 0; y < 30; y++) 01158 { 01159 asm volatile ("nop"); 01160 01161 } 01162 01163 /* 01164 asm volatile ("nop"); 01165 asm volatile ("nop"); 01166 asm volatile ("nop"); 01167 asm volatile ("nop"); 01168 asm volatile ("nop"); 01169 */ 01170 01171 } 01172 01173 //set data port 01174 void set_data(char data) 01175 { 01176 //PORTB 01177 //DB0 = PB0 01178 //DB1 = PB1 01179 01180 PORTB &= 0xFC; 01181 01182 //PORTD 01183 //DB2 = PD2 01184 //DB3 = PD3 01185 //DB4 = PD4 01186 //DB5 = PD5 01187 //DB6 = PD6 01188 //DB7 = PD7 01189 01190 PORTD &= 0x03; 01191 01192 PORTB |= (data & 0x03); 01193 PORTD |= (data & 0xFC); 01194 01195 } 01196 01197 void clear_screen(void) 01198 { 01199 char x, y; 01200 int z; 01201 01202 if (display == 0) 01203 { 01204 delay(); 01205 01206 for (x = 0; x < 8; x++) 01207 { 01208 //set x address 01209 //set control lines 01210 PORTC &= ~((1 << EN) | (1 << R_W) | (1 << RS));//down 01211 01212 set_data(0xB8 | x); 01213 delay(); 01214 PORTC |= (1 << EN);//up 01215 delay(); 01216 PORTC &= ~(1 << EN);//down 01217 delay(); 01218 PORTC |= ((1 << EN) | (1 << R_W) |(1 << RS));//all high 01219 01220 delay(); 01221 01222 //Set y address to zero 01223 //set control lines 01224 PORTC &= ~((1 << EN) | (1 << R_W) | (1 << RS));//down 01225 01226 set_data(0x40); 01227 delay(); 01228 PORTC |= (1 << EN);//up 01229 delay(); 01230 PORTC &= ~(1 << EN);//down 01231 delay(); 01232 PORTC |= ((1 << EN) | (1 << R_W) | (1 << RS));//all high 01233 01234 if (reverse == 1) set_data(0xFF); 01235 else set_data(0); 01236 01237 for (y = 0; y < 64; y++) 01238 { 01239 delay(); 01240 //y address increments after every write 01241 //write data, CS1, left half of screen 01242 PORTC &= ~((1 << EN) | (1 << R_W) | (1 << CS1));//down 01243 delay(); 01244 PORTC |= (1 << EN);//up 01245 delay(); 01246 PORTC &= ~(1 << EN);//down 01247 delay(); 01248 PORTC |= ((1 << EN) | (1 << R_W) | (1 << CS1));//all high 01249 01250 delay(); 01251 01252 //write data, CS2, right half of screen 01253 PORTC &= ~((1 << EN) | (1 << R_W) | (1 << CS2));//down 01254 delay(); 01255 PORTC |= (1 << EN);//up 01256 delay(); 01257 PORTC &= ~(1 << EN);//down 01258 delay(); 01259 PORTC |= ((1 << EN) | (1 << R_W) | (1 << CS2));//all high 01260 } 01261 01262 } 01263 01264 x_offset = 0; 01265 y_offset = 63; 01266 } 01267 01268 else if (display == 1) 01269 { 01270 while(!(read(0) & 3));//read status 01271 write(1, 0); 01272 while(!(read(0) & 3));//read status 01273 write(1, 0); 01274 while(!(read(0) & 3));//read status 01275 write(0, 0x24); 01276 01277 for(z = 0; z < 0xA00; z++) 01278 { 01279 while(!(read(0) & 3));//read status 01280 if (reverse == 1) write(1,0xFF); 01281 else if (reverse == 0) write(1, 0); 01282 while(!(read(0) & 3));//read status 01283 write(0, 0xC0); 01284 } 01285 01286 x_offset = 0; 01287 y_offset = 127; 01288 } 01289 01290 } 01291 01292 //sets horizontal position (data sheet calls this "y". Man, I just wanna punch these guys in the mouth...) 01293 void set_x(char x_spot) 01294 { 01295 //char a; 01296 01297 //a = x_spot; 01298 //if (a == 0) a = 63; 01299 //else a--; 01300 01301 if (x_spot == 0) x_spot = 63; 01302 else x_spot--; 01303 01304 PORTC &= ~((1 << EN) | (1 << R_W) | (1 << RS));//down 01305 01306 set_data(0x40 | x_spot); 01307 delay(); 01308 PORTC |= (1 << EN);//up 01309 delay(); 01310 PORTC &= ~(1 << EN);//down 01311 delay(); 01312 PORTC |= ((1 << EN) | (1 << R_W) | (1 << RS));//all high 01313 delay(); 01314 01315 } 01316 01317 void print_string(char S_R, char str[]) 01318 { 01319 int i; 01320 for(i=0;i<strlen(str);i++) 01321 print_char(S_R,str[i]); 01322 } 01323 01324 void print_dec(char S_R,int number) 01325 { 01326 char buf[5]; 01327 //sprintf(buf,"%3d",number); 01328 itoa(number,buf,10); 01329 print_string(S_R,buf); 01330 01331 } 01332 01333 //prints (S_R = 1) or erases (S_R = 0) a character to the screen 01334 //at x_offset, y_offset. Automatically augments offsets for next write 01335 void print_char(char S_R, char txt) 01336 { 01337 short text_array_offset = (txt - 32)*5, j; 01338 char x, k; 01339 01340 01341 01342 for (j = text_array_offset; j < text_array_offset+5; j++) 01343 { 01344 k = text_array[j]; 01345 01346 for (x = 0; x < 8; x++) 01347 { 01348 if(k & 0x80) pixel(S_R, x_offset, y_offset - x); 01349 01350 k <<= 1; 01351 } 01352 01353 x_offset++; 01354 01355 } 01356 01357 x_offset++; 01358 01359 if ((x_offset + 6) > (127 + display*32)) 01360 { 01361 x_offset = 0; 01362 if (y_offset <= 7) 01363 { 01364 y_offset = 63 + display*64; 01365 //clear_screen(); 01366 } 01367 else y_offset -= 8; 01368 01369 } 01370 01371 if (display == 0) pixel(0,0,0);//cheat for smallo display 01372 01373 } 01374 01375 01376 void set_page(char page) 01377 { 01378 //set control lines 01379 PORTC &= ~((1 << EN) | (1 << R_W) | (1 << RS));//down 01380 01381 set_data(0xB8 | page); 01382 delay(); 01383 PORTC |= (1 << EN);//up 01384 delay(); 01385 PORTC &= ~(1 << EN);//down 01386 delay(); 01387 PORTC |= ((1 << EN) | (1 << R_W) |(1 << RS));//all high 01388 01389 delay(); 01390 } 01391 01392 01393 void write_byte(char byte, char side) 01394 { 01395 PORTC |= (1 << RS);//make sure this thing is high 01396 set_data(byte); 01397 01398 delay(); 01399 //y address increments after every write 01400 //write data, CS1, left half of screen 01401 if (side == 1) PORTC &= ~((1 << EN) | (1 << R_W) | (1 << CS1));//down 01402 else if (side == 2) PORTC &= ~((1 << EN) | (1 << R_W) | (1 << CS2));//down 01403 delay(); 01404 PORTC |= (1 << EN);//up 01405 delay(); 01406 PORTC &= ~(1 << EN);//down 01407 delay(); 01408 PORTC |= ((1 << EN) | (1 << R_W) | (1 << CS1) | (1 << CS2));//all high 01409 01410 01411 } 01412 01413 01414 //display on 01415 void display_on(void) 01416 { 01417 set_data(0x3F); 01418 PORTC &= ~((1 << EN) | (1 << R_W) | (1 << RS));//down 01419 delay(); 01420 PORTC |= (1 << EN);//up 01421 delay(); 01422 PORTC &= ~(1 << EN);//down 01423 PORTC |= ((1 << EN) | (1 << R_W) | (1 << RS));//all high 01424 01425 } 01426 01427 //mapping to cartesian coordinates, (0,0) is in the lower left corner, (127,63) is in the upper right 01428 //void pixel(char S_R, char x, char y){ 01429 //pixel(S_R,x,y); 01430 //} 01431 void pixel(char S_R, char x, char y) 01432 { 01433 static char temp_page, temp_side, temp_x = 0, temp_data1 = 0, temp_data2 = 0; 01434 short address = 0; 01435 char byte = 0; 01436 01437 //don't try to print something outside of our range 01438 if (x > (127 + display*32)) return; 01439 if (y > (63 + display*64)) return; 01440 01441 if (reverse == 1) S_R ^= 1; 01442 01443 if (display == 0) 01444 { 01445 if (x >= 64) temp_side = 2, temp_x = x - 64; 01446 else temp_side = 1, temp_x = x; 01447 01448 temp_page = 7 - (y >> 3); 01449 01450 //data = (1 << (y - ((7 - temp_page) * 8))); 01451 temp_data1 = (1 << (7 - (y - ((7 - temp_page) * 8)))); 01452 01453 set_page(temp_page); 01454 set_x(temp_x); 01455 //set_x(0); 01456 01457 //need to read the existing byte here, then or it with the new byte 01458 temp_data2 = read_byte(temp_x, temp_side); 01459 01460 if (S_R == 0) 01461 { 01462 temp_data1 = ~temp_data1; 01463 temp_data1 &= temp_data2; 01464 } 01465 01466 else temp_data1 |= temp_data2; 01467 01468 set_x(temp_x);//reset this... 01469 01470 write_byte(temp_data1, temp_side); 01471 } 01472 01473 else if (display == 1) 01474 { 01475 address = ((127-y) * 20) + (x / 8); 01476 01477 //set address pointer 01478 while(!(read(0) & 3));//read status 01479 byte = (char)(address & 0xFF); 01480 01481 write(1, byte);//20 bytes, 160/8 01482 01483 while(!(read(0) & 3));//read status 01484 byte = (char)((address & 0xFF00) >> 8); 01485 01486 write(1, byte); 01487 01488 while(!(read(0) & 3));//read status 01489 write(0, 0x24); 01490 01491 byte = ~(x % 8); 01492 01493 byte |= 0xF8; 01494 if (S_R == 0) byte &= 0xF7; 01495 01496 //set-reset bit 01497 while(!(read(0) & 3));//read status 01498 write(0, byte); 01499 } 01500 01501 } 01502 01503 01504 //draws (S_R = 1) or erases (S_R = 0) a line from x1, y1 to x2, y2 01505 void line(char S_R, char x1, char y1, char x2, char y2) 01506 { 01507 float m, q; 01508 int x_dif, y_dif; 01509 int a, b, c; 01510 01511 if ((x1 > (127 + display*32)) | (x2 > (127 + display*32))) return; 01512 if ((y1 > (63 + display*64)) | (y2 > (63 + display*64))) return; 01513 01514 x_dif = x2 - x1; 01515 y_dif = y2 - y1; 01516 if (y_dif < 0) y_dif *= (-1); 01517 01518 01519 m = (float)(y2 - y1) / (float)(x2 - x1); 01520 01521 b = y1-(m*x1); 01522 01523 if(x_dif >= y_dif) 01524 { 01525 for (a = x1; a <= x2; a++) 01526 { 01527 pixel(S_R, (char)a, (char)((m*a)+b)); 01528 01529 } 01530 } 01531 01532 else 01533 { 01534 if (y2 > y1) 01535 { 01536 for (a = y1; a <= y2; a++) 01537 { 01538 if (x_dif == 0) c = x1; 01539 else 01540 { 01541 q = (((float)(a-b))/m); 01542 c = rnd(q); 01543 } 01544 01545 pixel(S_R, (char)c, (char)a); 01546 01547 } 01548 } 01549 01550 else if (y1 > y2) 01551 { 01552 for (a = y1; a >= y2; a--) 01553 { 01554 if (x_dif == 0) c = x1; 01555 else 01556 { 01557 q = (((float)(a-b))/m); 01558 c = rnd(q); 01559 } 01560 01561 pixel(S_R, (char)c, (char)a); 01562 01563 } 01564 } 01565 } 01566 01567 if (display == 0) pixel(0,0,0);//cheat 01568 } 01569 01570 01571 01572 char read_byte(char byte, char side) 01573 { 01574 char data1 = 0, data2 = 0; 01575 01576 if (byte == 0) byte = 63; 01577 else byte--; 01578 01579 set_x(byte); 01580 01581 PORTC |= ((1 << RESET) | (1 << EN) | (1 << R_W) | (1 << CS1) | (1 << CS2) | (1 << RS));//all high, just to make sure 01582 01583 DDRB &= 0xFC;//PB0 and PB1 as inputs 01584 DDRD = 0; 01585 01586 //PORTB |= 0x03;//pullups...? 01587 //PORTD |= 0XFC; 01588 //PORTB = 0; 01589 //PORTD = 0; 01590 01591 delay(); 01592 delay(); 01593 01594 if (side == 1) PORTC &= ~((1 << EN) | (1 << CS1));//down 01595 else if (side == 2) PORTC &= ~((1 << EN) | (1 << CS2));//down 01596 //PORTC &= ~((1 << EN) | (1 << CS1));//down 01597 01598 01599 01600 delay(); 01601 delay(); 01602 PORTC |= (1 << EN);//up 01603 01604 01605 delay(); 01606 delay(); 01607 01608 /* 01609 data1 = PINB; 01610 data1 &= 0x03; 01611 01612 data2 = PIND; 01613 data2 &= 0xFC; 01614 01615 data1 |= data2; 01616 */ 01617 01618 PORTC &= ~(1 << EN);//down 01619 01620 01621 01622 delay(); 01623 delay(); 01624 01625 PORTC |= ((1 << RESET) | (1 << EN) | (1 << R_W) | (1 << CS1) | (1 << CS2) | (1 << RS));//all high 01626 01627 //DDRB = 0b00000011; //PB0 and PB1 are outs 01628 //DDRD = 0b11111100; //PD2-PD7 are also outs. Ports B and D are the data bus. 01629 01630 01631 01632 delay(); 01633 delay(); 01634 01635 //PORTC &= ~((1 << EN) | (1 << CS1));//down 01636 if (side == 1) PORTC &= ~((1 << EN) | (1 << CS1));//down 01637 else if (side == 2) PORTC &= ~((1 << EN) | (1 << CS2));//down 01638 01639 01640 01641 delay(); 01642 delay(); 01643 PORTC |= (1 << EN);//up 01644 01645 //we can read here... 01646 01647 delay(); 01648 delay(); 01649 data1 = PINB; 01650 data1 &= 0x03; 01651 01652 data2 = PIND; 01653 data2 &= 0xFC; 01654 01655 data1 |= data2; 01656 //while(1); 01657 01658 PORTC &= ~(1 << EN);//down 01659 01660 //cannot read here... 01661 //while(1); 01662 01663 PORTC |= ((1 << RESET) | (1 << EN) | (1 << R_W) | (1 << CS1) | (1 << CS2) | (1 << RS));//all high 01664 01665 DDRB |= 0x03; //PB0 and PB1 are outs 01666 DDRD |= 0xFC; //PD2-PD7 are also outs. Ports B and D are the data bus. 01667 01668 delay(); 01669 01670 return data1; 01671 01672 } 01673 01674 //rounds a floar to the nearest int 01675 int rnd(float number) 01676 { 01677 int a; 01678 float b; 01679 01680 a = number / 1; 01681 b = number - a; 01682 01683 if (b >= 0.5) a++; 01684 01685 return a; 01686 01687 } 01688 01689 //draws (S_R = 1) or erases (S_R = 0) a circle ar x, y with radius r 01690 void circle(char S_R, int x, int y, int r) 01691 { 01692 int x1 = 0, x2 = 0; 01693 int x_line = 0, y_line = 0; 01694 int temp_y; 01695 int temp_x; 01696 01697 x1 = x - r; 01698 x2 = x + r; 01699 01700 for (temp_x = x1; temp_x <= x2; temp_x++) 01701 { 01702 temp_y = ((sqrt((r*r) - ((temp_x - x)*(temp_x - x)))) - y); 01703 01704 temp_y *= (-1); 01705 01706 if (temp_x > x1) 01707 { 01708 line(S_R, (char)x_line, (char)y_line, (char)temp_x, (char)temp_y); 01709 line(S_R, (char)x_line, (char)(2*y - y_line), (char)temp_x, (char)(2*y - temp_y)); 01710 } 01711 01712 else 01713 { 01714 pixel(S_R, (char)temp_x, (char)temp_y); 01715 pixel(S_R, (char)temp_x, (char)(y + y - temp_y)); 01716 } 01717 01718 x_line = temp_x; 01719 y_line = temp_y; 01720 01721 } 01722 01723 if (display == 0) pixel(0,0,0);//cheat 01724 01725 } 01726 01727 void thick_circle(char S_R, int x, int y, int r,int thickness) 01728 { 01729 int i; 01730 if(thickness > r) 01731 thickness = r; 01732 for(i = 0 ; i < thickness ; i++) 01733 circle(S_R,x,y,r-i); 01734 } 01735 01736 01737 void EEPROM_write(unsigned int uiAddress, unsigned char ucData) 01738 { 01739 /* Wait for completion of previous write */ 01740 while(EECR & (1<<EEPE)) 01741 ; 01742 /* Set up address and Data Registers */ 01743 EEAR = uiAddress; 01744 EEDR = ucData; 01745 /* Write logical one to EEMPE */ 01746 EECR |= (1<<EEMPE); 01747 /* Start eeprom write by setting EEPE */ 01748 EECR |= (1<<EEPE); 01749 } 01750 01751 unsigned char EEPROM_read(unsigned int uiAddress) 01752 { 01753 /* Wait for completion of previous write */ 01754 while(EECR & (1<<EEPE)) 01755 ; 01756 /* Set up address register */ 01757 EEAR = uiAddress; 01758 /* Start eeprom read by writing EERE */ 01759 EECR |= (1<<EERE); 01760 /* Return data from Data Register */ 01761 return EEDR; 01762 } 01763 01764 void set_backlight(unsigned char dutycycle) 01765 { 01766 //Set up Timer 0 01767 TCCR0A = 0x02;//CTC mode 01768 //TCCR0B = 0x02; 01769 //TIMSK0 = 0x02;//enable OCR0A 01770 //OCR0B = 255 - BL_dutycycle; 01771 01772 //OCR0A = 255 - (100 - BL_dutycycle); 01773 //OCR0A = dutycycle; 01774 01775 //SREG |= 0x80; 01776 01777 01778 if(BL_dutycycle >= 100) 01779 { 01780 TCCR0B = 0; 01781 TIMSK0 = 0;//disable timer ints 01782 01783 //Backlight on 01784 PORTB &= (~(1<<BL_EN)); 01785 } 01786 else if (BL_dutycycle == 0) 01787 { 01788 TCCR0B = 0; 01789 TIMSK0 = 0;//disable timer ints 01790 01791 //Backlight off 01792 PORTB |= (1<<BL_EN); 01793 } 01794 01795 01796 else 01797 { 01798 TCCR0B = 0; 01799 TIMSK0 = 0;//disable timer ints 01800 01801 OCR0A = 100 - BL_dutycycle; 01802 01803 TIMSK0 = 0x02;//enable match on A 01804 TCCR0B = 0x02; 01805 01806 SREG |= 0x80; 01807 01808 } 01809 01810 } 01811 01812 01813 //Deletes a full character space. Endpoint == 0 for a backwards delete, 01814 //Endpoint != 0 to erase spot for a new character write 01815 void del_char(char endpoint) 01816 { 01817 char a, y; 01818 01819 if (endpoint == 0)//Backwards delete 01820 { 01821 if (x_offset <= 5) 01822 { 01823 x_offset += (120 + display*36); 01824 y_offset += 8; 01825 01826 if (y_offset > (63 + display*64)) y_offset -= (64 + display*64); 01827 } 01828 01829 else x_offset -= 6; 01830 } 01831 01832 for (a = x_offset; a < x_offset + 6; a++) 01833 { 01834 for (y = y_offset - 7; y <= y_offset; y++) 01835 { 01836 pixel(0, a, y); 01837 01838 } 01839 } 01840 01841 if (display == 0) pixel(0,0,0);//cheat 01842 } 01843 01844 01845 //demonstration code 01846 void demo(void) 01847 { 01848 char x, y, temp; 01849 unsigned char x_endpoint, y_endpoint; 01850 int q = 0; 01851 01852 x_endpoint = 127 + display*32; 01853 y_endpoint = 63 + display*64; 01854 01855 while(1) 01856 { 01857 01858 ilab_logo(); 01859 ilab_logo(); 01860 ilab_logo(); 01861 ilab_logo(); 01862 clear_screen(); 01863 x_offset = 0; 01864 y_offset = y_endpoint; 01865 01866 01867 01868 for (y = 0; y < 5; y++) 01869 { 01870 for (x = 32; x < 123; x++) 01871 { 01872 del_char(1); 01873 print_char(1, x); 01874 if (RX_in > 0) return; 01875 } 01876 } 01877 01878 clear_screen(); 01879 01880 for (y = 0; y < 5; y++) 01881 { 01882 for (x = 32; x < 123; x++) 01883 { 01884 //x_offset += 4; 01885 y_offset -= 6; 01886 if (y_offset <= 8) y_offset = y_endpoint; 01887 del_char(1); 01888 print_char(1, x); 01889 if (RX_in > 0) return; 01890 } 01891 } 01892 01893 clear_screen(); 01894 01895 //draw circles================================ 01896 for (x = 5; x < (64 + display*56); x += 5) 01897 { 01898 circle(1,(64+display*16),(32 + display*32),x); 01899 if (RX_in > 0) return; 01900 } 01901 01902 01903 //draw lines=================================== 01904 y = y_endpoint; 01905 01906 for (x = 0; x < (x_endpoint); x += (16 + display*4)) 01907 { 01908 line(1,0,y,x,0); 01909 y -= (8 + display*8); 01910 } 01911 01912 y = 0; 01913 01914 for (x = 0; x < x_endpoint; x += (16 + display*4)) 01915 { 01916 line(1,x,0,x_endpoint,y); 01917 y += (8 + display*8); 01918 } 01919 01920 y = y_endpoint; 01921 01922 for (x = 0; x < x_endpoint; x += (16 + display*4)) 01923 { 01924 line(1,x,y_endpoint,x_endpoint,y); 01925 y -= (8 + display*8); 01926 } 01927 01928 y = 0; 01929 01930 for (x = 0; x < x_endpoint; x += (16 + display*4)) 01931 { 01932 line(1,0,y,x,y_endpoint); 01933 y += (8 + display*8); 01934 } 01935 01936 01937 01938 //erase circles================================ 01939 for (x = 5; x < (64 + display*56); x += 5) 01940 { 01941 circle(0,(64+display*16),(32 + display*32),x); 01942 if (RX_in > 0) return; 01943 } 01944 01945 //erase lines=================================== 01946 y = y_endpoint; 01947 01948 for (x = 0; x < x_endpoint; x += (16 + display*4)) 01949 { 01950 line(0,0,y,x,0); 01951 y -= (8 + display*8); 01952 } 01953 01954 y = 0; 01955 01956 for (x = 0; x < x_endpoint; x += (16 + display*4)) 01957 { 01958 line(0,x,0,x_endpoint,y); 01959 y += (8 + display*8); 01960 } 01961 01962 y = y_endpoint; 01963 01964 for (x = 0; x < x_endpoint; x += (16 + display*4)) 01965 { 01966 line(0,x,y_endpoint,x_endpoint,y); 01967 y -= (8 + display*8); 01968 } 01969 01970 y = 0; 01971 01972 for (x = 0; x < x_endpoint; x += (16 + display*4)) 01973 { 01974 line(0,0,y,x,y_endpoint); 01975 y += (8 + display*8); 01976 } 01977 01978 if (RX_in > 0) return; 01979 01980 //Boxes================================================================= 01981 y = 47 + display*64; 01982 for (x = 0; x <= (100 + display*40); x += 10) 01983 { 01984 erase_block(x, y, x+16, y+16); 01985 box(x, y, x+16, y+16); 01986 y -= (4 + display*3); 01987 } 01988 01989 01990 01991 y = (22 + display*6); 01992 //Logo================================================================= 01993 q = 0; 01994 while(q < 30) 01995 { 01996 temp = logo[q]; 01997 for (x = (100 + display*40); x < (108 + display*40); x++) 01998 { 01999 if (temp & 0x80) pixel(1,x,y); 02000 02001 temp <<= 1; 02002 } 02003 q++; 02004 temp = logo[q]; 02005 for (x = (108 + display*40); x < (116 + display*40); x++) 02006 { 02007 if (temp & 0x80) pixel(1,x,y); 02008 02009 temp <<= 1; 02010 } 02011 y--; 02012 q++; 02013 } 02014 02015 if (display == 0) pixel(0,0,0); 02016 02017 delay_ms(1000); 02018 reverse ^= 1; 02019 clear_screen(); 02020 02021 } 02022 02023 02024 } 02025 02026 02027 //erases a block of the screen. Block is decribed 02028 //by a diagonal line from x, y1 to x2, y2 02029 void erase_block(char x1, char y1, char x2, char y2) 02030 { 02031 static char temp_x = 0, temp_y = 0; 02032 02033 for (temp_y = y2; temp_y >= y1; temp_y--) 02034 { 02035 for (temp_x = x1; temp_x <= x2; temp_x++) 02036 { 02037 pixel(0, temp_x, temp_y); 02038 02039 } 02040 } 02041 02042 02043 02044 } 02045 02046 //draws a box. The box is decribed 02047 //by a diagonal line from x, y1 to x2, y2 02048 void box(char x1, char y1, char x2, char y2) 02049 { 02050 02051 line(1, x2, y2, x2, y1); 02052 line(1, x1, y2, x2, y2); 02053 line(1, x1, y2, x1, y1); 02054 line(1, x1, y1, x2, y1); 02055 } 02056 void solid_box(char x1, char y1, char x2, char y2) 02057 { 02058 int i; 02059 if(y1<y2) 02060 for(i=y1;i<y2;i++) 02061 line(1, x1, i, x2, i); 02062 else 02063 for(i=y2;i<y1;i++) 02064 line(1, x1, i, x2, i); 02065 02066 } 02067 //Draw Bar with value 02068 //value shold between 0~100 02069 //v_h = 0 is vertical bar and v_h = 1 is horizontal bar 02070 void bar(char x1, char y1, char width, char length,char value,char v_h) 02071 { 02072 int value_point; 02073 box(x1,y1,x1+width,y1+length); 02074 if(v_h == 1) 02075 { 02076 value_point = x1 + width*value/100; //horizontal 02077 solid_box(x1,y1,value_point,y1+length); 02078 erase_block(value_point+1,y1+1,x1+width-1,y1+length-1); 02079 02080 }else{ 02081 value_point = y1 + length*value/100; //vertical 02082 solid_box(x1,y1,x1+width,value_point); 02083 erase_block(x1+1,value_point+1,x1+width-1,y1+length-1); 02084 } 02085 02086 02087 } 02088 02089 02090 02091 void set_baud(char b) 02092 { 02093 if (b == 1) USART_Init( 1000000/2400-1);//4800 02094 else if (b == 2) USART_Init( 1000000/4800-1);//9600 02095 else if (b == 3) USART_Init( 1000000/9600-1);//19200 02096 else if (b == 4) USART_Init( 1000000/19200-1);//38400 02097 else if (b == 5) USART_Init( 1000000/28800-1);//57600 02098 else if (b == 6) USART_Init( 1000000/57600-1);//115200 02099 02100 } 02101 02102 02103 //Reads data or status 02104 //for data D_S = 1, for status D_S = 0 02105 //returns the value of the data bus 02106 char read(char D_S) 02107 { 02108 char data1 = 0, data2 = 0; 02109 02110 DDRB &= 0xFC;//PB0 and PB1 inputs 02111 DDRD &= 0x02;//everything but PD1 as input 02112 02113 PORTC &= ~((1 << RD) | (1 << CE));//CD high for status 02114 if (D_S == 1) PORTC &= ~(1 << CD);//CD down for data 02115 02116 delay_us(2); 02117 02118 data1 = PINB; 02119 data1 &= 0x03; 02120 02121 data2 = PIND; 02122 data2 &= 0xFC; 02123 02124 data1 |= data2; 02125 02126 PORTC |= ((1 << CD) | (1 << RD) | (1 << CE));//all up 02127 02128 delay_us(2); 02129 02130 return data1; 02131 02132 } 02133 02134 02135 //Writes data (D_C = 1) or command (D_C = anything else) 02136 void write(char D_C, char byte) 02137 { 02138 DDRB |= 0x03; //PB0 and PB1 are outs 02139 DDRD |= 0xFC; //PD2-PD7 are also outs. Ports B and D are the data bus 02140 02141 set_data(byte); 02142 02143 if (D_C == 1) PORTC &= ~((1 << WR) | (1 << CE) | (1 << CD));//down 02144 else PORTC &= ~((1 << WR) | (1 << CE));//down 02145 02146 delay_us(2); 02147 PORTC |= ((1 << CD) | (1 << WR) | (1 << CE));//all up 02148 delay_us(2); 02149 DDRB &= 0xFC;//PB0 and PB1 inputs 02150 DDRD &= 0x02;//everything but PD1 as input 02151 02152 delay_us(2); 02153 02154 } 02155 02156 02157 02158 void display_init(void) 02159 { 02160 //set graphics home address to 0 02161 while(!(read(0) & 3));//read status 02162 write(1, 0); 02163 while(!(read(0) & 3));//read status 02164 write(1, 0); 02165 while(!(read(0) & 3));//read status 02166 write(0, 0x42); 02167 02168 //set graphics area 02169 while(!(read(0) & 3));//read status 02170 write(1, 20);//20 bytes, 160/8 02171 while(!(read(0) & 3));//read status 02172 write(1, 0); 02173 while(!(read(0) & 3));//read status 02174 write(0, 0x43); 02175 02176 //set mode 02177 while(!(read(0) & 3));//read status 02178 write(0, 0x80);//Or, with internal character generator 02179 02180 //set display mode 02181 while(!(read(0) & 3));//read status 02182 write(0, 0x98);//Graphics on 02183 02184 } 02185 02186 02187