Giao tiếp với bàn phím 4x4 bằng tm4c123G

Nguyen Minh Minh

Trứng gà
Xin chào mọi người, mình đang thử giao tiếp ma trận phím bấm 4x4 bàng tm4c123g Đoạn code mình chưa trả về giá trị phím bấm, mình không biết tại sao, xim mọi người giúp đỡ. Thank. (Khi nối các chân của bàn phím mình nối trực tiếp vào port)

code
#include <stdint.h>
#include <stdbool.h>
#include "inc/hw_types.h"
#include "inc/hw_memmap.h"
#include "driverlib/sysctl.h"
#include "driverlib/gpio.h"

//
#define ROW1 GPIO_PIN_0
#define ROW2 GPIO_PIN_1
#define ROW3 GPIO_PIN_2
#define ROW4 GPIO_PIN_3

#define COL1 GPIO_PIN_4
#define COL2 GPIO_PIN_5
#define COL3 GPIO_PIN_6
#define COL4 GPIO_PIN_7

#define RED_LED GPIO_PIN_1
#define BLUE_LED GPIO_PIN_2
#define GREEN_LED GPIO_PIN_3

static unsigned char ban_phim[4][4]={'1','2','3','A',

'4','5','6','B',

'7','8','9','C',

'*','0','#','D'};

int co_nhan_khong(void)

{
//cho cac cot la muc 0
GPIOPinWrite(GPIO_PORTB_BASE,ROW1|ROW2|ROW3|ROW4, 0b0000);


if
(GPIOPinRead(GPIO_PORTB_BASE, COL1)==0|GPIOPinRead(GPIO_PORTB_BASE, COL2)==0|GPIOPinRead(GPIO_PORTB_BASE, COL3)==0|GPIOPinRead(GPIO_PORTB_BASE, COL4)==0)
{ return 1;}
else
{
return 0;
}
}

unsigned char nut_nhan()
{
uint8_t i;
if(co_nhan_khong())

{

SysCtlDelay(200000);

if(co_nhan_khong())//chong_rung
{


for(i=0;i<4;i++)

{

switch(i)
{
case 0:
{GPIOPinWrite(GPIO_PORTB_BASE, ROW1|ROW2|ROW3|ROW4,0b1110);
break;}
case 1:
{GPIOPinWrite(GPIO_PORTB_BASE, ROW1|ROW2|ROW3|ROW4,0b1101);
break;}
case 2:
{GPIOPinWrite(GPIO_PORTB_BASE, ROW1|ROW2|ROW3|ROW4,0b1011);
break;}
case 3:
{GPIOPinWrite(GPIO_PORTB_BASE, ROW1|ROW2|ROW3|ROW4,0b0111);
break;}
}

if(GPIOPinRead(GPIO_PORTB_BASE, COL1)) return ban_phim[3-i][0];

if(GPIOPinRead(GPIO_PORTB_BASE, COL2)) return ban_phim[3-i][1];

if(GPIOPinRead(GPIO_PORTB_BASE, COL3)) return ban_phim[3-i][2];

if(GPIOPinRead(GPIO_PORTB_BASE, COL4)) return ban_phim[3-i][3];
}

}

}



return 0;
}


int main(void)
{

SysCtlClockSet(SYSCTL_SYSDIV_4 | SYSCTL_USE_PLL | SYSCTL_XTAL_16MHZ| SYSCTL_OSC_MAIN);

SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOF);
SysCtlPeripheralEnable(SYSCTL_PERIPH_GPIOB);

GPIOPinTypeGPIOOutput(GPIO_PORTF_BASE, RED_LED | BLUE_LED | GREEN_LED);
GPIOPinTypeGPIOInput(GPIO_PORTB_BASE,COL1|COL2|COL3|COL4);
GPIOPinTypeGPIOOutput(GPIO_PORTB_BASE, ROW1|ROW2|ROW3|ROW4);
unsigned char key;
key='h';


while(1)
{
key = nut_nhan();
if ((key=='1'))
{
GPIOPinWrite(GPIO_PORTF_BASE, RED_LED | BLUE_LED | GREEN_LED, RED_LED);
SysCtlDelay(20000000);}
else
{
GPIOPinWrite(GPIO_PORTF_BASE, RED_LED | BLUE_LED | GREEN_LED, 0);
SysCtlDelay(20000000);}

}
}
 

vodanhbk

Trứng gà
Bạn có tham khảo cách quét keypad và chống rung như sau

PHP:
#include <msp430G2553.h>
#include<string.h>
#include "LCD.h"
#include "Basic_config.h"
const unsigned char keypad[4] = { 0x70, 0xB0,0xD0,0xE0};
const unsigned char phim[4][4] = { '+', '9', '8', '7',
                                  '-', '6', '5', '4',
                                  '*', '3', '2', '1',
                                  '/', '=', '0','C', };
unsigned char keypush;
void Config_IO_Int (void)
{
    P1DIR |= 0x0F; // all columns are input
    P1DIR &= 0x0F; // all rows are output
    P1REN |= 0XF0; // enable pull up/down registers on all rows
    P1OUT |= 0XF0; // select pull up registers on all rows
    P2SEL &= ~BIT6 + ~BIT7;
    P2SEL2 &= ~BIT6 + ~BIT7;
    lcd_init(1); //khoi tao LCD co dieu khien nen
    lcd_backlight(1);  // mo den nen
    lcd_clear();
    lcd_put_byte(0,DON & CURSOR_ON & BLINK_ON);        // display on ,cursor on
    lcd_delay_ms(2);    // wait for LCD
}
unsigned char checkkeypad()
{
    unsigned char i, j, key;
    for (i = 0;i < 4; i++)
    {
        P1OUT = 0xFF - (1 << i);
        key = P1IN & 0xF0;
        if (key != 0xF0)
        {
            for (j=0; j<4; j++)
                if (key == keypad[j])
                {
                    while ((P1IN & 0xF0) != 0xF0);
                return phim[i][j];
                }
        }
 
    }
return 0;
}
 
/// main
void main(void)
{
    Config_stop_WDT(); // stop WDT, see description in Basic_config.c
    Config_IO_Int();
    while (1)
    {
        P1OUT &= 0XF0;
    if (P1IN != 0xF0)
    {
        keypush = checkkeypad();
        if (keypush!=0)
        {
            lcd_putc(keypush);
        }
    }
    }
 
}
 
Top