ILI9225 TFT显示屏16位并口方式驱动

 所用屏及资料如后图:

        ILI9225,176*220,8位或16位并口屏,IM0接GND,电源及背光接3.3v

主控:CH32V307驱动(库文件和STM32基本一样)

ILI9225 TFT显示屏16位并口方式驱动_第1张图片

ILI9225 TFT显示屏16位并口方式驱动_第2张图片

ILI9225 TFT显示屏16位并口方式驱动_第3张图片

一、源码

ILI9225.c

#include "ILI9225.h"
#include "ch32v30x_gpio.h"
//---汉字的字库头文件---//
#include"charcode.h"

/* Global Variable */
unsigned char fch,fcl;  //前景色高位和低位
/*
 *
 * CS --_____________________________--,低:有效
 * RD ---------------------------------,高:写
 * WR  -__--__--__--__--__--__--__-----,低:写
 * RS _________________________________,低:Index
 * RS ---------------------------------,高:Data
 *
 */
void LCD_Writ_Bus16(unsigned char VH,unsigned char VL)
{
    u16 val;
    val=((VH<<8)&0xFF00)+VL;

    LCD_WR_SET;
    LCD_RD_SET;

    GPIO_Write(LCD_PORT,val);

    LCD_WR_CLR;
    LCD_WR_SET;

}

void LCD_Write_COM(unsigned char VL)
{
    LCD_CS_CLR;
    LCD_RS_CLR;
    LCD_Writ_Bus16(0x00,VL);
    LCD_CS_SET;
}


void LCD_Write_DATA(unsigned char VH,unsigned char VL)
{
    LCD_CS_CLR;
    LCD_RS_SET;
    LCD_Writ_Bus16(VH,VL);
    LCD_CS_SET;
}

void LCD_Write_DATA16(unsigned short val)
{
    LCD_CS_CLR;
    LCD_RS_SET;
    LCD_RD_SET;
    LCD_WR_CLR;

    GPIO_Write(LCD_PORT,val);

    LCD_WR_CLR;
    LCD_WR_SET;
    LCD_CS_SET;

}

void LCD_Write_COM_DATA(unsigned char com1,unsigned short dat1)
{
    LCD_Write_COM(com1);
    LCD_Write_DATA16(dat1);
}

/*
 * @prief InitLCD
 */
void InitLCD()
{

    LCD_Reset();
    Delay_Ms(150);
 
    printf("ili9225 Initial\r\n");
    Delay_Ms(50);

    LCD_CS_CLR;

    LCD_Write_COM_DATA(0x10, 0x0000);
    LCD_Write_COM_DATA(0x11, 0x0000);
    LCD_Write_COM_DATA(0x12, 0x0000);
    LCD_Write_COM_DATA(0x13, 0x0000);
    LCD_Write_COM_DATA(0x14, 0x0000);
    Delay_Ms(40);

    LCD_Write_COM_DATA(0x11, 0x0018);
    LCD_Write_COM_DATA(0x12, 0x6121);
    LCD_Write_COM_DATA(0x13, 0x006F);
    LCD_Write_COM_DATA(0x14, 0x495F);
    LCD_Write_COM_DATA(0x10, 0x0800);
    Delay_Ms(10);
    LCD_Write_COM_DATA(0x11, 0x103B);
    Delay_Ms(50);
    LCD_Write_COM_DATA(0x01, 0x011C);
    LCD_Write_COM_DATA(0x02, 0x0100);
    LCD_Write_COM_DATA(0x03, 0x1030);
    LCD_Write_COM_DATA(0x07, 0x0000);
    LCD_Write_COM_DATA(0x08, 0x0808);
    LCD_Write_COM_DATA(0x0b, 0x1100);
    LCD_Write_COM_DATA(0x0c, 0x0000);
    LCD_Write_COM_DATA(0x0f, 0x0D01);
    LCD_Write_COM_DATA(0x15, 0x0020);
    LCD_Write_COM_DATA(0x20, 0x0000);
    LCD_Write_COM_DATA(0x21, 0x0000);

    LCD_Write_COM_DATA(0x30, 0x0000);
    LCD_Write_COM_DATA(0x31, 0x00DB);
    LCD_Write_COM_DATA(0x32, 0x0000);
    LCD_Write_COM_DATA(0x33, 0x0000);

    LCD_Write_COM_DATA(0x34, 0x00DB);
    LCD_Write_COM_DATA(0x35, 0x0000);
    LCD_Write_COM_DATA(0x36, 0x00AF);
    LCD_Write_COM_DATA(0x37, 0x0000);
    LCD_Write_COM_DATA(0x38, 0x00DB);
    LCD_Write_COM_DATA(0x39, 0x0000);

    LCD_Write_COM_DATA(0x50, 0x0000);
    LCD_Write_COM_DATA(0x51, 0x0808);
    LCD_Write_COM_DATA(0x52, 0x080A);
    LCD_Write_COM_DATA(0x53, 0x000A);
    LCD_Write_COM_DATA(0x54, 0x0A08);
    LCD_Write_COM_DATA(0x55, 0x0808);
    LCD_Write_COM_DATA(0x56, 0x0000);
    LCD_Write_COM_DATA(0x57, 0x0A00);
    LCD_Write_COM_DATA(0x58, 0x0710);
    LCD_Write_COM_DATA(0x59, 0x0710);

    LCD_Write_COM_DATA(0x07, 0x0012);
    Delay_Ms(50);
    LCD_Write_COM_DATA(0x07, 0x1017);
    LCD_Write_COM(0x22);
    LCD_CS_SET;
}

