2.8寸彩屏测试程序

发布时间:2020/8/20 15:46:10

#include "lcd.h"
#include "stdlib.h"
#include "font.h" 
#include "usart.h"
#include "delay.h"
#include "picture.h" 


 
//画笔颜色,背景颜色
u16 POINT_COLOR = 0x0000,BACK_COLOR = 0xFFFF;  
u16 DeviceCode;  


//写寄存器函数
void LCD_WR_REG(u8 dat)
{ //delay_ms(2);
LCD_RS=0;//写地址  
  LCD_CS=0; 
DATAOUT(dat); 
LCD_WR=0;
LCD_WR=1; 
  LCD_CS=1;   
}


//写寄存器
void LCD_WriteReg(u8 LCD_Reg, u16 LCD_RegValue)
{
LCD_WR_REG(LCD_Reg);  
LCD_WR_DATA(LCD_RegValue);      

  
//读寄存器
u16 LCD_ReadReg(u8 LCD_Reg)
{   
u16 t;
LCD_WR_REG(LCD_Reg);  //写入要读的寄存器号  
GPIOD->CRL=0X88888888; //PB0-7  上拉输入
GPIOD->CRH=0X88888888; //PB8-15 上拉输入
GPIOD->ODR=0XFFFF;    //全部输出高


LCD_RS=1;
LCD_CS=0;
//读取数据(读寄存器时,并不需要读2次)
LCD_RD=0;   
LCD_RD=1;
t=DATAIN;  
LCD_CS=1; 


GPIOD->CRL=0X33333333; //PB0-7  上拉输出
GPIOD->CRH=0X33333333; //PB8-15 上拉输出
GPIOD->ODR=0XFFFF;    //全部输出高
return t;  
}
   
//开始写GRAM
void LCD_WriteRAM_Prepare(void)
{
LCD_WR_REG(0X2C);
}
 
//LCD写GRAM
void LCD_WriteRAM(u16 RGB_Code)
{    
LCD_WR_DATA(RGB_Code);//写十六位GRAM
}


//从ILI93xx读出的数据为GBR格式,而我们写入的时候为RGB格式。
//通过该函数转换
//c:GBR格式的颜色值
//返回值:RGB格式的颜色值
u16 LCD_BGR2RGB(u16 c)
{
u16  r,g,b,rgb;   
b=(c>>0)&0x1f;
g=(c>>5)&0x3f;
r=(c>>11)&0x1f;  
rgb=(b<<11)+(g<<5)+(r<<0);  
return(rgb);
}
 
//读取个某点的颜色值  
//x:0~239
//y:0~319
//返回值:此点的颜色
u16 LCD_ReadPoint(u16 x,u16 y)
{
u16 t;
if(x>=LCD_W||y>=LCD_H)return 0;//超过了范围,直接返回   
LCD_SetCursor(x,y);
LCD_WR_REG(0X2C);       //选择GRAM地址 
GPIOD->CRL=0X88888888; //PB0-7  上拉输入
GPIOD->CRH=0X88888888; //PB8-15 上拉输入
GPIOD->ODR=0XFFFF;     //全部输出高


LCD_RS=1;
LCD_CS=0;
//读取数据(读GRAM时,需要读2次)
LCD_RD=0;   
  LCD_RD=1;
  //dummy READ
LCD_RD=0;   
  LCD_RD=1;
t=DATAIN;  
LCD_CS=1;  


GPIOD->CRL=0X33333333; //PB0-7  上拉输出
GPIOD->CRH=0X33333333; //PB8-15 上拉输出
GPIOD->ODR=0XFFFF;    //全部输出高  
return t;//这几种IC直接返回颜色值
//else return LCD_BGR2RGB(t);
}


//LCD开启显示
void LCD_DisplayOn(void)
{   
LCD_WR_REG(0X29); //26万色显示开启
}
 
//LCD关闭显示
void LCD_DisplayOff(void)
{   
LCD_WR_REG(0X28);//关闭显示 

  
//设置光标位置
//Xpos:横坐标
//Ypos:纵坐标
#include <stm32f10x_lib.h>
 void LCD_SetCursor(u16 x, u16 y)
{


if(y<=255)
  {
    LCD_WR_REG(0x2a);//column address set
LCD_WR_DATA(0x00); //column address start
LCD_WR_DATA(0x00+x);
LCD_WR_DATA(0x00);
LCD_WR_DATA(0xef); //column address end


LCD_WR_REG(0x2b);//Row address set
LCD_WR_DATA(0x00); //Row address start
LCD_WR_DATA(0x00+y);
LCD_WR_DATA(0x01); //Row address end
LCD_WR_DATA(0x3f);
}
else
    {
LCD_WR_REG(0x2a);//column address set
LCD_WR_DATA(0x00); //column address start
LCD_WR_DATA(0x00+x);
LCD_WR_DATA(0x00);
LCD_WR_DATA(0xef); //column address end


LCD_WR_REG(0x2b);//Row address set
LCD_WR_DATA(0x01); //Row address start
LCD_WR_DATA(0x00+y-256);
LCD_WR_DATA(0x01); //Row address end
LCD_WR_DATA(0x3f);
}
 } 


//画点
//x:0~239
//y:0~319
//POINT_COLOR:此点的颜色
void LCD_DrawPoint(u16 x,u16 y)
{
LCD_SetCursor(x,y);//设置光标位置 
LCD_WR_REG(0X2C);//开始写入GRAM
LCD_WR_DATA(POINT_COLOR); 

 
//初始化lcd


void LCD_Init(void)

  RCC->APB2ENR|=1<<5;//先使能外设PORTD时钟
  RCC->APB2ENR|=1<<4;//先使能外设PORTC时钟


RCC->APB2ENR|=1<<0;    //开启辅助时钟
JTAG_Set(SWD_ENABLE);  //开启SWD
 
    GPIOC->CRL&=0XFFF00000;  
GPIOC->CRL|=0X00033333;  
GPIOC->ODR|=0X001F;
     
    GPIOD->CRH&=0X00000000;
GPIOD->CRH|=0X33333333;  
GPIOD->ODR|=0XFF00;
GPIOD->CRL&=0X00000000;  
GPIOD->CRL|=0X33333333;  
GPIOD->ODR|=0X00FF;
 
RESET=1;
delay_ms(1);
RESET=0;  //复位
delay_ms(10);
RESET=1; 
    delay_ms(120);

    LCD_WR_REG(0x28); //Display off

//LCD_WR_REG(0x01); //Software reset

LCD_WR_REG(0x11); //sleep out
delay_ms(120);
    
   //LCD_WR_REG(0x13); //partial off

   //LCD_WR_REG(0x20); //display inversion off

LCD_WR_REG(0x36); //Memory data access control
LCD_WR_DATA(0x00);

LCD_WR_REG(0x3a); //Interface pixel format
LCD_WR_DATA(0x55);//65K RGB interface,16bit/pixel  


LCD_WR_REG(0xb2); //Porch contol
LCD_WR_DATA(0x0c); 
LCD_WR_DATA(0x0c); 
LCD_WR_DATA(0x00); 
LCD_WR_DATA(0x33);
LCD_WR_DATA(0x33);


    LCD_WR_REG(0xb7); //Gate control
LCD_WR_DATA(0x35);//VGH=13.26V;VGL=-10.43V;

LCD_WR_REG(0xbb); //VCOM setting
LCD_WR_DATA(0x19);//VCOM=0.725V;


    LCD_WR_REG(0xc0); //LCM control
LCD_WR_DATA(0x2c);


LCD_WR_REG(0xc2); //VDV and VRH command enable
LCD_WR_DATA(0x01); 
    //LCD_WR_DATA(0xff);

LCD_WR_REG(0xc3); //VRH set 调节亮度
LCD_WR_DATA(0x12); 
 
LCD_WR_REG(0xc4); //VDV setting
LCD_WR_DATA(0x20); 

LCD_WR_REG(0xc6); //FR control 2
LCD_WR_DATA(0x0f); 

LCD_WR_REG(0xca); //
LCD_WR_DATA(0x0f); 

LCD_WR_REG(0xc8); //
LCD_WR_DATA(0x08);


LCD_WR_REG(0x55); //
LCD_WR_DATA(0x90);


LCD_WR_REG(0xd0); //Power control 1
LCD_WR_DATA(0xa4);
LCD_WR_DATA(0xa1); 

LCD_WR_REG(0xe0); //Positive voltage gamma control
LCD_WR_DATA(0xd0);
LCD_WR_DATA(0x04);
    LCD_WR_DATA(0x0d);
LCD_WR_DATA(0x11);
LCD_WR_DATA(0x13);
    LCD_WR_DATA(0x2b);
LCD_WR_DATA(0x3f);
    LCD_WR_DATA(0x54);
LCD_WR_DATA(0x4c);
LCD_WR_DATA(0x18);
    LCD_WR_DATA(0x0d);
LCD_WR_DATA(0x0b);
    LCD_WR_DATA(0x1f);
LCD_WR_DATA(0x23);


LCD_WR_REG(0xe1); //Negative voltage gamma control
    LCD_WR_DATA(0xd0);
LCD_WR_DATA(0x04);
    LCD_WR_DATA(0x0c);
LCD_WR_DATA(0x11);
LCD_WR_DATA(0x13);
    LCD_WR_DATA(0x2c);
LCD_WR_DATA(0x3f);
    LCD_WR_DATA(0x44);
LCD_WR_DATA(0x51);
LCD_WR_DATA(0x2f);
    LCD_WR_DATA(0x1f);
LCD_WR_DATA(0x1f);
    LCD_WR_DATA(0x20);
LCD_WR_DATA(0x23);


LCD_WR_REG(0x2a);//column address set
LCD_WR_DATA(0x00); //column address start
LCD_WR_DATA(0x00);
LCD_WR_DATA(0x00);
LCD_WR_DATA(0x8f); //column address end


LCD_WR_REG(0x2b);//Row address set
LCD_WR_DATA(0x00); //Row address start
LCD_WR_DATA(0x00);
LCD_WR_DATA(0x01); //Row address end
LCD_WR_DATA(0x2f);

LCD_WR_REG(0x29); //Display on
 
LCD_WR_REG(0x2c);  
}
   
//清屏函数
//Color:要清屏的填充色
void LCD_Clear(u16 Color)
{
u32 index=0;      
LCD_SetCursor(0,0);//设置光标位置 
LCD_WriteRAM_Prepare();     //开始写入GRAM  
for(index=0;index<76800;index++)
{
LCD_WR_DATA(Color);    
}
}


//清屏函数
#define RED         0xF800
#define YELLOW         0xFFE0
#define GREEN         0x07E0
#define GBLUE 0X07FF //青
#define BLUE         0x001F
#define BRED             0XF81F//紫
#define BLACK         0x0000
#define WHITE         0xFFFF
#define GRAY   0X7BEF //灰
#define BROWN 0XBC40 //棕色


void LCD_fill_REG()
{
u32 index=0;      
LCD_SetCursor(0,0);//设置光标位置 
LCD_WriteRAM_Prepare();     //开始写入GRAM  
for(index=0;index<7680;index++)
{
LCD_WR_DATA(RED);    
}
for(index=0;index<7680;index++)
{
LCD_WR_DATA(YELLOW);    
}
for(index=0;index<7680;index++)
{
LCD_WR_DATA(GREEN);    
}
for(index=0;index<7680;index++)
{
LCD_WR_DATA(GBLUE);    
}
for(index=0;index<7680;index++)
{
LCD_WR_DATA(BLUE);    
}
for(index=0;index<7680;index++)
{
LCD_WR_DATA(BRED);    
}
for(index=0;index<7680;index++)
{
LCD_WR_DATA(BLACK);    
}
for(index=0;index<7680;index++)
{
LCD_WR_DATA(WHITE);    
}
for(index=0;index<7680;index++)
{
LCD_WR_DATA(GRAY);    
}
for(index=0;index<7680;index++)
{
LCD_WR_DATA(BROWN);    
}
  }


//在指定区域内填充指定颜色
//区域大小:
//  (xend-xsta)*(yend-ysta)
void LCD_Fill(u16 xsta,u16 ysta,u16 xend,u16 yend,u16 color)
{          
u16 i,j;
u16 xlen=0;


#if USE_HORIZONTAL==1
xlen=yend-ysta+1;   
for(i=xsta;i<=xend;i++)
{
LCD_SetCursor(i,ysta);      //设置光标位置 
LCD_WriteRAM_Prepare();     //开始写入GRAM  
for(j=0;j<xlen;j++)LCD_WR_DATA(color);//设置光标位置  
}
#else
xlen=xend-xsta+1;   
for(i=ysta;i<=yend;i++)
{
LCD_SetCursor(xsta,i);      //设置光标位置 
LCD_WriteRAM_Prepare();     //开始写入GRAM  
for(j=0;j<xlen;j++)LCD_WR_DATA(color);//设置光标位置    
}
#endif      

 
//画线
//x1,y1:起点坐标
//x2,y2:终点坐标  
void LCD_DrawLine(u16 x1, u16 y1, u16 x2, u16 y2)
{
u16 t; 
int xerr=0,yerr=0,delta_x,delta_y,distance; 
int incx,incy,uRow,uCol; 


delta_x=x2-x1; //计算坐标增量 
delta_y=y2-y1; 
uRow=x1; 
uCol=y1; 
if(delta_x>0)incx=1; //设置单步方向 
else if(delta_x==0)incx=0;//垂直线 
else {incx=-1;delta_x=-delta_x;} 
if(delta_y>0)incy=1; 
else if(delta_y==0)incy=0;//水平线 
else{incy=-1;delta_y=-delta_y;} 
if( delta_x>delta_y)distance=delta_x; //选取基本增量坐标轴 
else distance=delta_y; 
for(t=0;t<=distance+1;t++ )//画线输出 
{  
LCD_DrawPoint(uRow,uCol);//画点 
xerr+=delta_x ; 
yerr+=delta_y ; 
if(xerr>distance) 

xerr-=distance; 
uRow+=incx; 

if(yerr>distance) 

yerr-=distance; 
uCol+=incy; 

}  

   
//画矩形
void LCD_DrawRectangle(u16 x1, u16 y1, u16 x2, u16 y2)
{
LCD_DrawLine(x1,y1,x2,y1);
LCD_DrawLine(x1,y1,x1,y2);
LCD_DrawLine(x1,y2,x2,y2);
LCD_DrawLine(x2,y1,x2,y2);
}


//在指定位置画一个指定大小的圆
//(x,y):中心点
//r    :半径
void Draw_Circle(u16 x0,u16 y0,u8 r)
{
int a,b;
int di;
a=0;b=r;  
di=3-(r<<1);             //判断下个点位置的标志
while(a<=b)
{
LCD_DrawPoint(x0-b,y0-a);             //3           
LCD_DrawPoint(x0+b,y0-a);             //0           
LCD_DrawPoint(x0-a,y0+b);             //1       
LCD_DrawPoint(x0-b,y0-a);             //7           
LCD_DrawPoint(x0-a,y0-b);             //2             
LCD_DrawPoint(x0+b,y0+a);             //4               
LCD_DrawPoint(x0+a,y0-b);             //5
LCD_DrawPoint(x0+a,y0+b);             //6 
LCD_DrawPoint(x0-b,y0+a);             
a++;
//使用Bresenham算法画圆     
if(di<0)di +=4*a+6;  
else
{
di+=10+4*(a-b);   
b--;

LCD_DrawPoint(x0+a,y0+b);
}



//在指定位置显示一个字符
//x:0~234
//y:0~308
//num:要显示的字符:" "--->"~"
//size:字体大小 12/16
//mode:叠加方式(1)还是非叠加方式(0)
//在指定位置显示一个字符
//x:0~234
//y:0~308
//num:要显示的字符:" "--->"~"
//size:字体大小 12/16
//mode:叠加方式(1)还是非叠加方式(0)
void LCD_ShowChar(u16 x,u16 y,u8 num,u8 size,u8 mode)
{  
#if USE_HORIZONTAL==1
#define MAX_CHAR_POSX 312
#define MAX_CHAR_POSY 232 
#else     
#define MAX_CHAR_POSX 232
#define MAX_CHAR_POSY 312
#endif 
    u8 temp;
    u8 pos,t;
u16 x0=x;
u16 colortemp=POINT_COLOR;      
    if(x>MAX_CHAR_POSX||y>MAX_CHAR_POSY)return;    
//设置窗口   
num=num-' ';//得到偏移后的值
if(!mode) //非叠加方式
{
for(pos=0;pos<size;pos++)
{
if(size==12)temp=asc2_1206[num][pos];//调用1206字体
else temp=asc2_1608[num][pos]; //调用1608字体
for(t=0;t<size/2;t++)
   {                 
       if(temp&0x01)POINT_COLOR=colortemp;
else POINT_COLOR=BACK_COLOR;
LCD_DrawPoint(x,y);
temp>>=1; 
x++;
   }
x=x0;
y++;
}
}else//叠加方式
{
for(pos=0;pos<size;pos++)
{
if(size==12)temp=asc2_1206[num][pos];//调用1206字体
else temp=asc2_1608[num][pos]; //调用1608字体
for(t=0;t<size/2;t++)
   {                 
       if(temp&0x01)LCD_DrawPoint(x+t,y+pos);//画一个点     
       temp>>=1; 
   }
}
}
POINT_COLOR=colortemp;        
}   
//m^n函数
u32 mypow(u8 m,u8 n)
{
u32 result=1;  
while(n--)result*=m;    
return result;
}
 
//显示2个数字
//x,y :起点坐标  
//len :数字的位数
//size:字体大小
//color:颜色
//num:数值(0~4294967295);  
void LCD_ShowNum(u16 x,u16 y,u32 num,u8 len,u8 size)
{        
u8 t,temp;
u8 enshow=0;   
for(t=0;t<len;t++)
{
temp=(num/mypow(10,len-t-1))%10;
if(enshow==0&&t<(len-1))
{
if(temp==0)
{
LCD_ShowChar(x+(size/2)*t,y,' ',size,0);
continue;
}else enshow=1; 
 
}
LCD_ShowChar(x+(size/2)*t,y,temp+'0',size,0); 
}



//显示2个数字
//x,y:起点坐标
//size:字体大小
//mode:模式 0,填充模式;1,叠加模式
//num:数值(0~99);  
void LCD_Show2Num(u16 x,u16 y,u16 num,u8 len,u8 size,u8 mode)
{        
u8 t,temp;   
for(t=0;t<len;t++)
{
temp=(num/mypow(10,len-t-1))%10;
LCD_ShowChar(x+(size/2)*t,y,temp+'0',size,mode); 
}

//显示字符串
//x,y:起点坐标  
//*p:字符串起始地址
//用16字体
void LCD_ShowString(u16 x,u16 y,const u8 *p)
{         
    while(*p!='\0')
    {       
        if(x>MAX_CHAR_POSX){x=0;y+=16;}
        if(y>MAX_CHAR_POSY){y=x=0;LCD_Clear(WHITE);}
        LCD_ShowChar(x,y,*p,16,0);
        x+=8;
        p++;
    }  
}
/************************************************
    显示240*320图片
************************************************/
void dis_picture() 
{
 u8 dat1,dat2;
 u16 i,j,dat;
 u32 k=0;
 LCD_SetCursor(0,0);//设置光标位置 
 LCD_WriteRAM_Prepare();     //开始写入GRAM
 for(i=0;i<320;i++)
  for(j=0;j<240;j++)
  {
   dat1=pictudre[k];
   dat2=pictudre[k+1];
   k=k+2;
   dat=((dat1&0x00ff)<<8)|(dat2&0x00ff);
   LCD_WR_DATA(dat);
   }
}  


上一篇:3.5寸RA8875控制器SPI串口例程
下一篇:HGS128321并口8080例程