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);}
}
}
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);}
}
}