Advertisement
Guest User

3.5"tft 8051 sourcecode

a guest
Oct 21st, 2013
256
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.61 KB | None | 0 0
  1. // THIS SOFTWARE IS PROVIDED "AS IS" AND WITH ALL FAULTS.
  2. // NO WARRANTIES, WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT
  3. // NOT LIMITED TO, IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  4. // A PARTICULAR PURPOSE APPLY TO THIS SOFTWARE. Tiny LCD SHALL NOT, UNDER ANY
  5. // CIRCUMSTANCES, BE LIABLE FOR SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
  6. // DAMAGES, FOR ANY REASON WHATSOEVER.
  7. //
  8. END DESCRIPTION *********************************************************/
  9. /*
  10. //   Rev. NO :: 1.0.0      28/08/13   15:44:50   Nishant singh.
  11.  
  12. *************************************************************************/
  13.  
  14. #include<REG51F.H>
  15.  
  16. #ifdef  SPI_8051
  17.     #include "spi_8051.h"
  18. #else
  19.     #include "bit_bagging_8051.h"
  20. #endif
  21.  
  22.  
  23. #include "font16x31.h"
  24.  
  25.                      
  26. #define TFT_VERTICAL
  27.  
  28. #ifdef TFT_VERTICAL
  29. #warning TFT_320_480_INITIALISED
  30. #include "butterfly.h"
  31. #else
  32. #warning TFT_480_320_INITIALISED   
  33. #endif
  34.  
  35.  
  36. unsigned short gbHeight;
  37. unsigned short gbWidth;
  38.  
  39.  
  40. /*
  41.  * This function will Set the Address on  GRAM of TFT.
  42.  *
  43.  * @param  Starting co-ordinate of  Address x .(unsigned short x)
  44.  * @param  Starting co-ordinate of  Address y .(unsigned short y)
  45.  * @param  End co-ordinate of  Address x .(unsigned short x1)
  46.  * @param  End co-ordinate of  Address y .(unsigned short y1)
  47.  *
  48.  * @return NONE
  49.  *
  50.  */
  51. void address_set(unsigned short postion_x,unsigned short postion_y,unsigned short postion_x1,unsigned short postion_y1)
  52. {
  53.     write_com(0x2A);                   //Column address set
  54.     write_dat(postion_x>>8);
  55.     write_dat(postion_x);          //Starting co-ordinate of  x
  56.     write_dat(postion_x1>>8);
  57.     write_dat(postion_x1);     //End co-ordinate of  x
  58.    
  59.     write_com(0x2B);                    //Row address set
  60.     write_dat(postion_y>>8);               
  61.     write_dat(postion_y);           //Starting co-ordinate of   y
  62.     write_dat(postion_y1>>8);
  63.     write_dat(postion_y1);       //End co-ordinate of  y
  64.    
  65.     write_com(0x2C);   
  66. }
  67.  
  68.  
  69. #ifdef TFT_VERTICAL
  70. /*
  71.  * This function will Display the image on  TFT
  72.  *
  73.  * @param  NONE
  74.  *
  75.  * @return NONE
  76.  *
  77.  */
  78. void TFT_Putphoto(void)
  79. {
  80.     unsigned char *img_byte;
  81.     unsigned long img_size;
  82.  
  83.    address_set(100,310, 219,389);
  84.  
  85.     TFT_RS = 1;
  86.     TFT_SCK = 1;
  87.     img_byte = gImage_butterfly;
  88.     img_size =  0x4B00;     //19200bytes
  89.     while(img_size--)
  90.     {    
  91.         write_dat(*img_byte);
  92.         img_byte++;
  93.     }
  94. }
  95.  
  96. #endif
  97.  
  98. /*
  99.  * This function will initialize TFT
  100.  *
  101.  * @param  NONE
  102.  *
  103.  * @return NONE
  104.  *
  105.  */
  106. void TFT_init(void)
  107. {
  108.  
  109.     TFT_CS = 0;
  110.     TFT_RST=1;
  111.     delay_ms(10);
  112.     TFT_RST=0;
  113.     delay_ms(150);
  114.     TFT_RST=1;
  115.     delay_ms(200);
  116.  
  117. /************* Start Initial Sequence **********/
  118.     write_com(0xB0);
  119.     write_dat(0x80); //serial DOUT enable control
  120.  
  121.     write_com(0xC0);
  122.     write_dat(0x0A);    // P-Gamma level
  123.     write_dat(0x0A);    // N-Gamma level
  124.      
  125.     write_com(0xC1);      // BT & VC Setting
  126.     write_dat(0x45);  
  127.     write_dat(0x07);    // VCI1 = 2.5V
  128.      
  129.     write_com(0xC2);    // DC1.DC0 Setting
  130.     write_dat(0x33);      
  131.    
  132.     write_com(0xC5);
  133.     write_dat(0x00);
  134.     write_dat(0x42);     // VCM Setting VCOM voltage level for reduce the flicker issue
  135.     write_dat(0x80);     // VCM Register Enable
  136.      
  137.     write_com(0xB1);
  138.     write_dat(0xD0);     // Frame Rate Setting
  139.     write_dat(0x11);
  140.      
  141.     write_com(0xB4);
  142.     write_dat(0x02);     // 00: Column inversion; 02: Dot2 inversion
  143.    
  144.     write_com(0xB6);      // RM.DM Setting
  145.     write_dat(0x00);
  146. #ifdef TFT_VERTICAL
  147.     write_dat(0x22);
  148. #else
  149.     write_dat(0x02);  
  150. #endif
  151.  
  152.     write_dat(0x3B);
  153.      
  154.     write_com(0xB7);      // Entry Mode
  155.     write_dat(0x07);
  156.  
  157.     write_com(0x36);     // Color filter setting
  158. #ifdef TFT_VERTICAL
  159.     write_dat(0x09);
  160. #else
  161.     write_dat(0x2B); //row/column exchange
  162. #endif
  163.  
  164.      
  165.     write_com(0xF0);      // Enter ENG , must be set before gamma setting
  166.     write_dat(0x36);
  167.     write_dat(0xA5);
  168.     write_dat(0xD3);
  169.    
  170.     write_com(0xE5);      // Open gamma function , must be set before gamma setting
  171.     write_dat(0x80);
  172.    
  173.     write_com(0xE5);  
  174.     write_dat(0x01);
  175.    
  176.     write_com(0xB3);  
  177.     write_dat(0x00);    
  178.      
  179.     write_com(0xE5);  
  180.     write_dat(0x00);    
  181.      
  182.     write_com(0xF0);     // Exit ENG , must be set before gamma setting
  183.     write_dat(0x36);
  184.     write_dat(0xA5);
  185.     write_dat(0x53);
  186.    
  187.     write_com(0xE0);      // Gamma setting
  188.     write_dat(0x00);
  189.     write_dat(0x35);
  190.     write_dat(0x33);
  191.     write_dat(0x00);
  192.     write_dat(0x00);
  193.     write_dat(0x00);
  194.     write_dat(0x00);
  195.     write_dat(0x35);
  196.     write_dat(0x33);
  197.     write_dat(0x00);
  198.     write_dat(0x00);
  199.     write_dat(0x00);
  200.    
  201.     write_com(0x3A);     // interface setting
  202.     write_dat(0x55);
  203.      
  204.     write_com(0x11);    // Exit sleep mode
  205.     delay_ms(250);
  206.     write_com(0x29);     // Display on
  207. }
  208.  
  209. /*
  210.  * This function will Drow the circle on  TFT .
  211.  *
  212.  * @param  center of circle co-ordinate of Rectangle x .(unsigned short x)
  213.  * @param  center of circle co-ordinate of Rectangle y .(unsigned short y)
  214.  * @param  radius (rad)
  215.  * @param  color   (color)
  216.  * @param  Background color.(unsigned short b_color)
  217.  *
  218.  * @return NONE
  219.  *
  220.  */
  221. void TFT_circle(unsigned short postion_x,unsigned short postion_y,unsigned char rad ,unsigned short color,unsigned short b_clr)  
  222. {
  223.    unsigned short i,j;
  224.  
  225.    address_set(postion_x-rad, postion_y-rad, postion_x+rad, postion_y+rad);
  226.  
  227.    for(i= postion_x-rad;i<=(postion_x+rad);i++)
  228.     {
  229.         for(j =postion_y-rad;j<=(postion_y+rad);j++)
  230.         {
  231.             if((((unsigned short)(i-postion_x)*(i-postion_x))+(unsigned short)((j-postion_y)*(j-postion_y))) < (unsigned short)(rad*rad))
  232.             {
  233.                 write_dat(color>>8);
  234.                 write_dat(color);
  235.             }
  236.             else
  237.             {
  238.                 write_dat(b_clr>>8);
  239.                 write_dat(b_clr);
  240.             }
  241.         }
  242.     }
  243. }
  244.  
  245. /*
  246.  * This function will clear the Display .
  247.  *
  248.  * @param  Background color.(unsigned short color)
  249.  *
  250.  * @return NONE
  251.  *
  252.  */
  253. void TFT_Clear_Screen(unsigned short color)
  254. {
  255.     unsigned short i,j;
  256.     #ifdef TFT_VERTICAL                                    //for Vertical Mode
  257.             address_set(0,0,320,480);                      //Set area on GRAM
  258.     #else                                                  //for Horizontal Mode
  259.             address_set(0,0,480,320);                       //Set area on GRAM
  260.     #endif
  261.  
  262.     for (i=0;i<320;i++)
  263.         for (j=0;j<480;j++)
  264.         {
  265.             write_dat(color>>8);
  266.             write_dat(color);
  267.         }
  268. }
  269.  
  270. /*
  271.  * This function will Drow the Rectangle on  TFT .
  272.  *
  273.  * @param  Starting co-ordinate of Rectangle x .(unsigned short postion_x)
  274.  * @param  Starting co-ordinate of Rectangle y .(unsigned short postion_y)
  275.  * @param  Background color.(unsigned short color)
  276.  *
  277.  * @return NONE
  278.  *
  279.  */
  280. void TFT_Rectangle( unsigned short postion_x, unsigned short postion_y, unsigned short color)
  281. {
  282.     unsigned short i,j;
  283.  
  284. #ifdef TFT_VERTICAL
  285.     i = 320/4;
  286.     j = 480/4;
  287. #else
  288.     j = 320/4;
  289.     i = 480/4;
  290. #endif
  291.  
  292.     address_set(postion_x, postion_y, postion_x+i-1, postion_y+j-1);  
  293.  
  294.     for(postion_x=0;postion_x<j; postion_x++)
  295.     {
  296.         for(postion_y=0;postion_y<i;postion_y++)
  297.         {
  298.           write_dat(color>>8);
  299.           write_dat(color);
  300.         }
  301.     }
  302. }
  303.  
  304. /*
  305.  * This function will Write the one char on  TFT .
  306.  *
  307.  * @param  Starting address of string  (char Data_WR)
  308.  * @param  Starting co-ordinate of string  x .(unsigned short x)
  309.  * @param  Starting co-ordinate of string  y .(unsigned short y)
  310.  * @param  Color of fount.  (unsigned short color)
  311.  * @param  Background color.(unsigned short b_clr)
  312.  *
  313.  * @return NONE
  314.  *
  315.  */
  316. void TFT_PutChar(char Data_WR, unsigned short x, unsigned short y, unsigned short color,unsigned short b_clr)
  317. {
  318.     unsigned short i,j,k,l;
  319.     unsigned short c;
  320.    
  321.     k = ((gbWidth*gbHeight)/8);//number of bytes per charater
  322.     l = k*Data_WR;//byte location of character
  323.  
  324.      
  325.     address_set( x, y, x+gbWidth-1, y+gbHeight-1);
  326.  
  327.     for(j=0; j<k; j++)
  328.     {
  329.         c=(Font16x31[l+j]);
  330.        
  331.         for(i=0; i<8; i++)
  332.             {      
  333.                 if((c & 0x80)==0x80)
  334.                 {
  335.                     write_dat(color>>8);
  336.                     write_dat(color);
  337.                 }
  338.                 else
  339.                 {
  340.                     write_dat(b_clr>>8);
  341.                     write_dat(b_clr);
  342.                 }
  343.                 c<<=1;    
  344.             }
  345.     }
  346. }
  347.  
  348. /*
  349.  * This function will Display the string on  TFT .
  350.  *
  351.  * @param  Starting address of string  (char *s)
  352.  * @param  Starting co-ordinate of string  x .(unsigned short x)
  353.  * @param  Starting co-ordinate of string  y .(unsigned short y)
  354.  * @param  Color of fount.  (unsigned short color)
  355.  * @param  Background color.(unsigned short b_clr)
  356.  *
  357.  * @return NONE
  358.  *
  359.  */
  360. void TFT_PutString(char *s, unsigned short x, unsigned short y, unsigned short color,unsigned short b_clr)
  361. {
  362.     while(*s)
  363.     {
  364.     #ifdef TFT_VERTICAL
  365.         if(x >=(319))
  366.     #else
  367.         if(x >=(479))
  368.     #endif
  369.         {
  370.             x=5;
  371.             y += (gbHeight+5);
  372.         #ifdef TFT_VERTICAL
  373.             if(y>=(479))
  374.         #else
  375.             if(y>=(319))
  376.         #endif
  377.             {
  378.                 y=5;
  379.             }
  380.         }
  381.  
  382.         TFT_PutChar((*s), x, y, color, b_clr);
  383.         x += gbWidth;              
  384.         s++;
  385.     }
  386. }
  387.  
  388.  
  389. void main()
  390. {
  391.  
  392.     #ifdef  SPI_8051
  393.         SPI_initialize();      
  394.     #endif     
  395.    
  396.     TFT_init();
  397.        
  398.     while(1)
  399.     {
  400.     #ifdef TFT_VERTICAL
  401.       gbHeight = 31;
  402.       gbWidth = 16;
  403.       TFT_Clear_Screen(WHITE);
  404.       TFT_PutString("     WELCOME       ",5,100, BLACK, BLUE2);
  405.       TFT_PutString("       TO          ",5,131, BLACK, BLUE2);
  406.       TFT_PutString("  www.tinylcd.com  ",5,162, RED, BLUE2);
  407.       TFT_Putphoto();
  408.       delay(1000);
  409.  
  410.       gbHeight = 31;
  411.       gbWidth = 16;
  412.       TFT_Clear_Screen(WHITE);
  413.       TFT_PutString(" Size:",5,25, BLACK, WHITE);
  414.       TFT_PutString("3.5'' TFT",85,65, RED, WHITE);
  415.       TFT_PutString(" Pixel:",5,100, BLACK, WHITE);
  416.       TFT_PutString("320x480 ",85,145, RED, WHITE);
  417.       TFT_PutString("   www.tinylcd.com",5,205, BLUE, WHITE);
  418.       TFT_Putphoto();  
  419.       delay(1000);
  420.  
  421.       TFT_Clear_Screen(WHITE);
  422.       TFT_Rectangle(0,0,BLUE);
  423.       TFT_Rectangle(30,30,RED);
  424.       TFT_Rectangle(60,60,YELLOW) ;
  425.       TFT_Rectangle(90,90,MAGENTA);
  426.       TFT_Rectangle(120,120,ORANGE);
  427.       TFT_Rectangle(150,150,GREEN);
  428.       TFT_Rectangle(180,180,CYAN);
  429.       TFT_Rectangle(210,210,BLUE2);
  430.       TFT_PutString("  www.tinylcd.com  ",5,430, BLUE, WHITE);
  431.       delay(1000);
  432.      
  433.       TFT_Clear_Screen(WHITE);
  434.       TFT_circle(160,120,30,BLUE, WHITE);
  435.       TFT_circle(80,240,30,RED, WHITE);
  436.       TFT_circle(240,240,30,GREEN, WHITE);
  437.       TFT_circle(160,360,30,YELLOW, WHITE);
  438.       TFT_PutString("www.tinylcd.com",30,410, BLUE, WHITE);
  439.       delay(1000);
  440.  
  441.       gbHeight = 31;
  442.       gbWidth = 16;
  443.       TFT_Clear_Screen(WHITE);
  444.       TFT_PutString("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z", 5, 5, BLACK, WHITE);
  445.       TFT_PutString("a b c d e f g h i j k l m n o p q r s t u v w x y z", 5, 150, BLACK, WHITE);
  446.       TFT_PutString("0 1 2 3 4 5 6 7 8 9",5,280, BLACK, WHITE);
  447.       TFT_Putphoto();
  448.       delay(1000);
  449.     #else
  450.       gbHeight = 31;
  451.       gbWidth = 16;
  452.       TFT_Clear_Screen(WHITE);
  453.       TFT_PutString("           WELCOME          ",5,90, BLACK, BLUE2);
  454.       TFT_PutString("             TO             ",5,121, BLACK, BLUE2);
  455.       TFT_PutString("        www.tinylcd.com     ",5,152, RED, BLUE2);
  456.       delay(1000);
  457.  
  458.       gbHeight = 31;
  459.       gbWidth = 16;
  460.       TFT_Clear_Screen(WHITE);
  461.       TFT_PutString("   Size:",5,25, BLACK, WHITE);
  462.       TFT_PutString("   3.5'' TFT",115,65, RED, WHITE);
  463.       TFT_PutString("   Pixels:",5,100, BLACK, WHITE);
  464.       TFT_PutString("  480x320 ",115,145, RED, WHITE);
  465.       TFT_PutString("    www.tinylcd.com",55,205, BLUE, WHITE);
  466.       delay(1000);
  467.  
  468.       TFT_Clear_Screen(WHITE);
  469.       TFT_Rectangle(0,0,BLUE) ;
  470.       TFT_Rectangle(30,30,RED);
  471.       TFT_Rectangle(60,60,YELLOW) ;
  472.       TFT_Rectangle(90,90,MAGENTA);
  473.       TFT_Rectangle(120,120,ORANGE);
  474.       TFT_Rectangle(150,150,GREEN);
  475.       TFT_Rectangle(180,180,CYAN);
  476.       TFT_Rectangle(210,210,BLUE2);
  477.       delay(1000);
  478.      
  479.       TFT_Clear_Screen(WHITE);
  480.       TFT_circle(240,80,30,BLUE, WHITE);
  481.       TFT_circle(120,160,30,RED, WHITE);
  482.       TFT_circle(360,160,30,GREEN, WHITE);
  483.       TFT_circle(240,240,30,YELLOW, WHITE);
  484.       TFT_PutString("www.tinylcd.com",120,280, BLUE, WHITE);
  485.       delay(1000);
  486.  
  487.       gbHeight = 31;
  488.       gbWidth = 16;
  489.       TFT_Clear_Screen(WHITE);
  490.       TFT_PutString("A B C D E F G H I J K L M N O P Q R S T U V W X Y Z", 5, 5, BLACK, WHITE);
  491.       TFT_PutString("a b c d e f g h i j k l m n o p q r s t u v w x y z", 5, 140, BLACK, WHITE);  
  492.       TFT_PutString("0 1 2 3 4 5 6 7 8 9",5,250, BLACK, WHITE);
  493.       delay(1000);
  494.     #endif
  495.     TFT_Clear_Screen(WHITE);
  496.     delay(1000);
  497.     TFT_Clear_Screen(BLACK);
  498.     delay(1000);
  499.     TFT_Clear_Screen(BLUE);
  500.     delay(1000);
  501.     TFT_Clear_Screen(RED);
  502.     delay(1000);
  503.     TFT_Clear_Screen(YELLOW);
  504.     delay(1000);
  505.     TFT_Clear_Screen(BLUE2);
  506.     delay(1000);
  507.     TFT_Clear_Screen(CYAN);
  508.     delay(1000);
  509.     TFT_Clear_Screen(GREEN);
  510.     delay(1000);
  511.     TFT_Clear_Screen(ORANGE);
  512.     delay(1000);
  513.     TFT_Clear_Screen(MAGENTA);
  514.     delay(1000);
  515.     }          
  516. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement