nios ii 之5110液晶屏(6*8、8*16 ASCII字符,16*16 汉字,常用图标,图案,超全字库+函数代码)

上一篇文章给出了5110液晶屏的驱动和基本操作函数,这里把自己辛苦完成的显示中英文字符,图标和图案的函数代码和字库提供出来。

其中字库和图案部分来自网上他人提供,还有很大部分是自己做的。网上的字库并不完整,我将其扩增了,非常全。我的字库包括:

1.全部 6*8 ASCII 码字库

2.22个 6*8 常用符号(sign),包括希腊字母,数学符号,物理单位,移动标志等

3.35个 10*8 常用图标(icon),包括电池电量,时钟,钥匙,火车,乐符,物理符号,播放器按键符号等

4.全部 8*16 ASCII码字库

5.16*16 汉字,其实就提供了几个,但是提供字模提取软件,非常方便

6.12幅点阵位图,高度为8的倍数

这里贴出几个图,手机拍的,效果不是很好:




至于显示函数,我提供了多种函数。有显示6*8和8*16 ASCII码的,显示6*8符号,10*8图标,显示16*16汉字,显示高度为8倍数的位图。显示函数提供了需要坐标和不需要坐标的两种,这样可以不需要自己计算要在哪显示(即紧接着显示),对于字符串和汉字句子可以自动换行。代码比较多,不过我都注释了,应该不难理解。废话不多说,下面贴出代码:

头文件:

#ifndef LCD_5110_SOLUTION_H_
#define LCD_5110_SOLUTION_H_

#define CMD		0x00
#define DATA	0x01
#define LCD_X	84
#define LCD_Y	48

#include
#include
#include
#include
#include"Font6_8.h"
#include"Icon10_8.h"
#include"Font8_16.h"
#include"Font16_16.h"
#include"Bitmap.h"
//write a byte
void LcdWrite(alt_u8 data,alt_u8 d_c);
//initial the 5110
void InitLcd();
//set the position of x axis and y axis
void SetXY(alt_u8 x,alt_u8 y);
//clear screen
void LcdClearAll();
//write a char(6*8),don't need coordinates
void WriteChar(alt_u8 value);
//write a char(6*8),need coordinates
void PutChar(alt_u8 value,alt_u8 x,alt_u8 y);
//write a string(6*8),don't need coordinates
void WriteStr(char * str);
//write a string(6*8),need coordinates,auto line breaks
void PutStr(char * str,alt_u8 x,alt_u8 y);
//draw a sign(6*8),don't need coordinates
void WriteSign(char * sign);
//draw a sign(6*8),need coordinates
void DrawSign(char * sign,alt_u8 x,alt_u8 y);
//draw a sign(10*8),don't need coordinates
void WriteIcon(char * icon);
//draw a sign(10*8),need coordinates,x is between 0 and 74
void DrawIcon(char * icon,alt_u8 x,alt_u8 y);
//write a char(8*16)
void WriteChar_8_16(alt_u8 value,alt_u8 x,alt_u8 y);
//write a string(8*16),auto line breaks,x is between 0 and 76
void WriteStr_8_16(char * str,alt_u8 x,alt_u8 y);
//write a Chinese character(16*16)
void WriteHanzi(char * Hanzi,alt_u8 x,alt_u8 y);
//write a Chinese sentence(16*16),auto line breaks,x is between 0 and 58
void WriteHanziStr(char * Hanzi,alt_u8 x,alt_u8 y,alt_u8 num);
//draw a picture(84*48),don't need coordinates
void DrawPicture(char bitmap[]);
//draw a picture(width*height(a multiple of 8)),need coordinates
void DrawBitmap(char bitmap[],alt_u8 x,alt_u8 y,alt_u8 width,alt_u8 height);

#endif /* LCD_5110_SOLUTION_H_ */

源文件:

