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

📄 lcd_driver.c

📁 128x64点阵液晶屏驱动
💻 C
字号:
#include "LCD_Driver.h"
#include "time.h"

int x0 = 0, y0 = 0;
int myX = 0, prevY = 0;
code unsigned char coordinate[4] = {0x80, 0x90, 0x88, 0x98};
char isCharacter = 0;
unsigned char prevchar;

void lcd_send_byte(char dat)
{
/*	int i;

	for(i = 0; i < 8; i++)
	{
		if(dat & 0x80)
			SET_LCD_SID(1);
		else
			SET_LCD_SID(0);
		//Delay(1);
		SET_LCD_SCLK(1);
		//Delay(1);
		SET_LCD_SCLK(0);
		//Delay(1);
		dat <<= 1;
	}*/
}

void lcd_send_word(int dat)
{
/*	int i;

	for(i = 0; i < 16; i++)
	{
		if(dat & 0x8000)
			SET_LCD_SID(1);
		else
			SET_LCD_SID(0);
		//Delay(1);
		SET_LCD_SCLK(1);
		//Delay(1);
		SET_LCD_SCLK(0);
		//Delay(1);
		dat <<= 1;
	}*/
}

void lcd_write_instruction(char ins)
{
/*	char start_byte;
	int command;
	
	start_byte = SYNC_CHARACTER;
	lcd_send_byte(start_byte);

	command = ins & 0xf0;
	command <<= 8;
	command &= 0xff00;
	command |= (ins & 0x0f) << 4;

	lcd_send_word(command);*/
	SET_LCD_RS(0);
	SET_LCD_RW(0);
	SET_LCD_DATA(ins);
	SET_LCD_E(1);
	SET_LCD_E(0);
}

void lcd_write_data(char dat)
{
/*	char start_byte;
	int command;

	start_byte = SYNC_CHARACTER | START_BYTE_RS;
	lcd_send_byte(start_byte);

	command = dat & 0xf0;
	command <<= 8;
	command &= 0xff00;
	command |= (dat & 0x0f) << 4;

	lcd_send_word(command);*/
	SET_LCD_RS(1);
	SET_LCD_RW(0);
	SET_LCD_DATA(dat);
	SET_LCD_E(1);
	SET_LCD_E(0);
}

void lcd_init(int x, int y)
{
//	SET_LCD_SCLK(0);
//	SET_LCD_CS(1);
	SET_LCD_RW(1);
	SET_LCD_RS(1);
	SET_LCD_E(1);
	SET_LCD_RESET(0);
	Delay(20);
	SET_LCD_RESET(1);
	Delay(20);

	lcd_write_instruction(0x38);
	Delay(20);
	lcd_write_instruction(0x38);
	Delay(20);				  
	lcd_write_instruction(0x38);
	Delay(20);

	lcd_write_instruction(0x01);
	Delay(20);
	lcd_write_instruction(0x06);
	Delay(20);
	lcd_write_instruction(0x0c);
	Delay(20);
	
	x0 = x;
	y0 = y;
}

void lcd_char(int x, int y, unsigned short *buf, int height, int width)
{
	int row, col;

	x -= x0;
	y -= y0;
	
	col = x / 16;
	
	if(y > 31)
	{
		y -= 32;
		col += 8;
	}

	if(y + height < 31)
		for(row = 0; row < height; row++)
		{
			lcd_write_instruction(0x3e);
			lcd_write_instruction(row + y | 0x80);
			lcd_write_instruction(col | 0x80);
			lcd_write_instruction(0x3a);
			lcd_write_data(buf[row] >> 8);
			lcd_write_data(buf[row] & 0x00ff);
		}
	else
	{
		x = 0;
		for(row = 0; row < 32 - y; row++)
		{
			lcd_write_instruction(0x3e);
			lcd_write_instruction(row + y | 0x80);
			lcd_write_instruction(col | 0x80);
			lcd_write_instruction(0x3a);
			lcd_write_data(buf[x] >> 8);
			lcd_write_data(buf[x] & 0x00ff);
			x++;
		}

		for(row = 0; row < y - 32 + height; row++)
		{
			lcd_write_instruction(0x3e);
			lcd_write_instruction(row | 0x80);
			lcd_write_instruction(col + 8 | 0x80);
			lcd_write_instruction(0x3a);
			lcd_write_data(buf[x] >> 8);
			lcd_write_data(buf[x] & 0x00ff);
			x++;
		}
	}

}

