main_128x64_5-22-09.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         
00006 */
00007 
00008 #include <avr/io.h>
00009 #include "rprintf.h"
00010 #include <math.h>
00011 #include <avr/interrupt.h>
00012 
00013 
00014 #define FOSC 16000000// Clock Speed
00015 #define BAUD 9600
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 
00026 //Define functions
00027 //======================
00028 void ioinit(void);      //Initializes IO
00029 void delay_ms(uint16_t x); //General purpose delay
00030 void delay(void);
00031 void USART_Init( unsigned int ubrr);
00032 void put_char(char byte);
00033 
00034 void set_data(char data);
00035 void set_x(char x_spot);
00036 void set_page(char page);
00037 void clear_screen(void);
00038 void write_byte(char byte, char side);
00039 char read_byte(char byte, char side);
00040 void display_on(void);
00041 
00042 unsigned char print_char(char txt);
00043 void pixel(char x, char y);
00044 void line(char x1, char y1, char x2, char y2);
00045 
00046 //======================
00047 
00048 
00049 char x_offset = 0;
00050 //char y_offset = 0;
00051 char side_mark = 1;//left side default
00052 char page_mark = 0;
00053 
00054 //Jacked from Sinister 7 code
00055 static char text_array[475] = {0x00,0x00,0x00,0x00,0x00,/*space*/
00056                               0x00,0xF6,0xF6,0x00,0x00,/*!*/
00057                               0x00,0xE0,0x00,0xE0,0x00,/*"*/
00058                               0x28,0xFE,0x28,0xFE,0x28,/*#*/
00059                               0x00,0x64,0xD6,0x54,0x08,/*$*/
00060                               0xC2,0xCC,0x10,0x26,0xC6,/*%*/
00061                               0x4C,0xB2,0x92,0x6C,0x0A,/*&*/
00062                               0x00,0x00,0xE0,0x00,0x00,/*'*/
00063                               0x00,0x38,0x44,0x82,0x00,/*(*/
00064                               0x00,0x82,0x44,0x38,0x00,/*)*/
00065                               0x88,0x50,0xF8,0x50,0x88,/***/
00066                               0x08,0x08,0x3E,0x08,0x08,/*+*/
00067                               0x00,0x00,0x05,0x06,0x00,/*,*/
00068                               0x08,0x08,0x08,0x08,0x08,/*-*/
00069                               0x00,0x00,0x06,0x06,0x00,/*.*/
00070                               0x02,0x0C,0x10,0x60,0x80,/*/*/
00071                               0x7C,0x8A,0x92,0xA2,0x7C,/*0*/
00072                               0x00,0x42,0xFE,0x02,0x00,/*1*/
00073                               0x42,0x86,0x8A,0x92,0x62,/*2*/
00074                               0x44,0x82,0x92,0x92,0x6C,/*3*/
00075                               0x10,0x30,0x50,0xFE,0x10,/*4*/
00076                               0xE4,0xA2,0xA2,0xA2,0x9C,/*5*/
00077                               0x3C,0x52,0x92,0x92,0x0C,/*6*/
00078                               0x80,0x86,0x98,0xE0,0x80,/*7*/
00079                               0x6C,0x92,0x92,0x92,0x6C,/*8*/
00080                               0x60,0x92,0x92,0x94,0x78,/*9*/
00081                               0x00,0x00,0x36,0x36,0x00,/*:*/
00082                               0x00,0x00,0x35,0x36,0x00,/*;*/
00083                               0x10,0x28,0x44,0x82,0x00,/*<*/
00084                               0x28,0x28,0x28,0x28,0x28,/*=*/
00085                               0x00,0x82,0x44,0x28,0x10,/*>*/
00086                               0x40,0x80,0x8A,0x90,0x60,/*?*/
00087                               0x7C,0x82,0xBA,0xBA,0x62,/*@*/
00088                               0x3E,0x48,0x88,0x48,0x3E,/*A*/
00089                               0xFE,0x92,0x92,0x92,0x6C,/*B*/
00090                               0x7C,0x82,0x82,0x82,0x44,/*C*/
00091                               0xFE,0x82,0x82,0x82,0x7C,/*D*/
00092                               0xFE,0x92,0x92,0x92,0x82,/*E*/
00093                               0xFE,0x90,0x90,0x90,0x80,/*F*/
00094                               0x7C,0x82,0x82,0x8A,0x4E,/*G*/
00095                               0xFE,0x10,0x10,0x10,0xFE,/*H*/
00096                               0x82,0x82,0xFE,0x82,0x82,/*I*/
00097                               0x84,0x82,0xFC,0x80,0x80,/*J*/
00098                               0xFE,0x10,0x28,0x44,0x82,/*K*/
00099                               0xFE,0x02,0x02,0x02,0x02,/*L*/
00100                               0xFE,0x40,0x20,0x40,0xFE,/*M*/
00101                               0xFE,0x60,0x10,0x0C,0xFE,/*N*/
00102                               0x7C,0x82,0x82,0x82,0x7C,/*O*/
00103                               0xFE,0x90,0x90,0x90,0x60,/*P*/
00104                               0x7C,0x82,0x82,0x86,0x7E,/*Q*/
00105                               0xFE,0x90,0x98,0x94,0x62,/*R*/
00106                               0x64,0x92,0x92,0x92,0x4C,/*S*/
00107                               0x80,0x80,0xFE,0x80,0x80,/*T*/
00108                               0xFC,0x02,0x02,0x02,0xFC,/*U*/
00109                               0xF8,0x04,0x02,0x04,0xF8,/*V*/
00110                               0xFC,0x02,0x0C,0x02,0xFC,/*W*/
00111                               0xC6,0x28,0x10,0x28,0xC6,/*X*/
00112                               0xC0,0x20,0x1E,0x20,0xC0,/*Y*/
00113                               0x86,0x8A,0x92,0xA2,0xC2,/*Z*/
00114                               0x00,0x00,0xFE,0x82,0x00,/*[*/
00115                               0x00,0x00,0x00,0x00,0x00,/*this should be / */
00116                               0x80,0x60,0x10,0x0C,0x02,/*]*/
00117                               0x20,0x40,0x80,0x40,0x20,/*^*/
00118                               0x01,0x01,0x01,0x01,0x01,/*_*/
00119                               0x80,0x40,0x20,0x00,0x00,/*`*/
00120                               0x04,0x2A,0x2A,0x2A,0x1E,/*a*/
00121                               0xFE,0x12,0x22,0x22,0x1C,/*b*/
00122                               0x1C,0x22,0x22,0x22,0x14,/*c*/
00123                               0x1C,0x22,0x22,0x12,0xFE,/*d*/
00124                               0x1C,0x2A,0x2A,0x2A,0x18,/*e*/
00125                               0x10,0x7E,0x90,0x80,0x40,/*f*/
00126                               0x18,0x25,0x25,0x25,0x1E,/*g*/
00127                               0xFE,0x10,0x10,0x10,0x0E,/*h*/
00128                               0x00,0x12,0x5E,0x02,0x00,/*i*/
00129                               0x02,0x01,0x01,0x11,0x5E,/*j*/
00130                               0xFE,0x08,0x08,0x14,0x22,/*k*/
00131                               0x00,0x82,0xFE,0x02,0x00,/*l*/
00132                               0x3E,0x20,0x1C,0x20,0x1E,/*m*/
00133                               0x3E,0x20,0x20,0x20,0x1E,/*n*/
00134                               0x1C,0x22,0x22,0x22,0x1C,/*o*/
00135                               0x3F,0x24,0x24,0x24,0x18,/*p*/
00136                               0x18,0x24,0x24,0x3F,0x01,/*q*/
00137                               0x3E,0x10,0x20,0x20,0x10,/*r*/
00138                               0x12,0x2A,0x2A,0x2A,0x04,/*s*/
00139                               0x00,0x10,0x3C,0x12,0x04,/*t*/
00140                               0x3C,0x02,0x02,0x02,0x3E,/*u*/
00141                               0x30,0x0C,0x02,0x0C,0x30,/*v*/
00142                               0x38,0x06,0x18,0x06,0x38,/*w*/
00143                               0x22,0x14,0x08,0x14,0x22,/*x*/
00144                               0x38,0x05,0x05,0x05,0x3E,/*y*/
00145                               0x22,0x26,0x2A,0x32,0x22,/*z*/
00146                               0x00,0x10,0x6C,0x82,0x82,/*{*/
00147                               //0x00,0x00,0xFF,0x00,0x00,/*|*/
00148                               0x04,0x02,0xFF,0x02,0x04,/*|, arrow*/
00149                               0x82,0x82,0x6C,0x10,0x00,/*}*/
00150                               0x08,0x10,0x18,0x08,0x10};/*~*/
00151                                                           
00152                                                           
00153 
00154 
00155 int main (void)
00156 {
00157         char x, y;
00158         //char a = 0;
00159     ioinit(); //Setup IO pins and defaults
00160         USART_Init( MYUBRR);
00161         rprintf_devopen(put_char); /* init rrprintf */
00162         
00163         //set_data(0x55);
00164 
00165         //PORTC |= ((1 << RESET) | (1 << EN) | (1 << R_W) | (1 << CS1) | (1 << CS2) | (1 << RS));//all high
00166         
00167         //while(1);
00168         
00169         /*
00170         while(1)
00171         {
00172                 PORTC &= ~((1 << EN) | (1 << R_W) | (1 << CS1) | (1 << CS2));//down
00173                 delay_ms(500);
00174                 //PORTC |= ((1 << EN) | (1 << R_W) | (1 << CS1) | (1 << CS2));//all high
00175                 PORTC |= ((1 << RESET) | (1 << EN) | (1 << R_W) | (1 << CS1) | (1 << CS2) | (1 << RS));//all high
00176                 delay_ms(500);
00177         }
00178         */
00179         /*
00180         DDRC = 0b00000001;
00181         
00182         while(1)
00183         {
00184                 PORTC |= 0b00000001;
00185                 delay_1uS();
00186                 PORTC &= 0b11111110;
00187                 delay_1uS();
00188         
00189         }
00190         */
00191         
00192         //Reset the display
00193         //PORTC = 0b11110111;
00194         PORTC &= ~(1 << RESET);
00195         delay_ms(50);
00196         //PORTC = 0b11111111;
00197         PORTC |= (1 << RESET);
00198         delay_ms(500);
00199         
00200         clear_screen();
00201         
00202         set_page(0);
00203         set_x(0);
00204         
00205         display_on();
00206         
00207         //set display start line to 0
00208         //set control lines
00209         PORTC &= ~((1 << EN) | (1 << R_W) | (1 << RS));//down
00210         
00211         set_data(0xC0);
00212         //set_data(0xFF);
00213         delay();
00214         PORTC |= (1 << EN);//up
00215         delay();
00216         PORTC &= ~(1 << EN);//down
00217         delay();
00218         PORTC |= ((1 << EN) | (1 << R_W) | (1 << RS));//all high
00219         
00220         delay();
00221         
00222         x_offset = 0;
00223 
00224         set_page(0);
00225         
00226         /*
00227         set_x(0);
00228         write_byte(0X55, 1);
00229         
00230         set_x(1);
00231         write_byte(0X55, 1);
00232         
00233         //set_x(0);
00234         x = read_byte(1,1);
00235         
00236         set_x(2);
00237         write_byte(x, 1);
00238         set_x(3);
00239         write_byte(x, 1);
00240         */
00241         
00242 
00243         //set_x(63);//???
00244         
00245         //x = read_byte(1);
00246         
00247         //set_x(3);
00248         
00249         //rite_byte(x, 1);
00250         
00251         //set_x(0);
00252         
00253         //write_byte(x, 1);
00254         
00255         //while(1);
00256         
00257         while(1)
00258         {
00259                 line(0, 0, 63, 63);
00260                 line(0, 10, 63, 52);
00261                 line(0, 21, 63, 42);
00262                 line(0, 32, 63, 32);
00263                 line(0, 42, 63, 21);
00264                 line(0, 52, 63, 10);
00265                 line(0, 63, 63, 0);
00266         
00267                 
00268                 line(64, 0, 127, 63);
00269                 line(64, 10, 127, 52);
00270                 line(64, 21, 127, 42);
00271                 line(64, 32, 127, 32);
00272                 line(64, 42, 127, 21);
00273                 line(64, 52, 127, 10);
00274                 line(64, 63, 127, 0);
00275                 
00276                 //set_x(20);
00277                 x_offset = 20;
00278                 set_page(0);
00279                 
00280                 print_char('C');
00281                 print_char('O');
00282                 print_char('C');
00283                 print_char('K');
00284                 
00285                 x_offset = 82;
00286                 print_char('B');
00287                 print_char('L');
00288                 print_char('O');
00289                 print_char('C');
00290                 print_char('K');
00291                 
00292                 x_offset = 14;
00293                 set_page(7);
00294                 
00295                 print_char('M');
00296                 print_char('O');
00297                 print_char('T');
00298                 print_char('H');
00299                 print_char('E');
00300                 print_char('R');
00301                 
00302                 x_offset = 79;
00303                 print_char('F');
00304                 print_char('U');
00305                 print_char('C');
00306                 print_char('K');
00307                 print_char('E');
00308                 print_char('R');
00309                 
00310                 delay_ms(1000);
00311                 clear_screen();
00312         }
00313         
00314         //line(0, 20, 127, 63);
00315         
00316         //line(20, 0, 127, 63);
00317         
00318         //line(0, 63, 127, 0);
00319         
00320         //pixel(127,63);
00321         //pixel(0,0);
00322         while(1);
00323         
00324         while(1)
00325         {
00326                 for (y = 0; y < 64; y++)
00327                 {
00328                         //set_x(0);
00329                         
00330                         //pixel(0,y);
00331                         //display_on();
00332                         //delay_ms(500);
00333                         
00334                         for(x = 0; x < 128; x++)
00335                         {
00336                                 pixel(x,y);
00337                                 //delay_ms(10);
00338                                 display_on();
00339                         }
00340                 
00341                 }
00342                 
00343                 delay_ms(1000);
00344                 clear_screen();
00345         
00346         }
00347         
00348         
00349         /*
00350         
00351         
00352         while(1);
00353         */
00354         
00355         /*
00356         for (a = 0; a < 64; a++)
00357         {
00358                 set_x(a);
00359                 write_byte(a,2);
00360                 delay_ms(500);
00361         }
00362         */
00363         
00364         /*
00365         set_x(63);
00366         write_byte(0xFF,1);
00367         
00368         set_x(0);
00369         write_byte(0x55,1);
00370         
00371         while(1);
00372         */
00373         
00374         /*
00375         while(1)
00376         {
00377                 for (a = 32; a < 128; a++)
00378                 {
00379                         print_char(a);
00380                         
00381                         display_on();
00382                         delay_ms(50);
00383                 }
00384         }        
00385         */
00386 
00387     
00388 }
00389 
00390 void ioinit (void)
00391 {
00392         
00393     //1 = output, 0 = input
00394    
00395         DDRB = 0b00000011; //PB0 and PB1 are outs
00396         DDRC = ((1<<EN) | (1<<RS) | (1<<R_W) | (1<<RESET) | (1<<CS1) | (1<<CS2));
00397         PORTC = ((1<<EN) | (1<<RS) | (1<<R_W) | (1<<RESET) | (1<<CS1) | (1<<CS2));
00398         DDRD = 0b11111100; //PD2-PD7 are also outs.  Ports B and D are the data bus.
00399 
00400 }
00401 
00402 //General short delays
00403 void delay_ms(uint16_t x)
00404 {
00405   uint8_t y, z;
00406   for ( ; x > 0 ; x--){
00407     for ( y = 0 ; y < 165 ; y++){
00408       for ( z = 0 ; z < 18 ; z++){
00409         asm volatile ("nop");
00410       }
00411     }
00412   }
00413 }
00414 
00415 void USART_Init( unsigned int ubrr)
00416 {
00417         // Set baud rate 
00418         UBRR0H = (unsigned char)(ubrr>>8);
00419         UBRR0L = (unsigned char)ubrr;
00420         
00421         // Enable receiver and transmitter 
00422         UCSR0A = (1<<U2X0);
00423         UCSR0B = (1<<RXCIE0)|(1<<RXEN0)|(1<<TXEN0);        //Enable Interrupts on receive character
00424         
00425         UCSR0C = (1<<UCSZ00)|(1<<UCSZ01);
00426         sei();
00427 }
00428 
00429 void put_char(char byte)
00430 {
00431         /* Wait for empty transmit buffer */
00432         while ( !( UCSR0A & (1<<UDRE0)) );
00433         /* Put data into buffer, sends the data */
00434         UDR0 = byte;
00435 }
00436 
00437 //delay for display timing
00438 void delay(void)
00439 {
00440         char y;
00441         
00442         for(y = 0; y < 20; y++)
00443         {
00444                 asm volatile ("nop");
00445                 
00446         }
00447         
00448         /*
00449         asm volatile ("nop");
00450         asm volatile ("nop");
00451         asm volatile ("nop");
00452         asm volatile ("nop");
00453         asm volatile ("nop");
00454         */
00455         
00456 }
00457 
00458 //set data port
00459 void set_data(char data)
00460 {
00461         //PORTB
00462         //DB0 = PB0
00463         //DB1 = PB1
00464         
00465         PORTB &= 0xFC;
00466         
00467         //PORTD
00468         //DB2 = PD2
00469         //DB3 = PD3
00470         //DB4 = PD4
00471         //DB5 = PD5
00472         //DB6 = PD6
00473         //DB7 = PD7
00474         
00475         PORTD &= 0x03;
00476         
00477         PORTB |= (data & 0x03);
00478         PORTD |= (data & 0xFC);
00479 
00480 }
00481 
00482 void clear_screen(void)
00483 {
00484         char x, y;
00485         
00486         delay();
00487         
00488         for (x = 0; x < 8; x++)
00489         {
00490                 //set x address
00491                 //set control lines
00492                 PORTC &= ~((1 << EN) | (1 << R_W) | (1 << RS));//down
00493                 
00494                 set_data(0xB8 | x);
00495                 delay();
00496                 PORTC |= (1 << EN);//up
00497                 delay();
00498                 PORTC &= ~(1 << EN);//down
00499                 delay();
00500                 PORTC |= ((1 << EN) | (1 << R_W) |(1 << RS));//all high
00501                 
00502                 delay();
00503                 
00504                 //Set y address to zero
00505                 //set control lines
00506                 PORTC &= ~((1 << EN) | (1 << R_W) | (1 << RS));//down
00507                 
00508                 set_data(0x40);
00509                 delay();
00510                 PORTC |= (1 << EN);//up
00511                 delay();
00512                 PORTC &= ~(1 << EN);//down
00513                 delay();
00514                 PORTC |= ((1 << EN) | (1 << R_W) | (1 << RS));//all high
00515                 
00516                 set_data(0);
00517                 
00518                 for (y = 0; y < 64; y++)
00519                 {
00520                         delay();
00521                         //y address increments after every write
00522                         //write data, CS1, left half of screen
00523                         PORTC &= ~((1 << EN) | (1 << R_W) | (1 << CS1));//down
00524                         delay();
00525                         PORTC |= (1 << EN);//up
00526                         delay();
00527                         PORTC &= ~(1 << EN);//down
00528                         delay();
00529                         PORTC |= ((1 << EN) | (1 << R_W) | (1 << CS1));//all high
00530                         
00531                         delay();
00532                         
00533                         //write data, CS2, right half of screen
00534                         PORTC &= ~((1 << EN) | (1 << R_W) | (1 << CS2));//down
00535                         delay();
00536                         PORTC |= (1 << EN);//up
00537                         delay();
00538                         PORTC &= ~(1 << EN);//down
00539                         delay();
00540                         PORTC |= ((1 << EN) | (1 << R_W) | (1 << CS2));//all high
00541                 }
00542                 
00543         }
00544 
00545 }
00546 
00547 //sets horizontal position (data sheet calls this "y". Man, I just wanna punch these guys in the mouth...)
00548 void set_x(char x_spot)
00549 {
00550         //char a;
00551         
00552         //a = x_spot;
00553         //if (a == 0) a = 63;
00554         //else a--;
00555         
00556         if (x_spot == 0) x_spot = 63;
00557         else x_spot--;
00558         
00559         PORTC &= ~((1 << EN) | (1 << R_W) | (1 << RS));//down
00560                         
00561         set_data(0x40 | x_spot);
00562         delay();
00563         PORTC |= (1 << EN);//up
00564         delay();
00565         PORTC &= ~(1 << EN);//down
00566         delay();
00567         PORTC |= ((1 << EN) | (1 << R_W) | (1 << RS));//all high
00568         delay();
00569         
00570 }
00571 
00572 
00573 unsigned char print_char(char txt)
00574 {
00575     short text_array_offset = (txt - 32)*5, j;
00576     char k, temp;
00577     
00578         /*
00579     if (txt == 10)
00580     {
00581         y_offset += 8;
00582         return(0);
00583     }
00584 
00585     else if (txt == 13)
00586     {
00587         x_offset = 0;
00588         return(0);
00589     }
00590         */
00591         
00592 
00593     for (j = text_array_offset; j < text_array_offset+6; j++)
00594     {
00595                 if (x_offset >= 128)
00596                 {
00597                         x_offset = 0;
00598                         page_mark++;
00599                         set_page(page_mark);
00600                 }
00601         
00602                 if (x_offset >= 64) 
00603                 {
00604                         side_mark = 2;
00605                         set_x(x_offset - 64);
00606                 }
00607                 else
00608                 {
00609                         side_mark = 1;
00610                         set_x(x_offset);
00611                 }
00612                 
00613                 k = text_array[j];
00614                 //The bit patern is reversed in the GDM12864H display (the lookup table was made for the Nokia display)
00615                 //Rather than rewrite the table, we cheat...
00616                 temp = (((k & 1) << 7) | ((k & 2) << 5) | ((k & 4) << 3) | ((k & 8) << 1) | ((k & 16) >> 1) | ((k & 32) >> 3) | ((k & 64) >> 5) | ((k & 128) >> 7));
00617         //temp = k;
00618                 if (j == text_array_offset + 5) temp = 0;//blank byte for letter spacing
00619                 write_byte(temp, side_mark);
00620                 
00621                 x_offset++;
00622                 
00623     }
00624 
00625     if ((x_offset + 6) > 128)
00626         {
00627                 x_offset = 0;
00628                 side_mark = 1;
00629                 page_mark++;
00630                 
00631                 if (page_mark >= 8) page_mark = 0;
00632                 
00633                 set_page(page_mark);
00634         }
00635         
00636     //else x_offset++;
00637         
00638         /*
00639     if ((y_offset + 8) > 132)
00640     {
00641         //clear_screen();
00642         return 1;
00643     }
00644     else return 0;
00645         */
00646         
00647         return 0;
00648 
00649 }
00650 
00651 
00652 void set_page(char page)
00653 {
00654         //set control lines
00655         PORTC &= ~((1 << EN) | (1 << R_W) | (1 << RS));//down
00656         
00657         set_data(0xB8 | page);
00658         delay();
00659         PORTC |= (1 << EN);//up
00660         delay();
00661         PORTC &= ~(1 << EN);//down
00662         delay();
00663         PORTC |= ((1 << EN) | (1 << R_W) |(1 << RS));//all high
00664         
00665         delay();
00666 }
00667 
00668 
00669 void write_byte(char byte, char side)
00670 {        
00671         PORTC |= (1 << RS);//make sure this thing is high
00672         set_data(byte);
00673         
00674         delay();
00675         //y address increments after every write
00676         //write data, CS1, left half of screen
00677         if (side == 1) PORTC &= ~((1 << EN) | (1 << R_W) | (1 << CS1));//down
00678         else if (side == 2) PORTC &= ~((1 << EN) | (1 << R_W) | (1 << CS2));//down
00679         delay();
00680         PORTC |= (1 << EN);//up
00681         delay();
00682         PORTC &= ~(1 << EN);//down
00683         delay();
00684         PORTC |= ((1 << EN) | (1 << R_W) | (1 << CS1) | (1 << CS2));//all high
00685         
00686         
00687 }
00688 
00689 
00690 //display on
00691 void display_on(void)
00692 {
00693         set_data(0x3F);
00694         PORTC &= ~((1 << EN) | (1 << R_W) | (1 << RS));//down
00695         delay();
00696         PORTC |= (1 << EN);//up
00697         delay();
00698         PORTC &= ~(1 << EN);//down
00699         PORTC |= ((1 << EN) | (1 << R_W) | (1 << RS));//all high
00700         
00701 }
00702  
00703 //mapping to cartesian coordinates, (0,0) is in the lower left corner, (127,63) is in the upper right
00704 void pixel(char x, char y)
00705 {
00706         static char temp_page, temp_side, temp_x = 0, temp_data1 = 0, temp_data2 = 0;
00707         
00708         if (x >= 64) temp_side = 2, temp_x = x - 64;
00709         else temp_side = 1, temp_x = x;
00710         
00711         temp_page = 7 - (y >> 3);
00712         
00713         //data = (1 << (y - ((7 - temp_page) * 8)));
00714         temp_data1 = (1 << (7 - (y - ((7 - temp_page) * 8))));
00715         
00716         set_page(temp_page);
00717         set_x(temp_x);
00718         //set_x(0);
00719         
00720         //need to read the existing byte here, then or it with the new byte
00721         temp_data2 = read_byte(temp_x, temp_side);
00722         
00723         temp_data1 |= temp_data2;
00724         
00725         set_x(temp_x);//reset this...
00726         
00727         write_byte(temp_data1, temp_side);
00728 
00729 }
00730 
00731 //y = mx + b
00732 void line(char x1, char y1, char x2, char y2)
00733 {
00734         float m;
00735         char a, b;
00736         
00737         m = (float)(y2 - y1) / (float)(x2 - x1);
00738         
00739         b = y1-(m*x1);
00740         
00741         for (a = x1; a <= x2; a++)
00742         {
00743                 pixel(a, (char)((m*a)+b));
00744                 //delay_ms(25);
00745         }
00746 
00747 }
00748 
00749 
00750 char read_byte(char byte, char side)
00751 {
00752         char data1 = 0, data2 = 0;
00753         
00754         if (byte == 0) byte = 63;
00755         else byte--;
00756         
00757         set_x(byte);
00758         
00759         PORTC |= ((1 << RESET) | (1 << EN) | (1 << R_W) | (1 << CS1) | (1 << CS2) | (1 << RS));//all high, just to make sure
00760         
00761         DDRB = 0;//all inputs
00762         DDRD = 0;
00763         
00764         //PORTB |= 0x03;//pullups...?
00765         //PORTD |= 0XFC;
00766         //PORTB = 0;
00767         //PORTD = 0;
00768         
00769         delay();
00770         delay();
00771         
00772         if (side == 1) PORTC &= ~((1 << EN) | (1 << CS1));//down
00773         else if (side == 2) PORTC &= ~((1 << EN) | (1 << CS2));//down
00774         //PORTC &= ~((1 << EN) | (1 << CS1));//down
00775         
00776         
00777         
00778         delay();
00779         delay();
00780         PORTC |= (1 << EN);//up
00781         
00782         
00783         delay();
00784         delay();
00785         
00786         /*
00787         data1 = PINB;
00788         data1 &= 0x03;
00789         
00790         data2 = PIND;
00791         data2 &= 0xFC;
00792         
00793         data1 |= data2;
00794         */
00795         
00796         PORTC &= ~(1 << EN);//down
00797         
00798         
00799         
00800         delay();
00801         delay();
00802         
00803         PORTC |= ((1 << RESET) | (1 << EN) | (1 << R_W) | (1 << CS1) | (1 << CS2) | (1 << RS));//all high
00804 
00805         //DDRB = 0b00000011; //PB0 and PB1 are outs
00806         //DDRD = 0b11111100; //PD2-PD7 are also outs.  Ports B and D are the data bus.
00807         
00808         
00809         
00810         delay();
00811         delay();
00812         
00813         //PORTC &= ~((1 << EN) | (1 << CS1));//down
00814         if (side == 1) PORTC &= ~((1 << EN) | (1 << CS1));//down
00815         else if (side == 2) PORTC &= ~((1 << EN) | (1 << CS2));//down
00816         
00817         
00818         
00819         delay();
00820         delay();
00821         PORTC |= (1 << EN);//up
00822         
00823         //we can read here...
00824 
00825         delay();
00826         delay();
00827         data1 = PINB;
00828         data1 &= 0x03;
00829         
00830         data2 = PIND;
00831         data2 &= 0xFC;
00832         
00833         data1 |= data2;
00834         //while(1);
00835         
00836         PORTC &= ~(1 << EN);//down
00837         
00838         //cannot read here...
00839         //while(1);
00840 
00841         PORTC |= ((1 << RESET) | (1 << EN) | (1 << R_W) | (1 << CS1) | (1 << CS2) | (1 << RS));//all high
00842 
00843         DDRB = 0b00000011; //PB0 and PB1 are outs
00844         DDRD = 0b11111100; //PD2-PD7 are also outs.  Ports B and D are the data bus.
00845         
00846         delay();
00847         
00848         return data1;
00849 
00850 }
00851 
00852 
00853 
00854 
Generated on Sun May 8 08:05:35 2011 for iLab Neuromorphic Vision Toolkit by  doxygen 1.6.3