#include"lcd_5110_solution.h"
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////*				以下是基本操作和初始化函数				*//////
//write a byte
void LcdWrite(alt_u8 data,alt_u8 d_c)
{
	alt_u8 i;
	//chose the chip
	IOWR_ALTERA_AVALON_PIO_DATA(SCE_BASE, 0x00);
	//chose data mode or command mode
	IOWR_ALTERA_AVALON_PIO_DATA(D_C_BASE, d_c);
	//sent 8 bits
	for(i = 0;i < 8;i ++)
	{
		IOWR_ALTERA_AVALON_PIO_DATA(SCLK_BASE, 0x00);
		IOWR_ALTERA_AVALON_PIO_DATA(SDIN_BASE, data >> (7 - i));
		IOWR_ALTERA_AVALON_PIO_DATA(SCLK_BASE, 0x01);
	}
	//release the chip
	IOWR_ALTERA_AVALON_PIO_DATA(SCE_BASE, 0x01);
}
//initial the 5110
void InitLcd()
{
	usleep(1000);
	//sent a reset pulse(low) to reset all internal registers
	IOWR_ALTERA_AVALON_PIO_DATA(REST_BASE, 0x00);
	usleep(1);
	IOWR_ALTERA_AVALON_PIO_DATA(REST_BASE, 0x01);
	usleep(1);
	//chose the chip
	IOWR_ALTERA_AVALON_PIO_DATA(SCE_BASE, 0x00);
	usleep(1);
	//release the chip
	IOWR_ALTERA_AVALON_PIO_DATA(SCE_BASE, 0x01);
	usleep(1);
	//function set PD = 0 and V = 0, select extended instruction set(H = 1 mode)
	LcdWrite(0x21,CMD);
	//set VOP: VOP is set to a + 65 × b [V]
	LcdWrite(0xbf,CMD);
	//RECOMMENDED MUX RATE(1:24),you can chose other MUX RATE
	LcdWrite(0x15,CMD);
	//function set PD = 0 and V = 0, select normal instruction set(H = 0 mode)
	LcdWrite(0x20,CMD);
	//display control set normal mode (D=1andE=0)
	LcdWrite(0x0c,CMD);
	//clear screen
	LcdClearAll();
}
//set the x axis and y axis
void SetXY(alt_u8 x,alt_u8 y)
{
	if(x > 83) x = 0;
	if(y > 5) y = 0;
	//set y axis
	LcdWrite(0x40 | y,CMD);
	//set y axis
	LcdWrite(0x80 | x,CMD);
}
//clear all screen
void LcdClearAll()
{
	alt_u16 i;
	SetXY(0,0);
	//write space continually
	for(i = 0;i < LCD_X * LCD_Y / 8;i ++)
	{
		LcdWrite(0,DATA);
	}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////*				以下是高度为8的各种操作函数				*//////
//*			包括6*8字符和字符串,6*8符号,10*8图标			   *//
//every row contains 14 characters,there are 6 rows (font = 6 * 8)//
/////////////////////////////////////////////////////////////////////////////////////////////////////
		/////////   	x		  /////////
		//0 1 2 3 4 5 6 7 8 9 10 11 12 13//		y = 0
		//0 1 2 3 4 5 6 7 8 9 10 11 12 13//		y = 1
		//0 1 2 3 4 5 6 7 8 9 10 11 12 13//		y = 2
		//0 1 2 3 4 5 6 7 8 9 10 11 12 13//		y = 3
		//0 1 2 3 4 5 6 7 8 9 10 11 12 13//		y = 4
		//0 1 2 3 4 5 6 7 8 9 10 11 12 13//		y = 5
/////////////////////////////////////////////////////////////////////////////////////////////////////
//write a character(6*8),don't need coordinates
void WriteChar(alt_u8 value)
{
	alt_u8 i;
	for(i = 0;i < 6;i ++)
	LcdWrite(ASCII_6_8[value - 0x20][i],DATA);
}
//write a char(6*8),need coordinates
void PutChar(alt_u8 value,alt_u8 x,alt_u8 y)
{
	if(x > 13) x = 0;
	SetXY(6 * x,y);
	WriteChar(value);
}
//write a string(6*8),don't need coordinates
void WriteStr(char * str)
{
	while(*str)
	{
		WriteChar(*str ++);
	}
}
//write a string(6*8),need coordinates,auto line breaks
void PutStr(char * str,alt_u8 x,alt_u8 y)
{
	if(x > 13) x = 0;
	SetXY(6 * x,y);
	WriteStr(str);
}
//draw a sign(6*8),don't need coordinates
void WriteSign(char * sign)
{
	alt_u8 i;
	for(i = 0;i < 6;i ++)
	LcdWrite(sign[i],DATA);
}
//draw a sign(6*8),need coordinates
void DrawSign(char * sign,alt_u8 x,alt_u8 y)
{
	SetXY(x,y);
	WriteSign(sign);
}
//draw a sign(10*8),don't need coordinates
void WriteIcon(char * icon)
{
	alt_u8 i;
	for(i = 0;i < 10;i ++)
	LcdWrite(icon[i],DATA);
}
//draw a sign(10*8),need coordinates,x is between 0 and 74
void DrawIcon(char * icon,alt_u8 x,alt_u8 y)
{
	//if need line breaks
	if(x > 74)
	{
		x = 0;
		y += 1;
	}
	SetXY(x,y);
	//draw a sign(10*8)
	WriteIcon(icon);
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////*				以下是高度为16的各种操作函数				*//////
//*		    	      包括8*16字符和字符串,16*16汉字		           *//
//every row contains 10 characters,there are 3 rows (font = 8 * 16)//
///////////////////////////////////////////////////////////////////////////////////////////////////
/////////       x       ///////////
		//0 1 2 3 ... 81 82 83//	y = 0
		//0 1 2 3 ... 81 82 83//
		//0 1 2 3 ... 81 82 83//	y = 1
		//0 1 2 3 ... 81 82 83//
		//0 1 2 3 ... 81 82 83//	y = 2
		//0 1 2 3 ... 81 82 83//
///////////////////////////////////////////////////////////////////////////////////////////////////
//write a char(8*16)
void WriteChar_8_16(alt_u8 value,alt_u8 x,alt_u8 y)
{
	alt_u8 i;
	//draw the first row
	SetXY(x,y * 2);
	for(i = 0;i < 8;i ++)
		LcdWrite(ASCII_8_16[value - 0x20][i],DATA);
	//draw the second row
	SetXY(x,y * 2 + 1);
	for(i = 8;i < 16;i ++)
		LcdWrite(ASCII_8_16[value - 0x20][i],DATA);
}
//write a string(8*16),auto line breaks,x is between 0 and 76
void WriteStr_8_16(char * str,alt_u8 x,alt_u8 y)
{
	alt_u8 i = 0;
	while(*str)
	{
		//if need line breaks
		if(x + (8 * i) > 76)
		{
			x = 0;
			i = 0;
			y += 2;
		}
		//write a char(8*16)
		WriteChar_8_16(*str,x + (8 * i),y);
		str ++;
		i ++;
	}
}
//every row contains 5 characters,there are 3 rows (font = 16 * 16)//
///////////////////////////////////////////////////////////////////////////////////////////////////
/////////       x       ///////////
		//0 1 2 3 ... 81 82 83//	y = 0
		//0 1 2 3 ... 81 82 83//
		//0 1 2 3 ... 81 82 83//	y = 1
		//0 1 2 3 ... 81 82 83//
		//0 1 2 3 ... 81 82 83//	y = 2
		//0 1 2 3 ... 81 82 83//
///////////////////////////////////////////////////////////////////////////////////////////////////
//write a Chinese character(16*16)
void WriteHanzi(char Hanzi[],alt_u8 x,alt_u8 y)
{
	alt_u8 i;
	//write the first row
	SetXY(x,y * 2);
	for(i = 0;i < 16;i ++)
	LcdWrite(Hanzi[i],DATA);
	//write the second row
	SetXY(x,2 * y + 1);
	for(i = 16;i < 32;i ++)
		LcdWrite(Hanzi[i],DATA);
}
//write a Chinese sentence(16*16),auto line breaks,x is between 0 and 58
void WriteHanziStr(char Hanzi[],alt_u8 x,alt_u8 y,alt_u8 num)
{
	alt_u8 num_i,i = 0;
	for(num_i = 0;num_i < num;num_i ++)
	{
		//if need line breaks
		if(x + (16 * num_i) > 58)
		{
			x = 0;
			i = 0;
			y += 1;
		}
		//write a Chinese character(16*16)
		WriteHanzi(Hanzi + 32 * num_i,x + (16 * i),y);
		i ++;
	}
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////*				以下是画图的各种操作函数				*//////
//*		    	      包括84*48,高度为8的倍数的图片		       *//
/////////////////////////////////////////////////////////////////////////////////////////////////////
		/////////   	x		  /////////
		//0 1 2 3 4 5 6 7 8 9 10 11 12 13//		y = 0
		//0 1 2 3 4 5 6 7 8 9 10 11 12 13//		y = 1
		//0 1 2 3 4 5 6 7 8 9 10 11 12 13//		y = 2
		//0 1 2 3 4 5 6 7 8 9 10 11 12 13//		y = 3
		//0 1 2 3 4 5 6 7 8 9 10 11 12 13//		y = 4
		//0 1 2 3 4 5 6 7 8 9 10 11 12 13//		y = 5
/////////////////////////////////////////////////////////////////////////////////////////////////////
//draw a picture(84*48),don't need coordinates
void DrawPicture(char bitmap[])
{
	alt_u16 i;
	for(i = 0;i < LCD_X * LCD_Y / 8;i ++)
		LcdWrite(bitmap[i],DATA);
}
//draw a picture(width*height(a multiple of 8)),need coordinates
void DrawBitmap(char bitmap[],alt_u8 x,alt_u8 y,alt_u8 width,alt_u8 height)
{
	alt_u8 width_i,height_i;
	//draw the height_i row
	for(height_i = 0;height_i < height / 8;height_i ++)
	{
		SetXY(x,y + height_i);
		//draw a row(width column)
		for(width_i = 0;width_i < width;width_i ++)
		{
			LcdWrite(bitmap[width_i + height_i * width],DATA);
		}
	}
}
主函数:
#include"lcd_5110_solution.h"
#include
int main()
{
	char i;
	int readdata;
	InitLcd();
	while(1)
	{
		scanf("%d",&readdata);
		switch(readdata)
		{
			case 1:	LcdClearAll();
					PutChar('+',0,1);
					WriteChar('1');
					PutStr("023456 {}[]|?`~@",0,2);
					break;
			case 2:	LcdClearAll();
					DrawSign(sign[1],1,3);
					break;
			case 3:	LcdClearAll();
					for(i = 0;i < 8;i ++)
					{
						DrawIcon(icon[i],70,0);
						usleep(250000);
					}
					break;
			case 4:	LcdClearAll();
					WriteHanziStr(Hanzi,10,0,4);
					WriteStr_8_16("ming1006",10,1);
					break;
			case 5: LcdClearAll();
					DrawBitmap(girl,20,0,45,48);
					break;
			case 6: LcdClearAll();
					DrawBitmap(apple,20,0,45,48);
					break;
			case 7:	LcdClearAll();
					for(i = 0;i < 22;i ++)
					{
						DrawSign(sign[i],15 * (i % 5),i / 5);
					}
					break;
			case 8:	LcdClearAll();
					for(i = 0;i < 35;i ++)
					{
						DrawIcon(icon[i],15 * (i % 6),i / 6);
					}
					break;
		}
	}
	return 0;
}
头文件中的字库因为实在太多了,这个地方放不下,我放在我CSDN资源里,大家可以免费去下,对了,我把整个工程也打包下,初学者可以参考下,嘿嘿,不过写得不是很专业啦!还请多多指教啊!

CSDN资源网址:单要字库这里 http://download.csdn.net/detail/ming1006/4048090点击打开链接

完整工程再加字库的这里 http://download.csdn.net/detail/ming1006/4048142点击打开链接




你可能感兴趣的:(液晶屏,nios,ii)