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

📄 lcd_back.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
//GND VCC V0 RS  RW  EN  D0  D1  D2  D3  D4  D5  D6  D7  BLA BLK
//GND +5V V0 PB0 PB1 PB2 PA0 PA1 PA2 PA3 PA4 PA5 PA6 PA7 GND +5V

#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);
INT8U Get_Data(void);
void DisplayOnOff(INT8U i);
void SetStartLine(INT8U i);
void LCD_Command_Set_X(INT8U x);
void LCD_Command_Set_Y(INT8U y);
void LCD_Command_WriteByte(INT8U data);
INT8U Read_Byte(void);
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);
INT8U LCD_Read_Data(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);

/*
void nop(INT8U cnt)
{
	while(cnt--) ;
}

void Delay(INT8U cnt)
{
	while(cnt--) ;
}
*/
void Delay_ms(INT16U time)
{
    for(;time!=0;time--)
		{
		    Delay(500);
		}
}


#define nop(X) {;}
#define Delay(X) {;}

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


//sbit RST=PC7;
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;
}

//显示开关控制命令,i=0开显示,i=1关显示//
void DisplayOnOff(INT8U onoff)
{
	Set_RW(0);
	Set_DI(0);
	Set_EN(0);
	Set_Data(0x3E + onoff);
	nop(10);
	Set_EN(1);
	nop(10);
	Set_EN(0);
}

//设置显示起始行,i=0~63//
void SetStartLine(INT8U line)
{
	Set_RW(0);
	Set_DI(0);																
	Set_EN(0);																
	Set_Data(0xC0 + line);
	nop(10);																
	Set_EN(1);																
	nop(10);																																
	Set_EN(0);																
}

//设置页(行)地址,i=0~7//
void LCD_Command_Set_X(INT8U row)
{
	Set_RW(0);
	Set_DI(0);
	Set_EN(0);
	Set_Data(0xB8 + row);
	nop(10);
	Set_EN(1);
	nop(10);
	Set_EN(0);
}

//设置Y(列)地址,i=0~127//
void LCD_Command_Set_Y(INT8U column)
{
	Set_RW(0);
	Set_DI(0);
	Set_EN(0);
	Set_Data(0x40 + column);
	nop(10);
	Set_EN(1);
	nop(10);
	Set_EN(0);
}

//写显示数据,i为送DDRAM的数据
void LCD_Command_WriteByte(INT8U data)
{
	Set_RW(0);
	Set_DI(1);
	Set_EN(0);
	Set_Data(data);
	nop(10);
	Set_EN(1);
	nop(10);
	Set_EN(0);
}

INT8U Read_Byte(void)
{
	INT8U data;
	DDRA = 0x00;
	PORTA = 0x00;
	Set_EN(0);
	Set_RW(1);
	Set_DI(1);
//	Set_EN(0);
	nop(100);
	Set_EN(1);
	nop(100);
	//data = Get_Data();
	data = PINA;
	Set_EN(0);
	return(data);
}



//LCD复位
void LCD_Reset(void)
{
	Set_RST(0);
	Set_RST(1);
}

//LCD清除显示行
void LCD_Clear_Line(INT8U wStartLine,INT8U wEndLine)
{
	INT8U i,j;
	for(i = wStartLine;i <= wEndLine;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);
		}
	}
}

//LCD初始化
void LCD_Init(void)
{
	LCD_Reset();
	LCD_Clear_Line(0,7);
	DisplayOnOff(1);
	SetStartLine(0);
}

//对128*64点阵LCD
//X : 行数 0~3
//Y : 列数 0~15
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++);
		}
	}
}

//对128*64点阵LCD
//X : 行数 0~3
//Y : 列数 0~15
void LCD_Write_Word(INT8U x,INT8U y,const INT8U *disp)
{
	INT8U i;
	INT8U x_address;
	INT8U y_address;
	if((x < 4) && (y < 16))
	{
		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++);
		}
	}
}

//x1 行 y列
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 <= (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=========================//



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;
		//data = LCD_Read_Data(x_address,y_address);
		LCD_Write_Data(x_address,y_address,data | (1 << (x % 8)));
	}
}

INT8U LCD_Read_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);
	}
	data = Read_Byte();
	return(data);
}

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);
}



⌨️ 快捷键说明

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