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

📄 disp_drive.c

📁 在keil和proteus中实现两片51单片机的通信。。。
💻 C
字号:
/*
****************************************************

		显示

****************************************************
*/
#include<rtx51tny.h>
#include <reg52.H>
#include "Typedef.H"
#include "Disp_drive.h"
#include "UserDef.H"


/* 端口定义 */
#define DISPDATAPORT	P0		//数据端口
sbit Pin_RS = P2^5;				//指令、数据选择(0=指令;1=数据)
sbit Pin_RW = P2^6;				//读写选择(0=写,1=读)
sbit Pin_E = P2^7;				//锁存(下降沿有效)


/*  全局变量 */
static INT8U idata DispBuffer[2][MAXDISPLEN];		//显示内容缓冲区
static INT8U idata DispPro[2][MAXDISPLEN];		//显示属性缓冲区
//显示属性定义
#define PRO_BLINK	(1<<1)	//闪烁


/***************************************

	LCD忙等待

****************************************/
void Busy(void)
	{
	INT8U bf;

	Pin_E = 0;
	DISPDATAPORT = 0xFF;	//使端口为输入状态
	Pin_RW = 1;				//读
	Pin_RS = 0;				//指令

	while (1)
		{
		Pin_E = 1;
		bf = DISPDATAPORT;
		Pin_E = 0;
		if ((bf & 0x80) == 0)
			break;
		}
	}


/***************************************

	LCD数据写

****************************************/
void LCD_Data(INT8U Data)
	{
	Busy();
	
	Pin_E = 0;
	Pin_RW = 0;		//写
	Pin_RS = 1;		//数据

	Pin_E = 1;

	DISPDATAPORT = Data;

	Pin_E = 0;
	}

/***************************************

	LCD命令写

****************************************/
void LCD_Cmd(INT8U cmd)
	{
	Busy();
	
	Pin_E = 0;
	Pin_RW = 0;		//写
	Pin_RS = 0;		//指令

	Pin_E = 1;

	DISPDATAPORT = cmd;

	Pin_E = 0;
	}

/***************************************

	显示扫描刷新程序

****************************************/
void DispRef(void)
	{
	static INT8U BlinkCnt = 0;		//闪烁显示计数器
	static BOOLEAN BlinkStatus = 0;	//当前闪烁状态
	INT8U n;

	/* 计算显示闪烁状态 */
	BlinkCnt ++;   							
	BlinkCnt %= T_BLINK;
	if (BlinkCnt == 0)
		BlinkStatus = !BlinkStatus;

	/* 若当前闪烁状态=0且当前显示位允许闪烁 */


	/* 输出行0 */
	LCD_Cmd(0x80);
	for (n=0;n<MAXDISPLEN;n++)
		{
		if ((BlinkStatus == 0) && ((DispPro[0][n] & PRO_BLINK) != 0))
			LCD_Data(' ');
		else
			LCD_Data(DispBuffer[0][n]);
		}

	/* 输出行1 */
	LCD_Cmd(0xC0);
	for (n=0;n<MAXDISPLEN;n++)
		{
		if ((BlinkStatus == 0) && ((DispPro[1][n] & PRO_BLINK) != 0))
			LCD_Data(' ');
		else
			LCD_Data(DispBuffer[1][n]);
		}
	}


/***************************************

	获取整数的长度	

****************************************/
 INT8U GetIntLen(INT32U val)
	{
	INT8U len;

	len = 0;
	while (val != 0)
		{
		val /= 10;
		len ++;
		}

	if (len == 0)
		len = 1;

	return len;
	}



/***************************************

	显示一个整数
	入口参数:整数数值,起始显示位置,长度

****************************************/
void DispInt(INT32U val,INT8U row,INT8U col,INT8U len)
	{
	INT8U n,end;

   	end = col + len - 1;
	for (n=0;n<len;n++)
		{
		DispBuffer[row][end] = val % 10 + '0';
	
		DispPro[0][end] = 0;
		val /= 10;
		end --;
		}
	}


