⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 3510i.c

📁 这是模仿zlg_GUI写的一个嵌入式GUI,采用3510手机屏.
💻 C
字号:
#include<iolpc2148.h>
#include"delay.h"
#include"mytype.h"
#include"3510i.h"
#include"english_6x8_pixel.h"
#include"math.h"

uint8 LCD_BGColor;  //设置背景色
uint8 LCD_PenColor; //设置画笔色

void LcdPortInit(void)  //LCD端口初始化
{
 IO1DIR_bit.P1_16=1;
 IO1DIR_bit.P1_18=1;
 IO1DIR_bit.P1_20=1;
 IO1DIR_bit.P1_22=1;
 SetLcdRst();
 SetLcdCs();
 SetLcdSclk();
}

void LCD_Reset(void)    //LCD重启
{
 ClrLcdRst();
 delay_ms(5);
 SetLcdRst();
 delay_ms(5);
}

void LCD_SendCommand(uint8  cmd)  //LCD发送命令
{
 uint8 i;
 SetSdataOut();
 ClrLcdCs();
 ClrLcdSclk();
 ClrLcdSdata();
 SetLcdSclk();
 
 for(i=0;i<8;i++)
  {
   ClrLcdSclk();
   if(cmd & 0x80)
    {
     SetLcdSdata();
    }
   else
    { 
     ClrLcdSdata();
    }
   SetLcdSclk();
   cmd<<=1;
  }
}

void LCD_SendData(uint8  Data)   //LCD发送数据
{
 uint32 i;
 SetSdataOut();
 ClrLcdCs();
 ClrLcdSclk();
 SetLcdSdata();
 SetLcdSclk();

 for(i=0;i<8;i++)
  {
   ClrLcdSclk();
   if(Data & 0x80)
    {
     SetLcdSdata();
    }
   else
    {
     ClrLcdSdata();
    }
   SetLcdSclk();
   Data<<=1;
  }
}

void LCD_ReadDummy(void)            //读Dummy
{
 SetSdataIn();
 ClrLcdCs();
 ClrLcdSclk();
 SetLcdSclk();
}

uint8  LCD_ReadData(void)          //读数据
{
 uint16 r = 0;
 uint8 i;

 SetSdataIn();

 ClrLcdCs();

 for(i=0;i<12;i++)
  {
   ClrLcdSclk();
   SetLcdSclk();
   r<<=1;
   if(LCD_SDATA_IN)
    {
     r++;
    }
  }
 return r;
}

void LCD_Initialize(void)               //LCD初始化
{

        uint8 i;

        LcdPortInit();

	LCD_Reset();

	LCD_SendCommand(0x01);		//software reset
	LCD_DataOver();
	delay_ms(5);

	LCD_SendCommand(0xc6);		//initial escape
	LCD_DataOver();

	LCD_SendCommand(0xb9);		//refresh set
	LCD_SendData(0x00);
	LCD_DataOver();

	LCD_SendCommand(0xb6);		//display control
	LCD_SendData(0x80);
	LCD_SendData(0x80);
	LCD_SendData(0x81);
	LCD_SendData(84);
	LCD_SendData(69);
	LCD_SendData(82);
	LCD_SendData(67);
	LCD_DataOver();

	LCD_SendCommand(0xb3);		//gray scale position set
	LCD_SendData(1);
	LCD_SendData(2);
	LCD_SendData(4);
	LCD_SendData(8);
	LCD_SendData(16);
	LCD_SendData(30);
	LCD_SendData(40);
	LCD_SendData(50);
	LCD_SendData(60);
	LCD_SendData(70);
	LCD_SendData(80);
	LCD_SendData(90);
	LCD_SendData(100);
	LCD_SendData(110);
	LCD_SendData(127);
	LCD_DataOver();

	LCD_SendCommand(0xb5);		//gamma curve set
	LCD_SendData(0x01);
	LCD_DataOver();
	
	LCD_SendCommand(0x36);		//memery access control
	LCD_SendData(0x00);
	LCD_DataOver();

	LCD_SendCommand(0xbd);		//common driver output select
	LCD_SendData(0x04);
	LCD_DataOver();

	LCD_SendCommand(0xbe);		//power control
	LCD_SendData(0x04);
	LCD_DataOver();

	LCD_SendCommand(0x11);		//sleep out
	LCD_DataOver();
	
	LCD_SendCommand(0xba);		//voltage control
	LCD_SendData(127);
	LCD_SendData(3);
	LCD_DataOver();

	LCD_SendCommand(0xb7);		//temperature gradient set
	for(i = 0; i < 14; i ++)
		LCD_SendData(0x00);
	LCD_DataOver();

	LCD_SendCommand(0x29);		//display ON
	LCD_DataOver();

	LCD_SendCommand(0x03);		//booster voltage ON
	LCD_DataOver();
	delay_ms(5);

	LCD_SendCommand(0x20);		//display inversion OFF
	LCD_DataOver();
        
        
        LCD_Interface(0);              //文本模式下256色,8位接口

	LCD_SendCommand(0x25);		//write contrast
	LCD_SendData(71);
	LCD_DataOver();
	
	LCD_BGColor = 0x00;
	LCD_PenColor = 0xff;
}

