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

QQ登錄

只需一步,快速開(kāi)始

搜索
查看: 2269|回復(fù): 6
收起左側(cè)

單片機(jī)按鍵+定時(shí)程序循環(huán)問(wèn)題

[復(fù)制鏈接]
ID:468415 發(fā)表于 2022-7-8 09:28 | 顯示全部樓層 |閱讀模式
麻煩大佬給看一下,這個(gè)程序中,count賦值大一點(diǎn)(目前是1),按鍵在按的時(shí)候就不正常了,單片機(jī)源碼如下:
#include "reg52.h"

/*宏定義*/
#define FOSC 11059200L
#define T1MS (65536-FOSC/12/1000)   /*1ms timer calculation method in 12Tmode*/
#define const_key_time_short1  60    /*短按的按鍵去抖延時(shí)時(shí)間*/
#define const_key_time_long1   1000     /*長(zhǎng)按的按鍵去抖延時(shí)時(shí)間*/
#define const_key_time_short2  60    /*短按的按鍵去抖延時(shí)時(shí)間*/
#define const_key_time_long2   1000     /*長(zhǎng)按的按鍵去抖延時(shí)時(shí)間*/
/*變量函數(shù)定義及申明*/
sbit ADDR0 = P1^0;
sbit ADDR1 = P1^1;
sbit ADDR2 = P1^2;
sbit ADDR3 = P1^3;
sbit ENLED = P1^4;
sbit Key_S1 = P2^4;
sbit Breathe_Led = P0^0;
sbit Led_Red = P0^1;
sbit Led_Green = P0^2;
sbit V_buss = P0^3;
sbit Op_Ion_Fan = P0^4;

unsigned char ucKeySec = 0;   /*被觸發(fā)的按鍵編號(hào)*/
unsigned int  uiKeyTimeCnt1 = 0; /*按鍵去抖延時(shí)計(jì)數(shù)器*/
unsigned char ucKeyLock1 = 0; /*按鍵觸發(fā)后自鎖的變量標(biāo)志*/
unsigned char ucShortTouchFlag1=0; /*短按觸發(fā)標(biāo)志*/
unsigned int  uiVoiceCnt = 0;  /*蜂鳴器叫的持續(xù)時(shí)間計(jì)數(shù)器*/
unsigned int number;
unsigned int num;
unsigned int min;
unsigned int count;
unsigned char flag = 0;
unsigned char on_off = 0;
unsigned char flag1 = 0;

void Init_T0(void)
        {
        TMOD = 0x01;                    /*set timer0 as mode1 (16-bit)*/
        TL0 = T1MS;                     /*initial timer0 low byte*/
        TH0 = T1MS >> 8;                /*initial timer0 high byte*/
        }
        
void Init_Peripheral(void)
        {
        ET0 = 1;/*允許定時(shí)中斷*/
        TR0 = 1;/*啟動(dòng)定時(shí)中斷*/
        EA = 1;/*開(kāi)總中斷*/
        }

void Init(void)
        {
        Init_T0();
        Op_Ion_Fan = 0;
        Led_Green = 1;
        Led_Red = 1;
        Breathe_Led = 1;
        }
void Power_off()
{
        Op_Ion_Fan = 0;
        Led_Red = 1;
        Led_Green = 1;
        Breathe_Led = 1;
}        
void Work_Mode1(void)
{
        Op_Ion_Fan = 1;
        Led_Red = 1;
        Led_Green = 0;
        Breathe_Led = 0;
}        

void Work_Mode2(void)
{
        Op_Ion_Fan = 0;
        Led_Red = 1;
        Led_Green = 0;
        Breathe_Led = 0;
}

void Strong_Mode()
{
        Op_Ion_Fan = 1;
        Led_Red = 0;
        Led_Green = 1;
        Breathe_Led = 0;
}

void Key_Scan(void)
        {
/*掃描S1*/
                if(Key_S1 == 1) /*如果按鍵沒(méi)有被按下,將一些標(biāo)志位及時(shí)清零*/
                {
                ucKeyLock1 = 0;/*自鎖標(biāo)志位清0*/
                uiKeyTimeCnt1 = 0;/*按鍵去抖延時(shí)計(jì)數(shù)器清零*/
                        if(ucShortTouchFlag1 == 1)/*短按*/
                        {
                        ucShortTouchFlag1 = 0;
                        ucKeySec = 1; /*觸發(fā)S1短按*/
                        }
                }
        else if(ucKeyLock1 == 0) /*如果有按鍵按下,且是第一次按下*/
                {
                uiKeyTimeCnt1 ++;
                if(uiKeyTimeCnt1 > const_key_time_short1)/*判定短按*/
                        {
                        ucShortTouchFlag1 = 1;/*激活短按有效標(biāo)志位*/
                        }
                if(uiKeyTimeCnt1 > const_key_time_long1)/*判定長(zhǎng)按*/
                        {
                        ucShortTouchFlag1 = 0;/*清除短按有效標(biāo)志位*/
                        uiKeyTimeCnt1 = 0;
                        ucKeyLock1 = 1;/*自鎖按鍵置位,避免一直觸發(fā)*/
                        ucKeySec = 2; /*觸發(fā)S1長(zhǎng)按*/
                        }
                }        
        }

