这是论坛一个大师的程序我仿制后只能计数255就归零了怎么回事,劳烦各位老师看下程序哪儿要改,谢谢!
- #include <reg51.h>
- sbit key0=P1^0;
- sbit key1=P1^1; //定义key0,key1接口
- unsigned char flag,con,mcount; //flag为计数到一秒时给时钟发的信号
- unsigned char count = 0;
- unsigned char seccount =0; //作为时钟的秒的计量单位
- unsigned char hour = 0; //时钟的时
- unsigned char min = 0; //时钟的分
- unsigned char sec = 0; //时钟的秒
- unsigned char keypress0,keypress1,mode,ms,s,ss,start;
- unsigned char
- Displaynum[]={0x14,0xd7,0x4c,0x45,0x87,0x25,0x24,0x57,0x04,0x05, 0x10,0xd3,0x48,0x41,0x83,0x21,0x20,0x53,0x00,0x01,0xff,0xfb};
- //数码管十六进制译码表
- unsigned char controlnum[]={0x08,0x04,0x02,0x01}; //控制四个数码管显示
- unsigned char DisplayBuff[]={0x00, 0x00, 0x00, 0x00}; //每个数码管的值
- void display_state(); //数码管显示子程序
- void keyscan(); //键盘扫描子程序
- void timer(); //秒表子程序
- unsigned char count1,flag1,t_adjust=0,tm_adjust=0;
- unsigned char bKey0Pressed = 0, bKey1Pressed = 0;
- unsigned char ucStatus = 0;
- void main()
- {
- TMOD = 0x01; //定时器0工作在模式1,GATE=0,C/t=0,16位模
- TH0= (2^16-5000)/256;//定时器0 预存值,
- TL0= (2^16-5000)%256;
- TR0= 1; //定时器0 启动
- ET0= 1; //定时器中断0 允许
- EA= 1; //开中断
- IT0=1; //外部中断0下降沿触发
- IT1=1; //外部中断1 下降沿触发
- EX0=1; //外部中断0允许
- EX1=1; //外部中断1允许
- PX1=1; //外部中断1 为高优先级中断
- while(1) //死循环,
- {
- if(flag == 1) //时钟显示
- {
- flag = 0;
- sec++;
- if(sec ==60)
- {sec = 0;
- min++;
- if(min ==60)
- {min = 0;
- hour ++;
- if(hour == 24)
- {
- hour = 0;
- }
- }
- }
- }
- display_state();
- keyscan();
- timer();
- }
- }
- //////////////////////////////////ISR中断服务程序,每5ms产生一个定时器0 中断
- void Display() interrupt 1
- {
- TH0 = (2^16-5000)/256;
- TL0 = (2^16-5000)%256;
- seccount ++;
- mcount ++;
- keypress0++;
- keypress1++;
- P2=0x00; //P2口先清零
- P0 = Displaynum[DisplayBuff[count]]; //P0口输出
- P2 = controlnum[count]; //P2口输出
- count++;
- if(count==4)
- count=0;
- if(seccount == 200) //200*5ms=1s
- { seccount =0;
- flag =1; //每过1秒给时钟信号加一秒
- }
- }
- ///////////////////////////////////////////
- void display_state()
- {
- if(mode == 0) //时钟模式,显示时间
- {
- DisplayBuff[0] = hour/10;
- DisplayBuff[1] = (hour%10)+10; //"+10"是为了显示加点的数字
- DisplayBuff[2] = min/10;
- DisplayBuff[3] = min%10;
- }
- if(mode == 1) //秒表模式,显示秒表
- {
- DisplayBuff[0] = ss +10; //"+10"是为了显示加点的数字
- DisplayBuff[1] = s/10;
- DisplayBuff[2] = (s%10)+10;
- DisplayBuff[3] = ms;
- }
- if(mode == 2) // 计数器模式,显示计数
- {
- DisplayBuff[0] = count1/1000; //千位
- DisplayBuff[1] = (count1-(count1/1000)*1000)/100; //百位
- DisplayBuff[2] = (count1-(count1/100)*100)/10; //十位
- DisplayBuff[3] = count1%10; //个位
- }
- if(mode==3) //显示时钟的秒,
- {
- DisplayBuff[0] = 20; //第一位不显示
- DisplayBuff[1] = 21; //第二位显示一个点
- DisplayBuff[2] = sec/10;//显示秒的十位
- DisplayBuff[3] = sec%10;//显示秒的个位
- }
- }
- //**************//
- //键盘扫描子程序//
- //**************//
- void keyscan()
- {
- if(!key0 && bKey0Pressed == 0) //检测到key0按下
- {
- keypress0 = 0;
- bKey0Pressed = 1; //防止重入此判断
- while(!key0); //如果没放开,就等下去
- }
- if(key0 && bKey0Pressed == 1) //检测到key0放开
- {
- bKey0Pressed = 0; //防止重入此判断
- if (keypress0<120) //如果是短按
- {
- if(ucStatus == 0x02)
- {
- ucStatus = 0x00;
- mode = 0; //时钟模式
- t_adjust=0;
- }
- else if(ucStatus == 0x00)
- {
- ucStatus = 0x01;
- mode = 1; //秒表模式
- }
- else if(ucStatus == 0x01)
- {
- ucStatus = 0x02;
- mode = 2; //计数模式
- }
- }
- else if(keypress0>200) //如果是长按
- mode=3; //显示时钟的秒的模式
- }
- ////////////////////////////////
- if(mode == 0) //进入时钟模式后
- {
- if(!key1 && (bKey1Pressed == 0))
- { keypress1 = 0;
- bKey1Pressed = 1;
- while(!key1);
- }
- if(key1 && (bKey1Pressed == 1))
- {
- bKey1Pressed = 0;
- if(keypress1 >= 200) //如果是长按
- {
- t_adjust++;
- if(t_adjust==3)
- t_adjust=0;
- }
- else if(keypress1 < 120)
- switch (t_adjust)
- {
- case 1:{ //调整分
- min++;
- sec = 0;
- if(min == 60)
- min = 0;
- }
- break;
- case 2: { //调整时
- hour ++ ;
- sec = 0;
- if(hour == 24)
- hour = 0;
- }
- break;
- default:break;
- }
- }
- }
- else if(mode == 1) //进入秒表模式
- {
- if(!key1 && bKey1Pressed == 0)
- {
- keypress1 = 0;
- bKey1Pressed = 1;
- while(!key1);
- }
- if(key1 && bKey1Pressed == 1)
- {
- bKey1Pressed = 0;
- if(keypress1 < 120) //短按
- {
- if(flag1==0) { start=2;flag1=1;} //start=2代表秒表开始计时
- else {start=0;flag1=0;} //start=0,秒表停止计时
- }
- else if(keypress1>200) //长按
- start=1; //秒表清零
- }
- }
- }
- void timer() //秒表子程序
- {
- if(mode==1) //进入秒表模式才执行
- {
- if(start==1) //清零
- {
- ms = 0;
- s = 0;
- ss = 0;
- mcount=0;
- }
- if(start==2) //秒表开始计时
- {
- if(mcount==20) //20*5ms=0.1s
- {
- mcount=0;
- ms++;
- if(ms == 10) // 10*0.1s=1s,s++
- {ms = 0;
- s ++;
- if(s == 100) //100s
- {s = 0;
- ss ++;
- if(ss == 10)
- {ss = 0;
- s = 0;
- ms = 0;
- }
- }
- }
- }
- }
- if (start==0);//秒表停止
- }
- }
- //////////////////////
- void it0(void) interrupt 0 using 1 //外部中断0 服务程序
- {
- if(mode==2) //只有在计数模式才执行
- {
- count1++; //每响应一次,计数加一
- if(count1==10000) //超出四位时,不能再加
- count1 =9999;
- }
- }
- void it2(void) interrupt 2 using 2 //外部中断0 服务程序
- {
- if(mode==2) //只有在计数模式才执行
- {
- if(count1!=0)
- {count1--;} //计数减一,不显示负数,当减到零时便不能再减
- }
- }
复制代码
|