#include "main.h" void LEDInit(void) { //for LED1 // GPIO_Init(GPION, PIN5, 1, 0, 0); //输出, 接RED LED // GPIO_Init(GPION, PIN6, 1, 0, 0); //输出, 接GREEN LED // GPIO_SetBit(GPION, PIN6); // GPIO_ClrBit(GPION, PIN5); //for LD1,LD2 GPIO_Init(GPION, PIN1, 1, 0, 0); //GPION.1 配置为输出引脚,推挽输出 GPIO_Init(GPION, PIN2, 1, 0, 0); //GPION.2 配置为输出引脚,推挽输出 GPIO_SetBit(GPION, PIN1); GPIO_ClrBit(GPION, PIN2); } void KEYInit(void) { //for code-8 GPIO_Init(GPION, PIN9, 0, 1, 0); //GPION.9 配置为输入引脚,开启上拉 GPIO_Init(GPION, PIN10, 0, 1, 0); //GPION.10 配置为输入引脚,开启上拉 GPIO_Init(GPION, PIN12, 0, 1, 0); //GPION.12 配置为输入引脚,开启上拉 GPIO_Init(GPION, PIN13, 0, 1, 0); //GPION.13 配置为输入引脚,开启上拉 GPIO_Init(GPION, PIN11, 0, 1, 0); //GPION.11 配置为输入引脚,开启上拉 GPIO_Init(GPION, PIN14, 0, 1, 0); //GPION.14 配置为输入引脚,开启上拉 GPIO_Init(GPIOB, PIN12, 0, 1, 0); //GPIOB.12 配置为输入引脚,开启上拉 GPIO_Init(GPIOA, PIN9, 0, 1, 0); //GPIOA.9 配置为输入引脚,开启上拉 //for key1 key2 GPIO_Init(GPION, PIN3, 0, 1, 0); //GPION.3 配置为输入引脚,开启上拉 GPIO_Init(GPION, PIN4, 0, 1, 0); //GPION.4 配置为输入引脚,开启上拉 } __IO uint8_t keyPressDown = 0x0; __IO uint8_t keyLastMKey = 0x0; __IO uint8_t keyRelease = 0x0; uint8_t KEYRead(void) { __IO uint8_t read_keys = 0, down_keys = 0; //当前读取的键值,当按键按下时,相应的位为1 read_keys = (~GPIO_GetBits(GPION, PIN3, 2)) & 0x03; //1 读键值 //处理按键 keyPressDown = read_keys & (read_keys ^ keyLastMKey); //2 得到按下触发值 keyRelease = (read_keys ^ keyPressDown ^ keyLastMKey); //3 得到释放触发值 keyLastMKey = read_keys; //4 得到所有未释放的键值 down_keys = keyPressDown; // up_keys=keyRelease; return down_keys; } void DIOInit(void) { //for DO Y0-Y7 PP23 PP22 PP19 PP18 PM20 PM18 PM16 PC7 GPIO_Init(GPIOP, PIN23, 1, 0, 0); //GPIOP.23 配置为输出引脚,推挽输出 GPIO_Init(GPIOP, PIN22, 1, 0, 0); //GPIOP.22 配置为输出引脚,推挽输出 GPIO_Init(GPIOP, PIN19, 1, 0, 0); //GPIOP.19 配置为输出引脚,推挽输出 GPIO_Init(GPIOP, PIN18, 1, 0, 0); //GPIOP.18 配置为输出引脚,推挽输出 GPIO_Init(GPIOM, PIN20, 1, 0, 0); //GPIOM.20 配置为输出引脚,推挽输出 GPIO_Init(GPIOM, PIN18, 1, 0, 0); //GPIOM.18 配置为输出引脚,推挽输出 GPIO_Init(GPIOM, PIN16, 1, 0, 0); //GPIOM.16 配置为输出引脚,推挽输出 GPIO_Init(GPIOC, PIN7, 1, 0, 0); //GPIOC.7 配置为输出引脚,推挽输出 GPIO_SetBit(GPIOP, PIN23); GPIO_SetBit(GPIOP, PIN22); GPIO_SetBit(GPIOP, PIN19); GPIO_SetBit(GPIOP, PIN18); GPIO_SetBit(GPIOM, PIN20); GPIO_SetBit(GPIOM, PIN18); GPIO_SetBit(GPIOM, PIN16); GPIO_SetBit(GPIOC, PIN7 ); //for DI X0-X3 PB3-PB6 GPIO_Init(GPIOB, PIN3, 0, 1, 0); //GPIOB.3 配置为输入引脚,开启上拉 GPIO_Init(GPIOB, PIN4, 0, 1, 0); //GPIOB.4 配置为输入引脚,开启上拉 GPIO_Init(GPIOB, PIN5, 0, 1, 0); //GPIOB.5 配置为输入引脚,开启上拉 GPIO_Init(GPIOB, PIN6, 0, 1, 0); //GPIOB.6 配置为输入引脚,开启上拉 } __IO uint8_t dioPressDown = 0x0; __IO uint8_t dioLastMKey = 0x0; __IO uint8_t dioRelease = 0x0; uint16_t DINRead(void) { __IO uint8_t read_keys = 0, down_keys = 0, up_keys = 0; //当前读取的键值,当按键按下时,相应的位为1 read_keys = (~GPIO_GetBits(GPIOB, PIN3, 4)) & 0xff; //1 读键值 //处理按键 dioPressDown = read_keys & (read_keys ^ dioLastMKey); //2 得到按下触发值 dioRelease = (read_keys ^ dioPressDown ^ dioLastMKey); //3 得到释放触发值 dioLastMKey = read_keys; //4 得到所有未释放的键值 down_keys = dioPressDown; up_keys = dioRelease; uint16_t ret = up_keys; ret <<= 8; ret |= down_keys; return ret; } void DOWrite(uint8_t data) { //Y0-Y7 PP23 PP22 PP19 PP18 PM20 PM18 PM16 PC7 if(data & 0x01) { GPIO_SetBit(GPIOP, PIN23); } else { GPIO_ClrBit(GPIOP, PIN23); } if(data & 0x02) { GPIO_SetBit(GPIOP, PIN22); } else { GPIO_ClrBit(GPIOP, PIN22); } if(data & 0x04) { GPIO_SetBit(GPIOP, PIN19); } else { GPIO_ClrBit(GPIOP, PIN19); } if(data & 0x08) { GPIO_SetBit(GPIOP, PIN18); } else { GPIO_ClrBit(GPIOP, PIN18); } if(data & 0x10) { GPIO_SetBit(GPIOM, PIN20); } else { GPIO_ClrBit(GPIOM, PIN20); } if(data & 0x20) { GPIO_SetBit(GPIOM, PIN18); } else { GPIO_ClrBit(GPIOM, PIN18); } if(data & 0x40) { GPIO_SetBit(GPIOM, PIN16); } else { GPIO_ClrBit(GPIOM, PIN16); } if(data & 0x80) { GPIO_SetBit(GPIOC, PIN7 ); } else { GPIO_ClrBit(GPIOC, PIN7 ); } }