void key_Service(void)
{
        if((ucKeySec == 2) && (on_off == 0) )/*開(kāi)機(jī)并確定是否為USB供電*/
        {
                on_off = 1;
                ucKeySec = 0;
                flag = 1;
        }
        if((ucKeySec == 2) && (on_off == 1))
        {
                on_off = 0;
                Power_off();
                ucKeySec = 0;
        }
        if((on_off == 1) && (flag1 == 0)&& (flag == 1) && (count >= 1))
        {
                flag1 = 1;
                count   = 0;
                Work_Mode1();
        }
        if((on_off == 1) && (flag1 == 1) && (flag == 1) && (count >= 1))
        {
                flag1 = 0;
                count   = 0;
                Work_Mode2();
        }
        if((ucKeySec == 1) && (on_off == 1) && (flag == 1))
        {
                flag = 0;
                Strong_Mode();
                ucKeySec = 0;
        }
        if((ucKeySec == 1) && (on_off == 1) && (flag == 0))
        {
                flag = 1;
                ucKeySec = 0;
        }
}

void ISR_T0(void) interrupt 1
        {
        TF0 = 0;  /*清除中斷標(biāo)志*/
  TR0 = 0; /*關(guān)中斷*/
/*掃描按鍵*/
        Key_Scan();
        TL0 = T1MS;                     /*initial timer0 low byte*/
        TH0 = T1MS >> 8;                /*initial timer0 high byte*/
  TR0 = 1; /*開(kāi)中斷*/        
        number++;
        if(number >= 100)
        {
                num++;
                number = 0;
                }
                if(num>=10)
                {
                        num = 0;
                        count ++;
                }
        }


void Delay_Long(unsigned int uiDelayLong)
        {
   unsigned int i;
   unsigned int j;
   for(i=0;i<uiDelayLong;i++)
                        {
      for(j=0;j<500;j++)  /*內(nèi)嵌循環(huán)的空指令數(shù)量*/
         {
             ; /*一個(gè)分號(hào)相當(dāng)于執(zhí)行一條空語(yǔ)句*/
         }
                        }
        }

        /*主函數(shù)*/
void main()
        {
        /*單片機(jī)初始化*/
        Init();
        /*延時(shí),延時(shí)時(shí)間一般是0.3-2秒之間,等待外圍芯片和模塊上電穩(wěn)定*/
        Delay_Long(100);
        /*單片機(jī)外圍初始化*/
        Init_Peripheral();
        ENLED = 0;
        ADDR3 = 1;
        ADDR2 = 1;
        ADDR1 = 1;
        ADDR0 = 0;
        P2 = 0xF7;
        while(1)
                {
                /*按鍵服務(wù)函數(shù)*/
        key_Service();
                }
        }
回復(fù)

使用道具 舉報(bào)

ID:883242 發(fā)表于 2022-7-8 13:00 | 顯示全部樓層
先把count改成char型試試。
回復(fù)

使用道具 舉報(bào)

ID:468415 發(fā)表于 2022-7-8 13:02 | 顯示全部樓層
Hephaestus 發(fā)表于 2022-7-8 13:00
先把count改成char型試試。

好的,我試下,謝謝!
回復(fù)

使用道具 舉報(bào)

ID:140489 發(fā)表于 2022-7-8 13:07 | 顯示全部樓層
if(num>=10)
                {
                        num = 0;
                        count ++;
                }

把count++改成count=1;試試
回復(fù)

使用道具 舉報(bào)

ID:468415 發(fā)表于 2022-7-8 13:25 | 顯示全部樓層
lids 發(fā)表于 2022-7-8 13:07
if(num>=10)
                {
                        num = 0;

count = 1 的話,后面如何增加這個(gè)數(shù)值呢?因?yàn)槟壳癱ount = 1 就是1秒鐘,后面如何做時(shí)間加長(zhǎng)處理呢?
回復(fù)

使用道具 舉報(bào)

ID:213173 發(fā)表于 2022-7-8 14:09 | 顯示全部樓層
Key_Scan函數(shù)已經(jīng)放在T0中斷函數(shù)中運(yùn)行,其中uiKeyTimeCnt1就是1ms計(jì)時(shí)單位,鍵值ucKeySec=2就含有長(zhǎng)按1秒的時(shí)間信息,再增加count判斷就多余了。把number、num、count統(tǒng)統(tǒng)去掉。
回復(fù)

使用道具 舉報(bào)

ID:468415 發(fā)表于 2022-7-8 14:23 | 顯示全部樓層
wulin 發(fā)表于 2022-7-8 14:09
Key_Scan函數(shù)已經(jīng)放在T0中斷函數(shù)中運(yùn)行,其中uiKeyTimeCnt1就是1ms計(jì)時(shí)單位,鍵值ucKeySec=2就含有長(zhǎng)按1秒 ...

count是1秒定時(shí),里面有一個(gè)循環(huán)模式,這個(gè)變量值是需要調(diào)整的,有可能是20秒,也有可能是60秒,所以是需要這個(gè)count的
回復(fù)

使用道具 舉報(bào)

本版積分規(guī)則

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

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

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