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

📄 lcd.c

📁 8051单片机访问外围器件的代码
💻 C
字号:
/**
 * @file
 * Driver for the LCD module
 * 
 * LCD.c
 * Copyright(C) 2007 Agate Logic Corporation. All rights reserved.
 * This file is licensed under the terms of Agate Logic SDK License Agreement.
 * 
 * @author bxu@agatelogic.com.cn
*/

#define LCD_GLOBALS	///< define macro "LCD_GLOBALS" as empty

#include <AG1F1.h>
#include "lcd.h"
#include <intrins.h>
#define ALLDELAY _nop_();_nop_();_nop_();_nop_();

/**
 * @brief write data to the command register of LCDM.
 * 
 * The Function write the specified data to the command register of LCDM.
 * 
 * @param c The data to write.
 * 
 * @return N/A
*/
void wr_code(uchar c)
{
  uchar uctmp;
  LCD_RS=0;
  ALLDELAY
  LCD_RW=0;
  ALLDELAY
  LCD_DATA=c;
  ALLDELAY
  LCD_EN=1;
  ALLDELAY
  LCD_EN=0;
  ALLDELAY
  LCD_RS=0;
  ALLDELAY
  LCD_RW=1;
  ALLDELAY
loop1:
  LCD_EN=1;
  ALLDELAY
  uctmp=LCD_DATA;
  ALLDELAY
  LCD_EN=0;
  ALLDELAY
  if((uctmp&0x80)==0x80)
  {
    goto loop1;
  }
  ALLDELAY
}

/**
 * @brief write data to the data register of LCDM.
 * 
 * The Function write the specified data to the data register of LCDM.
 * 
 * @param d The data to write.
 * 
 * @return N/A
*/
void wr_data(uchar d)
{
  uchar uctmp;
  LCD_RS=1;
  ALLDELAY
  LCD_RW=0;
  ALLDELAY
  LCD_DATA=d;
  ALLDELAY
  LCD_EN=1;
  ALLDELAY
  LCD_EN=0;
  ALLDELAY
  LCD_RS=0;
  ALLDELAY
  LCD_RW=1;
  ALLDELAY
loop1:
  LCD_EN=1;
  ALLDELAY
  uctmp=LCD_DATA;
  ALLDELAY
  LCD_EN=0;
  ALLDELAY
  if((uctmp&0x80)==0x80)
  {
    goto loop1;
  }
  ALLDELAY
}

/**
 * @brief Clear display of LCDM.
 * 
 * The Function clear display of LCDM.
 * 
 * @return N/A
*/
void cls_lcd()
{
  wr_code(0x01);
  glb_col=0;
  glb_row=0;
}

/**
 * @brief Initialize the LCD module.
 * 
 * The Function executes the initialization for the LCD Module.
 * 
 * @return N/A
*/
void initial_lcd()
{
  wr_code(0x30);
  wr_code(0x30);
  wr_code(0x30);
  wr_code(0x38);
  cls_lcd();
  wr_code(0x0c);
  wr_code(0x06);
}

/**
 * @brief write data to the CGRAM of LCDM.
 * 
 * The Function write the specified data array to the CGRAM of LCDM.
 * 
 * @param cg_code The address of CGRAM(0 based) to write to.
 * @param cg_data The data array to write.
 * @param length The length of the data array.
 * 
 * @return N/A
*/
void cg_write(uchar cg_code, uchar* cg_data, uchar length)
{
	uchar addr;
	if(cg_code>CG_MAX_CODE)return;
	cg_code<<=3;
	addr=0x40+cg_code;
	wr_code(addr);
	for(addr=0;addr<length;addr++,cg_data++)
	{
		wr_data(*cg_data);
	}
}

/**
 * @brief Set the cursor pos.
 * 
 * The Function set the cursor to the specified position.
 * 
 * @param row The row number of the cursor position to set.
 * @param col The column number of the cursor position to set.
 * 
 * @return N/A
*/
void set_cursor(uchar row, uchar col)
{
	uchar addr;
	if((glb_col==col)&&(glb_row==row))return;
	glb_col=col;
	glb_row=row;
	if(glb_col)
		addr=glb_row+0x40;
	else
		addr=glb_row;
	addr|=0x80;
	wr_code(addr);
}

/**
 * @brief Update the cursor to the next position.
 * 
 * The Function increase the cursor position to the next position where the next character is to be showed at.
 * 
 * @return N/A
*/
void update_cursor()
{
	uchar addr;
	glb_row++;
	if(glb_row>MAX_ROW)
	{
		glb_row=0;
		glb_col++;
		if(glb_col>MAX_COL)
		{
			glb_col=0;
		}
		if(glb_col)
			addr=glb_row+0x40;
		else
			addr=glb_row;
		addr|=0x80;
		wr_code(addr);
	}
}

