main_VU_11-14-08.c

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