这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 嵌入式开发 » MCU » 12864的问题求助,谢谢

共6条 1/1 1 跳转至

12864的问题求助,谢谢

菜鸟
2017-06-06 15:14:09     打赏
感谢您能抽出时间来看一下愚生的问题。查阅了些资料,依然不知问题所在。可能是在下比较愚笨,无法看清本质,还请各位大侠指点迷津,在下感激不尽!这是一个12864显示的小函数。如有陋处,还请多多包涵,本人小白一枚。
问题如下:
我的想法是,按某个键一下,12864上的显示就会在dis12[][10]这个数组里面一个一个显示。但不知道为什么,只能显示51%,在按的话,就成了50%了,且后面都是50%。就酱。
代码不美,将就的看吧。控制的按键的部分已经红字标出。
该链接附有视频,在愚生的QQ空间相册里,12864的那个,密码12864。
https://user.qzone.qq.com/183137 ... eNPCldR1a6RwLVnUdc_


步入正题,代码如下:
#include
#include
#include
#define uchar unsigned char
#define uint unsigned int
#define LCD_data P0
sbit col1=P1^7;   //列端口定义
sbit col2=P1^6;
sbit col3=P2^0;
sbit col4=P2^1;
sbit LCD_EN=P3^4;
sbit LCD_RS=P3^5;
sbit LCD_RW=P3^6;
sbit LCD_PSB=P3^7;
uchar code dis0[]={"****智能控制****"};
uchar code dis1[]={"当前功能显示区"};
uchar code dis2[]={"当前水温:"};
uchar code dis3[]={"当前水位比: 50%"};
uchar code dis4[]={"按S17 开始上水"};
uchar code dis5[]={"目标水位比: 50%"};
uchar code dis6[]={"按 S5 停止上水"};
uchar code dis7[]={"上水中..."};
uchar code dis8[]={"按S18 开始加热"};
uchar code dis9[]={"目标水温:"};
uchar code dis10[]={"按 S6 停止加热"};
uchar code dis11[]={"加热中..."};
uchar code dis12[][10]={"51%","52%","53%","54%","55%","56%","57%","58%","59%","60%", //0-9
"61%","62%","63%","64%","65%","66%","67%","68%","69%","70%", //10-19
"71%","72%","73%","74%","75%","76%","77%","78%","79%","80%", //20-29
"81%","82%","83%","84%","85%","86%","87%","88%","89%","90%", //30-39
"91%","92%","93%","94%","95%","96%","97%","98%","99%","100%"};//40-49
uchar code dis13[][10]={"49%","48%","47%","46%","45%","44%","43%","42%","41%","40%",//0-9
"39%","38%","37%","36%","35%","34%","33%","32%","31%","30%"};//10-19
/*uchar code dis12[]={0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
0x00,0x00,0x00,0x00};//清行*/
void delay1ms(uint);
void dekay(uchar);
void keyscan();
void write_cmd(uchar cmd);
void write_dat(uchar dat);
void lcd_pos(uchar X,uchar Y);
void lcd_init();
uchar num,i,m,n,temp;
uint counter,a,counter1,b;
uint add0;
void main()//固定界面显示
{
//uint add0,add1,minus0,minus1;

delay1ms(10);
lcd_init();


lcd_pos(0,0);//智能控制
i=0;
while(dis0!='\0')
{
write_dat(dis0);
i++;
}


lcd_pos(1,0);//当前水温
i=0;
while(dis2!='\0')
{
write_dat(dis2);
i++;
}


lcd_pos(2,0);//当前水位比
i=0;
while(dis3!='\0')
{
write_dat(dis3);
i++;
}


lcd_pos(3,0);//当前功能显示区
i=0;
while(dis1!='\0')
{
write_dat(dis1);
i++;
}


delay1ms(10);
while(1)
{
//P1=0xff;
//P2=0xff;
//P3=0xff;
keyscan();
}
}
void delay1ms(uint x)//延时
{
uint i,j;
for(j=0;j for(i=0;i<110;i++);
}
void delay(uchar i)//延时
{
uchar j,k;
for(j=i;j>0;j--)
for(k=125;k>0;k--);
}
void keyscan()
{
col1=1;col2=1;col3=0;col4=1;//上水按键区
temp=P2;
temp=P2&0x3f;
if(col3==0)
{
delay(80);
if(temp!=0x3f)
{
temp=P2&0x3c;
switch(temp)
{
case 0x1c:        //进入上水功能界面
counter++;
a=counter%2;
switch(a)
{
case 1:         //第一功能键,显示上水界面

write_cmd(0x01);
delay1ms(5);

delay(20);
lcd_pos(0,0);
i=0;
while(dis0!='\0')
{
write_dat(dis0);
i++;
}

delay(20);
lcd_pos(1,0);
i=0;
while(dis4!='\0')
{
write_dat(dis4);
i++;
}

delay(20);
lcd_pos(2,0);
i=0;
while(dis5!='\0')
{
write_dat(dis5);
i++;
}

delay(20);
lcd_pos(3,0);
i=0;
while(dis1!='\0')
{
write_dat(dis1);
i++;
}
break;

case 0:           //第二功能键,开始上水

write_cmd(0x01);
delay1ms(5);

delay(20);
lcd_pos(0,0);
i=0;
while(dis0!='\0')
{
write_dat(dis0);
i++;
}

delay(20);
lcd_pos(1,0);
i=0;
while(dis6!='\0')
{
write_dat(dis6);
i++;
}

delay(20);
lcd_pos(2,0);
i=0;
while(dis3!='\0')
{
write_dat(dis3);
i++;
}

delay(20);
lcd_pos(3,0);
i=0;
while(dis7!='\0')
{
write_dat(dis7);
i++;
}
break;
}
break;

case 0x2c:        //增加水位比

add0++;
if(add0==1)
{
write_cmd(0x01);
delay1ms(5);

delay(20);
lcd_pos(0,0);
i=0;
while(dis0!='\0')
{
write_dat(dis0);
i++;
}

delay(20);
lcd_pos(1,0);
i=0;
while(dis4!='\0')
{
write_dat(dis4);
i++;
}

delay(20);
lcd_pos(2,0);
i=0;
while(dis5!='\0')
{
write_dat(dis5);
i++;
}

delay(20);
lcd_pos(3,0);
i=0;
while(dis1!='\0')
{
write_dat(dis1);
i++;
}
break;
}


else if(add0>1&&add0<51)
{
write_cmd(0x01);
delay1ms(5);

delay(20);
lcd_pos(0,0);
i=0;
while(dis0!='\0')
{
write_dat(dis0);
i++;
}

delay(20);
lcd_pos(1,0);
i=0;
while(dis4!='\0')
{
write_dat(dis4);
i++;
}

delay(20);
lcd_pos(2,0);
i=0;
while(dis5!='\0')
{
write_dat(dis5);
i++;
}
//delay(20);
lcd_pos(2,6);
while(dis12[m][n]!='\0')
{
write_dat(dis12[m][n]);
n++;
if(n==9||n==19||n==29||n==39)
{
m++;
}
if(n==9&&m==4)
{
n=0;
m=0;
}
}

delay(20);
lcd_pos(3,0);
i=0;
while(dis1!='\0')
{
write_dat(dis1);
i++;
}
break;
}

case 0x34:  //降低水位比

write_cmd(0x01);
delay1ms(5);

delay(20);
lcd_pos(0,0);
i=0;
while(dis0!='\0')
{
write_dat(dis0);
i++;
}

delay(20);
lcd_pos(1,0);
i=0;
while(dis4!='\0')
{
write_dat(dis4);
i++;
}

delay(20);
lcd_pos(2,0);
i=0;
while(dis5!='\0')
{
write_dat(dis5);
i++;
}

delay(20);
lcd_pos(3,0);
i=0;
while(dis1!='\0')
{
write_dat(dis1);
i++;
}
break;

case 0x38: //退出键以及上水中止键

//a=1;
counter=0;
write_cmd(0x01);
delay1ms(5);

delay(20);
lcd_pos(0,0);
i=0;
while(dis0!='\0')
{
write_dat(dis0);
i++;
}

delay(20);
lcd_pos(1,0);
i=0;
while(dis2!='\0')
{
write_dat(dis2);
i++;
}

delay(20);
lcd_pos(2,0);
i=0;
while(dis3!='\0')
{
write_dat(dis3);
i++;
}

delay(20);
lcd_pos(3,0);
i=0;
while(dis1!='\0')
{
write_dat(dis1);
i++;
}
break;
}
}
}

col1=1;col2=1;col3=1;col4=0; //加热按键区
temp=P2;
temp=P2&0x3f;
if(col4==0)
{
delay(80);
if(temp!=0x3f)
{
temp=P2&0x3c;
switch(temp)
{
case 0x1c:        //进入加热功能界面
counter1++;
b=counter1%2;
switch(b)
{
case 1:         //第一功能键,显示加热界面

write_cmd(0x01);
delay1ms(5);

delay(20);
lcd_pos(0,0);
i=0;
while(dis0!='\0')
{
write_dat(dis0);
i++;
}

delay(20);
lcd_pos(1,0);
i=0;
while(dis9!='\0')
{
write_dat(dis9);
i++;
}

delay(20);
lcd_pos(2,0);
i=0;
while(dis8!='\0')
{
write_dat(dis8);
i++;
}

delay(20);
lcd_pos(3,0);
i=0;
while(dis1!='\0')
{
write_dat(dis1);
i++;
}
break;

case 0:           //第二功能键,开始加热

write_cmd(0x01);
delay1ms(5);

delay(20);
lcd_pos(0,0);
i=0;
while(dis0!='\0')
{
write_dat(dis0);
i++;
}

delay(20);
lcd_pos(1,0);
i=0;
while(dis2!='\0')
{
write_dat(dis2);
i++;
}

delay(20);
lcd_pos(2,0);
i=0;
while(dis10!='\0')
{
write_dat(dis10);
i++;
}

delay(20);
lcd_pos(3,0);
i=0;
while(dis11!='\0')
{
write_dat(dis11);
i++;
}
break;
}
break;

case 0x2c:        //提高温度

write_cmd(0x01);
delay1ms(5);

delay(20);
lcd_pos(0,0);
i=0;
while(dis0!='\0')
{
write_dat(dis0);
i++;
}

delay(20);
lcd_pos(1,0);
i=0;
while(dis9!='\0')
{
write_dat(dis9);
i++;
}

delay(20);
lcd_pos(2,0);
i=0;
while(dis8!='\0')
{
write_dat(dis8);
i++;
}

delay(20);
lcd_pos(3,0);
i=0;
while(dis1!='\0')
{
write_dat(dis1);
i++;
}
break;

case 0x34:  //降低温度

write_cmd(0x01);
delay1ms(5);

delay(20);
lcd_pos(0,0);
i=0;
while(dis0!='\0')
{
write_dat(dis0);
i++;
}

delay(20);
lcd_pos(1,0);
i=0;
while(dis9!='\0')
{
write_dat(dis9);
i++;
}

delay(20);
lcd_pos(2,0);
i=0;
while(dis8!='\0')
{
write_dat(dis8);
i++;
}

delay(20);
lcd_pos(3,0);
i=0;
while(dis1!='\0')
{
write_dat(dis1);
i++;
}
break;

case 0x38: //退出键以及加热中止键

write_cmd(0x01);
delay1ms(5);

delay(20);
lcd_pos(0,0);
i=0;
while(dis0!='\0')
{
write_dat(dis0);
i++;
}

delay(20);
lcd_pos(1,0);
i=0;
while(dis2!='\0')
{
write_dat(dis2);
i++;
}

delay(20);
lcd_pos(2,0);
i=0;
while(dis3!='\0')
{
write_dat(dis3);
i++;
}

delay(20);
lcd_pos(3,0);
i=0;
while(dis1!='\0')
{
write_dat(dis1);
i++;
}
break;
}
}
}
while(temp!=0x3c)
{
temp=P2;
temp=temp&0x3c;
}
}
void write_cmd(uchar cmd)//写指令数据到LCD
{
LCD_RS=0;
LCD_RW=0;
LCD_EN=0;
P0=cmd;
delay1ms(5);
LCD_EN=1;
delay1ms(5);
LCD_EN=0;
}
void write_dat(uchar dat)//写显示数据到LCD
{
LCD_RS=1;
LCD_RW=0;
LCD_EN=0;
P0=dat;
delay1ms(5);
LCD_EN=1;
delay1ms(5);
LCD_EN=0;
}
void lcd_pos(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;
write_cmd(pos);
}
void lcd_init()//LCD初始化
{
LCD_PSB=1;
write_cmd(0x30);
delay1ms(5);
write_cmd(0x0c);
delay1ms(5);
write_cmd(0x01);
delay1ms(5);
}



关键词: 12864     51单片机    

菜鸟
2017-06-06 15:17:50     打赏
2楼

红色控制代码部分,在下有所修改。修改后,依然存在问题,相册的第二个视频是问题所表现的。

修改如下:


case 0x2c: //增加水位比

add0++;
m=0;
n=0;
if(add0==1)
{
write_cmd(0x01);
delay1ms(5);  

delay(20);
lcd_pos(0,0);
i=0;
while(dis0[i]!='\0')
{
write_dat(dis0[i]);
i++;
}

delay(20);
lcd_pos(1,0);
i=0;
while(dis4[i]!='\0')
{
write_dat(dis4[i]);
i++;
}

delay(20);
lcd_pos(2,0);
i=0;
while(dis5[i]!='\0')
{
write_dat(dis5[i]);
i++;
}

delay(20);
lcd_pos(3,0);
i=0;
while(dis1[i]!='\0')
{
write_dat(dis1[i]);
i++;
}
break;
}

else if(add0>1&&add0<51)
{
write_cmd(0x01);
delay1ms(5);  

delay(20);
lcd_pos(0,0);
i=0;
while(dis0[i]!='\0')
{
write_dat(dis0[i]);
i++;
}

delay(20);
lcd_pos(1,0);
i=0;
while(dis4[i]!='\0')
{
write_dat(dis4[i]);
i++;
}

delay(20);
lcd_pos(2,0);
i=0;
while(dis5[i]!='\0')
{
write_dat(dis5[i]);
i++;
}  
lcd_pos(2,6);
while(dis12[m][n]!='\0')
{
write_dat(dis12[m][n]);
}
n++;
if(n==9)
{
m++;
n=0;
}
if(n==9&&m==4)
{
n=0;
m=0;
}

delay(20);
lcd_pos(3,0);
i=0;
while(dis1[i]!='\0')
{
write_dat(dis1[i]);
i++;
}
break;
}


专家
2017-06-06 17:14:22     打赏
3楼

你显示的m n参数没有初始化,建议初始化下。重点检查这两个参数


专家
2017-06-07 08:26:26     打赏
4楼
建议还是初始化下。

专家
2017-06-07 10:43:36     打赏
5楼
//m是显示百分几的位置
//n是 字符串的位置
lcd_pos(2,6);
n=0; //每次都初始化,必须要从0开始
while( dis12[m][n] != '\0' )
{
write_dat(dis12[m][n]);
n++;
if(n==9||n==19||n==29||n==39)

m++;
}
if(n==9&&m==4)
{
n=0;
m=0;
}
}


高工
2017-06-11 21:13:41     打赏
6楼
刷显示的时候加断点跟踪下

共6条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]