void LCD_Interface(uint8 Color)          //12位接口或8位接口设置
{
if (Color) {
	LCD_SendCommand(0x3a);		//12位interface pixel format
	LCD_SendData(0x03);             //0x03 为4096色,0x02为256色
	LCD_DataOver(); 
	}
else{
    LCD_SendCommand(0x3a);		//8位interface pixel format
	LCD_SendData(0x02);
	LCD_DataOver();
	
        
        
	LCD_SendCommand(0x2d);		//colour set
//red
	LCD_SendData(0x00);
	LCD_SendData(0x02);
	LCD_SendData(0x03);
	LCD_SendData(0x04);
	LCD_SendData(0x05);
	LCD_SendData(0x06);
	LCD_SendData(0x08);
//green
	LCD_SendData(0x0f);
	LCD_SendData(0x00);
	LCD_SendData(0x02);
	LCD_SendData(0x03);
	LCD_SendData(0x04);
	LCD_SendData(0x05);
	LCD_SendData(0x06);
	LCD_SendData(0x08);
	LCD_SendData(0x0f);
//blue
	LCD_SendData(0x00);
	LCD_SendData(0x03);
	LCD_SendData(0x06);
	LCD_SendData(0x0f);
	LCD_DataOver();
     }
}

//以背景色清屏
void LCD_ClearScreen(void)
{
	uint8  x, y;
	LCD_SendCommand(0x2a);		//column address set
	LCD_SendData(0);
	LCD_SendData(97);
	LCD_DataOver();

	LCD_SendCommand(0x2b);		//page address set
	LCD_SendData(0);
	LCD_SendData(66);
	LCD_DataOver();
	
	LCD_SendCommand(0x2c);		//memory write
	for(y = 0; y < 67; y ++)
		for(x = 0; x < 98; x ++)
			LCD_SendData(LCD_BGColor);
	LCD_DataOver();
}


//LCD读块数据
void LCD_ReadPixel(uint8  x1, uint8  y1, uint8  x2, uint8  y2, uint8  *b)
{
	uint8  x, y;
	LCD_SendCommand(0x2a);		//column address set
	LCD_SendData(x1);
	LCD_SendData(x2);
	LCD_DataOver();

	LCD_SendCommand(0x2b);		//page address set
	LCD_SendData(y1);
	LCD_SendData(y2);
	LCD_DataOver();
	
	LCD_SendCommand(0x2e);		//RAM data read
	
	for(y = y1; y <= y2; y ++)
		for(x = x1; x <= x2; x ++)
		{
			LCD_ReadDummy();
			*(b ++) = LCD_ReadData();
		}
	LCD_DataOver();

	LCD_SendCommand(0x00);		//NOP
	LCD_SendData(0x00);
	LCD_DataOver();
}


