1.首先要实现:当按下PB0按键并保持时,STK板上的user LED点 亮;当松开PB0按键时,该LED灯熄灭;这一功能的实现。
这功能实现的思路是:首先调用gpio相应的端口并设置为pushpull模式,然后判断PB0端口为高电位(即按键按下状态)则led等亮,范围pb0为低电位(即按键松开)则led等熄灭。Gpio的初始化及模式设定函数如下: 函数:
/* 把PB0按键设为pushpull模式 */
GPIO_PinModeSet(gpioPortB,0,gpioModePushPull,0);
/* 将PB1管脚设为pushpull按键模式,实现长按led灯以每秒一次的频率闪烁 */ GPIO_PinModeSet(gpioPortB,1,gpioModePushPullDrive,1); while (1) {
/* PB0 button is pulled-up, so pressing it will result in reading 1 on pin PB0 */ if(!GPIO_PinInGet(gpioPortB, 1))
GPIO_PortOutSet(gpioPortB, 0x3); /* Drive high PB0 and PB1 */ else
GPIO_PortOutClear(gpioPortB, 0x3); /* Drive low PB0 and PB1 */ }
然后写c语言程序,实现判断按键事件程序如下图所示:
2. 实现:当按下PB1按键然后抬起时, STK板上的user LED按
照每秒一次的频率闪烁;这一功能思路跟上一问大致一样,调用PB1端口,初始化状态为低电位状态,然后电位每次发生变化时出发LED状态变换,来实现单片机上的LED灯每秒一次的频率闪烁状态和熄灭状态的交替。
为了实现出发事件。我调用了库函数里面的timer触发器,记录PB1端口的点位变化次数为i,然后i每增加一次调用触发事件。 程序主函数代码如下: #include #include \"efm32_cmu.h\" #include \"efm32_emu.h\" #include \"efm32_gpio.h\" #include \"efm32_lcd.h\" #include \"efm32_prs.h\" #include \"efm32_system.h\" #include \"efm32_timer.h\" #include \"segmentlcd.h\" #include \"efm32_chip.h\" #include \"stdio.h\" #include \"rtc.h\" /* 把PB0按键设为pushpull模式 */ GPIO_PinModeSet(gpioPortB,0,gpioModePushPull,0); /* 将PB1管脚设为pushpull按键模式,实现长按led灯以每秒一次的频率闪烁 */ GPIO_PinModeSet(gpioPortB,1,gpioModePushPullDrive,1); while (1) { /* PB0 button is pulled-up, so pressing it will result in reading 1 on pin PB0 */ if(!GPIO_PinInGet(gpioPortB, 1)) GPIO_PortOutSet(gpioPortB, 0x3); /* Drive high PB0 and PB1 */ else GPIO_PortOutClear(gpioPortB, 0x3); /* Drive low PB0 and PB1 */ } void Delay(efm32int nCount) //volatile { for(; nCount != 0; nCount--); } /*初始化嵌入式Flash接口,初始化PLL使其达到系统可用频率*/ void RCC_Configuration(void) { /* Setup the microcontroller system. Initialize the Embedded Flash Interface, initialize the PLL and update the SystemFrequency variable. */ SystemInit(); } int main() { int i; RCC_Configuration(); //初始化FLASH及其PLL,系统时钟配置 RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA | RCC_APB2Periph_GPIOB,ENABLE); //外设时钟配置,开启GPIOA和GPIOB的时钟 GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6|GPIO_Pin_7|GPIO_Pin_9; GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //将A5口配置为通用推挽输出 GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //口线翻转速度为50MHz GPIO_Init(GPIOA, &GPIO_InitStructure); //配置GPIOA口 while(1) { GPIO_SetBits(GPIOA,GPIO_Pin_6); //A5口输出高电平 GPIO_SetBits(GPIOA, GPIO_Pin_7); //A6口输出低电平 for(i=0;i<1000000;i++); GPIO_ResetBits(GPIOA, GPIO_Pin_6); GPIO_ResetBits(GPIOA, GPIO_Pin_7); for(i=0;i<1000000;i++); } } 总结: 这次试验中发现,正确调用并初始化Gpio端口很重要。因为各个端口在不同的模式下其功能也不同,因此要解决好这些问题。 因篇幅问题不能全部显示,请点此查看更多更全内容