標(biāo)題:
LCD12864顯示變量,顯示數(shù)字 單片機(jī)程序
[打印本頁(yè)]
作者:
magica1
時(shí)間:
2023-10-20 15:37
標(biāo)題:
LCD12864顯示變量,顯示數(shù)字 單片機(jī)程序
最近想用LCD12864顯示變量,但是商家并沒有提供此函數(shù),在網(wǎng)上找了好久,終于找到了,稍微改進(jìn)了一下
單片機(jī)源程序如下:
lcd12864.c如下
#include "lcd12864.h"
//LCD初始化
void lcd12864_init(void)
{
LCD12864_PSB = 1; //選擇8位或4位并口方式
lcd12864_write_cmd(0x30); //顯示模式設(shè)置,開始要求每次檢測(cè)忙信號(hào)
lcd12864_write_cmd(0x01); //顯示清屏
lcd12864_write_cmd(0x06); //顯示光標(biāo)移動(dòng)設(shè)置
lcd12864_write_cmd(0x0C); //顯示開及光標(biāo)設(shè)置
}
//寫指令
void lcd12864_write_cmd(u8 cmd)
{
LCD12864_RS = 0;
LCD12864_WR = 0;
LCD12864_E = 0;
LCD12864_DATAPORT = cmd;
delay_ms(1);
LCD12864_E = 1;
delay_ms(1);
LCD12864_E = 0;
}
//寫數(shù)據(jù)
void lcd12864_write_data(u8 dat)
{
LCD12864_RS = 1;
LCD12864_WR = 0;
LCD12864_E = 0;
LCD12864_DATAPORT = dat;
delay_ms(1);
LCD12864_E = 1;
delay_ms(1);
LCD12864_E = 0;
}
//清屏
void lcd12864_clear(void)
{
lcd12864_write_cmd(0x01); //顯示清屏
}
/*******************************************************************************
* 函 數(shù) 名 : lcd12864_show_char
* 函數(shù)功能 : LCD12864顯示一個(gè)字符
* 輸 入 : x,y:顯示坐標(biāo),x=0~7,y=0~3;
DData:顯示的字符
* 輸 出 : 無
*******************************************************************************/
void lcd12864_show_char(u8 X, u8 Y, u8 DData)
{
if(Y<=0)Y=0;
if(Y>3)Y=3;
X &= 0x0F; //限制X不能大于16,Y不能大于1
switch (Y) {
case 0:
X |= 0x80;
break;
case 1:
X |= 0x90;
break;
case 2:
X |= 0x88;
break;
case 3:
X |= 0x98;
break;
}
lcd12864_write_cmd(X); //這里不檢測(cè)忙信號(hào),發(fā)送地址碼
lcd12864_write_data(DData);
}
/*******************************************************************************
* 函 數(shù) 名 : lcd12864_show_string
* 函數(shù)功能 : LCD12864顯示字符串
* 輸 入 : x,y:顯示坐標(biāo),x=0~7,y=0~3;
DData:顯示字符串?dāng)?shù)據(jù)
* 輸 出 : 無
*******************************************************************************/
void lcd12864_show_string(u8 X, u8 Y, u8 *DData)
{
u8 ListLength, X2;
ListLength = 0;
X2 = X;
if(Y<=0)Y=0;
if(Y>3)Y=3;
X &= 0x0F; //限制X不能大于16,Y在1-4之內(nèi)
switch (Y) {
case 0:
X2 |= 0x80;
break;
case 1:
X2 |= 0x90;
break;
case 2:
X2 |= 0x88;
break;
case 3:
X2 |= 0x98;
break;
}
lcd12864_write_cmd(X2); //發(fā)送地址碼
while (DData[ListLength] >= 0x20) { //若到達(dá)字串尾則退出
if (X <= 0x0F) { //X坐標(biāo)應(yīng)小于0x0F
lcd12864_write_data(DData[ListLength]);
ListLength++;
X++;
delay_ms(5);
}
}
}
/*******************************************************************************
* 函 數(shù) 名 : lcd12864_show_num
* 函數(shù)功能 : LCD12864顯示數(shù)字
* 輸 入 : x,y:顯示坐標(biāo),x=0~7,y=0~3;
number:顯示字符串?dāng)?shù)據(jù)
Length:要顯示數(shù)字的長(zhǎng)度
* 輸 出 : 無
*******************************************************************************/
void lcd12864_show_num(u8 X, u8 Y, u32 number, u8 Length)
{
u8 array[11];
u8 i;
array[Length] = 0;
for (i = Length; i > 0; i--) {
array[i - 1] = number % 10 + '0';
number /= 10;
}
for (i = 0; i < Length - 1; i++) {
if (array == '0') {
array = ' ';
}
else {
break;
}
}
lcd12864_show_string(X, Y, array);
}
/*******************************************************************************
* 函 數(shù) 名 : lcd12864_show_image
* 函數(shù)功能 : LCD12864顯示圖片
* 輸 入 : DData:要顯示的圖片
陰碼,順向(高位在前),逐行
* 輸 出 : 無
*******************************************************************************/
void lcd12864_show_image(u8 code *DData)
{
u8 x, y, i;
u16 tmp = 0;
for(i = 0; i < 9; i+=8) //分兩屏,上半屏和下半屏
{
for(x=0; x<32; x++)
{
lcd12864_write_cmd(0x34);
lcd12864_write_cmd(0x80+x); //列地址
lcd12864_write_cmd(0x80+i); //行地址,下半屏
for(y=0;y<16;y++)
{
lcd12864_write_data(DData[tmp+y]); //讀取數(shù)據(jù)寫入LCD
}
tmp +=16;
}
}
lcd12864_write_cmd(0x36); //擴(kuò)充功能設(shè)定
lcd12864_write_cmd(0x30);
}
lcd12864.h如下
#ifndef _lcd12864_H
#define _lcd12864_H
#include "public.h"
sbit LCD12864_RS=P2^6;//數(shù)據(jù)命令選擇
sbit LCD12864_WR=P2^5;//讀寫選擇
sbit LCD12864_E=P2^7;//使能信號(hào)
#define LCD12864_DATAPORT P0 //LCD12864數(shù)據(jù)端口定義
sbit LCD12864_PSB=P3^2;//8位或4并口/串口選擇
void lcd12864_init(void); //LCD初始化
void lcd12864_write_cmd(u8 WCLCD); //寫指令
void lcd12864_write_data(u8 WDLCD); //寫數(shù)據(jù)
void lcd12864_clear(void); //清屏
void lcd12864_show_char(u8 X,u8 Y,u8 DData); //顯示一個(gè)字符
void lcd12864_show_string(u8 X, u8 Y, u8 *DData); //顯示一串字符
void lcd12864_show_image(u8 code *DData); //顯示圖形
void lcd12864_show_num(u8 X, u8 Y, u32 number,u8 Length); //顯示一串?dāng)?shù)字
#endif
public.c
#include "public.h"
/*******************************************************************************
* 函 數(shù) 名 : delay_10us
* 函數(shù)功能 : 延時(shí)函數(shù),ten_us=1時(shí),大約延時(shí)10us
* 輸 入 : ten_us
* 輸 出 : 無
*******************************************************************************/
void delay_10us(u16 ten_us)
{
while(ten_us--);
}
/*******************************************************************************
* 函 數(shù) 名 : delay_ms
* 函數(shù)功能 : ms延時(shí)函數(shù),ms=1時(shí),大約延時(shí)1ms
* 輸 入 : ms:ms延時(shí)時(shí)間
* 輸 出 : 無
*******************************************************************************/
void delay_ms(u16 ms)
{
u16 i,j;
for(i=ms;i>0;i--)
for(j=110;j>0;j--);
}
public.h
#ifndef _public_H
#define _public_H
#include "reg52.h"
typedef unsigned int u16; //對(duì)系統(tǒng)默認(rèn)數(shù)據(jù)類型進(jìn)行重定義
typedef unsigned char u8;
typedef unsigned long u32;
void delay_10us(u16 ten_us);
void delay_ms(u16 ms);
#endif
復(fù)制代碼
原理圖: 無
仿真: 無
代碼:
lcd12864.zip
(2.54 KB, 下載次數(shù): 19)
2023-10-20 15:35 上傳
點(diǎn)擊文件名下載附件
下載積分: 黑幣 -5
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1