新建一个工程,将程序改成如下
#include <msp430.h>
#define uint8 unsigned char #define uint16 unsigned int
uint8 key_data(void)//读取按键是否被按下 { uint8 key_value=0; if((P1IN&BIT1) == 0) { __delay_cycles(10000);//延时防抖动 if((P1IN&BIT1) == 0) { key_value=1; while((P1IN&BIT1) == 0); } } return key_value; }
int main(void) { WDTCTL = WDTPW | WDTHOLD; // Stop WDT
// Configure GPIO P1DIR |= BIT0; // Clear P1.0 output latch for a defined power-on state P1DIR &= ~BIT1; //配置BIT1为输入 P1OUT|=BIT1; //配置 BIT1为1 P1REN |= BIT1; //配置BIT1上拉 /*当GPIO作为输入时,PxREN和PxOUT一起配合设置内部的上/下拉电阻, *PxREN置1时使能上/下拉电阻,PxOUT置1时上拉,置0时下拉*******/ PM5CTL0 &= ~LOCKLPM5; // Disable the GPIO power-on default high-impedance mode // to activate previously configured port settings
while(1) { uint8 key_value=0; key_value=key_data(); if(key_value==1) { key_value=0; P1OUT ^= BIT0; } } }
编译,运行,即可通过S1(P1.1)控制LED2(P1.0)了
本文参考:https://e2echina.ti.com/group/universityprogram/students/f/11/p/149721/424157#424157