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

📄 lcd.c

📁 AVR单片机 C语言程序设计经典实用
💻 C
📖 第 1 页 / 共 2 页
字号:
//******************************************************************************
// File Name : LCD.c
// Author    : Steaven
// Created   : 2008-07-27
// Modified  : 
// Revision  : V0.0
//******************************************************************************

//PIN DESCRIPTION
//01  2   3  4   5   6   7   8   9   10  11  12  13  14  15  16  17  18  19  20
//GND VCC V0 RS  RW  EN  D0  D1  D2  D3  D4  D5  D6  D7  CS1 CS2 RST VEE BLA BLK
//GND +5V V0 PB0 PB1 PB2 PA0 PA1 PA2 PA3 PA4 PA5 PA6 PA7 PC6 PC5 PC7 VEE +5V GND

#include "app.h" 

//constant definition
#define cLCD_X_MAX   64
#define cLCD_Y_MAX  128

//variable defintion
INT16U wDataBaseLength; //database length

//local functions declaration
void Set_DI(INT8U RS);
void Set_RW(INT8U RW);
void Set_EN(INT8U EN);
void Set_CS1(INT8U CS1);
void Set_CS2(INT8U CS2);
void Set_RST(INT8U RST);
void Set_Data(INT8U data);

//******************************************************************************
// Function    : Set_RS
// Input       : RS - RS Control Line Level
// Output      : none
// Description : LCD(1602) Low Level Interface Function
//******************************************************************************
void Set_DI(INT8U RS)
{
	if(RS == 0)
	{
		PORTB &= ~0x01;
	}
	else
	{
		PORTB |= 0x01;
	}
}

//******************************************************************************
// Function    : Set_RW
// Input       : RW - RW Control Line Level
// Output      : none
// Description : Low Level Interface Function
//******************************************************************************
void Set_RW(INT8U RW)
{
	if(RW == 0)
	{
		PORTB &= ~0x02;
	}
	else
	{
		PORTB |= 0x02;
	}
}

//******************************************************************************
// Function    : Set_EN
// Input       : RW - RW Control Line Level
// Output      : none
// Description : Low Level Interface Function
//******************************************************************************
void Set_EN(INT8U EN)
{
	if(EN == 0)
	{
		PORTB &= ~0x04;
	}
	else
	{
		PORTB |= 0x04;
	}
}

//******************************************************************************
// Function    : Set_CS2
// Input       : CS2 - CS2 Control Line Level
// Output      : none
// Description : Low Level Interface Function
//******************************************************************************
void Set_CS2(INT8U CS2)
{
	if(CS2 == 0) 
	{
		PORTC &= ~0x20;
	}
 	else
 	{
 		PORTC |= 0x20;
 	}
}

//******************************************************************************
// Function    : Set_CS1
// Input       : CS1 - CS1 Control Line Level
// Output      : none
// Description : Low Level Interface Function
//******************************************************************************
void Set_CS1(INT8U CS1)
{
	if(CS1 == 0)
	{
		PORTC &= ~0x40;
	}
	else
	{
		PORTC |= 0x40;
	}
}


//******************************************************************************
// Function    : Set_CS1
// Input       : CS1 - CS1 Control Line Level
// Output      : none
// Description : Low Level Interface Function
//******************************************************************************
void Set_RST(INT8U RST)
{
	if(RST == 0)
	{
		PORTC &= ~0x80;
	}
	else
	{
		PORTC |= 0x80;
	}
}
 
//******************************************************************************
// Function    : Set_Data
// Input       : data - Data Line Level
// Output      : none
// Description : Low Level Interface Function
//******************************************************************************
void Set_Data(INT8U data)
{
	PORTA  = data;
}

//******************************************************************************
// Function    : LCD_Command_DispOnOff
// Input       : onoff = 1 -> LCD Display Enabled,else Disabled
// Output      : none
// Description : Command Level Function,Display ONOFF Control
//******************************************************************************
void LCD_Command_DispOnOff(INT8U onoff)
{
	Set_RW(0);
	Set_DI(0);
	Set_EN(0);
	Set_Data(0x3E + onoff);
	Set_EN(1);
	Set_EN(0);
}

//******************************************************************************
// Function    : LCD_Command_SetStartLine
// Input       : startline - Display Start Line,See Specification
// Output      : none
// Description : Command Level Function,Set Display Start Line
//******************************************************************************
void LCD_Command_SetStartLine(INT8U startline)
{
	Set_RW(0);
	Set_DI(0);																
	Set_EN(0);																
	Set_Data(0xC0 + startline);
	Set_EN(1);																
	Set_EN(0);																
}

//******************************************************************************
// Function    : LCD_Command_Set_X
// Input       : x - Display Row,from 0 to 7 for 128*64 LCD
// Output      : none
// Description : Command Level Function,Set Operation X Address
//******************************************************************************
void LCD_Command_Set_X(INT8U x)
{
	Set_RW(0);
	Set_DI(0);
	Set_EN(0);
	Set_Data(0xB8 + x);
	Set_EN(1);
	Set_EN(0);
}

//******************************************************************************
// Function    : LCD_Command_Set_Y
// Input       : y - Display Column,from 0 to 63 for 128*64 LCD
// Output      : none
// Description : Command Level Function,Set Operation Y Address
//******************************************************************************
void LCD_Command_Set_Y(INT8U column)
{
	Set_RW(0);
	Set_DI(0);
	Set_EN(0);
	Set_Data(0x40 + column);
	Set_EN(1);
	Set_EN(0);
}

//******************************************************************************
// Function    : LCD_Command_WriteByte
// Input       : data - Display Data
// Output      : none
// Description : Command Level Function,Write Display Data to DDRAM
//******************************************************************************
void LCD_Command_WriteByte(INT8U data)
{
	Set_RW(0);
	Set_DI(1);
	Set_EN(0);
	Set_Data(data);
	Set_EN(1);
	Set_EN(0);
}

//******************************************************************************
// Function    : LCD_Reset
// Input       : none
// Output      : none
// Description : Command Level Function,Soft RESET LCD Module
//******************************************************************************
void LCD_Reset(void)
{
	Set_RST(0);
	Set_RST(1);
}

//******************************************************************************
// Function    : LCD_Init
// Input       : none
// Output      : none
// Description : Application Level Function,Init LCD
//******************************************************************************
void LCD_Init(void)
{
	LCD_Reset();
	LCD_Clear_Line(0,7);
	LCD_Command_DispOnOff(1);
	LCD_Command_SetStartLine(0);
}

//******************************************************************************
// Function    : LCD_Clear_Line
// Input       : startline,endline - Line Range to be Cleare
// Output      : none
// Description : Command Level Function,Clear Screen Display
//******************************************************************************
void LCD_Clear_Line(INT8U startline,INT8U endline)
{
	INT8U i,j;
	for(i = startline;i <= endline;i++)
	{
		Set_CS1(1);
		Set_CS2(1);
		LCD_Command_Set_X(i);
		LCD_Command_Set_Y(0);
		for(j = 0;j < 64;j++)
		{
			LCD_Command_WriteByte(0x00);
		}
	}
}

//******************************************************************************
// Function    : LCD_Write_Data
// Input       : x in [0,7] and y in [0,127]
// Output      : none
// Description : Command Level Function,在指定行列写一个数据
//******************************************************************************
void LCD_Write_Data(INT8U x,INT8U y,INT8U data)
{
	if(y < 64)
	{
		Set_CS1(1);
		Set_CS2(0);
		LCD_Command_Set_X(x);
		LCD_Command_Set_Y(y);
	}
	else
	{
		Set_CS1(0);
		Set_CS2(1);
		LCD_Command_Set_X(x);
		LCD_Command_Set_Y(y - 64);		
	}
	LCD_Command_WriteByte(data);
}

//******************************************************************************
// Function    : LCD_Write_Dot
// Input       : x in [0,63] and y in [0,127]
// Output      : none
// Description : Application Level Function,在LCD的指定象素位置描点,注意此函数
//               是覆盖式描点,原位置的一个字节数据将被此点取代
//******************************************************************************
void LCD_Write_Dot(INT8U x,INT8U y)
{
	INT8U x_address,y_address;
	INT8U data = 0;
	if((x < 64) && (y < 128))
	{
		x_address = x >> 3;
		y_address = y;
		LCD_Write_Data(x_address,y_address,data | (1 << (x % 8)));
	}
}

//******************************************************************************
// Function    : LCD_Write_Char
// Input       : x in [0,3] and y in [0,15],disp - start address of display buffer
// Output      : none
// Description : Application Level Function,在LCD的指定显示单元显示一个字符。
//               每个显示单元默认为8*16点阵字体,对128*64点阵LCD,x最多显示4行
//               字符,y最多显示16个字符
//******************************************************************************
void LCD_Write_Char(INT8U x,INT8U y,const INT8U *disp)
{
	INT8U i;
	INT8U x_address;
	INT8U y_address;
	if((x < 4) && (y < 16))
	{
		for(i = 0;i < 8;i++)
		{
			x_address = x << 1;
			y_address = (y << 3) + i;
			LCD_Write_Data(x_address,y_address,*disp++);
		}
		for(i = 0;i < 8;i++)
		{
			x_address = (x << 1) + 1;
			y_address = (y << 3) + i;
			LCD_Write_Data(x_address,y_address,*disp++);
		}
	}
}

