main_128x64_11-12-08.c

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