大哲老师 发表于 2024-6-16 11:59:47

我还是直接硬件延时吧哈哈哈哈

fjstcmcu 发表于 2024-6-16 15:19:05

zhudean11 发表于 2023-12-28 09:46
是的,定时器资源必定有限

STC的定时器够多了

zxyyl 发表于 2024-8-28 20:29:05

zhudean11 发表于 2023-12-28 09:45
可否分享个例程,谢谢!

#include "key.h"
#include "intrins.h"

unsigned short key=0;
void Timer0_Isr(void) interrupt 1
{
        static int a=0;
        if(a==0)get_line_a();
        if(a==1)get_line_b();
        if(a==2)get_line_c();
        if(a==3)get_line_d();
        a++;
        (a)%=4;
}
void Delay10us(void)        //@24.000MHz
{
        unsigned char data i;

        _nop_();
        _nop_();
        i = 57;
        while (--i);
}

void Timer0_Init(void)                //20毫秒@24.000MHz
{
        AUXR &= 0x7F;                        //定时器时钟12T模式
        TMOD &= 0xF0;                        //设置定时器模式
        TL0 = 0xC0;                                //设置定时初始值
        TH0 = 0x63;                                //设置定时初始值
        TF0 = 0;                                //清除TF0标志
        TR0 = 1;                                //定时器0开始计时
        ET0 = 1;                                //使能定时器0中断
        PT0=1;
        PT0H=1;
        EA=1;
}

int get_line_a()
{
        P52=1;
        P1=~0x01;
        Delay10us();
        if(P14==0)
        {
                key=13;
                return 0;
        }
        if(P15==0)
        {
                key=9;
                return 0;
        }
        if(P16==0)
        {key=5;return 0;}
        if(P17==0){key=1;return 0;}
        return 0;
}

int get_line_b()
{
        P52=1;
        P1=~0x02;
        Delay10us();
        if(P14==0)
        {
                key=14;
                return 0;
        }
        if(P15==0)
        {
                key=10;
                return 0;
        }
        if(P16==0){key=6;return 0;}
        if(P17==0){key=2;return 0;}
        return 0;
}
int get_line_c()
{
        P1=~0x04;
        P52=0;
        Delay10us();
        if(P15==0)
        {
                key=11;
                P52=1;
                return 0;
        }
        if(P14==0)
        {
                key=15;
                P52=1;
                return 0;
        }
        if(P16==0){key=7;P52=1;return 0;}
        if(P17==0){key=3;P52=1;return 0;}
        P52=1;
        return 0;
}
int get_line_d()
{
        P52=1;
        P1=~0x08;
        Delay10us();
        if(P14==0)
        {
                key=16;
                return 0;
        }
        if(P15==0)
        {
                key=12;
                return 0;
        }
        if(P16==0){key=8;return 0;}
        if(P17==0){key=4;return 0;}
        return 0;
}
这个是32g的程序,扫描16个按键,是使用的江科大的板子,用降龙棍作为主控,没有main函数,因为这个是一个模块,在主函数里调用即可

zhudean11 发表于 2024-8-29 06:25:13

zxyyl 发表于 2024-8-28 20:29
#include "key.h"
#include "intrins.h"



{:4_196:}感谢。学习一下

bkeuqoaq 发表于 2024-11-1 10:18:35

定时扫描,记录连续按下的次数,从记录次数很容易导出短按,长按,连按,松开,按下等信息

wlhet 发表于 2024-11-4 08:00:18

学习了 感谢
页: 1 2 [3]
查看完整版本: 按键扫描优化方法