标题:
单片机+lcd12864清屏显示出错?
[打印本页]
作者:
hqpain
时间:
2019-12-12 11:43
标题:
单片机+lcd12864清屏显示出错?
实验器材:51单片机(实际用的是stc12,51单片机出问题了
)、lcd12864夜晶带中文字库、4x4矩阵键盘、蜂鸣器、led(后面两个器材有问题,可忽略)
问题:昨天研究到深夜lcd显示技术,遇到
显示后
清屏不起作用,前后的数据来回显示。求走过路过的大佬指教指教小弟
,留下大佬光辉的足迹
代码奉上:
lcd12864.h
#ifndef __LCD12864_H
#define __LCD12864_H
#include<stc15.h>
#define xchar unsigned char code
#define uchar unsigned char
#endif
#ifndef uint
#define uint unsigned int
#endif
#define LCD12864_DATAPORT P2
sbit LCD12864_RS = P0^7;
sbit LCD12864_RW = P0^6;
sbit LCD12864_EN = P0^5;
sbit LCD12864_PSB = P0^4;
sbit LCD12864_RST = P0^3;
void LCD12864_Delay1ms(uint c);
uchar LCD12864_Busy();
void LCD12864_WriteCmd(uchar cmd);
void LCD12864_WriteData(uchar dat);
void LCD12864_Init();
void LCD12864_ClearScreen(void);
void LCD12864_SetWindow(uchar x, uchar y);
void LCD12864_Delay1ms(uint c)
{
uchar a,b;
for(; c>0; c--)
{
for(b=199; b>0; b--)
{
for(a=1; a>0; a--);
}
}
}
uchar LCD12864_Busy(void)
{
uchar i = 0;
LCD12864_RS = 0;
LCD12864_RW = 1;
LCD12864_EN = 1;
LCD12864_Delay1ms(1);
while((LCD12864_DATAPORT & 0x80) == 0x80)
{
i++;
if(i > 100)
{
LCD12864_EN = 0;
return 0;
}
}
LCD12864_EN = 0;
return 1;
}
void LCD12864_WriteCmd(uchar cmd)
{
uchar i;
i = 0;
while( LCD12864_Busy() == 0)
{
LCD12864_Delay1ms(1);
i++;
if( i>100)
{
return;
}
}
LCD12864_RS = 0;
LCD12864_RW = 0;
LCD12864_EN = 0;
LCD12864_DATAPORT = cmd;
LCD12864_EN = 1;
LCD12864_Delay1ms(5);
LCD12864_EN = 0;
}
void LCD12864_WriteData(uchar dat)
{
uchar i;
i = 0;
while( LCD12864_Busy() == 0)
{
LCD12864_Delay1ms(1);
i++;
if( i>100)
{
return;
}
}
LCD12864_RS = 1;
LCD12864_RW = 0;
LCD12864_EN = 0;
LCD12864_DATAPORT = dat;
LCD12864_EN = 1;
LCD12864_Delay1ms(5);
LCD12864_EN = 0;
}
#ifdef LCD12864_PICTURE
uchar LCD12864_ReadData(void)
{
uchar i, readValue;
i = 0;
while( LCD12864_Busy() == 0)
{
LCD12864_Delay1ms(1);
i++;
if( i>100)
{
return 0;
}
}
LCD12864_RS = 1;
LCD12864_RW = 1;
LCD12864_EN = 0;
LCD12864_Delay1ms(1);
LCD12864_EN = 1;
LCD12864_Delay1ms(1);
readValue = LCD12864_DATAPORT;
LCD12864_EN = 0;
return readValue;
}
#endif
void LCD12864_Init()
{
LCD12864_PSB = 1;
LCD12864_RST = 1;
LCD12864_WriteCmd(0x30);
LCD12864_WriteCmd(0x0c);
LCD12864_WriteCmd(0x01);
}
#ifdef LCD12864_PICTURE
void LCD12864_ClearScreen(void)
{
uchar i,j;
LCD12864_WriteCmd(0x34);
for(i=0;i<32;i++)
{
LCD12864_WriteCmd(0x80+i);
LCD12864_WriteCmd(0x80);
for(j=0;j<32;j++)
{
LCD12864_WriteData(0x01);
}
}
LCD12864_WriteCmd(0x36);
LCD12864_WriteCmd(0x30);
}
#endif
void LCD12864_SetWindow(uchar x, uchar y)
{
uchar pos;
if(x == 0)
{
x = 0x80;
}
else if(x == 1)
{
x = 0x90;
}
else if(x == 2)
{
x = 0x88;
}
else if(x == 3)
{
x = 0x98;
}
pos = x + y;
LCD12864_WriteCmd(pos);
}
复制代码
main.c
#include<STC15.H>
#include"lcd12864.h"
#include<string.h>
#include<intrins.h>
#include<math.h>
uchar hang;
#define KeyBus P1
sbit fengmingqi=P0^0;
sbit led=P3^0;
xchar mimacuowu[]={"密码错误"};
xchar mimazhengque[]={"密码正确"};
xchar CorpInf1[]={"请设置密码:"};
xchar CorpInf2[]={"请输入密码:"};
xchar CorpInf[]={
"lcd12864清屏前后的shuju"
};
unsigned char keyscan(void)
{
unsigned char temH, temL, key;
KeyBus = 0x0f;
if(KeyBus!=0x0f)
{
temL = KeyBus;
KeyBus = 0xf0;
_nop_();_nop_();_nop_();_nop_();
temH = KeyBus;
switch(temL)
{
case 0x0e: key = 1; break;
case 0x0d: key = 2; break;
case 0x0b: key = 3; break;
case 0x07: key = 4; break;
default: return 0;
}
switch(temH)
{
case 0xe0: return key;break;
case 0xd0: return key + 4;break;
case 0xb0: return key + 8;break;
case 0x70: return key + 12;break;
default: return 0;
}
}return 0;
}
void gpio()
{
P0M1=0;P0M0=0;
P1M1=0;P1M0=0;
P2M1=0;P2M0=0;
P3M1=0;P3M0=0;
P4M1=0;P4M0=0;
P5M1=0;P5M0=0;
P6M1=0;P6M0=0;
P7M1=0;P7M0=0;
}
void lcd(xchar *CorpInf,uchar n)
{
unsigned char i=0,m=0;
switch(n)
{
case 0:LCD12864_SetWindow(0, 0);m=0;break;
case 1:LCD12864_SetWindow(1, 0);m=1;break;
case 2:LCD12864_SetWindow(2, 0);m=2;break;
case 3:LCD12864_SetWindow(3, 0);m=3;break;
}
while(CorpInf[i]!='\0')
{
LCD12864_WriteData(CorpInf[i]);
i++;
switch(m)
{
case 0:if(i==16){LCD12864_SetWindow(1,0);}if(i==32){LCD12864_SetWindow(2,0);}if(i==48){LCD12864_SetWindow(3,0);}break;
case 1:if(i==16)LCD12864_SetWindow(2,0);if(i==32)LCD12864_SetWindow(3,0);break;
case 2:if(i==16)LCD12864_SetWindow(3,0);break;
default:break;
}
}
}
int mima_shuru()
{
uint i,ws,shuju=0;uchar string[]={0,0,0,0,0,0,0,0,0,0,0,0,0};//键盘值,位数,输入的数据,写入的数据
for(ws=0;ws<13;ws++)
{
i=keyscan();
while(i==0); //等待键盘输入
led=0;fengmingqi=1;
i-=1;
if(i==10)break; //10为确定
if(i>10)
{
ws--;continue; //大于10为无效键
}
string[ws]=i*pow(10,ws); //进行10次幂写入数据
}
for(;ws>=0;ws--)
{
shuju=shuju+string[ws]; //计算出每次写入的数据之和
}return shuju; //返回输入的数据
}
void main()
{
uint xh=1,yzm,mima; //对错是否循环,验证码(测试的密码),设定的密码
gpio(); //I/O初始化
// LCD12864_Init(); //lcd初始化
// lcd(CorpInf,0); //lcd显示屏初始界面
// LCD12864_Delay1ms(5000);
//
// //LCD12864_Init();
// //LCD12864_ClearScreen();
// LCD12864_WriteData(0x01);
// LCD12864_Delay1ms(5);
//
//
//
// //LCD12864_Delay1ms(5000); //延时显示5s
//
// //LCD12864_ClearScreen();
//
// lcd(CorpInf1,0);
// LCD12864_Delay1ms(5000);
//设置密码
while(xh)
{
mima=mima_shuru();
lcd(CorpInf2,0);
yzm=mima_shuru(); //测试的密码
if(yzm==mima)
{
lcd(mimazhengque,2);//密码正确,退出循环
xh=2;
}else lcd(mimacuowu,3);//密码错误,继续输入密码
}
//
}
复制代码
IMG_20191212_003029.gif
(2.37 MB, 下载次数: 51)
下载附件
2019-12-12 11:34 上传
作者:
hqpain
时间:
2019-12-12 11:45
LCD12864_Init()函数的注释忘取消了
作者:
hqpain
时间:
2019-12-12 12:13
问题已解觉,是我自己的问题,清屏代码复制成写数据【哭笑】,好像还没审核过,算了当作回忆留在那
欢迎光临 (http://www.51hei.com/bbs/)
Powered by Discuz! X3.1