/***************************************

	显示一个整数(可闪烁)
	入口参数:整数数值,起始显示位置,长度

****************************************/
void DispInt_blink(INT32U val,INT8U row,INT8U col,INT8U len,BOOLEAN blink)
	{
	INT8U n,end;

   	end = col + len - 1;
	for (n=0;n<len;n++)
		{
		DispBuffer[row][end] = val % 10 + '0';

	if (blink == TRUE)
		DispPro[row][col+n] = PRO_BLINK;
	else
		DispPro[row][col+n] = 0; 
	//	DispPro[0][end] = 0;
		val /= 10;
		end --;
		}
	}






/***************************************

	显示一个字符
	入口参数:字符,显示位置,是否闪烁

****************************************/
void DispChr(INT8U cv,INT8U row,INT8U col,BOOLEAN blink)
	{
	DispBuffer[row][col] = cv;
	if (blink == TRUE)
		DispPro[row][col] = PRO_BLINK;
	else
		DispPro[row][col] = 0;
	}

/***************************************

	显示一个字符串
	入口参数:字符串指针,显示位置,是否闪烁

****************************************/
void DispStr(INT8U *str,INT8U row,INT8U col,BOOLEAN blink)
	{
	while ((*str)!=0)
		{
		DispBuffer[row][col] = *str;
		if (blink == TRUE)
			DispPro[row][col] = PRO_BLINK;
		else
			DispPro[row][col] = 0;

		str ++;
		col ++;
		}
	}

/***************************************

	显示一个浮点数
	注:无超显示范围判断
		使用全部的显示区域

****************************************/
void DispFloat(float val,INT8U row,INT8U col,INT8U Len)
	{
	INT8U IntLen,n;
	INT32U iv,ivf;


	/* 符号位处理 */
	if (val < 0)
		{
		DispBuffer[row][col] = '-';
		val = -val;
		col ++;
		Len --;
		}
		
	/* 整数位数计算 */
	IntLen = GetIntLen((INT32U)val);	

	/* 整数部分 */
	iv = (INT32U)val;

	/*  */
	for (n=0;n<(Len-IntLen);n++)
		{
		val *= 10;
		}
	ivf = (INT32U)val;
	ivf += 5;
	ivf /= 10;

	/* 显示整数部分 */
	DispInt(iv,row,col, IntLen);
	/* 显示小数点 */
	DispChr('.',row,col+IntLen ,FALSE);
	/* 显示小数部分 */
	for (n=0;n<(Len-IntLen-1);n++)
		{
		DispInt(ivf%10 , row , col + Len -n -1, 1);
		ivf /= 10;
		}

	/* 计算实际有效位数,去掉小数部分尾数的所有0 */
	n = col + Len - 1;
	while (n >= IntLen)
		{
		if (DispBuffer[row][n] != '0')
			{
			break;
			}
		DispBuffer[row][n] = ' ';
		n --;
		}
	}

/***************************************

	显示清屏

****************************************/
void DispCls(void)
	{
	INT8U col,row;

	for (row=0;row<2;row++)
		{
		for (col=0;col<MAXDISPLEN;col++)
			{
			DispBuffer[row][col] = ' ';
			DispPro[row][col] = 0;
			}
		}
	}



/***************************************

	屏初始化

****************************************/
void DispInit(void)
	{

	LCD_Cmd(0x38);		//Function Set
						//data bus length = 1 (8 bits)
						//display lines = 1 (2 lines)
						//character font = 0(5x7 Dot Font)
		
	LCD_Cmd(0x06);		//Entry Mode Set
						//Sets DD RAM counter to increment

	LCD_Cmd(0x0C);		//Sets Display 
					
	LCD_Cmd(0x01);		//Clears Display
						//returns cursor to the Home Position (Address 00)
	}

⌨️ 快捷键说明

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