/**
 * @brief Display a character.
 * 
 * The Function display the specified character at the current position.
 * 
 * @param thechar The character to display.
 * 
 * @return N/A
*/
void display_char(uchar thechar)
{
	wr_data(thechar);
	update_cursor();
}

/**
 * @brief Display a character at the specified positon.
 * 
 * The Function display the specified character at the specified position.
 * 
 * @param row The row where to display at.
 * @param col The column where to display at.
 * @param thechar The character to display.
 * 
 * @return N/A
*/
void display_char_at(uchar row, uchar col, uchar thechar)
{
	if(row>MAX_ROW)return;
	if(col>MAX_COL)return;
	set_cursor(row,col);
	display_char(thechar);
}

/**
 * @brief Display a character in hexadecimal format.
 * 
 * The Function display the specified character in hexadecimal format at the current position.
 * 
 * @param thehexchar The character to display.
 * 
 * @return N/A
*/
void display_hex_char(uchar thehexchar)
{
	uchar uctmp;
	uctmp=(thehexchar>>4)&0xf;
	if(uctmp>9)
		uctmp+='A'-10;
	else
		uctmp+='0';
	display_char(uctmp);
	uctmp=thehexchar&0xf;
	if(uctmp>9)
		uctmp+='A'-10;
	else
		uctmp+='0';
	display_char(uctmp);
}

/**
 * @brief Display a character in hexadecimal format without display the prefix 0.
 * 
 * The Function display the specified character in hexadecimal format without display
 * the prefix 0 at the current position.
 * That is mean if the character less than 0x10 only one hex number will be displayed, 
 * for example, 0xF is displayed as "F" but not "0F".
 * 
 * @param thehexchar The character to display.
 * 
 * @return N/A
*/
void display_hex_char_noprefix(uchar thehexchar)
{
	uchar uctmp;
	uctmp=(thehexchar>>4)&0xf;
	if(uctmp>9)
		uctmp+='A'-10;
	else
		uctmp+='0';
	if(uctmp=='0')
		;
	else
		display_char(uctmp);
	uctmp=thehexchar&0xf;
	if(uctmp>9)
		uctmp+='A'-10;
	else
		uctmp+='0';
	display_char(uctmp);
}

/**
 * @brief Display a character in time format.
 * 
 * The Function display the specified character in time format at the current position.
 * 
 * @param thetime The character to display.
 * 
 * @return N/A
*/
void display_time(uchar thetime)
{
	uchar uctmp;
	uctmp=(thetime>>4)&0xf;
	uctmp+='0';
	display_char(uctmp);
	uctmp=thetime&0xf;
	uctmp+='0';
	display_char(uctmp);
}

/**
 * @brief Display a character in time format at the specified position.
 * 
 * The Function display the specified character in time format at the specified position.
 * 
 * @param row The row where to display at.
 * @param col The column where to display at.
 * @param thetime The character to display.
 * 
 * @return N/A
*/
void display_time_at(uchar row, uchar col, uchar thetime)
{
	if(row>MAX_ROW)return;
	if(col>MAX_COL)return;
	set_cursor(row,col);
	display_time(thetime);
}

/**
 * @brief Display a string.
 * 
 * The Function display the specified string at the current position.
 * 
 * @param thestring The string to display.
 * 
 * @return N/A
*/
void display_string(uchar* thestring)
{
	for(;*thestring!=0;thestring++)
	{
		display_char(*thestring);
	}
}

/**
 * @brief Display a string at the specified position.
 * 
 * The Function display the specified string at the specified position.
 * 
 * @param row The row where to display at.
 * @param col The column where to display at.
 * @param thestring The string to display.
 * 
 * @return N/A
*/
void display_string_at(uchar row, uchar col, uchar* thestring)
{
	if(row>MAX_ROW)return;
	if(col>MAX_COL)return;
	set_cursor(row,col);
	for(;*thestring!=0;thestring++)
	{
		display_char(*thestring);
	}
}

/**
 * @brief Display a string length specified at the specified position.
 * 
 * The Function display the specified string at the specified position,
 * and the length of the string is specified.
 * 
 * @param row The row where to display at.
 * @param col The column where to display at.
 * @param thestring The string to display.
 * @param length The string length to display.
 * 
 * @return N/A
*/
void display_string_at_length(uchar row, uchar col, uchar* thestring, uchar length)
{
	uchar uctmp;
	if(row>MAX_ROW)return;
	if(col>MAX_COL)return;
	set_cursor(row,col);
	for(uctmp=0;uctmp<length;uctmp++,thestring++)
	{
		display_char(*thestring);
	}
}

⌨️ 快捷键说明

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