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

📄 lcd.c

📁 ATmegal128连接128*16的LCD模块的驱动程序
💻 C
字号:
#include    <iom128.h>
#include    "TypeDef.h"
#include    "lcd.h"
#include    "font\E6x8.h"


byte LcdCS=0x20;


void InitLcd(void);
void LcdClear(void);
void Puts(byte x, byte y, byte *msg, byte rev);		//	CS_1苞 CS_2俊 吧媚 免仿窍绰 窃荐
void Puts1(byte x, byte y, byte *msg);				//	CS_1俊父 免仿窍绰 窃荐
void Puts1R(byte x, byte y, byte *msg);				//	CS_1俊父 Reverse肺 免仿窍绰 窃荐
void Putc(byte ch);
void PutcR(byte ch);
void Putc_xy(byte x, byte y, byte ch);
void PutcR_xy(byte x, byte y, byte ch);
void LcdWriteData(byte data);
void LcdWriteCommand(byte command);
void LcdChipSelect(byte chip_select);
void PutcM_xy(byte x, byte y, byte ch);
void PutcRM_xy(byte x, byte y, byte ch);
byte CheckState(void);


void InitLcd(void)
{
	LcdCS	= LCD_CS_12;
	LcdChipSelect(LcdCS);
	LcdWriteCommand(LCD_RESET);
	LcdClear();
	LcdWriteCommand(LCD_START);
	LcdWriteCommand(LCD_ON);
}


void Puts(byte x, byte y, byte *msg, byte rev)
{
	byte	ch;

	if (x <= COLUMNS_2)
	{
		LcdCS	= LCD_CS_1;
		LcdWriteCommand(LCD_PAGE + y);		//	Set Page
		LcdWriteCommand(x*FONT_WIDTH +0x41);	//	Set Column
	}
	else
	{
		LcdCS	= LCD_CS_2;
		LcdWriteCommand(LCD_PAGE + y);		//	Set Page
		LcdWriteCommand((x-11)*FONT_WIDTH + 0x43);	//Set Column
	}

	ch	= *msg;
	while(ch)
	{
		if (x == COLUMNS_2)
		{
			if (rev == DISP_REVERSE)
			PutcRM_xy(x,y,ch);
		    else
			PutcM_xy(x,y,ch);//此处已经切换到了右半屏
		}
		else
		{
			if (rev == DISP_REVERSE)
			PutcR(ch);
			else
			Putc(ch);
		}
		x++;
		msg++;
		ch	= *msg;
	}

	if (rev)
		LcdWriteData(0xff);
	else
		LcdWriteData(0x00);
}


void Puts1(byte x, byte y, byte *msg)
{
	LcdCS	= LCD_CS_1;
	LcdWriteCommand(LCD_PAGE + y);		//	Set Page
	LcdWriteCommand(x*FONT_WIDTH + 0x41);	//	Set Column

	while(*msg)	Putc(*msg++);
}


void Puts1R(byte x, byte y, byte *msg)
{
	LcdCS	= LCD_CS_1;
	LcdWriteCommand(LCD_PAGE + y);		//	Set Page
	LcdWriteCommand(x*FONT_WIDTH + 0x41);	//	Set Column

	while(*msg)	PutcR(*msg++);
}


void Putc(byte ch)
{
	byte	i;
	word	ptr;

	ptr	= ch * FONT_WIDTH;
	for(i=0; i<FONT_WIDTH; i++, ptr++)	LcdWriteData(E6x8[ptr]);
}


void PutcR(byte ch)
{
	byte	i;
	word	ptr;

	ptr	= ch * FONT_WIDTH;
	for(i=0; i<FONT_WIDTH; i++, ptr++)	LcdWriteData(~E6x8[ptr]);
}


void Putc_xy(byte x, byte y, byte ch)
{
	byte	i;
	word	ptr;

	if (x < COLUMNS_2)
	{
		LcdCS	= LCD_CS_1;
		LcdWriteCommand(LCD_PAGE + y);		//	Set Page
		LcdWriteCommand(x*FONT_WIDTH + 0x41);	//	Set Column
		ptr	= ch * FONT_WIDTH;
	    for(i=0; i<FONT_WIDTH; i++, ptr++)	LcdWriteData(E6x8[ptr]);
	}
	else if(x == COLUMNS_2)
	{
		LcdCS	= LCD_CS_1;
		LcdWriteCommand(LCD_PAGE + y);
		LcdWriteCommand(x*FONT_WIDTH + 0x41);
		ptr	= ch * FONT_WIDTH;
		for(i=0; i<3; i++, ptr++)	LcdWriteData(E6x8[ptr]);
		LcdCS	= LCD_CS_2;
		LcdWriteCommand(LCD_PAGE + y);
		LcdWriteCommand(0x40);
		for(i=0; i<3; i++, ptr++)	LcdWriteData(E6x8[ptr]);	
	}
	else
	{
		LcdCS	= LCD_CS_2;
		LcdWriteCommand(LCD_PAGE + y);		//	Set Page
		LcdWriteCommand((x-11)*FONT_WIDTH + 0x43);	//	Set Column
		ptr	= ch * FONT_WIDTH;
	    for(i=0; i<FONT_WIDTH; i++, ptr++)	LcdWriteData(E6x8[ptr]);

	}
	LcdWriteData(0x00);

}


void PutcM_xy(byte x, byte y, byte ch)//for the middle of the LCD
{
	byte	i;
	word	ptr;

	if(x == COLUMNS_2)
	{
		LcdCS	= LCD_CS_1;
		LcdWriteCommand(LCD_PAGE + y);
		LcdWriteCommand(x*FONT_WIDTH + 0x41);
		ptr	= ch * FONT_WIDTH;
		for(i=0; i<3; i++, ptr++)	LcdWriteData(E6x8[ptr]);
		LcdCS	= LCD_CS_2;
		LcdWriteCommand(LCD_PAGE + y);
		LcdWriteCommand(0x40);
		for(i=0; i<3; i++, ptr++)	LcdWriteData(E6x8[ptr]);
	}
}



void PutcR_xy(byte x, byte y, byte ch)
{
	byte	i;
	word	ptr;

	if (x < COLUMNS_2)
	{
		LcdCS	= LCD_CS_1;
		LcdWriteCommand(LCD_PAGE + y);		//	Set Page
		LcdWriteCommand(x*FONT_WIDTH + 0x41);	//	Set Column
		ptr	= ch * FONT_WIDTH;
	    for(i=0; i<FONT_WIDTH; i++, ptr++)	LcdWriteData(~E6x8[ptr]);
	}
	else if(x == COLUMNS_2)
	{
		LcdCS	= LCD_CS_1;
		LcdWriteCommand(LCD_PAGE + y);
		LcdWriteCommand(x*FONT_WIDTH + 0x41);
		ptr	= ch * FONT_WIDTH;
		for(i=0; i<3; i++, ptr++)	LcdWriteData(~E6x8[ptr]);
		LcdCS	= LCD_CS_2;
		LcdWriteCommand(LCD_PAGE + y);
		LcdWriteCommand(0x40);
		for(i=0; i<3; i++, ptr++)	LcdWriteData(~E6x8[ptr]);	
	}
	else
	{
		LcdCS	= LCD_CS_2;
		LcdWriteCommand(LCD_PAGE + y);		//	Set Page
		LcdWriteCommand((x-11)*FONT_WIDTH + 0x43);	//	Set Column
		ptr	= ch * FONT_WIDTH;
	    for(i=0; i<FONT_WIDTH; i++, ptr++)	LcdWriteData(~E6x8[ptr]);

	}
	LcdWriteData(0xff);
}


void PutcRM_xy(byte x, byte y, byte ch)//for the middle of the LCD
{
	byte	i;
	word	ptr;

	if(x == COLUMNS_2)
	{
		LcdCS	= LCD_CS_1;
		LcdWriteCommand(LCD_PAGE + y);
		LcdWriteCommand(x*FONT_WIDTH + 0x41);
		ptr	= ch * FONT_WIDTH;
		for(i=0; i<3; i++, ptr++)	LcdWriteData(~E6x8[ptr]);
		LcdCS	= LCD_CS_2;
		LcdWriteCommand(LCD_PAGE + y);
		LcdWriteCommand(0x40);
		for(i=0; i<3; i++, ptr++)	LcdWriteData(~E6x8[ptr]);
	}
}


void LcdClear(void)
{
	byte	x, page;

	PORTA_Bit6	= 0;
	LcdCS	= LCD_CS_12;
	for(page=0; page<NO_PAGES; page++)
	{
		LcdWriteCommand(LCD_PAGE+page);		// Set Page
		LcdWriteCommand(0x40);				// Set Column
		for(x=0; x<64; x++)	LcdWriteData(0x00);
	}
	PORTA_Bit6	= 1;
}


void LcdWriteData(byte data)
{
	while (CheckState());
	bLCD_D_I	= 1;
	bLCD_R_W	= 0;
	LCD_DATA	= data;
	bLCD_E		= 1;
	LcdChipSelect(LcdCS);
	bLCD_E		= 0;
	//LcdChipSelect(0);
}


void LcdWriteCommand(byte command)
{
	while (CheckState());
	bLCD_D_I	= 0;
	bLCD_R_W	= 0;
	LCD_DATA	= command;
	bLCD_E		= 1;
	LcdChipSelect(LcdCS);
	bLCD_E		= 0;
	//LcdChipSelect(0);
}


byte CheckState(void)
{
	byte state;
	LcdChipSelect(LcdCS);
	bLCD_D_I	= 0;
	bLCD_R_W	= 1;
	PORTC		= 0xff;
	DDRC		= 0x00;
	bLCD_E		= 1;
	DDRC		= 0x00;
	state		= PINC;
	bLCD_E		= 0;
	DDRC		= 0xff;
	if (state == 0x80)
	return 1;
	else return 0;
}


void LcdChipSelect(byte chip_select)
{
	bLCD_CS1	= (chip_select & 0x01) ? 0 : 1;
	bLCD_CS2	= (chip_select & 0x02) ? 0 : 1;
}




⌨️ 快捷键说明

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