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

📄 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
//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" 

//local functions declaration
void Set_RS(INT8U RS);
void Set_RW(INT8U RW);
void Set_EN(INT8U EN);
void Set_Data(INT8U data);
void LCD_Write_Command(INT8U command,INT8U status);
void LCD_Write_Data(INT8U data,INT8U status);
void Delay_ms(INT8U time);
INT8U LCD_Read_BF(void);
INT8U LCD_Read_Data(INT8U status);

//******************************************************************************
// Function    : Set_RS
// Input       : RS - RS Control Line Level
// Output      : none
// Description : LCD(1602) Low Level Interface Function
//******************************************************************************
void Set_RS(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_RW
// Input       : data - Data Line Level
// Output      : none
// Description : Low Level Interface Function
//******************************************************************************
void Set_Data(INT8U data)
{
	PORTA  = data;
}

//******************************************************************************
// Function    : LCD_Read_BF
// Input       : none
// Output      : 0 = LCD Controller is Free,0x80 = LCD Busy
// Description : Read LCD Controller Status
//******************************************************************************
INT8U LCD_Read_BF(void)
{
	INT8U status;
	DDRA = 0x00;
	Set_EN(0);
	Set_RS(0);
	Set_RW(1);
	Set_EN(1);
	NOP();
	status = PINA;
	Set_EN(0);
	DDRA = 0xFF;
	return(status & 0x80);
}

//******************************************************************************
// Function    : LCD_Write_Command
// Input       : command - Command to be written to LCD
//               status  - Detect LCD Status when 1 
// Output      : none
// Description : Write Command to LCD
//******************************************************************************
void LCD_Write_Command(INT8U command,INT8U status)
{
	if(status == 1)
	{
		while(LCD_Read_BF() != 0) {;}
	}
	Set_EN(0);
	Set_RS(0);
	Set_RW(0);
	Set_Data(command);
	Set_EN(1);
	NOP();
	NOP();
	Set_EN(0);
}

//******************************************************************************
// Function    : LCD_Write_Data
// Input       : data   - Data to be written to LCD
//               status - Detect LCD Status when 1 
// Output      : none
// Description : Write Data to LCD
//******************************************************************************
void LCD_Write_Data(INT8U data,INT8U status)
{
	if(status == 1)
	{
		while(LCD_Read_BF() != 0) {;}
	}
	Set_EN(0);
	Set_RS(1);
	Set_RW(0);
	Set_Data(data);
	Set_EN(1);
	NOP();
	NOP();
	Set_EN(0);
}

//******************************************************************************
// Function    : LCD_Read_Data
// Input       : status - Detect LCD Status when 1 
// Output      : Data read from LCD
// Description : Read Data from LCD
//******************************************************************************
INT8U LCD_Read_Data(INT8U status)
{
	INT8U data;
	if(status == 1)
	{
		while(LCD_Read_BF() != 0) {;}
	}
	DDRA = 0x00;
	Set_EN(0);
	Set_RS(1);
	Set_RW(1);
	Set_EN(1);
	NOP();
	NOP();
	data = PINA;
	Set_EN(0);
	DDRA = 0xFF;
	return(data);
}

//******************************************************************************
// Function    : LCD_Reset
// Input       : none 
// Output      : none
// Description : LCD software reset,must execute according to time sequence
//******************************************************************************
void LCD_Reset(void)
{
	Delay_ms(15);
	LCD_Write_Command(0x38,0);
	Delay_ms(5);
	LCD_Write_Command(0x38,0);
	Delay_ms(5);
	LCD_Write_Command(0x38,0);
	LCD_Write_Command(0x38,1);
	LCD_Write_Command(0x08,1);
	LCD_Write_Command(0x01,1);
	LCD_Write_Command(0x06,1);
	LCD_Write_Command(0x0C,1);
}

//******************************************************************************
// Function    : LCD_Show_Char
// Input       : x - Row of LCD(0 to 1);
//               y - Column of LCD(0 to 15);
//               code - Code of Char  
// Output      : none
// Description : Display a Char at Setting Row and Column
//******************************************************************************
void LCD_Show_Char(INT8U x,INT8U y,INT8U code)
{
	INT8U address;
	if((x < 2) && (y < 16))
	{
		address = 0x40 * x + y;
		LCD_Write_Command(0x80 | address,1);
		LCD_Write_Data(code,1);
	}
}

//******************************************************************************
// Function    : LCD_Show_String
// Input       : x - Row of LCD(0 to 1);
//               y - Column of LCD(0 to 15);
//               pdata - Pointer to a String  
// Output      : none
// Description : Display a String from at Setting Row and Column
//******************************************************************************
void LCD_Show_String(INT8U x,INT8U y,INT8U *pdata)
{
	INT8U x_address = x;
	INT8U y_address = y;
	while(*pdata != '\0')
	{
		LCD_Show_Char(x_address,y_address,*pdata++);
		y_address++;
		if((x_address == 0) && (y_address >= 16))
		{ 
			x_address = 1;
			y_address = 0;
		}
		else if((x_address == 1) && (y_address >= 16))
		{
			return;
		}
	}
}

//******************************************************************************
// Function    : Delay_ms
// Input       : time - Delay Time  
// Output      : none
// Description : Inaccurate ms Level Delay for Software Reset of LCD
//******************************************************************************
void Delay_ms(INT8U time)
{
	INT16U cnt = 0;
	while(time--)
	{
		for(cnt = 0;cnt < 5000;cnt++)
		{
			NOP();
		}
	}
}

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

⌨️ 快捷键说明

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