void LCD_Reset(void)
{
    LCD_RST_CLR;
    Delay_Ms(50);
    LCD_RST_SET;
    Delay_Ms(150);
}

/*
 * @prief clrScr 清屏
 */
void clrScr(unsigned short color)
{
    int i;
    u8 ch,cl;
    ch=color>>8;
    cl=color&0xff;

    clrXY();

    for (i=0; i<((disp_x_size+1)*(disp_y_size+1)); i++)
    {
       LCD_Write_DATA(ch,cl);

    }

}

void setXY(unsigned char x1, unsigned char y1, unsigned char x2, unsigned char y2)
{
    LCD_Write_COM_DATA(0x36,x2);
    LCD_Write_COM_DATA(0x37,x1);
    LCD_Write_COM_DATA(0x38,y2);
    LCD_Write_COM_DATA(0x39,y1);
    LCD_Write_COM_DATA(0x20,x1);
    LCD_Write_COM_DATA(0x21,y1);
    LCD_Write_COM(0x22);
}

void clrXY()
{
    setXY(0,0,disp_x_size,disp_y_size);
}
void setPixel(unsigned char color)
{

    LCD_Write_DATA((color>>8),(color&0xFF));    // rrrrrggggggbbbbb

}


void drawPixel(unsigned char  x, unsigned char  y)
{
    setXY(x, y, x, y);
    setPixel((fch<<8)|fcl);
    clrXY();
}


void setColor(unsigned short color)
{
    fch=color>>8;
    fcl=color & 0xFF;
}


void drawHLine(unsigned char x, unsigned char y, unsigned char l)
{
    setXY(x, y, x+l, y);

    for(int i=0; i x1 ? x2 - x1 : x1 - x2);
        short           xstep =  x2 > x1 ? 1 : -1;
        unsigned int    dy = (y2 > y1 ? y2 - y1 : y1 - y2);
        short           ystep =  y2 > y1 ? 1 : -1;
        int             col = x1, row = y1;
        if (dx < dy)
        {
            int t = - (dy >> 1);
            while (1)
            {
                setXY (col, row, col, row);
                LCD_Write_DATA(fch, fcl);
                if (row == y2)
                    return;
                row += ystep;
                t += dx;
                if (t >= 0)
                {
                    col += xstep;
                    t   -= dy;
                }
            }
        }
        else
                {
                    int t = - (dx >> 1);
                    while (1)
                    {
                        setXY (col, row, col, row);
                        LCD_Write_DATA(fch, fcl);
                        if (col == x2)
                            return;
                        col += xstep;
                        t += dy;
                        if (t >= 0)
                        {
                            row += ystep;
                            t   -= dx;
                        }
                    }
                }
        }
        clrXY();
}

void drawRect(int x1, int y1, int x2, int y2)
{
    drawHLine(x1, y1, x2-x1);
    drawHLine(x1, y2, x2-x1);
    drawVLine(x1, y1, y2-y1);
    drawVLine(x2, y1, y2-y1);
}

void drawRect1(unsigned char x, unsigned char y, unsigned char w, unsigned char h, unsigned short c)
{
  setColor(c);
  drawRect(x, y,x+w-1 , y+h-1);
}

