Advertisement
Guest User

ILI9325 Control

a guest
Jul 22nd, 2010
1,083
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 8.67 KB | None | 0 0
  1. //---------------------------------------------//
  2. // File: ILI9331.c
  3.  
  4. //--------------- Required Headers ------------//
  5. #include "ILI9331.h"
  6. #include "font.h"
  7. #include "delay.h"
  8. #include "tu.h"
  9.  
  10. void LCD_WR_DATA16(unsigned int data)
  11. {
  12.     LCD_RD_H();
  13.     LCD_RS_H();
  14.   LCD_CS_L();  
  15.   LCD_WR_L();
  16.      
  17.   DATAH_LCD_PORT = data >> 8;
  18.   DATAL_LCD_PORT = data & 0x00FF;
  19.  
  20.   LCD_WR_H();
  21.   LCD_CS_H();
  22.  
  23. }
  24. void LCD_WR_REG16(unsigned int index)
  25. {
  26.   LCD_RD_H();
  27.     LCD_RS_L();
  28.   LCD_CS_L();  
  29.   LCD_WR_L();
  30.  
  31.   DATAH_LCD_PORT = index >> 8;
  32.     DATAL_LCD_PORT = index & 0x00FF;
  33.  
  34.   LCD_WR_H();
  35.   LCD_CS_H();
  36. }
  37.  
  38. void LCD_WR_REG(unsigned int index,unsigned int data)
  39. {
  40.     LCD_WR_REG16(index);
  41.   LCD_WR_DATA16(data);
  42. }
  43.  
  44.  
  45. void LCD_Init(void)
  46. {
  47.     // Init data pins
  48.     DATA_OUTPUT();
  49.  
  50.     // Init control pins
  51.     LCD_BL_DDR |= 1<<LCD_BL_BIT;
  52.     LCD_CS_DDR |= 1<<LCD_CS_BIT;
  53.     LCD_RS_DDR |= 1<<LCD_RS_BIT;
  54.     LCD_WR_DDR |= 1<<LCD_WR_BIT;
  55.     LCD_RD_DDR |= 1<<LCD_RD_BIT;
  56.     LCD_RST_DDR|= 1<<LCD_RST_BIT;
  57.  
  58.     LCD_RST_L();
  59.     delay_ms(10);
  60.     LCD_RST_H();
  61.     delay_ms(50);
  62.  
  63.     // Start init sequence
  64.     LCD_WR_REG(0x00E3, 0x3008);        
  65.     LCD_WR_REG(0x00E7, 0x0012);
  66.     LCD_WR_REG(0x00EF, 0x1231); // set the internal timing;    
  67.     LCD_WR_REG(0x0000, 0x0001); // start fosc
  68.     LCD_WR_REG(0x0001, 0x0100); // set SS bit
  69.     LCD_WR_REG(0x0002, 0x0700); // set 1 line inversion
  70.  
  71.     #if   ID_AM==000      
  72.         LCD_WR_REG(0x0003, 0x1000);
  73.     #elif ID_AM==001        
  74.         LCD_WR_REG(0x0003, 0x1008);      
  75.     #elif ID_AM==010  
  76.         LCD_WR_REG(0x0003, 0x1010);        
  77.     #elif ID_AM==011
  78.         LCD_WR_REG(0x0003, 0x1018);
  79.     #elif ID_AM==100  
  80.         LCD_WR_REG(0x0003, 0x1020);      
  81.     #elif ID_AM==101  
  82.         LCD_WR_REG(0x0003, 0x1028);      
  83.     #elif ID_AM==110  
  84.         LCD_WR_REG(0x0003, 0x1030);      
  85.     #elif ID_AM==111  
  86.         LCD_WR_REG(0x0003, 0x1038);
  87.     #endif    
  88.  
  89.     LCD_WR_REG(0x0004, 0x0000); // Resize register
  90.   LCD_WR_REG(0x0008, 0x0202); // set the back porch and front porch
  91.   LCD_WR_REG(0x0009, 0x0000); // set non-display area refresh cycle ISC[3:0]
  92.   LCD_WR_REG(0x000A, 0x0000); // FMARK function
  93.   LCD_WR_REG(0x000C, 0x0000); // RGB interface setting
  94.   LCD_WR_REG(0x000D, 0x0000); // Frame marker Position
  95.   LCD_WR_REG(0x000F, 0x0000); // RGB interface polarity
  96.  
  97.     //Power On sequence //
  98.   LCD_WR_REG(0x0010, 0x0000); // SAP, BT[3:0], AP, DSTB, SLP, STB
  99.   LCD_WR_REG(0x0011, 0x0007); // DC1[2:0], DC0[2:0], VC[2:0]
  100.   LCD_WR_REG(0x0012, 0x0000); // VREG1OUT voltage
  101.   LCD_WR_REG(0x0013, 0x0000); // VDV[4:0] for VCOM amplitude
  102.     delay_ms(200);
  103.  
  104.     LCD_WR_REG(0x0010, 0x1490); // SAP, BT[3:0], AP, DSTB, SLP, STB
  105.   LCD_WR_REG(0x0011, 0x0227); // DC1[2:0], DC0[2:0], VC[2:0]
  106.     delay_ms(50);
  107.  
  108.   LCD_WR_REG(0x0012, 0x001C); // VREG1OUT voltage
  109.     delay_ms(50);
  110.  
  111.   LCD_WR_REG(0x0013, 0x1A00); // VDV[4:0] for VCOM amplitude
  112.     LCD_WR_REG(0x0029, 0x0025); // Set VCM[5:0] for VCOMH
  113.   LCD_WR_REG(0x002B, 0x000C); // Set Frame Rate
  114.     delay_ms(50);
  115.  
  116.     // GRAM Setup    
  117.     #if   ID_AM==000        
  118.         LCD_WR_REG(0x0020, 0x00ef);
  119.         LCD_WR_REG(0x0021, 0x013f);      
  120.     #elif ID_AM==001
  121.         LCD_WR_REG(0x0020, 0x00ef);
  122.         LCD_WR_REG(0x0021, 0x013f);      
  123.     #elif ID_AM==010
  124.         LCD_WR_REG(0x0020, 0x0000);
  125.         LCD_WR_REG(0x0021, 0x013f);      
  126.     #elif ID_AM==011
  127.         LCD_WR_REG(0x0020, 0x0000);
  128.         LCD_WR_REG(0x0021, 0x013f);      
  129.     #elif ID_AM==100
  130.         LCD_WR_REG(0x0020, 0x00ef);
  131.         LCD_WR_REG(0x0021, 0x0000);      
  132.     #elif ID_AM==101  
  133.         LCD_WR_REG(0x0020, 0x00ef);
  134.         LCD_WR_REG(0x0021, 0x0000);      
  135.     #elif ID_AM==110
  136.         LCD_WR_REG(0x0020, 0x0000);
  137.         LCD_WR_REG(0x0021, 0x0000);      
  138.     #elif ID_AM==111
  139.         LCD_WR_REG(0x0020, 0x0000);
  140.         LCD_WR_REG(0x0021, 0x0000);        
  141.     #endif
  142.  
  143.     // Adjust the Gamma Curve
  144.     LCD_WR_REG(0x0030, 0x0107);
  145.     LCD_WR_REG(0x0031, 0x0306);
  146.     LCD_WR_REG(0x0032, 0x0207);
  147.     LCD_WR_REG(0x0035, 0x0206);
  148.     LCD_WR_REG(0x0036, 0x0408);
  149.     LCD_WR_REG(0x0037, 0x0106);
  150.     LCD_WR_REG(0x0038, 0x0102);
  151.     LCD_WR_REG(0x0039, 0x0207);
  152.     LCD_WR_REG(0x003C, 0x0504);
  153.     LCD_WR_REG(0x003D, 0x0503);
  154.          
  155.     // Set GRAM area
  156.   LCD_WR_REG(0x0050, 0x0000); // Horizontal GRAM Start Address
  157.   LCD_WR_REG(0x0051, 0x00EF); // Horizontal GRAM End Address
  158.     LCD_WR_REG(0x0052, 0x0000); // Vertical GRAM Start Address
  159.   LCD_WR_REG(0x0053, 0x013F); // Vertical GRAM Start Address
  160.   LCD_WR_REG(0x0060, 0xA700); // Gate Scan Line 0xA700
  161.   LCD_WR_REG(0x0061, 0x0001); // NDL, VLE, REV
  162.   LCD_WR_REG(0x006A, 0x0000); // set scrolling line
  163.  
  164.     // Partial Display Control
  165.   LCD_WR_REG(0x0080, 0x0000);
  166.   LCD_WR_REG(0x0081, 0x0000);
  167.   LCD_WR_REG(0x0082, 0x0000);
  168.   LCD_WR_REG(0x0083, 0x0000);
  169.   LCD_WR_REG(0x0084, 0x0000);
  170.   LCD_WR_REG(0x0085, 0x0000);
  171.  
  172.     // Panel Control
  173.   LCD_WR_REG(0x0090, 0x0010);
  174.   LCD_WR_REG(0x0092, 0x0600);
  175.  
  176.     // Turn Display ON
  177.   LCD_WR_REG(0x0007, 0x0133); // 262K color and display ON
  178.  
  179.     LCD_Clear(BLACK); // Clear screen
  180.     LCD_BL_H(); // Turn backlight on
  181. }
  182.  
  183. void LCD_Clear(unsigned int Color)
  184. {
  185.     unsigned long index = 0;
  186.  
  187.   LCD_WR_REG(0x0020, 0x0000);
  188.   LCD_WR_REG(0x0021, 0x0000);      
  189.  
  190.   LCD_WR_REG(0x0050, 0x0000);
  191.   LCD_WR_REG(0x0051, 239);
  192.   LCD_WR_REG(0x0052, 0x0000);
  193.   LCD_WR_REG(0x0053, 319);
  194.   LCD_WR_REG16(0x0022);
  195.  
  196.   LCD_RD_H();
  197.   LCD_RS_H();
  198.   LCD_CS_L();  
  199.  
  200.     for(index = 0; index < 76800; index++)
  201.   {
  202.     LCD_WR_L();
  203.     DATAH_LCD_PORT = Color >> 8;
  204.     DATAL_LCD_PORT = Color & 0x00FF;
  205.     LCD_WR_H();  
  206.   }
  207.    
  208.     LCD_CS_H();
  209. }
  210.  
  211. void LCD_write_english(char data,unsigned int color,unsigned int xcolor)
  212. {
  213.   unsigned char avl,i,n;
  214.    
  215.     LCD_WR_REG16(0x0022);  
  216.   for (i=0;i<16;i++)
  217.   {
  218.         avl=pgm_read_byte( &english_16x8[data-32][i]);
  219.       for (n=0;n<8;n++)
  220.       {
  221.         if(avl&0x80)
  222.                 LCD_WR_DATA16(color);
  223.       else
  224.                 LCD_WR_DATA16(xcolor);    
  225.         avl<<=1;
  226.       }
  227.     }
  228. }
  229.  
  230. void LCD_write_english_string(unsigned int x,unsigned int y,char *s,unsigned int color,unsigned int xcolor)
  231. {
  232.     unsigned int k=0;
  233.    
  234.     while (*s)
  235.   {
  236.     Lcd_SetBox(y,x+k,15,8);
  237.     LCD_write_english( *s,color,xcolor);
  238.     k=k+8;
  239.     s++;
  240.   }
  241. }  
  242.  
  243. void LCD_write_SUM(unsigned int x,unsigned int y,unsigned char SUM,unsigned int color,unsigned int xcolor)//дÊý×Ö
  244. {
  245.     unsigned char avl,i,n;
  246.  
  247.     Lcd_SetBox(y,x,15,8);
  248.   LCD_WR_REG16(0x0022);  
  249.   for (i=0;i<16;i++)
  250.   {
  251.     avl=pgm_read_byte( &english_16x8[SUM+16][i]);
  252.       for (n=0;n<8;n++)
  253.       {
  254.         if(avl&0x80)
  255.                 LCD_WR_DATA16(color);
  256.       else
  257.                 LCD_WR_DATA16(xcolor);  
  258.         avl<<=1;
  259.       }
  260.     }
  261. }
  262.  
  263. void Lcd_SetBox(unsigned int xStart,unsigned int yStart,unsigned int xLong,unsigned int yLong)
  264. {
  265.     #if ID_AM==000    
  266.         LCD_SetCursor(xStart+xLong-1,312-yStart+yLong-1);
  267.     #elif ID_AM==001
  268.         LCD_SetCursor(xStart+xLong-1,312-yStart+yLong-1);
  269.     #elif ID_AM==010
  270.         LCD_SetCursor(xStart,312-yStart+yLong-1);    
  271.     #elif ID_AM==011
  272.         LCD_SetCursor(xStart,312-yStart+yLong-1);    
  273.     #elif ID_AM==100
  274.         LCD_SetCursor(xStart+xLong-1,312-yStart);          
  275.     #elif ID_AM==101
  276.         LCD_SetCursor(xStart+xLong-1,312-yStart);        
  277.     #elif ID_AM==110
  278.         LCD_SetCursor(xStart,312-yStart);  
  279.     #elif ID_AM==111
  280.         LCD_SetCursor(xStart,312-yStart);      
  281.     #endif
  282.      
  283.     LCD_WR_REG(0x0050,xStart);
  284.     LCD_WR_REG(0x0051,xStart+xLong-1);
  285.     LCD_WR_REG(0x0052,312-yStart);
  286.     LCD_WR_REG(0x0053,312-yStart+yLong-1);
  287. }
  288.  
  289. void LCD_SetCursor(unsigned int Xpos, unsigned int Ypos)
  290. {
  291.   LCD_WR_REG(0x20, Xpos);
  292.   LCD_WR_REG(0x21, Ypos);
  293. }
  294.  
  295. void GUI_Point(unsigned int x, unsigned int y,unsigned int color)
  296. {
  297.     LCD_WR_REG(0x0020,x);
  298.   LCD_WR_REG(0x0021,y);
  299.  
  300.     LCD_WR_REG16(0x0022);
  301.     LCD_WR_DATA16(color);    
  302. }
  303.  
  304. void Test(void)
  305. {      
  306.     unsigned long n,i;
  307.  
  308.     LCD_WR_REG(0x0020,0x0000);
  309.   LCD_WR_REG(0x0021,0x0000);
  310.   for(i=0;i<7;i++)      
  311.   {              
  312.         LCD_WR_REG(0x0050,0x00);
  313.       LCD_WR_REG(0x0051,239);
  314.       LCD_WR_REG(0x0052,0x00);
  315.       LCD_WR_REG(0x0053,319);
  316.            
  317.     LCD_WR_REG16(0x0022);      
  318.     for(n=0;n<76800;n++)
  319.     {              
  320.             if(i==0)
  321.                 LCD_WR_DATA16(BLUE);  
  322.       if(i==1)
  323.                 LCD_WR_DATA16(RED);
  324.       if(i==2)
  325.                 LCD_WR_DATA16(GREEN);
  326.       if(i==3)
  327.                 LCD_WR_DATA16(CYAN);
  328.       if(i==4)
  329.                 LCD_WR_DATA16(MAGENTA);
  330.       if(i==5)
  331.                 LCD_WR_DATA16(YELLOW);               
  332.       if(i==6)
  333.                 LCD_WR_DATA16(BLACK);
  334.         }    
  335.     delay_ms(100);                  
  336.     }      
  337. }
  338.  
  339. void Test_1(void)
  340. {
  341.     unsigned int i;
  342.   // unsigned char  val;
  343.   LCD_WR_REG(0x0020,20);
  344.   LCD_WR_REG(0x0021,105);    
  345.  
  346.     // Start a new window 20,207 to 105,207
  347.   LCD_WR_REG(0x0050,20);
  348.   LCD_WR_REG(0x0051,207);
  349.   LCD_WR_REG(0x0052,105);
  350.   LCD_WR_REG(0x0053,207);
  351.   LCD_WR_REG16(0x0022);
  352.        
  353.   LCD_RD_H();
  354.   LCD_RS_H();
  355.   LCD_CS_L();
  356.  
  357.   for (i=0;i<21218;)
  358.   {
  359.     LCD_WR_L();
  360.     DATAH_LCD_PORT = pgm_read_byte(&gImage[i++]);
  361.     DATAL_LCD_PORT = pgm_read_byte(&gImage[i++]);
  362.     LCD_WR_H();
  363.     }
  364.  
  365.   LCD_CS_H();
  366. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement