求助!12864液晶显示光标如何控制和很好的使用(附加程序)
时间:10-02
整理:3721RD
点击:
/*****************************
出版公司:2013.07.26
编程时间:
编程者:
编程环境:keil uv4 9.0
*****************************/
/****************包含头文件********************/
#include<reg52.h>
/****************宏定义********************/
#define uchar unsigned char
#define uint unsigned int
/****************引脚声明********************/
sbit lcdrs = P1^0;
sbit lcdrw = P1^1;
sbit lcden = P2^5;
sbit lcdpsb = P1^2;
sbit dula = P2^6;
sbit wela = P2^7;
sbit key1 = P3^4;
sbit key2 = P3^5;
sbit key3 = P3^6;
sbit key4 = P3^7;
/****************定义变量、数组********************/
uchar code table[]={0x30,0x31,0x32,0x33,
0x34,0x35,0x36,0x37,
0x38,0x39};
uchar code table1[]="Have a happy day";
uchar num0;
uchar num_sec;
uchar num_min;
uchar num_hour;
uint num=0; //定义T1中断变量
uchar flag;
uint num_day=0;
uint num_mon=0;
uint num_year=2013;
/****************************************
函数功能:延时函数
输入参数:xms
输出参数:
****************************************/
void delay(uint xms)
{
uint i,j;
for(i = xms;i > 0;i--)
for(j = 110;j > 0;j--);
}
/****************************************
函数功能:写命令函数
输入参数:com
输出参数:
****************************************/
void write_command(uchar com)
{
lcdrs=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
/****************************************
函数功能:写数据函数
输入参数:dat
输出参数:
****************************************/
void write_data(uchar dat)
{
lcdrs=1;
P0=dat;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
/****************************************
函数功能:时间显示
输入参数:num_sec,num_min,num_year
输出参数:
****************************************/
void display_time(uchar num_sec,uchar num_min,uchar num_hour)
{
uchar sec_shi,sec_ge; //分别计算十位,个位
uchar min_shi,min_ge; //用于显示
uchar hour_shi,hour_ge;
hour_shi = num_hour / 10;
hour_ge = num_hour % 10;
min_shi = num_min / 10;
min_ge = num_min % 10;
sec_shi = num_sec / 10;
sec_ge = num_sec % 10;
//write_command(0x90); //给定显示地址
write_data(table[hour_shi]);
write_data(table[hour_ge]);
write_data(0x20);
write_data(0x3a);
write_data(0x20);
write_data(table[min_shi]);
write_data(table[min_ge]);
write_data(0x20);
write_data(0x3a);
write_data(0x20);
write_data(table[sec_shi]);
write_data(table[sec_ge]);
}
/****************************************
函数功能:日期显示
输入参数:num_day,num_mon,num_year
输出参数:
****************************************/
void display_day(uint num_day,uint num_mon,uint num_year)
{
uchar day_shi,day_ge;
uchar mon_shi,mon_ge;
uchar year_qian,year_bai,year_shi,year_ge;
//int num_year_=2000;
year_qian =num_year/ 1000;
year_bai = num_year%1000 / 100;
year_shi = num_year%100 / 10;
year_ge = num_year % 10;
mon_shi = num_mon / 10;
mon_ge = num_mon % 10;
day_shi = num_day / 10;
day_ge = num_day % 10;
//write_command(0x80); //给定显示地址
write_data(year_qian+0x30);
write_data(year_bai+0x30);
write_data(year_shi+0x30);
write_data(year_ge+0x30);
write_data(0x20);
write_data(0x2d);
write_data(0x20);
write_data(table[mon_shi]);
write_data(table[mon_ge]);
write_data(0x20);
write_data(0x2d);
write_data(0x20);
write_data(table[day_shi]);
write_data(table[day_ge]);
}
/****************************************
函数功能:调整函数
输入参数:num_sec,num_min,num_hour
num_day,num_mon,num_year
输出参数:
****************************************/
void adjust()
{
/* if(key1 == 0)
{ //我的问题是:用光标选择调节对象,并调节之,希望有大神可以帮忙解决一下,万分感谢!
delay(10);
if(key1 == 0);
{
while(!key1);
num_sec++;
if(num_sec == 60)
{
num_sec = 0;
}
}
}
if(key2 == 0)
{
delay(10);
if(key2 == 0)
{
while(!key2);
num_min++;
if(num_min == 60)
{
num_min = 0;
}
}
}
if(key3 == 0)
{
delay(10);
if(key3 == 0)
{
while(!key3);
num_hour++;
if(num_hour == 24)
{
num_hour = 0;
}
}
}
if(key4 == 0)
{
delay(10);
if(key4 == 0)
{
while(!key4);
if(key1 == 0)
{
delay(10);
if(key1 == 0)
{
num_day++;
}
while(!key1);
}
if(key2 == 0)
{
delay(10);
if(key2 == 0)
{
num_mon++;
if(num_mon == 13)
{
num_mon = 0;
}
}
while(!key2);
}
if(key3 == 0)
{
delay(10);
if(key3 == 0)
{
num_year++;
if(num_year == 2021)
{
num_year = 2013;
}
}
while(!key3);
}
}
}
*/
}
/****************************************
函数功能:初始化函数
输入参数:
输出参数:
****************************************/
void initial()
{
lcdrw = 0;
lcden = 0;
lcdpsb = 1;
dula = 0;
wela = 0;
TMOD = 0X11;
TH1 = (65536 - 45872) / 256;
TL1 = (65536 - 45872) % 256;
TH0 =( 65536 - 45872) / 256;
TL0 = (65536 - 45872) % 256;
EA = 1;
ET0 = 1;
ET1 = 1;
TR0 = 1;
TR1 = 1;
write_command(0x30);
write_command(0x0f); //控制光标!
write_command(0x02);
write_command(0x01);
}
/****************************************
函数功能:主函数
输入参数:
输出参数:
****************************************/
void main()
{
uchar i;
initial();
write_command(0x88);
for(i = 0;i < 16;i++)
{
write_data(table1[i]);
delay(5);
}
while(1)
{
write_command(0x80);
display_day(num_day,num_mon,num_year);
write_command(0x90);
display_time(num_sec,num_min,num_hour);
adjust();
}
}
/****************************************
函数功能:T0中断函数
输入参数:
输出参数:
****************************************/
void T0_time() interrupt 1
{
TH0 = (65536 - 45872) / 256;
TL0 = (65536 - 45872) % 256;
num0++;
if(num0 == 1)
{
num0 = 0;
num_sec++;
if(num_sec == 60)
{
num_sec = 0;
num_min++;
if(num_min == 60)
{
num_min = 0;
num_hour++;
if(num_hour == 24)
{
num_hour = 0;
flag = 1;
}
}
}
}
}
/****************************************
函数功能:T1中断函数
输入参数:
输出参数:
****************************************/
void T1_time() interrupt 3
{
TH1 = (65536 - 45872) / 256;
TL1 = (65536 - 45872) % 256;
if(flag == 1) //
{
num_day++; //
num++; //用于检测月份
if(num <= 31)
{
num_mon = 1;
}
if(num == 31)
{
num_day = 0;
num_mon++; //num_mon = 2;
}
if((num_year % 4 == 0
&&num_year % 100 != 0)
||(num_year % 400 ==0)) //判断是否是闰年
{
if(num == 60)
{
num_day = 0;
num_mon++; //num_mon = 3;
}
if(num == 91)
{
num_day = 0;
num_mon++; //num_mon = 4;
}
if(num == 121)
{
num_day = 0;
num_mon++; //num_mon = 5;
}
if(num == 152)
{
num_day = 0;
num_mon++; //num_mon = 6;
}
if(num == 182)
{
num_day = 0;
num_mon++; //num_mon = 7;
}
if(num == 213)
{
num_day = 0;
num_mon++; //num_mon = 8;
}
if(num == 244)
{
num_day = 0;
num_mon++; //num_mon = 9;
}
if(num == 274)
{
num_day = 0;
num_mon++; //num_mon = 10;
}
if(num == 305)
{
num_day = 0;
num_mon++; //num_mon = 11;
}
if(num == 335)
{
num_day = 0;
num_mon++; //num_mon = 12;
}
if(num == 366)
{
num_day = 0;
num_mon = 1; //num_mon = 1;
num_year++;
}
}
else
{
if(num == 59)
{
num_day = 0;
num_mon++; //num_mon = 3;
}
if(num == 90)
{
num_day = 0;
num_mon++; //num_mon = 4;
}
if(num == 120)
{
num_day = 0;
num_mon++; //num_mon = 5;
}
if(num == 151)
{
num_day = 0;
num_mon++; //num_mon = 6;
}
if(num == 181)
{
num_day = 0;
num_mon++; //num_mon = 7;
}
if(num == 212)
{
num_day = 0;
num_mon++; //num_mon = 8;
}
if(num == 243)
{
num_day = 0;
num_mon++; //num_mon = 9;
}
if(num == 274)
{
num_day = 0;
num_mon++; //num_mon = 10;
}
if(num == 304)
{
num_day = 0;
num_mon++; //num_mon = 11;
}
if(num == 334)
{
num_day = 0;
num_mon++; //num_mon = 12;
}
if(num == 365)
{
num_day = 0;
num_mon = 1; //num_mon = 1;
num_year++;
}
}
flag = 0;
}
}
出版公司:2013.07.26
编程时间:
编程者:
编程环境:keil uv4 9.0
*****************************/
/****************包含头文件********************/
#include<reg52.h>
/****************宏定义********************/
#define uchar unsigned char
#define uint unsigned int
/****************引脚声明********************/
sbit lcdrs = P1^0;
sbit lcdrw = P1^1;
sbit lcden = P2^5;
sbit lcdpsb = P1^2;
sbit dula = P2^6;
sbit wela = P2^7;
sbit key1 = P3^4;
sbit key2 = P3^5;
sbit key3 = P3^6;
sbit key4 = P3^7;
/****************定义变量、数组********************/
uchar code table[]={0x30,0x31,0x32,0x33,
0x34,0x35,0x36,0x37,
0x38,0x39};
uchar code table1[]="Have a happy day";
uchar num0;
uchar num_sec;
uchar num_min;
uchar num_hour;
uint num=0; //定义T1中断变量
uchar flag;
uint num_day=0;
uint num_mon=0;
uint num_year=2013;
/****************************************
函数功能:延时函数
输入参数:xms
输出参数:
****************************************/
void delay(uint xms)
{
uint i,j;
for(i = xms;i > 0;i--)
for(j = 110;j > 0;j--);
}
/****************************************
函数功能:写命令函数
输入参数:com
输出参数:
****************************************/
void write_command(uchar com)
{
lcdrs=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
/****************************************
函数功能:写数据函数
输入参数:dat
输出参数:
****************************************/
void write_data(uchar dat)
{
lcdrs=1;
P0=dat;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
/****************************************
函数功能:时间显示
输入参数:num_sec,num_min,num_year
输出参数:
****************************************/
void display_time(uchar num_sec,uchar num_min,uchar num_hour)
{
uchar sec_shi,sec_ge; //分别计算十位,个位
uchar min_shi,min_ge; //用于显示
uchar hour_shi,hour_ge;
hour_shi = num_hour / 10;
hour_ge = num_hour % 10;
min_shi = num_min / 10;
min_ge = num_min % 10;
sec_shi = num_sec / 10;
sec_ge = num_sec % 10;
//write_command(0x90); //给定显示地址
write_data(table[hour_shi]);
write_data(table[hour_ge]);
write_data(0x20);
write_data(0x3a);
write_data(0x20);
write_data(table[min_shi]);
write_data(table[min_ge]);
write_data(0x20);
write_data(0x3a);
write_data(0x20);
write_data(table[sec_shi]);
write_data(table[sec_ge]);
}
/****************************************
函数功能:日期显示
输入参数:num_day,num_mon,num_year
输出参数:
****************************************/
void display_day(uint num_day,uint num_mon,uint num_year)
{
uchar day_shi,day_ge;
uchar mon_shi,mon_ge;
uchar year_qian,year_bai,year_shi,year_ge;
//int num_year_=2000;
year_qian =num_year/ 1000;
year_bai = num_year%1000 / 100;
year_shi = num_year%100 / 10;
year_ge = num_year % 10;
mon_shi = num_mon / 10;
mon_ge = num_mon % 10;
day_shi = num_day / 10;
day_ge = num_day % 10;
//write_command(0x80); //给定显示地址
write_data(year_qian+0x30);
write_data(year_bai+0x30);
write_data(year_shi+0x30);
write_data(year_ge+0x30);
write_data(0x20);
write_data(0x2d);
write_data(0x20);
write_data(table[mon_shi]);
write_data(table[mon_ge]);
write_data(0x20);
write_data(0x2d);
write_data(0x20);
write_data(table[day_shi]);
write_data(table[day_ge]);
}
/****************************************
函数功能:调整函数
输入参数:num_sec,num_min,num_hour
num_day,num_mon,num_year
输出参数:
****************************************/
void adjust()
{
/* if(key1 == 0)
{ //我的问题是:用光标选择调节对象,并调节之,希望有大神可以帮忙解决一下,万分感谢!
delay(10);
if(key1 == 0);
{
while(!key1);
num_sec++;
if(num_sec == 60)
{
num_sec = 0;
}
}
}
if(key2 == 0)
{
delay(10);
if(key2 == 0)
{
while(!key2);
num_min++;
if(num_min == 60)
{
num_min = 0;
}
}
}
if(key3 == 0)
{
delay(10);
if(key3 == 0)
{
while(!key3);
num_hour++;
if(num_hour == 24)
{
num_hour = 0;
}
}
}
if(key4 == 0)
{
delay(10);
if(key4 == 0)
{
while(!key4);
if(key1 == 0)
{
delay(10);
if(key1 == 0)
{
num_day++;
}
while(!key1);
}
if(key2 == 0)
{
delay(10);
if(key2 == 0)
{
num_mon++;
if(num_mon == 13)
{
num_mon = 0;
}
}
while(!key2);
}
if(key3 == 0)
{
delay(10);
if(key3 == 0)
{
num_year++;
if(num_year == 2021)
{
num_year = 2013;
}
}
while(!key3);
}
}
}
*/
}
/****************************************
函数功能:初始化函数
输入参数:
输出参数:
****************************************/
void initial()
{
lcdrw = 0;
lcden = 0;
lcdpsb = 1;
dula = 0;
wela = 0;
TMOD = 0X11;
TH1 = (65536 - 45872) / 256;
TL1 = (65536 - 45872) % 256;
TH0 =( 65536 - 45872) / 256;
TL0 = (65536 - 45872) % 256;
EA = 1;
ET0 = 1;
ET1 = 1;
TR0 = 1;
TR1 = 1;
write_command(0x30);
write_command(0x0f); //控制光标!
write_command(0x02);
write_command(0x01);
}
/****************************************
函数功能:主函数
输入参数:
输出参数:
****************************************/
void main()
{
uchar i;
initial();
write_command(0x88);
for(i = 0;i < 16;i++)
{
write_data(table1[i]);
delay(5);
}
while(1)
{
write_command(0x80);
display_day(num_day,num_mon,num_year);
write_command(0x90);
display_time(num_sec,num_min,num_hour);
adjust();
}
}
/****************************************
函数功能:T0中断函数
输入参数:
输出参数:
****************************************/
void T0_time() interrupt 1
{
TH0 = (65536 - 45872) / 256;
TL0 = (65536 - 45872) % 256;
num0++;
if(num0 == 1)
{
num0 = 0;
num_sec++;
if(num_sec == 60)
{
num_sec = 0;
num_min++;
if(num_min == 60)
{
num_min = 0;
num_hour++;
if(num_hour == 24)
{
num_hour = 0;
flag = 1;
}
}
}
}
}
/****************************************
函数功能:T1中断函数
输入参数:
输出参数:
****************************************/
void T1_time() interrupt 3
{
TH1 = (65536 - 45872) / 256;
TL1 = (65536 - 45872) % 256;
if(flag == 1) //
{
num_day++; //
num++; //用于检测月份
if(num <= 31)
{
num_mon = 1;
}
if(num == 31)
{
num_day = 0;
num_mon++; //num_mon = 2;
}
if((num_year % 4 == 0
&&num_year % 100 != 0)
||(num_year % 400 ==0)) //判断是否是闰年
{
if(num == 60)
{
num_day = 0;
num_mon++; //num_mon = 3;
}
if(num == 91)
{
num_day = 0;
num_mon++; //num_mon = 4;
}
if(num == 121)
{
num_day = 0;
num_mon++; //num_mon = 5;
}
if(num == 152)
{
num_day = 0;
num_mon++; //num_mon = 6;
}
if(num == 182)
{
num_day = 0;
num_mon++; //num_mon = 7;
}
if(num == 213)
{
num_day = 0;
num_mon++; //num_mon = 8;
}
if(num == 244)
{
num_day = 0;
num_mon++; //num_mon = 9;
}
if(num == 274)
{
num_day = 0;
num_mon++; //num_mon = 10;
}
if(num == 305)
{
num_day = 0;
num_mon++; //num_mon = 11;
}
if(num == 335)
{
num_day = 0;
num_mon++; //num_mon = 12;
}
if(num == 366)
{
num_day = 0;
num_mon = 1; //num_mon = 1;
num_year++;
}
}
else
{
if(num == 59)
{
num_day = 0;
num_mon++; //num_mon = 3;
}
if(num == 90)
{
num_day = 0;
num_mon++; //num_mon = 4;
}
if(num == 120)
{
num_day = 0;
num_mon++; //num_mon = 5;
}
if(num == 151)
{
num_day = 0;
num_mon++; //num_mon = 6;
}
if(num == 181)
{
num_day = 0;
num_mon++; //num_mon = 7;
}
if(num == 212)
{
num_day = 0;
num_mon++; //num_mon = 8;
}
if(num == 243)
{
num_day = 0;
num_mon++; //num_mon = 9;
}
if(num == 274)
{
num_day = 0;
num_mon++; //num_mon = 10;
}
if(num == 304)
{
num_day = 0;
num_mon++; //num_mon = 11;
}
if(num == 334)
{
num_day = 0;
num_mon++; //num_mon = 12;
}
if(num == 365)
{
num_day = 0;
num_mon = 1; //num_mon = 1;
num_year++;
}
}
flag = 0;
}
}
感谢分享
厉害了小编
小编厉害了