void fillRect(int x1, int y1, int x2, int y2)
{
/*
    for (int i=0; i<((x2-x1)/2)+1; i++)
    {
        drawVLine(x1+i, y1, y2-y1);
        drawVLine(x2-i, y1, y2-y1);
    }
    */
    setXY(x1, y1, x2, y2);

    for(int i=0; i<(x2-x1)*(y2-y1)-1; i++)
    {
        LCD_Write_DATA(fch, fcl);
    }
    clrXY();
}
void drawRoundRect(int x1, int y1, int x2, int y2)
{
    if ((x2-x1)>4 && (y2-y1)>4)
    {
        drawPixel(x1+1,y1+1);
        drawPixel(x2-1,y1+1);
        drawPixel(x1+1,y2-1);
        drawPixel(x2-1,y2-1);
        drawHLine(x1+2, y1, x2-x1-4);
        drawHLine(x1+2, y2, x2-x1-4);
        drawVLine(x1, y1+2, y2-y1-4);
        drawVLine(x2, y1+2, y2-y1-4);
    }
}

void fillRoundRect(int x1, int y1, int x2, int y2)
{
    if ((x2-x1)>4 && (y2-y1)>4)
    {
        for (int i=0; i<((y2-y1)/2)+1; i++)
        {
            switch(i)
            {
            case 0:
                drawHLine(x1+2, y1+i, x2-x1-4);
                drawHLine(x1+2, y2-i, x2-x1-4);
                break;
            case 1:
                drawHLine(x1+1, y1+i, x2-x1-2);
                drawHLine(x1+1, y2-i, x2-x1-2);
                break;
            default:
                drawHLine(x1, y1+i, x2-x1);
                drawHLine(x1, y2-i, x2-x1);
            }
        }
    }
}


void drawCircle(int x, int y, int radius)
{
    int f = 1 - radius;
    int ddF_x = 1;
    int ddF_y = -2 * radius;
    int x1 = 0;
    int y1 = radius;


    setXY(x, y + radius, x, y + radius);
    LCD_Write_DATA(fch,fcl);
    setXY(x, y - radius, x, y - radius);
    LCD_Write_DATA(fch,fcl);
    setXY(x + radius, y, x + radius, y);
    LCD_Write_DATA(fch,fcl);
    setXY(x - radius, y, x - radius, y);
    LCD_Write_DATA(fch,fcl);

    while(x1 < y1)
    {
        if(f >= 0)
        {
            y1--;
            ddF_y += 2;
            f += ddF_y;
        }
        x1++;
        ddF_x += 2;
        f += ddF_x;
        setXY(x + x1, y + y1, x + x1, y + y1);
        LCD_Write_DATA(fch,fcl);
        setXY(x - x1, y + y1, x - x1, y + y1);
        LCD_Write_DATA(fch,fcl);
        setXY(x + x1, y - y1, x + x1, y - y1);
        LCD_Write_DATA(fch,fcl);
        setXY(x - x1, y - y1, x - x1, y - y1);
        LCD_Write_DATA(fch,fcl);
        setXY(x + y1, y + x1, x + y1, y + x1);
        LCD_Write_DATA(fch,fcl);
        setXY(x - y1, y + x1, x - y1, y + x1);
        LCD_Write_DATA(fch,fcl);
        setXY(x + y1, y - x1, x + y1, y - x1);
        LCD_Write_DATA(fch,fcl);
        setXY(x - y1, y - x1, x - y1, y - x1);
        LCD_Write_DATA(fch,fcl);
    }
    clrXY();
}

void fillCircle(int x, int y, int radius)
{
    for(int y1=-radius; y1<=0; y1++)
        for(int x1=-radius; x1<=0; x1++)
            if(x1*x1+y1*y1 <= radius*radius)
            {
                drawHLine(x+x1, y+y1, 2*(-x1));
                drawHLine(x+x1, y-y1, 2*(-x1));
                break;
            }
}

void show_color_bar()
{
    unsigned long i,j;
    clrXY();

        for (i=0; i210){LCD_Write_DATA(0xFA,0x00);}
                  else if(i>180){LCD_Write_DATA(0x04,0x00);}
                  else if(i>150){LCD_Write_DATA(0xC6,0x18);}
                  else if(i>120){LCD_Write_DATA(0x00,0x1F);}
                  else if(i>90){LCD_Write_DATA(0x84,0x10);}
                  else if(i>60){LCD_Write_DATA(0x80,0x00);}
                  else if(i>30){LCD_Write_DATA(0xFF,0xE0);}
                  else {LCD_Write_DATA(0x80,10);}
                }
            }
}


/****************************************************************************
*函数名:GUI_Dot
*输  入:x:点的X坐标
*      * y:点的Y坐标
*      * color:点的颜色
*输  出:
*功  能:给一块3*3像素涂上颜色。
****************************************************************************/

void GUI_Dot(uint x, uint y, uint color)
{
    u8 i;

    setXY(x , y , x, y);  //单个像素  XS YS XE YE
    for(i=0; i<9; i++)
    {
        LCD_Write_DATA(color>>8,color&0xFF);
    }
}


void GUI_Write32CnChar(unsigned char x, unsigned char y, unsigned char *cn, unsigned short wordColor, unsigned short backColor)
{
    u8 i, j, wordNum;
    uint color;
    while (*cn != '\0')
    {
        setXY(x, y, x+31, y+28);
        for (wordNum=0; wordNum<20; wordNum++)
        {   //wordNum扫描字库的字数
            if ((CnChar32x29[wordNum].Index[0]==*cn)
                 &&(CnChar32x29[wordNum].Index[1]==*(cn+1)))
            {
                for(i=0; i<116; i++)
                {   //MSK的位数
                    color=CnChar32x29[wordNum].Msk[i];
                    for(j=0;j<8;j++)
                    {
                        if((color&0x80)==0x80)
                        {

                            LCD_Write_DATA(wordColor>>8,wordColor&0xFF);
                        }
                        else
                        {
                            LCD_Write_DATA(backColor>>8,backColor&0xFF);
                        }
                        color<<=1;
                    }//for(j=0;j<8;j++)结束
                }
            }
        } //for (wordNum=0; wordNum<20; wordNum++)结束
        cn += 2;
        x += 32;
    }
}


void GUI_WriteASCII(unsigned char  x, unsigned char  y, unsigned char  *p, unsigned short  wordColor, unsigned short  backColor)
{
    u8 j, wordByte,wordNum;
    uint color;
    while(*p != '\0')
    {
        wordNum = *p - 32;
        setXY(x,y,x+15, y+23);
        for (wordByte=0; wordByte<48; wordByte++)
        {
            color = ASCII16x24[wordNum][wordByte];
            for (j=0; j<8; j++)
            {
                if ((color&0x80) == 0x80)
                {
                    LCD_Write_DATA(wordColor>>8,wordColor&0xFF);
                }
                else
                {
                    LCD_Write_DATA(backColor>>8,backColor&0xFF);
                }
                color <<= 1;
            }
        }
        p++;
        x +=16;
    }
}

ILI9225.h

#ifndef USER_ILI9225_H_
#define USER_ILI9225_H_

#define disp_x_size 176
#define disp_y_size 220



//*********************************
// COLORS
//*********************************
// VGA color palette
#define VGA_BLACK       0x0000
#define VGA_WHITE       0xFFFF
#define VGA_RED         0xF800
#define VGA_GREEN       0x0400
#define VGA_BLUE        0x001F
#define VGA_SILVER      0xC618
#define VGA_GRAY        0x8410
#define VGA_MAROON      0x8000
#define VGA_YELLOW      0xFFE0
#define VGA_OLIVE       0x8400
#define VGA_LIME        0x07E0
#define VGA_AQUA        0x07FF
#define VGA_TEAL        0x0410
#define VGA_NAVY        0x0010
#define VGA_FUCHSIA     0xF81F
#define VGA_PURPLE      0x8010
#define VGA_TRANSPARENT 0xFFFFFFFF


/*
 *  GND-1;VDD(3V3)-2;  IM0-25 接GND 16bit; LED-:31~33;LED+:34
 */
#define LCD_CS GPIO_Pin_1       //4
#define LCD_RS GPIO_Pin_2       //5
#define LCD_WR GPIO_Pin_3       //6
#define LCD_RST GPIO_Pin_4      //24
#define LCD_RD GPIO_Pin_5       //7

#define LCD_PORT GPIOD          //8-23


#define LCD_CS_SET GPIO_WriteBit(GPIOA, LCD_CS,Bit_SET)
#define LCD_CS_CLR GPIO_WriteBit(GPIOA, LCD_CS,Bit_RESET)

#define LCD_RS_SET GPIO_WriteBit(GPIOA, LCD_RS,Bit_SET)
#define LCD_RS_CLR GPIO_WriteBit(GPIOA, LCD_RS,Bit_RESET)

#define LCD_WR_SET GPIO_WriteBit(GPIOA, LCD_WR,Bit_SET)
#define LCD_WR_CLR GPIO_WriteBit(GPIOA, LCD_WR,Bit_RESET)

#define LCD_RST_SET GPIO_WriteBit(GPIOA, LCD_RST,Bit_SET)
#define LCD_RST_CLR GPIO_WriteBit(GPIOA, LCD_RST,Bit_RESET)

#define LCD_RD_SET GPIO_WriteBit(GPIOA, LCD_RD,Bit_SET)
#define LCD_RD_CLR GPIO_WriteBit(GPIOA, LCD_RD,Bit_RESET)

void LCD_Writ_Bus16(unsigned char VH,unsigned char VL);
void LCD_Write_COM(unsigned char VL);
void LCD_Write_DATA(unsigned char VH,unsigned char VL);
void LCD_Write_DATA16(unsigned short val);
void LCD_Write_COM_DATA(unsigned char com1,unsigned short dat1);
void InitLCD();
void LCD_Reset(void);
void clrScr(unsigned short color);
void clrXY();
void setPixel(unsigned char color);
void drawPixel(unsigned char  x, unsigned char  y);
void setColor(unsigned short color);
void drawHLine(unsigned char x, unsigned char y, unsigned char l);
void drawVLine(unsigned char x, unsigned char y, unsigned char l);
void drawLine(unsigned char  x1, unsigned char  y1, unsigned char  x2, unsigned char  y2);

void fillRoundRect(int x1, int y1, int x2, int y2);
void drawCircle(int x, int y, int radius);
void fillCircle(int x, int y, int radius);
void drawRect(int x1, int y1, int x2, int y2);
void drawRect1(unsigned char x, unsigned char y, unsigned char w, unsigned char h, unsigned short c);
void fillRect(int x1, int y1, int x2, int y2);
void drawRoundRect(int x1, int y1, int x2, int y2);
void show_color_bar();

void GUI_Write32CnChar(unsigned char x, unsigned char y, unsigned char *cn, unsigned short wordColor, unsigned short backColor);
void GUI_WriteASCII(unsigned char  x, unsigned char  y, unsigned char  *p, unsigned short  wordColor, unsigned short  backColor);

#endif /* USER_ILI9225_H_ */

main.c

#include "debug.h"
#include "ILI9225.h"


/* Global typedef */



/* Global define */
#define CHAR32_SHOW
/* Global Variable */

void LCD_GPIO_Init(void)
{
   GPIO_InitTypeDef GPIO_InitStructure = {0};

   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);

   GPIO_InitStructure.GPIO_Pin = LCD_CS|LCD_RS|LCD_WR|LCD_RST|LCD_RD;
   GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP;
   GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;
   GPIO_Init(GPIOA, &GPIO_InitStructure);

   //16位并口
   RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
   GPIO_InitStructure.GPIO_Pin = GPIO_Pin_All;
   GPIO_Init(LCD_PORT, &GPIO_InitStructure);


}


/*********************************************************************
 * @fn      main
 *
 * @brief   Main program.
 *
 * @return  none
 */
int main(void)
{
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);
	SystemCoreClockUpdate();
	Delay_Init();
	USART_Printf_Init(115200);	
	printf("SystemClk:%d\r\n",SystemCoreClock);
	printf( "ChipID:%08x\r\n", DBGMCU_GetCHIPID() );
	printf("This is printf example\r\n");

	LCD_GPIO_Init();

	InitLCD();

	while(1)
    {
	    show_color_bar();
	    GUI_Write32CnChar(10,10,"科技日期",VGA_WHITE,VGA_BLUE);
	    GUI_WriteASCII(10,60,"abcdefghijkl",VGA_WHITE,VGA_RED);
	    GUI_WriteASCII(10,90,"ABCDEFGHIJKL",VGA_WHITE,VGA_RED);
	    GUI_WriteASCII(10,120,"!@#$%^&*()~;:",VGA_WHITE,VGA_RED);
	    Delay_Ms(3000);
	    setColor(VGA_WHITE);
	    clrScr(VGA_RED);
	    fillRoundRect(50,50,100,100);
	    Delay_Ms(1000);
	    clrScr(VGA_GREEN);
	    fillRoundRect(50,50,100,100);
	    Delay_Ms(1000);
	    clrScr(VGA_BLUE);
	    fillRoundRect(50,50,100,100);
	    Delay_Ms(1000);
	    clrScr(VGA_YELLOW);
	    fillRoundRect(50,50,100,100);
	    setColor(VGA_RED);
	    drawCircle(100,100,50);
	    Delay_Ms(1000);

	}
}

二、总结

1、不会看屏幕引脚定义.开始找了一款LGDP4525和这个定义是一样的,试了一天不行。又查了一下,相同款还有ILI9225驱动的,引脚定义完全相同。试了一下可以。

ILI9225演示用

你可能感兴趣的:(单片机,嵌入式硬件,c语言)