标题:
LCD12864显示字符或数字程序,显示数字函数可以显示大于0的整数,小数,负数
[打印本页]
作者:
兰小方
时间:
2017-7-29 12:28
标题:
LCD12864显示字符或数字程序,显示数字函数可以显示大于0的整数,小数,负数
程序里面的void Transmit_Display( u8 x, u8 y, float tiggtal, int N)函数只需把参数传进去,x,y是控制显示坐标,最后一个是控制保留小数点后面几位小数,
自带数组显示函数和字符串显示函数
单片机源程序如下:
#include "lcd12864.h"
#include "delay.h"
#include "GPIO_Int.h"
#include "math.h"
#include "usart.h"
/*********************************************
数据读写端口
*********************************************/
void Lcd_Send_Date(u8 date)
{
u8 i;
LCD_RS=1;
for(i=0;i<8;i++)
{
if((date<<i)&0x80)
{
LCD_RW=1;
}
else
{
LCD_RW=0;
}
LCD_EN=0;
LCD_EN=1;
}
}
/***********************************************
写入命令
***********************************************/
void Write_Cmd(u8 com)
{
Lcd_Send_Date(0xf8);
Lcd_Send_Date(0xf0&com);
Lcd_Send_Date(com<<4);
delay_ms(1);
}
/***********************************************
写入数据
***********************************************/
void Write_Data(u8 dat)
{
Lcd_Send_Date(0xfa);
Lcd_Send_Date(dat&0xf0);
Lcd_Send_Date(dat<<4);
delay_ms(1);
}
/***********************************************
液晶屏初始化
***********************************************/
void Init_ST7920()
{
delay_ms(40); //大于40MS的延时程序
Write_Cmd(0x30); //选择基本指令集
Write_Cmd(0x40); //对CGRAM第一个自定义字符操作,
delay_us(100); //延时大于100us
Write_Cmd(0x30); //选择8bit数据流
delay_us(40); //延时大于37us
Write_Cmd(0x0c); //开显示(无游标、不反白)
delay_us(100); //延时大于100us
Write_Cmd(0x01); //清除显示,并且设定地址指针为00H
delay_ms(15); //延时大于10ms
Write_Cmd(0x06); //指定在资料的读取及写入时,设定游标的移动方向及指定显示的移位,光标从右向左加1位移动
delay_us(100); //延时大于100us
}
void Display_Count(u8 x,u8 y,u8 data)
{
switch(data)
{
case 0:LCD_DisplayInit(x,y,"零");
break;
case 1:LCD_DisplayInit(x,y,"一");
break;
case 2:LCD_DisplayInit(x,y,"二");
break;
case 3:LCD_DisplayInit(x,y,"三");
break;
case 4:LCD_DisplayInit(x,y,"四");
break;
default: break;
}
}
/***********************************************
显示字符串
x:横坐标值,范围0~8
y:纵坐标值,范围1~4
***********************************************/
void LCD_Struct_Display( LCD_DisplayTypedef *Display_Structure )
{
switch(Display_Structure->ordinate)
{
case 1: Write_Cmd(0x80+( (Display_Structure->abscissa) -1) );break;
case 2: Write_Cmd(0x90+( (Display_Structure->abscissa) -1) );break;
case 3: Write_Cmd(0x88+( (Display_Structure->abscissa) -1) );break;
case 4: Write_Cmd(0x98+( (Display_Structure->abscissa) -1) );break;
default: break;
}
while(*( Display_Structure->character_string ) != '\0')
{
if((*( Display_Structure->character_string )) == 'n')
{
USART_RX_STA = 0;
break;
}
Write_Data( (*( Display_Structure->character_string )) );
( Display_Structure->character_string )++;
delay_us(50);
}
}
/********************************************************
普通在LCD显示字符
*******************************************************/
void LCD_DisplayInit(u8 x, u8 y,u8 *spr)
{
LCD_DisplayTypedef LCD_DisplayStructure;
LCD_DisplayStructure.abscissa = x;
LCD_DisplayStructure.ordinate = y;
LCD_DisplayStructure.character_string = spr;
LCD_Struct_Display(&LCD_DisplayStructure);
}
/***********************************************
显示串口接收的字符串
向串口发送x,y,"字符串"
***********************************************/
void USART1_ReceiveInit(u8 *USART1_Buffer)
{
LCD_DisplayTypedef LCD_DisplayStructure;
LCD_DisplayStructure.abscissa = (*USART1_Buffer - 48);
LCD_DisplayStructure.ordinate = (*(USART1_Buffer+2) - 48);
LCD_DisplayStructure.character_string = USART1_Buffer+4;
LCD_Struct_Display(&LCD_DisplayStructure);
}
/****************************************************************
********进行数字转换输出到LCD显*********
******************************************************************/
/***************************************************************
计算并返回小数点的位置
***************************************************************/
int Radix_point(float value)
{
int medium_int = 0, count_RP = 0;
medium_int = (int)value;
if(value == 0)
{
return count_RP;
}
if(value < 0)
{
value = (-value);
}
if(medium_int > 0)
{
while(medium_int)
{
count_RP++;
medium_int /= 10;
}
return count_RP;
}
else
{
while(!medium_int)
{
value *= 10;
medium_int = (int)(value);
count_RP++;
}
return count_RP;
}
}
/*********************************************************
把FLOAT型书转换成INT型返回出去
***********************************************************/
int Transform_val(float val, int N)
{
int value = 0;
if(val < 0)
{
val = (-val);
}
if(val >= 1 )
{
value = (int)(val)*pow(10,N) + (int)((val - (int)(val))*pow(10,N));
}
else
if(1>val > 0)
{
value = (int)((val)*pow(10,N));
}
return value;
}
/******************************************************************
数字显示函数
*******************************************************************/
void Display_number(int channel_first, float channel_scond)
{
int current=1, val, count = 1, i = 0, j = 0;
float medium_float;
j = (Radix_point(channel_scond)) - 1;
val = channel_first;
if(channel_scond < 0)
{
medium_float = (-channel_scond);
}
else
{
medium_float = channel_scond;
}
while( (val>=10) )
{
val /= 10;
count *= 10;
}
if(channel_scond < 0)
{
Write_Data('-');
}
if(medium_float >= 1)
{
while(count)
{
if(Radix_point(medium_float) == i)
{
Write_Data('.');
current = 0;
i = 0;
}
else
{
Write_Data('0' + (channel_first/count%10));
count /=10;
if(current)
i++;
}
}
}
else
if(medium_float > 0)
{
Write_Data('0');
Write_Data('.');
while(j > 0)
{
Write_Data('0');
j--;
}
while(count)
{
Write_Data('0' + (channel_first/count%10));
count /=10;
}
}
else
if(medium_float == 0)
{
Write_Data('0');
}
}
/***************************************************************
数字传送显示
**************************************************************/
void Transmit_Display( u8 x, u8 y, float tiggtal, int N)
{
switch(y)
{
case 1: Write_Cmd(0x80+ (x -1)) ;break;
case 2: Write_Cmd(0x90+ (x -1)) ;break;
case 3: Write_Cmd(0x88+ (x -1)) ;break;
case 4: Write_Cmd(0x98+ (x -1)) ;break;
default: break;
}
Display_number(Transform_val(tiggtal,N),tiggtal);
}
复制代码
LCD12864.h
#ifndef _LCD12864_H_
#define _LCD12864_H_
#include "sys.h"
/******定义一个结构体******/
typedef struct
{
u8 USART1_DiaplayEnable;
u8 abscissa; //x
u8 ordinate; //y
u8 *character_string;
}LCD_DisplayTypedef;
/**************************/
void Write_Cmd(u8 com);
void Write_Data(u8 dat);
void Init_ST7920(void);
void Display_Count(u8 x,u8 y,u8 data);
/* 结构体显示 */
void LCD_DisplayInit(u8 x, u8 y,u8 *spr);
void LCD_Struct_Display( LCD_DisplayTypedef *Display_Structure );
/* 串口接收显示 */
void USART1_ReceiveInit(u8 *USART1_Buffer);
/* 数字传送显示 */
void Transmit_Display( u8 x, u8 y, float tiggtal, int N);
#endif
复制代码
作者:
danpianji80c51
时间:
2017-7-30 11:09
谢谢楼主分享
作者:
51hww34
时间:
2017-7-30 20:57
学习学习 感谢分享
作者:
yilantinglengyu
时间:
2018-4-15 12:04
xaingxiazai
作者:
ppokwsx
时间:
2018-5-24 18:05
谢谢分享
作者:
大海的胸怀
时间:
2021-6-22 14:08
不能用啊
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1