Andrew Russell / Mbed 2 deprecated Display_Drver_Andrew_1

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /***********************************************************************************/
00002 /***** Newhaven NHD-5.7-640480WF-CTXL# Display Driver **********/
00003 /***** for mbed (www.mbed,org) NXP LPC1768 32 bit ARM MCU ******/
00004 /********* Adapted by Andrew C. Russell,  March 2011 ***********/
00005 
00006 /************I hereby acknowledge and thank the following ******/
00007 /***software authors whose code I have adapted and/or adopted***/
00008 /*******************to create these drivers:- ******************/
00009 
00010 //Curt Lagerstam - Newhaven Display International, LLC for the dispay initialization.
00011 //James P Lynch - code adapted from his Nokia LCD display tutorial at
00012 //http://www.sparkfun.com/tutorial/Nokia%206100%20LCD%20Display%20Driver.pdf
00013 //Akifumi 'Tedd' Okano for assistance and adapting the .bmp display driver
00014 /***********************************************************************************/
00015 
00016 #include "mbed.h"
00017 #include "font.h"
00018 #include "stdlib.h"
00019 #include "string.h"
00020 
00021 
00022 /***********************************************************************************/
00023 /********************************** Colors *****************************************/
00024 /********* taken directly from http://web.njit.edu/~kevin/rgb.txt.html**************/
00025 #define BLACK       0x000000
00026 #define WHITE       0xFFFFFF
00027 #define BLACK       0x000000
00028 #define RED         0xFF0000
00029 #define GREEN       0x00FF00
00030 #define BLUE        0x0000FF
00031 #define GREY        0xBEBEBE
00032 #define DEEPINK     0xFF1493
00033 
00034 #define PASET       0x2B
00035 #define CASET       0x2A
00036 #define DISON       0x29
00037 #define DISOFF      0x28
00038 #define DEEPSLEEP   0xE5
00039 #define RAMWR       0x2C
00040 #define RAMCT       0x3C
00041 #define RESET       0x01
00042 #define DISINV      0x21
00043 #define DISNOR      0x20
00044 #define NOP         0x00
00045 #define XMAX        640
00046 #define YMAX        480
00047 #define TRUE        1
00048 
00049 // Font sizes
00050 #define SMALL 0
00051 #define MEDIUM 1
00052 #define LARGE 2
00053 
00054 //---------------------------------------------------------
00055 DigitalOut CS(p13);                         /* chip select the SSD1963 active LOW */
00056 //DigitalOut RS(p14);                       /* reset  to SSD1963  - not used in this implementation*/
00057 DigitalOut nWR(p15);                        /* write out to SSD1963 active LOW */
00058 DigitalOut nRD(p16);                        /* read data from SSD1963 active LOW - but not used */
00059 DigitalOut DC(p17);                         /* Data/Command Select: 1=Command,  0=Data); */
00060 DigitalOut myled(LED1);                     /* for test purposes only - on the mbed module */
00061 
00062 /* data bus I/O pins */
00063 BusOut DB(p5,p6,p7,p8,p9,p10,p11,p12);    /* databus D0-D7 */
00064 /* forward references */
00065 unsigned int fill;
00066 unsigned int radius;
00067 
00068 //unsigned char FONT6x8[97][8];
00069 //unsigned char FONT8x8[97][8];
00070 //unsigned char FONT8x16[97][8];
00071 unsigned char command;
00072 unsigned char data1;
00073 int DOWN;
00074 int RIGHT;
00075 void LCDSetXY(unsigned int x,unsigned int y);
00076 void LCDClearScreen(void);
00077 //void LCDSetXY(int x, int y);
00078 void LCDSetPixel(unsigned int x, unsigned int y, unsigned int color);
00079 void LCDSetLine(int x1, int y1, int x2, int y2, int color);
00080 void LCDSetRect(unsigned int x0, unsigned  int y0, unsigned int x1, unsigned int y1, unsigned int fill, unsigned int color);
00081 void LCDSetCircle(unsigned int x0, unsigned int y0, unsigned int radius, unsigned int color);
00082 void LCDPutChar(char c, unsigned int x, unsigned int y, unsigned int size, unsigned int fcolor, unsigned int bcolor);
00083 void LCDPutString (char *lcd_string, const char *font_style, unsigned char x, unsigned char y,
00084                    unsigned char fcolor, unsigned char bcolor);
00085 
00086 //******************************************************************************
00087 void Write_Command(unsigned char command) {
00088     CS=1; /* just to make sure */
00089     nRD = 1; /* make sure the RD is HIGH just to be sure */
00090     DC=0;
00091     nWR = 0;
00092     CS=0;
00093     DB=command;
00094     CS = 1;
00095     nWR = 1;
00096 
00097 }
00098 //;******************************************************************************
00099 void Write_Data(unsigned char data1) {
00100     CS=1; /* just to make sure */
00101     nRD = 1;
00102     DC=1;
00103     nWR = 0;
00104     CS=0;
00105     DB=data1;//write(data1);
00106     CS = 1;
00107     nWR = 1;
00108 }
00109 
00110 //====================================================
00111 void Command_Write(unsigned char REG,unsigned char VALUE) {
00112     Write_Command(REG);
00113     Write_Data(VALUE);
00114 }
00115 //======================================================
00116 void SendData(unsigned long color) {
00117     Write_Data((color)>>16);        //red
00118     Write_Data((color)>>8);         //green
00119     Write_Data(color);              //blue
00120 }
00121 /********************************************************************************/
00122 void WindowSet(unsigned int s_x,unsigned int e_x,unsigned int s_y,unsigned int e_y) {
00123     Write_Command(CASET);           //SET page address
00124     Write_Data((s_x)>>8);           //SET start page address=0
00125     Write_Data(s_x);
00126     Write_Data((e_x)>>8);           //SET end page address=639
00127     Write_Data(e_x);
00128     Write_Command(PASET);           //SET column address
00129     Write_Data((s_y)>>8);           //SET start column address=0
00130     Write_Data(s_y);
00131     Write_Data((e_y)>>8);           //SET end column address=479
00132     Write_Data(e_y);
00133 }
00134 
00135 /***************************************************************************/
00136 /* this routine locates the screen pointer at position XY in the frame buffer */
00137 void LCDSetXY(unsigned int x,unsigned int y) {
00138     Write_Command(CASET);           //SET page address
00139     Write_Data((x)>>8);             //SET start page address=0
00140     Write_Data(x);
00141     Write_Data((XMAX)>>8);          //SET end page address=XMAX
00142     Write_Data(XMAX);
00143     Write_Command(PASET);           //SET column address
00144     Write_Data((y)>>8);             //SET start column address=0
00145     Write_Data(y);
00146     Write_Data((YMAX)>>8);          //SET end column address=YMAX
00147     Write_Data(YMAX);
00148     Write_Command(RAMWR);           //ready to now start writing the data
00149                                     //when returnung to the caller
00150 }
00151 
00152 /***************************************************************************/
00153 // initialize controller
00154 void Init_SSD1963 (void) {
00155 
00156     Write_Command(0x01);            //Software Reset
00157     wait_ms(10);
00158     Write_Command(0x01);            //Software Reset
00159     wait_ms(10);
00160     Write_Command(0x01);            //Software Reset
00161     wait_ms(10);
00162     Command_Write(0xe0,0x01);       //START PLL
00163     wait_us(1);
00164     Command_Write(0xe0,0x03);    //LOCK PLL
00165     Write_Command(0xb0);         //SET LCD MODE  SET TFT 18Bits MODE
00166     Write_Data(0x0c);            //SET TFT MODE & hsync+Vsync+DEN MODE
00167     Write_Data(0x80);            //SET TFT MODE & hsync+Vsync+DEN MODE
00168     Write_Data(0x02);            //SET horizontal size=640-1 HightByte
00169     Write_Data(0x7f);            //SET horizontal size=640-1 LowByte
00170     Write_Data(0x01);            //SET vertical size=480-1 HightByte
00171     Write_Data(0xdf);            //SET vertical size=480-1 LowByte
00172     Write_Data(0x00);            //SET even/odd line RGB seq.=RGB
00173     Command_Write(0xf0,0x00);    //SET pixel data I/F format=8bit
00174     Command_Write(0x3a,0x60);    // SET R G B format = 6 6 6
00175     Write_Command(0xe6);         //SET PCLK freq=4.94MHz  ; pixel clock frequency
00176     Write_Data(0x02);
00177     Write_Data(0xff);
00178     Write_Data(0xff);
00179     Write_Command(0xb4);         //SET HBP,
00180     Write_Data(0x02);            //SET HSYNC Total=760
00181     Write_Data(0xf8);
00182     Write_Data(0x00);            //SET HBP 68
00183     Write_Data(0x44);
00184     Write_Data(0x0f);            //SET VBP 16=15+1
00185     Write_Data(0x00);            //SET Hsync pulse start position
00186     Write_Data(0x00);
00187     Write_Data(0x00);            //SET Hsync pulse subpixel start position
00188     Write_Command(0xb6);         //SET VBP,
00189     Write_Data(0x01);            //SET Vsync total
00190     Write_Data(0xf8);
00191     Write_Data(0x00);            //SET VBP=19
00192     Write_Data(0x13);
00193     Write_Data(0x07);            //SET Vsync pulse 8=7+1
00194     Write_Data(0x00);            //SET Vsync pulse start position
00195     Write_Data(0x00);
00196     Write_Command(0x2a);         //SET column address
00197     Write_Data(0x00);            //SET start column address=0
00198     Write_Data(0x00);
00199     Write_Data(0x02);            //SET end column address=639
00200     Write_Data(0x7f);
00201     Write_Command(0x2b);         //SET page address
00202     Write_Data(0x00);            //SET start page address=0
00203     Write_Data(0x00);
00204     Write_Data(0x01);            //SET end page address=479
00205     Write_Data(0xdf);
00206     Write_Command(0x29);         //SET display on
00207 }
00208 
00209 //========================== This section displays 4 blocks of colour =================================
00210 void QUADS() {
00211     unsigned int i,j;
00212     WindowSet(0x0000,0x027f,0x0000,0x01df); /*this incorprates Write_Command*/
00213     Write_Command(RAMWR);  /* start writing the data to memory */
00214     for (j= 0 ;j<240;j++) {
00215         for (i=0;i<320;i++) {
00216             SendData(0x00FF00);             //GREEN
00217         }
00218         for (i=0;i<320;i++) {
00219             SendData(0xFF0000);             //RED
00220         }
00221     }
00222     for (j= 0 ;j<240;j++) {
00223         for (i=0;i<320;i++) {
00224             SendData(0xBEBEBE);             //GREY
00225         }
00226         for (i=0;i<320;i++) {
00227             SendData(0xFF1493);             //DEEP PINK
00228         }
00229     }
00230 }
00231 /********************************************************************************/
00232 void clear_to_color(void) {
00233     int m,n;
00234     WindowSet(0,640,0,480); // 0-640 wide by 0-480 high
00235     Write_Command(RAMWR);
00236     for (m=0;m<480;m++) {
00237         for (n=0;n<640;n++) {
00238             SendData(BLUE);
00239         }
00240     }
00241 }
00242 
00243 /***************************************************************************/
00244 /******* this routine lights up a single pixel to a specific colour ********/
00245 void LCDSetPixel(unsigned int x,unsigned int y, unsigned int color) {
00246     Write_Command(CASET);           //SET page address
00247     Write_Data((x)>>8);             //SET start page address=0
00248     Write_Data(x);
00249     Write_Data((XMAX)>>8);          //SET end page address=XMAX
00250     Write_Data(XMAX);
00251     Write_Command(PASET);           //SET column address
00252     Write_Data((y)>>8);             //SET start column address=0
00253     Write_Data(y);
00254     Write_Data((YMAX)>>8);          //SET end column address=YMAX
00255     Write_Data(YMAX);
00256     Write_Command(RAMWR);           //ready to now start writing the data
00257     //when returnung to the caller
00258     SendData(color);
00259     Write_Command(NOP);
00260 }
00261 /***************************************************************************/
00262 /*********This routine draws a line between 2 points on the screen**********/
00263 void LCDSetLine(int x0, int y0, int x1, int y1, int color) {
00264     int dy = y1 - y0;
00265     int dx = x1 - x0;
00266     int stepx, stepy;
00267     if (dy < 0) {
00268         dy = -dy;
00269         stepy = -1;
00270     } else {
00271         stepy = 1;
00272     }
00273     if (dx < 0) {
00274         dx = -dx;
00275         stepx = -1;
00276     } else {
00277         stepx = 1;
00278     }
00279     dy <<= 1; // dy is now 2*dy
00280     dx <<= 1; // dx is now 2*dx
00281     LCDSetPixel(x0, y0, color);
00282     if (dx > dy) {
00283         int fraction = dy - (dx >> 1); // same as 2*dy - dx
00284         while (x0 != x1) {
00285             if (fraction >= 0) {
00286                 y0 += stepy;
00287                 fraction -= dx; // same as fraction -= 2*dx
00288             }
00289             x0 += stepx;
00290             fraction += dy; // same as fraction -= 2*dy
00291             LCDSetPixel(x0, y0, color);
00292         }
00293     } else {
00294         int fraction = dx - (dy >> 1);
00295         while (y0 != y1) {
00296             if (fraction >= 0) {
00297                 x0 += stepx;
00298                 fraction -= dy;
00299             }
00300             y0 += stepy;
00301             fraction += dx;
00302             LCDSetPixel(x0, y0, color);
00303         }
00304     }
00305 }
00306 /***************************************************************************/
00307 /********** draw a rectangle, filled or not filled *************************/
00308 void LCDSetRect(unsigned int x0, unsigned int y0, unsigned int x1, unsigned int y1, unsigned int fill, unsigned int color) {
00309     unsigned int xmin, xmax, ymin, ymax;
00310     int i;
00311 //unsigned char FILL;
00312 // check if the rectangle is to be filled
00313     if (fill == TRUE) {
00314 // best way to create a filled rectangle is to define a drawing box
00315 // and loop two pixels at a time
00316 // calculate the min and max for x and y directions
00317         xmin = (x0 <= x1) ? x0 : x1;
00318         xmax = (x0 > x1) ? x0 : x1;
00319         ymin = (y0 <= y1) ? y0 : y1;
00320         ymax = (y0 > y1) ? y0 : y1;
00321         Write_Command(CASET);           //SET page address
00322         Write_Data((x0)>>8);             //SET start page address=0
00323         Write_Data(x0);
00324         Write_Data((x1)>>8);          //SET end page address=XMAX
00325         Write_Data(x1);
00326         Write_Command(PASET);           //SET column address
00327         Write_Data((y0)>>8);             //SET start column address=0
00328         Write_Data(y0);
00329         Write_Data((y1)>>8);          //SET end column address=YMAX
00330         Write_Data(y1);
00331         Write_Command(RAMWR);           //ready to now start writing the data
00332         for (i = 0; i < ((((xmax - xmin + 1) * (ymax - ymin + 1)) / 2) + 1); i++) {
00333             SendData(color);
00334         }
00335     } else {
00336         LCDSetLine(x0, y0, x1, y0, color);
00337         LCDSetLine(x0, y1, x1, y1, color);
00338         LCDSetLine(x0, y0, x0, y1, color);
00339         LCDSetLine(x1, y0, x1, y1, color);
00340     }
00341 }
00342 
00343 /***************************************************************************/
00344 void LCDSetCircle(unsigned int x0, unsigned int y0, unsigned int radius, unsigned int color) {
00345     int f = 1 - radius;
00346     int ddF_x = 0;
00347     int ddF_y = -2 * radius;
00348     int x = 0;
00349     int y = radius;
00350     LCDSetPixel(x0, y0 + radius, color);
00351     LCDSetPixel(x0, y0 - radius, color);
00352     LCDSetPixel(x0 + radius, y0, color);
00353     LCDSetPixel(x0 - radius, y0, color);
00354     while (x < y) {
00355         if (f >= 0) {
00356             y--;
00357             ddF_y += 2;
00358             f += ddF_y;
00359         }
00360         x++;
00361         ddF_x += 2;
00362         f += ddF_x + 1;
00363         LCDSetPixel(x0 + x, y0 + y, color);
00364         LCDSetPixel(x0 - x, y0 + y, color);
00365         LCDSetPixel(x0 + x, y0 - y, color);
00366         LCDSetPixel(x0 - x, y0 - y, color);
00367         LCDSetPixel(x0 + y, y0 + x, color);
00368         LCDSetPixel(x0 - y, y0 + x, color);
00369         LCDSetPixel(x0 + y, y0 - x, color);
00370         LCDSetPixel(x0 - y, y0 - x, color);
00371     }
00372 }
00373 /***************************************************************************/
00374 void LCDPutChar(char c, unsigned int x, unsigned int y, int size, unsigned int fColor, unsigned int bColor) {
00375     extern const unsigned char FONT6x8[97][8];
00376     extern const unsigned char FONT8x8[97][8];
00377     extern const unsigned char FONT8x16[97][16];
00378     int i,j;
00379     unsigned int nCols;
00380     unsigned int nRows;
00381     unsigned int nBytes;
00382     unsigned char PixelRow;
00383     unsigned char Mask;
00384     unsigned int Word0;
00385     unsigned int Word1;
00386     unsigned char *pFont;
00387     unsigned char *pChar;
00388     unsigned char *FontTable[] = {(unsigned char *)FONT6x8,
00389                                   (unsigned char *)FONT8x8,
00390                                   (unsigned char *)FONT8x16
00391                                  };
00392 // get pointer to the beginning of the selected font table
00393     pFont = (unsigned char *)FontTable[size];
00394 // get the nColumns, nRows and nBytes in the font table
00395     nCols = *pFont;
00396     nRows = *(pFont + 1);
00397     nBytes = *(pFont + 2);
00398 // get pointer to the last byte of the desired character - i.e. last byte in the row
00399     pChar = pFont + (nBytes * (c - 0x1F)) + nBytes - 1;
00400 // Row address set (command 0x2B) - x position on the LCD 
00401     Write_Command(PASET);
00402     Write_Data(x);
00403     Write_Data(x + nRows - 1);
00404 // Column address set (command 0x2A) - y position on the LCD
00405     Write_Command(CASET);
00406     Write_Data(y); Write_Data(y);
00407     Write_Data(y + nCols - 1);
00408 // Write Memory - get ready to start writing the character
00409     Write_Command(RAMWR);
00410 // loop on each row, working backwards from top to bottom
00411     for (i = nRows - 1; i >= 0; i--) {
00412 // copy pixel row from font table and then decrement row
00413         PixelRow = *pChar--;
00414 // loop on each pixel in the row (left to right)
00415 // Note: we do two pixels each loop
00416         Mask = 0x80;
00417         for (j = 0; j < nCols; j += 2) {
00418 // if pixel bit set, use foreground color; else use the background color
00419 // now get the pixel color for two successive pixels
00420             if ((PixelRow & Mask) == 0)
00421                 Word0 = bColor;
00422             else
00423                 Word0 = fColor;
00424             Mask = Mask >> 1;
00425             if ((PixelRow & Mask) == 0)
00426                 Word1 = bColor;
00427             else
00428                 Word1 = fColor;
00429             Mask = Mask >> 1;
00430 // use this information to output three data bytes
00431             Write_Data((Word0 >> 4) & 0xFF);
00432             Write_Data(((Word0 & 0xF) << 4) | ((Word1 >> 8) & 0xF));
00433             Write_Data(Word1 & 0xFF);
00434         }
00435     }
00436 // terminate the Write Memory command
00437     Write_Command(NOP);
00438 }
00439 // *************************************************************************************************
00440 
00441 
00442 
00443 
00444 int main(void) {
00445 
00446     int h;
00447     Serial pc(USBTX, USBRX);
00448     wait_ms(10);
00449     Init_SSD1963();
00450 
00451 
00452     LCDSetXY(230,230);
00453     QUADS();
00454     clear_to_color();
00455 
00456 
00457     LCDSetXY(340,200);
00458     {
00459         for (h=0;h<200;h++)
00460             SendData(WHITE);
00461     }
00462 
00463     LCDSetXY(350,210);
00464     {
00465         for (h=0;h<200;h++)
00466             SendData(DEEPINK);
00467     }
00468 
00469     LCDSetPixel(400,300,BLACK);//working
00470     LCDSetPixel(405,305,DEEPINK);//working
00471     LCDSetLine(0,300,250,10,DEEPINK);//working
00472     LCDSetRect(100,100,250,250,1,DEEPINK);//working
00473     LCDSetRect(200,200,350,350,0,BLACK);//working
00474     LCDSetCircle(320,240,150,WHITE);//working
00475     LCDSetCircle(320,240,155,WHITE);//working
00476     LCDSetCircle(320,240,160,WHITE);//working
00477     LCDPutChar('E',10,10,1, WHITE, BLACK);
00478     //LCDPutStr("This guy is nuts", 115, 2, LARGE, BLACK, CYAN);
00479     Write_Command(NOP);
00480 
00481     while (1) {};
00482 }