本帖最后由 hanhan1129 于 2016-6-2 19:46 编辑
目的:
用1602显示6行数据,分3页显示。每行数据输入后,按确定键保存当前输入的数据。两行输入完成后,按翻页键
翻到下一页,继续输入数据。当翻页键按到第4次时,重新显示第一页之前输入的数据,后面两页依次类推。
目前的主要问题是第4次按下时,无法显示之前输入的数据。
程序如下:
#include <REG52.H>
#include <absacc.h>
#include <intrins.h>
#define uint unsigned int
#define uchar unsigned char
#define ulong unsigned long
/******************LCD1602**********************/
sbit LCD_RS = P2^3; //
sbit LCD_RW = P2^4; //
sbit LCD_EN = P2^5; //
sbit dula = P2^6; //
sbit wela = P2^7; //
sbit pulse = P1^1; //
#define LCD_DA P0
#define KEY P3 //
/**************************/
uchar code
title[6][16]=
{
"T :",
"t1:",
"t2:",
"d1:",
"d2:",
"N :"
};
/*****************************/
uchar code table1[]={" SVC TEST "};
uchar code table2[]={" Set Parameters "};
/************************/
long int data_a=0;
long int data_b=0;
ulong data_1=0;
ulong data_2=0;
ulong data_3=0;
ulong data_4=0;
ulong data_5=0;
ulong data_6=0;
ulong dispaly[10];
uchar i,j;
uchar key=0xff;
uchar a=0;
uchar b=8;
uchar x=0;
uchar flag=0;
uchar flag1=0;
uchar enterflag=0;
/****************************/
void delay(uint time)
{
uint i,j;
for(i=time;i>0;i--)
for(j=110;j>0;j--);
}
/****************************/
void DelayS(uint time)
{
delay(1000*time);
}
/****************************/
void delay_fmq(uint time)
{
uint x,y;
for(x=time;x>0;x--)
for(y=5;y>0;y--);
}
/****************************/
void write_com(uchar com)
{
LCD_RS=0;
LCD_RW=0;
LCD_EN=0;
LCD_DA=com;
delay(2);
LCD_EN=1;
delay(2);
LCD_EN=0;
}
/***************************/
void write_date(uchar date)
{
LCD_RS=1;
LCD_RW=0;
LCD_EN=0;
LCD_DA=date;
delay(2);
LCD_EN=1;
delay(2);
LCD_EN=0;
}
/***************************/
void LCD_Clear(void)
{
write_com(0x01);
delay(5);
}
/**********************/
void W_lcd(unsigned char x,unsigned char y,unsigned char Data)
{
if(y==0)
{
write_com(0x80 + x);
}
else
{
write_com(0xc0 + x);
}
write_date(Data);
}
/*****************/
void LCD_Write_String(unsigned char x,unsigned char y,unsigned char *s)
{
if(y==0)
{
write_com(0x80 + x);
}
else
{
write_com(0xC0 + x);
}
while(*s)
{
write_date(*s);
s++;
}
}
/*******************************/
void LCD_init()
{
dula=0;
wela=0;
write_com(0x38);
delay(5);
write_com(0x38);
delay(5);
write_com(0x38);
delay(5);
write_com(0x0f);
delay(5);
write_com(0x06);
delay(5);
write_com(0x01);
delay(5);
}
/********************************/
void Start(unsigned long T,unsigned long t1,unsigned long t2,unsigned long d1,unsigned long d2,unsigned long N)
{
unsigned long Low_time,High_time;
unsigned long Low_S,High_S;
unsigned int Low_Ms,High_Ms;
unsigned char n;
for(n=1;n<=N;n++)
{
Low_time=t1+n*d1;
High_time=t2+n*d2;
Low_S=(T+Low_time)/1000;
Low_Ms=(T+Low_time)%1000;
High_S=High_time/1000;
High_Ms=High_time%1000;
pulse =0;
DelayS(Low_S);
delay(Low_Ms);
pulse =1;
DelayS(High_S);
delay(High_Ms);
}
}
/**************************/
void stop()
{
pulse=1;
}
/**************************/
short keycheckdown()
{
short temp1,temp2,temp,a=0xff;
KEY=0xf0;
delay(20);
temp1=KEY;
KEY=0xff;
delay(20);
KEY=0x0f;
delay(20);
temp2=KEY;
KEY=0xff;
temp=(temp1&0xf0)|(temp2&0xf);
switch(temp)
{
case 0xee :a=7;
break;
case 0xed :a=8;
break;
case 0xeb :a=9;
break;
case 0xe7 :a=0x0a;
break;
case 0xde :a=4;
break;
case 0xdd :a=5;
break;
case 0xdb :a=6;
break;
case 0xd7 :a=0x0b;
break;
case 0xbe :a=1;
break;
case 0xbd :a=2;
break;
case 0xbb :a=3;
break;
case 0xb7 :a=0x0c;
break;
case 0x7e :a=0;
break;
case 0x7d :a=0x0e;
break;
case 0x7b :a=0x0f;
break;
case 0x77 :a=0x0d;
break;
default :a=0xff;
}
return a;
}
/*********************************/
void display_a()
{
dispaly[6]=data_a%10000000/1000000;
dispaly[5]=data_a%1000000/100000;
dispaly[4]=data_a%100000/10000;
dispaly[3]=data_a%10000/1000;
dispaly[2]=data_a%1000/100;
dispaly[1]=data_a%100/10;
dispaly[0]=data_a%10;
write_com(0x80+3);
if(data_a>999999)
{
write_date('0'+dispaly[6]);
}
if(data_a>99999)
{
write_date('0'+dispaly[5]);
}
if(data_a>9999)
{
write_date('0'+dispaly[4]);
}
if(data_a>999)
{
write_date('0'+dispaly[3]);
}
if(data_a>99)
{
write_date('0'+dispaly[2]);
}
if(data_a>9)
{
write_date('0'+dispaly[1]);
}
write_date('0'+dispaly[0]);
}
/*********************************/
void display_b()
{
write_com(0xc0+3);
dispaly[6]=data_b%10000000/1000000;
dispaly[5]=data_b%1000000/100000;
dispaly[4]=data_b%100000/10000;
dispaly[3]=data_b%10000/1000;
dispaly[2]=data_b%1000/100;
dispaly[1]=data_b%100/10;
dispaly[0]=data_b%10;
if(data_b>999999)
{
write_date('0'+dispaly[6]);
}
if(data_b>99999)
{
write_date('0'+dispaly[5]);
}
if(data_b>9999)
{
write_date('0'+dispaly[4]);
}
if(data_b>999)
{
write_date('0'+dispaly[3]);
}
if(data_b>99)
{
write_date('0'+dispaly[2]);
}
if(data_b>9)
{
write_date('0'+dispaly[1]);
}
write_date('0'+dispaly[0]);
}
void display_1()
{
if(data_1<10000000&&data_1>-1)
{
dispaly[6]=data_1%10000000/1000000;
dispaly[5]=data_1%1000000/100000;
dispaly[4]=data_1%100000/10000;
dispaly[3]=data_1%10000/1000;
dispaly[2]=data_1%1000/100;
dispaly[1]=data_1%100/10;
dispaly[0]=data_1%10;
write_com(0x80+3);
if(data_1>999999)
{
write_date('0'+dispaly[6]);
}
if(data_1>99999)
{
write_date('0'+dispaly[5]);
}
if(data_1>9999)
{
write_date('0'+dispaly[4]);
}
if(data_1>999)
{
write_date('0'+dispaly[3]);
}
if(data_1>99)
{
write_date('0'+dispaly[2]);
}
if(data_1>9)
{
write_date('0'+dispaly[1]);
}
write_date('0'+dispaly[0]);
}
}
void display_2()
{
if(data_2<10000000&&data_2>-1)
{
dispaly[6]=data_2%10000000/1000000;
dispaly[5]=data_2%1000000/100000;
dispaly[4]=data_2%100000/10000;
dispaly[3]=data_2%10000/1000;
dispaly[2]=data_2%1000/100;
dispaly[1]=data_2%100/10;
dispaly[0]=data_2%10;
write_com(0x80+0x40+3);
if(data_2>999999)
{
write_date('0'+dispaly[6]);
}
if(data_2>99999)
{
write_date('0'+dispaly[5]);
}
if(data_2>9999)
{
write_date('0'+dispaly[4]);
}
if(data_2>999)
{
write_date('0'+dispaly[3]);
}
if(data_2>99)
{
write_date('0'+dispaly[2]);
}
if(data_2>9)
{
write_date('0'+dispaly[1]);
}
write_date('0'+dispaly[0]);
}
}
void display_3()
{
if(data_3<10000000&&data_3>-1)
{
dispaly[6]=data_3%10000000/1000000;
dispaly[5]=data_3%1000000/100000;
dispaly[4]=data_3%100000/10000;
dispaly[3]=data_3%10000/1000;
dispaly[2]=data_3%1000/100;
dispaly[1]=data_3%100/10;
dispaly[0]=data_3%10;
write_com(0x80+3);
if(data_3>999999)
{
write_date('0'+dispaly[6]);
}
if(data_3>99999)
{
write_date('0'+dispaly[5]);
}
if(data_3>9999)
{
write_date('0'+dispaly[4]);
}
if(data_3>999)
{
write_date('0'+dispaly[3]);
}
if(data_3>99)
{
write_date('0'+dispaly[2]);
}
if(data_3>9)
{
write_date('0'+dispaly[1]);
}
write_date('0'+dispaly[0]);
}
}
void display_4()
{
if(data_4<10000000&&data_4>-1)
{
dispaly[6]=data_4%10000000/1000000;
dispaly[5]=data_4%1000000/100000;
dispaly[4]=data_4%100000/10000;
dispaly[3]=data_4%10000/1000;
dispaly[2]=data_4%1000/100;
dispaly[1]=data_4%100/10;
dispaly[0]=data_4%10;
write_com(0x80+0x40+3);
if(data_4>999999)
{
write_date('0'+dispaly[6]);
}
if(data_4>99999)
{
write_date('0'+dispaly[5]);
}
if(data_4>9999)
{
write_date('0'+dispaly[4]);
}
if(data_4>999)
{
write_date('0'+dispaly[3]);
}
if(data_4>99)
{
write_date('0'+dispaly[2]);
}
if(data_4>9)
{
write_date('0'+dispaly[1]);
}
write_date('0'+dispaly[0]);
}
}
void display_5()
{
if(data_5<10000000&&data_5>-1)
{
dispaly[6]=data_5%10000000/1000000;
dispaly[5]=data_5%1000000/100000;
dispaly[4]=data_5%100000/10000;
dispaly[3]=data_5%10000/1000;
dispaly[2]=data_5%1000/100;
dispaly[1]=data_5%100/10;
dispaly[0]=data_5%10;
write_com(0x80+3);
if(data_5>999999)
{
write_date('0'+dispaly[6]);
}
if(data_5>99999)
{
write_date('0'+dispaly[5]);
}
if(data_5>9999)
{
write_date('0'+dispaly[4]);
}
if(data_5>999)
{
write_date('0'+dispaly[3]);
}
if(data_5>99)
{
write_date('0'+dispaly[2]);
}
if(data_5>9)
{
write_date('0'+dispaly[1]);
}
write_date('0'+dispaly[0]);
}
}
void display_6()
{
if(data_6<10000000&&data_6>-1)
{
dispaly[6]=data_6%10000000/1000000;
dispaly[5]=data_6%1000000/100000;
dispaly[4]=data_6%100000/10000;
dispaly[3]=data_6%10000/1000;
dispaly[2]=data_6%1000/100;
dispaly[1]=data_6%100/10;
dispaly[0]=data_6%10;
write_com(0x80+0x40+3);
if(data_6>999999)
{
write_date('0'+dispaly[6]);
}
if(data_6>99999)
{
write_date('0'+dispaly[5]);
}
if(data_6>9999)
{
write_date('0'+dispaly[4]);
}
if(data_6>999)
{
write_date('0'+dispaly[3]);
}
if(data_6>99)
{
write_date('0'+dispaly[2]);
}
if(data_6>9)
{
write_date('0'+dispaly[1]);
}
write_date('0'+dispaly[0]);
}
}
/*****************************/
void Keypro(uchar Funkey)
{
switch(Funkey)
{
case 1: write_com(0x0f);
write_com(0x10);
break;
case 2: write_com(0x0f);
write_com(0x14);
break;
case 3: if(enterflag==3) enterflag=0;
if(flag==0)
{
if(enterflag==1)
{
data_1=data_a;
data_a=0;
display_b();
}
if(enterflag==2)
{
data_2=data_b;
data_b=0;
}
}
if(flag==1)
{
if(enterflag==1)
{
data_3=data_a;
data_a=0;
display_b();
}
if(enterflag==2)
{
data_4=data_b;
data_b=0;
}
}
if(flag==2)
{
if(enterflag==1)
{
data_5=data_a;
data_a=0;
display_b();
}
if(enterflag==2)
{
data_6=data_b;
data_b=0;
}
}
break;
case 4:
if(flag==3) {flag=0;}
if(flag==0)
{
LCD_Clear();
LCD_Write_String(0, 0, Title[flag*2]);
display_1();
LCD_Write_String(0, 1, Title[flag*2+1]);
display_2();
}
if(flag==1)
{
LCD_Clear();
LCD_Write_String(0, 0, Title[flag*2]);
display_3();
LCD_Write_String(0, 1, Title[flag*2+1]);
display_4();
}
if(flag==2)
{
LCD_Clear();
LCD_Write_String(0, 0, Title[flag*2]);
display_5();
LCD_Write_String(0, 1, Title[flag*2+1]);
display_6();
}
write_com(0x80+3);
break;
case 0: break;
}
}
void main()
{
LCD_init();
LCD_Write_String(0,0,Title[0]);
LCD_Write_String(0,1,Title[1]);
while(1)
{
key=keycheckdown();
if(0xff!=key)
{
if(key<10)
{
if(a<7)
{
data_a=data_a*10+key;
b=8;
display_a();
}
a++;
if(b<7)
{
data_b=data_b*10+key;
a=8;
display_b();
}
b++;
}
else
{
switch(key)
{
case 0x0a: a=0;b=0; //left
x=1;
Keypro(x);
break;
case 0x0b: a=0;b=0; //right
x=2;
Keypro(x);
break;
case 0x0c: a=0;b=0; //down
flag++;
enterflag=0;
x=4;
Keypro(x);
break;
case 0x0d: a=8;b=0; //enter
enterflag++;
x=3;
Keypro(x);
break;
}
}
do{KEY=0xf0;}
while(KEY!=0xf0);
}
}
}
应该没有吧,我每次都保存到单独的变量中啊
一周热门 更多>