//写ASCII码,取模方式为列行式,大小6x8;
void LCD_WriteEnglishChar(uint8  c, uint8  x, uint8  y)
{
	uint8  row,line;
	uint8  temp[6][8];
	c-=32;
	for(row=0;row<6;row++)
	{
		for(line=0;line<8;line++)
		{
			if(get_bit(font6x8[c][row],line))
				temp[row][line]=LCD_PenColor;
			else
				temp[row][line]=LCD_BGColor;
		}
	}
	LCD_SendCommand(0x2a);		//column address set
	LCD_SendData(x);
	LCD_SendData(x+5);
	LCD_DataOver();

	LCD_SendCommand(0x2b);		//page address set
	LCD_SendData(y);
	LCD_SendData(y+7);
	LCD_DataOver();
	
	LCD_SendCommand(0x2c);
	for(row=0;row<8;row++)
		for(line=0;line<6;line++)
			LCD_SendData(temp[line][row]);
	LCD_DataOver();
}

//写ASCII码字符串;
void LCD_WriteEnglishString(uint8 *string, uint8  x, uint8  y)
{
	while(*string)
	{
		LCD_WriteEnglishChar(*string, x, y);
		string++;
		x+=6;
		if(x>=92)
		{
			x=0;
			y+=8;
			if((*string)==32)//32 is space char
				string++;
			if(y>=67)
			 	y=0;
		}
	}
}

//16*16的字
/*void LCD_WriteChineseChar(uint8  (*c)[32], uint8  x, uint8  y)
{
	uint8  row,line;
	uint8  temp[16][16];
	for(row=0;row<16;row++)
	{
		for(line=0;line<8;line++)
		{
			if(get_bit((*c)[row],line))
				temp[row][line]=LCD_PenColor;
			else
				temp[row][line]=LCD_BGColor;
		}
	}
	for(row=0;row<16;row++)
	{
		for(line=0;line<8;line++)
		{
			if(get_bit((*c)[row+16],line))
				temp[row][line+8]=LCD_PenColor;
			else
				temp[row][line+8]=LCD_BGColor;
		}
	}
	LCD_SendCommand(0x2a);		//column address set
	LCD_SendData(x);
	LCD_SendData(x+15);
	LCD_DataOver();

	LCD_SendCommand(0x2b);		//page address set
	LCD_SendData(y);
	LCD_SendData(y+15);
	LCD_DataOver();
	
	LCD_SendCommand(0x2c);
	for(row=0;row<16;row++)
		for(line=0;line<16;line++)
			LCD_SendData(temp[line][row]);
	LCD_DataOver();
}
void LCD_WriteChineseString(uint8  (*c)[32], uint8  num, uint8  x, uint8  y)
{
	uint8  i;
	for(i=0;i<num;i++)
	{
		LCD_WriteChineseChar(c, x, y);
		c++;
		x+=16;
		if(x>=96)
		{
			x=0;
			y+=16;
			if(y>=62)
			 	y=0;
		}
	}
}*/

//写汉字,取模方式为列行式,大小为12x12
void LCD_WriteChineseChar(const uint8  (*c)[24], uint8  x, uint8  y)
{
	uint8  row,line;
	uint8  temp[12][16];
	for(row=0;row<12;row++)
	{
		for(line=0;line<8;line++)
		{
			if(get_bit((*c)[row],line))
				temp[row][line]=LCD_PenColor;
			else
				temp[row][line]=LCD_BGColor;
		}
	}
	for(row=0;row<12;row++)
	{
		for(line=0;line<8;line++)
		{
			if(get_bit((*c)[row+12],line))
				temp[row][line+8]=LCD_PenColor;
			else
				temp[row][line+8]=LCD_BGColor;
		}
	}
	LCD_SendCommand(0x2a);		//column address set
	LCD_SendData(x);
	LCD_SendData(x+11);
	LCD_DataOver();

	LCD_SendCommand(0x2b);		//page address set
	LCD_SendData(y);
	LCD_SendData(y+15);
	LCD_DataOver();
	
	LCD_SendCommand(0x2c);
	for(row=0;row<16;row++)
		for(line=0;line<12;line++)
			LCD_SendData(temp[line][row]);
	LCD_DataOver();
}

//写汉字串,取模方式为列行式,大小为12x12
void LCD_WriteChineseString(const uint8  (*c)[24], uint8  num, uint8  x, uint8  y)
{
	uint8  i;
	for(i=0;i<num;i++)
	{
		LCD_WriteChineseChar(c, x, y);
		c++;
		x+=12;
		if(x>=96)
		{
			x=0;
			y+=13;//行与行之间空一搁
			if(y>=62)
			 	y=0;
		}
	}
}

//以当前画笔色,填充区域
void LCD_EraseBlock(uint8  x1,uint8  y1, uint8  x2, uint8  y2)
{
	uint8  x, y;
	LCD_SendCommand(0x2a);		//column address set
	LCD_SendData(x1);
	LCD_SendData(x2);
	LCD_DataOver();

	LCD_SendCommand(0x2b);		//page address set
	LCD_SendData(y1);
	LCD_SendData(y2);
	LCD_DataOver();
	
	LCD_SendCommand(0x2c);		//memory write
	for(y = y1; y <= y2; y ++)
		for(x = x1; x <= x2; x ++)
			LCD_SendData(LCD_PenColor);
	LCD_DataOver();
}

//画点
void LCD_DrawPoint(uint8  x, uint8  y)
{
	LCD_SendCommand(0x2a);		//column address set
	LCD_SendData(x);
	LCD_SendData(x);
	LCD_DataOver();

	LCD_SendCommand(0x2b);		//page address set
	LCD_SendData(y);
	LCD_SendData(y);
	LCD_DataOver();
	
	LCD_SendCommand(0x2c);		//memory write
	LCD_SendData(LCD_PenColor);
	LCD_DataOver();
}
//画线
void LCD_DrawLine(uint8  x1, uint8  y1, uint8  x2, uint8  y2)
{
	uint8  x, y, t;
	if((x1 == x2) && (y1 == y2))
		LCD_DrawPoint(x1, y1);
	else if(fabs(y2 - y1) > fabs(x2 - x1))
	{
		if(y1 > y2)
		{
			t = y1;
			y1 = y2;
			y2 = t;
			t = x1;
			x1 = x2;
			x2 = t;
		}
		for(y = y1; y <= y2; y ++)
		{
			x = (y - y1) * (x2 - x1) / (y2 - y1) + x1;
			LCD_DrawPoint(x, y);
		}
	}
	else
	{
		if(x1 > x2)
		{
			t = y1;
			y1 = y2;
			y2 = t;
			t = x1;
			x1 = x2;
			x2 = t;
		}
		for(x = x1; x <= x2; x ++)
		{
			y = (x - x1) * (y2 - y1) / (x2 - x1) + y1;
			LCD_DrawPoint(x, y);
		}
	}
}
//画矩形
void LCD_DrawRectangle(uint8  x1, uint8  y1, uint8  x2, uint8  y2)
{
	LCD_DrawLine(x1, y1, x2, y1);
	LCD_DrawLine(x2, y1, x2, y2);
	LCD_DrawLine(x2, y2, x1, y2);
	LCD_DrawLine(x1, y2, x1, y1);
}

//填充图片,采用4096色,12位接口
void LcdBlockWrite(uint8 x1, uint8 y1, uint8 x2, uint8 y2, const uint8 *b)
{
 uint32 x, y;
 
 LCD_Interface(1);  //图形模式下4096色
 
 LCD_SendCommand(0x2a);		//column address set
 LCD_SendData(x1);
 LCD_SendData(x2);
 SetLcdCs();

 LCD_SendCommand(0x2b);		//page address set
 LCD_SendData(y1);
 LCD_SendData(y2);
 SetLcdCs();

 LCD_SendCommand(0x2c);		//memory write
 for(y=y1;y<=y2;y++)
  {
   for(x=x1;x<=x2;x+=2)
    {                                         
      /*这里使用了小俊的图形转换工具,                                         
      要按位取反*/
     LCD_SendData(*(b++)^0xFF);
     LCD_SendData(*(b++)^0xFF);
     LCD_SendData(*(b++)^0xFF);
    }
  }
 SetLcdCs();
 
 LCD_Interface(0);  //返回文本模式下256色
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -