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

📄 lcd.c

📁 AVR单片机C语言程序设计实例精粹
💻 C
字号:
//******************************************************************************
// 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 "iom16v.h" 
#include "DataType.h"
#include "LCD.h"
#include "macros.h" 

#define cLCD_X_MAX   64
#define cLCD_Y_MAX  128

//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);
void LCD_Command_DispOnOff(INT8U onoff);
void LCD_Command_SetStartLine(INT8U startline);
void LCD_Command_Set_X(INT8U x);
void LCD_Command_Set_Y(INT8U y);
void LCD_Command_WriteByte(INT8U data);
void LCD_Reset(void);
void LCD_Clear_Line(INT8U wStartLine,INT8U wEndLine);
void LCD_Init(void);
void WriteWord(INT8U const *disp,INT8U x,INT8U y);
void WriteCharacter(const INT8U *disp,INT8U x,INT8U y,INT8U cs);
void WritePIC(const INT8U *disp);
void LCD_Write_Char(INT8U y,INT8U x,const INT8U *pdata);
void LCD_Write_Dot(INT8U x,INT8U y);
void LCD_Write_Data(INT8U x,INT8U y,INT8U data);	
void LCD_Write_Picture(INT8U x1,INT8U y1,INT8U x2,INT8U y2,const INT8U *pdata);

//******************************************************************************
// 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++);
		}
	}
}

//=========================END OF FILE=========================//

⌨️ 快捷键说明

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