简体   繁体   English

带有XC8 v1.43的PIC16F1717,用于按键的变量

[英]PIC16F1717 with XC8 v1.43, variable for keypad presses

I have a 4x3 matrix keypad connected to PORT B (RB1-RB7) of a PIC16F1717. 我有一个4x3矩阵键盘连接到PIC16F1717的端口B(RB1-RB7)。 I scan for keypad presses by setting each of the rows high in turn, and reading the column values. 我通过依次将每一行设置为高并读取列值来扫描键盘按键。 The key pressed can be decoded by matching the row and column. 可以通过匹配行和列来对按下的键进行解码。 I am debouncing with a short delay: 我的弹跳动作稍有延迟:

while(1)
{
    //scan for key presses
    __delay_ms(10);
    ROW1 = 1;
    ROW2 = 0;
    ROW3 = 0;
    ROW4 = 0;
    if (COL1 == 1)
    {
        __delay_ms(100);
        if (COL1 == 1)
        {
            key = 1;
            keyCount = keyCount ++ 1;
        }
    }
    else if (COL2 == 1)
    {
        __delay_ms(100);
        if (COL2 == 1)
        {
            key = 2;
            keyCount = keyCount + 1;

        }
    }
    //and so on for the other rows and columns

The two variable key and keyCount keep track of which key has been pressed, and the number of keys pressed, respectively. 两个变量key和keyCount分别跟踪已按下的键和按下的键数。 When 4 keys have been pressed, I want to execute the following code: 当按下4个键时,我要执行以下代码:

    if (keyCount == 4)
    {
        LED = 1;
        __delay_ms(500);
        LED = 0;
        __delay_ms(500);
        LED = 1;
        __delay_ms(500);
        LED = 0;
        __delay_ms(500);
        LED = 1;
        __delay_ms(500);
        servoDemo();
        __delay_ms(500);
        LED = 0;
        keyCount = 0;

    }

This is also in the while(1) loop inside the main function. 这也在main函数内部的while(1)循环中。 The problem I am having is that incrementing the keyCount does not work. 我遇到的问题是增加keyCount不起作用。 Previously, I tried keyCount++ and it worked, but before that this wouldn't work either. 以前,我尝试过keyCount ++并能正常工作,但在此之前也无法工作。 I tried debugging by setting keyCount = 4 instead of incrementing it by one, and it worked. 我尝试通过设置keyCount = 4而不是将其递增1来进行调试,并且它可以正常工作。

Both variables were declared (and initialised) inside the main function, but outside the while(1) loop. 这两个变量都在主函数中声明(并初始化),但在while(1)循环之外。 Also, I defined the following: 另外,我定义了以下内容:

//pin definitions
//////////////////////////
#define ROW1 PORTBbits.RB2
#define ROW2 PORTBbits.RB7
#define ROW3 PORTBbits.RB6
#define ROW4 PORTBbits.RB4

#define COL1 PORTBbits.RB3
#define COL2 PORTBbits.RB1
#define COL3 PORTBbits.RB5

#define SERVOSIG PORTDbits.RD0
#define LED PORTDbits.RD1

The problem is that you have no edge detection on your keys. 问题是您的按键上没有边缘检测。 That means that as long as you press a key the if statement is entered every loop cycle. 这意味着只要您按一个键,每个循环周期都将输入if语句。 Here is a small example which may give you the basic idea how to implement a loop detection. 这是一个小示例,可以为您提供实现循环检测的基本思路。

int key[12];
int key_old[12];

read_keys(key); //function that reads the keys in

while(1)
{
    memcpy(key_old, key, 12*sizeof(key[0])); //copy key to key_old
    read_keys(key); //function that reads the keys in

    for(int i=0;i<12;i++)
    {
        if(key[i] && !key_old[i])
        {
            //positive edge on key i
        }
    }

    __delay_ms(50); //Debouncing
}

This code is not perfect but should give a basic idea as mentioned. 这段代码并不完美,但是应该给出一个基本的想法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM