【MSPM0L1306 LaunchPad】按键输入测试
[复制链接]
本帖最后由 TL-LED 于 2023-10-12 22:41 编辑
这篇测试下开发板按键输入,通过LED1和LED2绿色指示灯,来指示按键输入变化。
一、硬件电路
开发板上的两个按键分别使用了PA14和PA18端口。
按键的硬件电路图部分如下;
二、程序
2.1、key.c
#include "key/key.h"
#include "led/led.h"
void key_init(void)
{
DL_GPIO_initDigitalInputFeatures(GPIO_SWITCHES_USER_SWITCH_2_IOMUX,
DL_GPIO_INVERSION_DISABLE, DL_GPIO_RESISTOR_PULL_UP,
DL_GPIO_HYSTERESIS_DISABLE, DL_GPIO_WAKEUP_DISABLE);
DL_GPIO_initDigitalInputFeatures(GPIO_SWITCHES_USER_SWITCH_1_IOMUX,
DL_GPIO_INVERSION_DISABLE, DL_GPIO_RESISTOR_NONE,
DL_GPIO_HYSTERESIS_DISABLE, DL_GPIO_WAKEUP_DISABLE);
}
uint8_t key_scan(uint8_t mode)
{
static uint8_t key_up = 1;
static uint8_t sw1_sta=0;
static uint8_t sw2_sta=1;
uint8_t keyval = 0;
if(DL_GPIO_readPins(GPIO_SWITCHES_PORT, GPIO_SWITCHES_USER_SWITCH_1_PIN))
{
sw1_sta=1;
}
else
{
sw1_sta=0;
}
if(!DL_GPIO_readPins(GPIO_SWITCHES_PORT, GPIO_SWITCHES_USER_SWITCH_2_PIN))
{
sw2_sta=1;
}
else
{
sw2_sta=0;
}
if (mode) key_up = 1;
if (key_up && ((sw2_sta == 1)||(sw1_sta == 1)))
{
delay_cycles(32000*10);
key_up = 0;
if (sw1_sta == 1) keyval = SW1_PRES;
if (sw2_sta == 1) keyval = SW2_PRES;
}
else if ((sw1_sta==0) && (sw2_sta==0))
{
key_up = 1;
}
return keyval;
}
void key_test(void)
{
uint8_t key;
key = key_scan(0);
if (key)
{
switch (key)
{
case SW1_PRES:
led1_r_tog();
break;
case SW2_PRES:
led2_g_tog();
break;
}
}
}
2.2、key.h
#ifndef __KEY_H
#define __KEY_H
#include <ti/driverlib/driverlib.h>
#define GPIO_SWITCHES_PORT (GPIOA)
#define GPIO_SWITCHES_USER_SWITCH_1_PIN (DL_GPIO_PIN_18)
#define GPIO_SWITCHES_USER_SWITCH_1_IOMUX (IOMUX_PINCM19)
#define GPIO_SWITCHES_USER_SWITCH_2_PIN (DL_GPIO_PIN_14)
#define GPIO_SWITCHES_USER_SWITCH_2_IOMUX (IOMUX_PINCM15)
#define SW1_PRES 1
#define SW2_PRES 2
void key_init(void);
uint8_t key_scan(uint8_t mode);
void key_test(void);
#endif
2.3、main.c
#include "ti_msp_dl_config.h"
#include "led/led.h"
#include "systick/systick.h"
#include "uart/uart.h"
#include "key/key.h"
int main(void)
{
SYSCFG_DL_init();
init_systick();
init_led();
init_uart(115200);
key_init();
printf("mspm0l1306 board test! \r\n");
while (1)
{
key_test();
}
}
三、运行结果
下载程序后,复位开发板运行
key
|