请高人指点在keyscan函数中的if(k1num==4)中 当我按下键K1四下后 我启动定时器开始计数 为什么按完第四下 需要等待将近14秒 定时器才开始计数
请问毛病到底出在哪里啊 求大虾解决疑惑啊
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit RS=P2^6;
sbit RW=P2^5;
sbit E=P2^7;
sbit k1=P1^0;
sbit k2=P1^1;
sbit k3=P1^2;
uchar count,k1num;
char shi,fen,miao;
uchar code table[]="my name is yuan.";
uchar code table1[]=" 00:00:00:0 ";
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=110;y>0;y--);
}
void write_com(uchar com)
{
delay(5);
RS=0;//等于0时为写命令
E=0; //
RW=0; //写入指令或者数据
delay(5);
P0=com;
E=1; //读取信息
delay(5);
E=0;//from 1 to 0,the falling edge ,execute the command
}
void write_date(uchar date)
{
delay(5);
RS=1;// 一定注意按照时序图来写
E=0;
delay(5);
E=1;
P0=date;
delay(5);
E=0;
}
void init()
{
uchar num;
miao=0;
fen=0;
shi=0;
E=0;
write_com(0x38); //显示5*7点阵设置 16*2 八位数据接口
delay(5);
write_com(0x38);
delay(5);
write_com(0x38);
write_com(0x08);//显示关闭
write_com(0x01);//显示清屏
write_com(0x06);//每写一个 指针地址自动加一
write_com(0x0c);//指令屏幕开显示,不显示光标,不闪烁
write_com(0x80);
for(num=0;num<16;num++)
{
write_date(table[num]);
delay(30);
}
write_com(0x80+0x40);//写在第二行
for(num=0;num<16;num++)
{
write_date(table1[num]);
delay(30);
}
TMOD=0x01;
TH0=0x3c;
TL0=0xb0;
EA=1;
ET0=1;
TR0=1;
}
void write_sfm(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);
write_date(0x30+shi); //对应的表中 从十位(即地址0x80+0x40+ADD)开始 0x30加几屏幕就显示几 可以查表
write_date(0x30+ge);/*写完十位(即地址0x80+0x40+10)
地址自动加一开始写这一位的个位数字*/
}
void keyscan()
{
if(k1==0)
{
delay(20);
if(k1==0)
{
k1num++; //检测按键0被按下几次
while(!k1);//检测按键是否松手
if(k1num==1)
{
TR0=0;//关闭定时器,时间静止
write_com(0x80+0x40+10);
write_com(0x0f);//光标闪烁
}
if(k1num==2)
{
write_com(0x80+0x40+7);
}
if(k1num==3)
{
write_com(0x80+0x40+4);
}
if(k1num==4)
{
k1num=0;
write_com(0x0c);//光标不显示不闪烁
TMOD=0x01;
TH0=0x3c;
TL0=0xb0;
EA=1;
ET0=1;
TR0=1; //需调式,草的,这毛病到底出哪儿了
}
}
}
if(k1num!=0)//意味着k1num被按下
{
if(k2==0) //如果被按下 进行PLUS
{
delay(20);
if(k2==0)
{
while(!k2);
if(k1num==1) //检测光标在哪个位置闪烁 进行加法修改数字
{
miao++;
if(miao==60)
miao=0;
write_sfm(9,miao);
write_com(0x80+0x40+10); //由于设置的指令时加完 指针地址自动加一,所以要保持他在这个地址
}
if(k1num==2)
{
fen++;
if(fen==60)
fen=0;
write_sfm(6,fen);
write_com(0x80+0x40+7);
}
if(k1num==3)
{
shi++;
if(shi==24)
shi=0;
write_sfm(3,shi);
write_com(0x80+0x40+4);
}
}
}
/*如果被按下 进行加法*/
if(k3==0)
{
delay(20);
if(k3==0)
{
while(!k3);
if(k1num==1)
{
miao--;
if(miao==-1)
miao=59;
write_sfm(9,miao);
write_com(0x80+0x40+10);
}
if(k1num==2)
{
fen--;
if(fen==-1)
fen=59;
write_sfm(6,fen);
write_com(0x80+0x40+7);
}
if(k1num==3)
{
shi--;
if(shi==-1)
shi=23;
write_sfm(3,shi);
write_com(0x80+0x40+4);
}
}
}
}
}
void main()
{ uchar paomiao;
init();
while(1)
{
keyscan();
if(count==2)
{
count=0;
paomiao++;
if(paomiao==9)
{
paomiao=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
}
write_sfm(3,shi);
}
write_sfm(6,fen);
}
write_sfm(9,miao);
}
write_com(0x80+0x40+12); //打开跑秒的地址
write_date(0x30+paomiao);//按照表格 显示跑秒的数字
}
}
// while(1);
}
void timer0() interrupt 1
{
TH0=0x3c;
TL0=0xb0;
count++;
}
友情提示: 此问题已得到解决,问题已经关闭,关闭后问题禁止继续编辑,回答。
他用的是ubuntu 嘿嘿~~
if(count==2)
{
count=0;
这里就晚了
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit lcdrs=P3^4;
sbit lcden=P3^7;
sbit lcdrw=P3^6;
sbit K4=P2^3;
sbit s1=P2^7;
sbit s2=P2^6;
sbit s3=P2^5;
sbit a1=P1^0;
sbit a2=P1^1;
sbit a3=P1^2;
sbit a4=P1^3;
sbit a5=P1^4;
uchar count,s1num;
char miao,shi,fen;
uchar code table[]=" 2011-06-17 MON";
sbit sda=P1^6;
sbit scl=P1^5;
void delay(uint z)
{
uint x,y;
for(x=z;x>0;x--)
for(y=120;y>0;y--);
}
void delay0() //微妙延迟函数
{ ;; }
void start() //开始信号
{
sda=1;
delay0();
scl=1;
delay0();
sda=0;
delay0();
}
void stop() //停止
{
sda=0;
delay0();
scl=1;
delay0();
sda=1;
delay0();
}
void respons() //应答
{
uchar i;
scl=1;
delay0();
while((sda==1)&&(i<250))i++;
scl=0;
delay0();
}
void init_24c02() //I2C初始化函数
{
sda=1;
delay0();
scl=1;
delay0();
}
void write_byte(uchar date)
{
uchar i,temp;
temp=date;
for(i=0;i<8;i++)
{
temp=temp<<1;
scl=0;
delay0();
sda=CY;
delay0();
scl=1;
delay0();
}
scl=0;
delay0();
sda=1;
delay0();
}
uchar read_byte()
{
uchar i,k;
scl=0;
delay0();
sda=1;
delay0();
for(i=0;i<8;i++)
{
scl=1;
delay0();
k=(k<<1)|sda;
scl=0;
delay0();
}
return k;
}
void write_add(uchar address,uchar date)
{
start();
write_byte(0xa0);
respons();
write_byte(address);
respons();
write_byte(date);
respons();
stop();
}
uchar read_add(uchar address)
{
uchar date;
start();
write_byte(0xa0);
respons();
write_byte(address);
respons();
start();
write_byte(0xa1);
respons();
date=read_byte();
stop();
return date;
}
//
void write_com(uchar com)
{
lcdrs=0;
lcdrw=0;
lcden=0;
P0=com;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_date(uchar date)
{
lcdrs=1;
lcdrw=0;
lcden=0;
P0=date;
delay(5);
lcden=1;
delay(5);
lcden=0;
}
void write_sfm(uchar add,uchar date)
{
uchar shi,ge;
shi=date/10;
ge=date%10;
write_com(0x80+0x40+add);
write_date(0x30+shi);
write_date(0x30+ge);
}
void init() //初始化函数
{
uchar num;
K4=0;
a1=a2=a3=a4=a5=0;
lcden=0;
fen=0;
miao=0;
shi=0;
count=0;
s1num=0;
init_24c02();
write_com(0x38);
write_com(0x0c);
write_com(0x06);
write_com(0x01);
write_com(0x80);
for(num=0;num<15;num++)
{
write_date(table[num]);
delay(5);
}
write_com(0x80+0x40+6);
write_date(':');
delay(5);
write_com(0x80+0x40+9);
write_date(':');
delay(5);
miao=read_add(1);
fen=read_add(2);
shi=read_add(3);
write_sfm(10,miao);
write_sfm(7,fen);
write_sfm(4,shi);
TMOD=0x01;
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
EA=1;
ET0=1;
TR0=1;
}
void keyscan() //按键扫描函数
{
if(s1==0)
{
delay(5);
if(s1==0)
{ s1num++;
while(!s1);
if(s1num==1)
{
TR0=0;
write_com(0x80+0x40+10);
write_com(0x0f);
}
}
if(s1num==2)
{
write_com(0x80+0x40+7);
}
if(s1num==3)
{
write_com(0x80+0x40+4);
}
if(s1num==4)
{
s1num=0;
write_com(0x0c);
TR0=1;
}
}
if(s1num!=0)
{
if(s2==0)
{
delay(5);
if(s2==0)
{
while(!s2);
if(s1num==1)
{
miao++;
if(miao==60)
miao=0;
write_sfm(10,miao);
write_com(0x80+0x40+10);
write_add(1,miao); //数据改变立即存入24c02
}
if(s1num==2)
{
fen++;
if(fen==60)
fen=0;
write_sfm(7,fen);
write_com(0x80+0x40+7);
write_add(2,fen); //数据改变立即存入24c02
}
if(s1num==3)
{
shi++;
if(shi==24)
shi=0;
write_sfm(4,shi);
write_com(0x80+0x40+4);
write_add(3,shi); //数据改变立即存入24c02
}
}
}
if(s3==0)
{
delay(5);
if(s3==0)
{
while(!s3);
if(s1num==1)
{
miao--;
if(miao==-1)
miao=59;
write_sfm(10,miao);
write_com(0x80+0x40+10);
write_add(1,miao); //数据改变立即存入24c02
}
if(s1num==2)
{
fen--;
if(fen==-1)
fen=59;
write_sfm(7,fen);
write_com(0x80+0x40+7);
write_add(2,fen); //数据改变立即存入24c02
}
if(s1num==3)
{
shi--;
if(shi==-1)
shi=23;
write_sfm(4,shi);
write_com(0x80+0x40+4);
write_add(3,shi); //数据改变立即存入24c02
}
}
}
}
}
void main()
{
init();
while(1)
{
keyscan();
}
}
void timer0() interrupt 1
{
TH0=(65536-50000)/256;
TL0=(65536-50000)%256;
count++;
if(count==20)
{
count=0;
miao++;
if(miao==60)
{
miao=0;
fen++;
if(fen==60)
{
fen=0;
shi++;
if(shi==24)
{
shi=0;
}
write_sfm(4,shi);
write_add(3,shi); //数据改变立即存入24c02
}
write_sfm(7,fen);
write_add(2,fen); //数据改变立即存入24c02
}
write_sfm(10,miao);
write_add(1,miao); //数据改变立即存入24c02
}
}
一周热门 更多>