//******************************************************************************
// Function    : LCD_Write_Word
// Input       : x in [0,3] and y in [0,14],disp - start address of display buffer
// Output      : none
// Description : Application Level Function,在LCD的指定显示单元显示一个中文。
//               每个显示单元默认为16*16点阵字体,对128*64点阵LCD,x最多显示4行
//               字符,y最多显示16个字符,但不允许显示半个汉字
//******************************************************************************
void LCD_Write_Word(INT8U x,INT8U y,const INT8U *disp)
{
	INT8U i;
	INT8U x_address;
	INT8U y_address;
	if((x < 4) && (y < 15))
	{
		for(i = 0;i < 16;i++)
		{
			x_address = x << 1;
			y_address = (y << 3) + i;
			LCD_Write_Data(x_address,y_address,*disp++);
		}
		for(i = 0;i < 16;i++)
		{
			x_address = (x << 1) + 1;
			y_address = (y << 3) + i;
			LCD_Write_Data(x_address,y_address,*disp++);
		}
	}
}

//******************************************************************************
// Function    : LCD_Write_Picture
// Input       : x in [0,7] and y in [0,127],
//               x1_length - 纵向象素 y1_length - 横向象素,x1_length必须为8的
//               整数倍,y1_length可以不必为8的整数倍。
// Output      : none
// Description : Application Level Function,在LCD的指定区域显示图片
//******************************************************************************
void LCD_Write_Picture(INT8U x1,INT8U y1,INT8U x_length,INT8U y_length,const INT8U *pdata)
{
	INT8U x_address;
	INT8U y_address;
	for(x_address = x1;x_address <= x1 + (x_length >> 3) - 1;x_address++)
	{
		for(y_address = y1;y_address < y1 + y_length;y_address++)
		{
			LCD_Write_Data(x_address,y_address,*pdata++);
		}
	}
}

//data type definition
struct DataBaseIndex
{
	INT16U Code;
    const INT8U  *Ptr;
};

//mask list
const struct DataBaseIndex DataBaseIndexList[] = 
{
	0x10,eChar_10,
	' ',eChar_20,
	'!',eChar_21,
	'"',eChar_22,
	'#',eChar_23,
	'$',eChar_24,
	'%',eChar_25,
	'&',eChar_26,
	0x27,eChar_27,
	'(',eChar_28,
	')',eChar_29,
	'*',eChar_2A,
	'+',eChar_2B,
	',',eChar_2C,
	'-',eChar_2D,
	'.',eChar_2E,
	'/',eChar_2F,
	
	'0',eChar_30,
	'1',eChar_31,
	'2',eChar_32,
	'3',eChar_33,
	'4',eChar_34,
	'5',eChar_35,
	'6',eChar_36,
	'7',eChar_37,
	'8',eChar_38,
	'9',eChar_39,
	
	':',eChar_3A,
	';',eChar_3B,
	'<',eChar_3C,
	'=',eChar_3D,
	'>',eChar_3E,
	'?',eChar_3F,
	'@',eChar_40,
	
	'A',eChar_41,
	'B',eChar_42,
	'C',eChar_43,
	'D',eChar_44,
	'E',eChar_45,
	'F',eChar_46,
	'G',eChar_47,
	'H',eChar_48,
	'I',eChar_49,
	'J',eChar_4A,
	'K',eChar_4B,
	'L',eChar_4C,
	'M',eChar_4D,
	'N',eChar_4E,
	'O',eChar_4F,
	'P',eChar_50,
	'Q',eChar_51,
	'R',eChar_52,
	'S',eChar_53,
	'T',eChar_54,
	'U',eChar_55,
	'V',eChar_56,
	'W',eChar_57,
	'X',eChar_58,
	'Y',eChar_59,
	'Z',eChar_5A,
	
	'[',eChar_5B,
	0x5C,eChar_5C,
	']',eChar_5D,
	'^',eChar_5E,
	'_',eChar_5F,
	0x60,eChar_60,
	
	'a',eChar_61,
	'b',eChar_62,
	'c',eChar_63,
	'd',eChar_64,
	'e',eChar_65,
	'f',eChar_66,
	'g',eChar_67,
	'h',eChar_68,
	'i',eChar_69,
	'j',eChar_6A,
	'k',eChar_6B,
	'l',eChar_6C,
	'm',eChar_6D,
	'n',eChar_6E,
	'o',eChar_6F,
	'p',eChar_70,
	'q',eChar_71,
	'r',eChar_72,
	's',eChar_73,
	't',eChar_74,
	'u',eChar_75,
	'v',eChar_76,
	'w',eChar_77,
	'x',eChar_78,
	'y',eChar_79,
	'z',eChar_7A,
	0xDD,eChar_DD,
	0xB0B4,cChar_B0B4,
	0xB2E2,cChar_B2E2,
	0xB3F5,cChar_B3F5,
	0xB4FD,cChar_B4FD,
	0xB5C8,cChar_B5C8,
	0xB7E4,cChar_B7E4,
	0xBBAF,cChar_BBAF,

⌨️ 快捷键说明

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