找回密碼
 立即注冊(cè)

QQ登錄

只需一步,快速開始

搜索
查看: 2427|回復(fù): 5
打印 上一主題 下一主題
收起左側(cè)

需要通過按鍵K2、K3分別實(shí)現(xiàn)長按連續(xù)加、連續(xù)減的功能,短按已實(shí)現(xiàn),如何修改單片機(jī)程序

[復(fù)制鏈接]
跳轉(zhuǎn)到指定樓層
樓主
謝謝大家,單片機(jī)源程序如下:
#include<reg52.h>
#define uchar unsigned char
#define uint unsigned int
sbit k1=P1^0;
sbit k2=P1^1;
sbit k3=P1^2;
sbit k4=P1^3;
sbit buz=P3^0;
uchar code duan[]={0x3f,0x06,0x5b,0x4f,0x66,0x6d,0x7d,0x07,0x7f,0x6f,0x00};
uchar count,shi,ge,dcount,dicount;
unsigned int n = 0;
void delay(int ms)
{
   uchar i,j;
   for(i=ms;i>0;i--)
   for(j=200;j>0;j--);
}
void init()
{
        P0=0x6f;
        P2=0x6f;
        P1=0xff;
        buz=0;
        TMOD=0x01;
        IE=0x82;
        TH0=(65536-50000)/256;
        TL0=(65536-50000)%256;
        TR0=1;
        dcount=99;      
}
void keyscan()
{
     if(k1==0)
     {    delay(5);
          if(k1==0)
         {   
                  TR0=0;
         }
      }               
     if(TR0==0)
     {
       if(k2==0)
      {  delay(5);
if(k2==0)
              {
                  dcount++;
                  if(dcount==100)
                  dcount=0;
                  while(!k2)
              {      
                   shi=dcount/10;
                   ge=dcount%10;
                    P0=duan[shi];
                     P2=duan[ge];
               }
               }

        }
                      if(k3==0)
        {             delay(5);
                    if(k3==0)
               {
                       dcount--;
                      if(dcount==-1)
                  dcount=99;
                    while(!k3)
                {
                    shi=dcount/10;
                         ge=dcount%10;
                            P0=duan[shi];
                           P2=duan[ge];
                 }
               }

                        }
                }

              if(k4==0)
              {       delay(5);
                      if(k4==0)
                  {
                        TR0=1;

                   }
              }  



}
void timer0() interrupt 1
{
        TH0=(65536-50000)/256;
        TL0=(65536-50000)%256;
        if(++count==20)
        {
        count=0;
        dcount--;
        if(dcount==-1)
        {
        TR0=0;
        while(k1!=0)
          {
                P0=0x00;
                P2=0x00;
                buz=1;
                delay(1000);
                P0=0x3f;
                P2=0x3f;
                buz=0;
                delay(1000);
                }
        dcount=99;
        }
        shi=dcount/10;
        ge=dcount%10;
        P0=duan[shi];
        P2=duan[ge];
        }

}
void main()
{
   init();
   while(1)
     {
      keyscan();
      }

}

屏幕截圖 2022-12-19 150929.jpg (174.19 KB, 下載次數(shù): 62)

屏幕截圖 2022-12-19 150929.jpg
分享到:  QQ好友和群QQ好友和群 QQ空間QQ空間 騰訊微博騰訊微博 騰訊朋友騰訊朋友
收藏收藏1 分享淘帖 頂 踩
回復(fù)

使用道具 舉報(bào)

沙發(fā)
ID:45879 發(fā)表于 2022-12-19 17:57 | 只看該作者
在松開按鍵里面做文章  while(!k2)   while(!k3)
回復(fù)

使用道具 舉報(bào)

板凳
ID:1056573 發(fā)表于 2022-12-20 02:10 | 只看該作者
設(shè)置個(gè)flag標(biāo)志,增加void keyDown()和void keyUP():按下鍵flag=1,松開按鍵flag=0。在void keyscan()里加while(flag)函數(shù),若果flag==1就循環(huán)連續(xù)加或減,若flag==0就退出循環(huán)。

評(píng)分

參與人數(shù) 1黑幣 +20 收起 理由
admin + 20 共享資料的黑幣獎(jiǎng)勵(lì)!

查看全部評(píng)分

回復(fù)

使用道具 舉報(bào)

地板
ID:1056573 發(fā)表于 2022-12-20 03:47 | 只看該作者
加void Keyup()和void KeyDown(),設(shè)置個(gè)flag標(biāo)志按鍵按下和松開。
回復(fù)

使用道具 舉報(bào)

5#
ID:161164 發(fā)表于 2022-12-20 09:55 | 只看該作者
  1. while(!k2)
  2. {
  3.         shi=dcount/10;
  4.         ge=dcount%10;
  5.         P0=duan[shi];
  6.         P2=duan[ge];
  7.         delay1ms();
  8.         if(DelayCnt++>=500)
  9.         {
  10.                 DelayCnt=0;
  11.                 dcount++;
  12.         }
  13. }
復(fù)制代碼


回復(fù)

使用道具 舉報(bào)

6#
ID:839835 發(fā)表于 2022-12-20 11:19 | 只看該作者
不要在定時(shí)器中斷做這么多操作,只做計(jì)數(shù)即可。長按其實(shí)很簡單
void scan()
{
static  uin32t_t cnt;
if(k2== 0) //按鍵按下
{
   cnt++;
   if(cnt >=100000)//數(shù)據(jù)越大,時(shí)間越長
   {
     cnt  = 0;
      //長按功能
   }
}


}
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

小黑屋|51黑電子論壇 |51黑電子論壇6群 QQ 管理員QQ:125739409;技術(shù)交流QQ群281945664

Powered by 單片機(jī)教程網(wǎng)

快速回復(fù) 返回頂部 返回列表