按键控制实验
#include "key.h"void KEY_Init(void)
{EALLOW;SysCtrlRegs.PCLKCR3.bit.GPIOINENCLK = 1;// 开启GPIO时钟//KEY端口配置GpioCtrlRegs.GPAMUX1.bit.GPIO12=0;GpioCtrlRegs.GPADIR.bit.GPIO12=0;GpioCtrlRegs.GPAPUD.bit.GPIO12=0;GpioCtrlRegs.GPAMUX1.bit.GPIO13=0;GpioCtrlRegs.GPADIR.bit.GPIO13=0;GpioCtrlRegs.GPAPUD.bit.GPIO13=0;GpioCtrlRegs.GPAMUX1.bit.GPIO14=0;GpioCtrlRegs.GPADIR.bit.GPIO14=0;GpioCtrlRegs.GPAPUD.bit.GPIO14=0;GpioCtrlRegs.GPBMUX2.bit.GPIO48=0;GpioCtrlRegs.GPBDIR.bit.GPIO48=1;GpioCtrlRegs.GPBPUD.bit.GPIO48=0;GpioCtrlRegs.GPBMUX2.bit.GPIO49=0;GpioCtrlRegs.GPBDIR.bit.GPIO49=1;GpioCtrlRegs.GPBPUD.bit.GPIO49=0;GpioCtrlRegs.GPBMUX2.bit.GPIO50=0;GpioCtrlRegs.GPBDIR.bit.GPIO50=1;GpioCtrlRegs.GPBPUD.bit.GPIO50=0;EDIS;GpioDataRegs.GPBSET.bit.GPIO48=1;GpioDataRegs.GPBSET.bit.GPIO49=1;GpioDataRegs.GPBSET.bit.GPIO50=1;}char KEY_Scan(char mode)
{static char keyl1=1;static char keyl2=1;static char keyl3=1;//第1列扫描KEY_L1_SetL;KEY_L2_SetH;KEY_L3_SetH;if(keyl1==1&&(KEY_H1==0||KEY_H2==0||KEY_H3==0)){DELAY_US(10000);keyl1=0;if(KEY_H1==0){return KEY1_PRESS;}else if(KEY_H2==0){return KEY4_PRESS;}else if(KEY_H3==0){return KEY7_PRESS;}}else if(KEY_H1==1&&KEY_H2==1&&KEY_H3==1){keyl1=1;}if(mode)keyl1=1;//第2列扫描KEY_L2_SetL;KEY_L1_SetH;KEY_L3_SetH;if(keyl2==1&&(KEY_H1==0||KEY_H2==0||KEY_H3==0)){DELAY_US(10000);keyl2=0;if(KEY_H1==0){return KEY2_PRESS;}else if(KEY_H2==0){return KEY5_PRESS;}else if(KEY_H3==0){return KEY8_PRESS;}}else if(KEY_H1==1&&KEY_H2==1&&KEY_H3==1){keyl2=1;}if(mode)keyl2=1;//第3列扫描KEY_L3_SetL;KEY_L1_SetH;KEY_L2_SetH;if(keyl3==1&&(KEY_H1==0||KEY_H2==0||KEY_H3==0)){DELAY_US(10000);keyl3=0;if(KEY_H1==0){return KEY3_PRESS;}else if(KEY_H2==0){return KEY6_PRESS;}else if(KEY_H3==0){return KEY9_PRESS;}}else if(KEY_H1==1&&KEY_H2==1&&KEY_H3==1){keyl3=1;}if(mode)keyl3=1;return KEY_UNPRESS;
}
#ifndef KEY_H_
#define KEY_H_#include "DSP2833x_Device.h"
#include "DSP2833x_Examples.h"#define KEY_L1_SetL (GpioDataRegs.GPBCLEAR.bit.GPIO48=1)
#define KEY_L2_SetL (GpioDataRegs.GPBCLEAR.bit.GPIO49=1)
#define KEY_L3_SetL (GpioDataRegs.GPBCLEAR.bit.GPIO50=1)#define KEY_L1_SetH (GpioDataRegs.GPBSET.bit.GPIO48=1)
#define KEY_L2_SetH (GpioDataRegs.GPBSET.bit.GPIO49=1)
#define KEY_L3_SetH (GpioDataRegs.GPBSET.bit.GPIO50=1)#define KEY_H1 (GpioDataRegs.GPADAT.bit.GPIO12)
#define KEY_H2 (GpioDataRegs.GPADAT.bit.GPIO13)
#define KEY_H3 (GpioDataRegs.GPADAT.bit.GPIO14)#define KEY1_PRESS 1
#define KEY2_PRESS 2
#define KEY3_PRESS 3
#define KEY4_PRESS 4
#define KEY5_PRESS 5
#define KEY6_PRESS 6
#define KEY7_PRESS 7
#define KEY8_PRESS 8
#define KEY9_PRESS 9
#define KEY_UNPRESS 0void KEY_Init(void);
char KEY_Scan(char mode);#endif /* APP_KEY_KEY_H_ */
#include "DSP2833x_Device.h"
#include "DSP2833x_Examples.h"
#include "leds.h"
#include "beep.h"
#include "relay.h"
#include "key.h"void main()
{int i = 0;char key = 0;InitSysCtrl();//系统时钟初始化,默认已开启F28335所有外设时钟LED_Init();BEEP_Init();Relay_Init();KEY_Init();RELAY_ON;while(1){key = KEY_Scan(0);switch(key){case KEY1_PRESS:LED2_TOGGLE;break;case KEY2_PRESS:LED3_TOGGLE;break;case KEY3_PRESS:LED4_TOGGLE;break;case KEY4_PRESS:LED5_TOGGLE;break;case KEY5_PRESS:LED6_TOGGLE;break;case KEY6_PRESS:LED7_TOGGLE;break;}i++;if(i%200){LED1_TOGGLE;}DELAY_US(1000);//系统的延迟函数单位是us 此处为延迟100us}
}