void lcd_zh(int x, int y, unsigned char mat[16][2])
{
	int row, col;

	x -= x0;
	y -= y0;
	
	col = x / 16;

	if(y > 31)
	{
		y -= 32;
		col += 8;
	}

	if(y <= 16)
		for(row = 0; row < 16; row++)
		{
			lcd_write_instruction(0x3e);
			lcd_write_instruction(row + y | 0x80);
			lcd_write_instruction(col | 0x80);
			lcd_write_instruction(0x3a);
			lcd_write_data(mat[row][0]);
			lcd_write_data(mat[row][1]);
		}
	else
	{
		x = 0;
		for(row = 0; row < 32 - y; row++)
		{
			lcd_write_instruction(0x3e);
			lcd_write_instruction(row + y | 0x80);
			lcd_write_instruction(col | 0x80);
			lcd_write_instruction(0x3a);
			lcd_write_data(mat[x][0]);
			lcd_write_data(mat[x][1]);
			x++;
		}

		for(row = 0; row < y - 16; row++)
		{
			lcd_write_instruction(0x3e);
			lcd_write_instruction(row | 0x80);
			lcd_write_instruction(col + 8 | 0x80);
			lcd_write_instruction(0x3a);
			lcd_write_data(mat[x][0]);
			lcd_write_data(mat[x][1]);
			x++;
		}
	}
}

/*
	现在无需使用外部的hzk16字库以及sigma本身的字符字库。直接
	将汉字和字符的内码送过来即可。如果当前字节最高位为1,则表
	明当前字节和下一字节联合表示一个汉字,直接调用lcd_zh函数。
	如果当前字节小于0x7f,则表示当前字节是英文半角字符,那么
	将其填入参数ch[0],在调用lcd_char之前先判断下一个字节是否
	大于0x7f,如果是,则参数的ch[1]填入0x20,否则ch[1]填入下
	一个字节的值。
*/

/*void lcd_char(int x, int y, unsigned char ch[2])
{
	unsigned char coor;

	if (y != prevY)
	{
		isCharacter = 0;
		myX = 0;
		prevY = y;
	}
	
	//x -= x0;
	y -= y0;

	x = myX / 16;
	y /= 16;

	if(y > 3)
		return;

	coor = coordinate[y] + x;

	lcd_write_instruction(coor);
	if(isCharacter)
	{
		lcd_write_data(prevchar);
		lcd_write_data(ch[0]);
		isCharacter = 0;
		myX += 8;
	}
	else
	{
		lcd_write_data(ch[0]);
		lcd_write_data(ch[1]);
		prevchar = ch[0];
		isCharacter = 1;
		myX += 8;
	}

}

void lcd_zh(int x, int y, unsigned char mat[2])
{
	unsigned char coor;

	if(isCharacter)
		myX += 8;

	if (y != prevY)
	{
		myX = 0;
		prevY = y;
	}
	//x -= x0;
	y -= y0;

	x = myX / 16;
	y /= 16;
	
	if(y > 3)
		return;

	coor = coordinate[y] + x;

	lcd_write_instruction(coor);
	lcd_write_data(mat[0]);
	lcd_write_data(mat[1]);
	
	isCharacter = 0;
	myX += 16;
}*/

void lcd_clear(void)
{

	int i, j;
	for(i = 0; i < 32; i++)
	{
		lcd_write_instruction(0x3e);
		lcd_write_instruction(i | 0x80);
		lcd_write_instruction(0 | 0x80);
		lcd_write_instruction(0x3a);
		for(j = 0; j < 8; j++)
		{
			lcd_write_data(0);
			lcd_write_data(0);
		}
	}

	for(i = 0; i < 32; i++)
	{
		lcd_write_instruction(0x3e);
		lcd_write_instruction(i | 0x80);
		lcd_write_instruction(8 | 0x80);
		lcd_write_instruction(0x3a);
		for(j = 0; j < 8; j++)
		{
			lcd_write_data(0);
			lcd_write_data(0);
		}
	}

//	lcd_write_instruction(0x01);
	Delay(80);
}

⌨️ 快捷键说明

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