Last time we have made a good starting point with setting up a project template for STM32F103ZET6 development board using GNU tools. Using same project template we can move forward and start programing other elements. This time a quick note about adding button library. This is really modest implementation which simply initializes port pins and then reads their status.

Development board is equipped with four user programmable buttons named WAKEUP, TAMPER, USER1 and USER2. We are not going to care about meaning of names just use them as general purpose buttons for now.

For this we just need to create another pair of driver files buttons.c and buttons.h. In buttons.h file we simply define button ports and pins with some friendly names. Also as well we have to think about clocking proper peripheral buses.

#define BWAKEUPGPIO_Pin_0#define BWAKEUPPORTGPIOA#define BWAKEUPPORTCLKRCC_APB2Periph_GPIOA#define BTAMPERGPIO_Pin_13#define BTAMPERPORTGPIOC#define BTAMPERPORTCLKRCC_APB2Periph_GPIOC#define BUSER1GPIO_Pin_8#define BUSER1PORTGPIOA#define BUSER1PORTCLKRCC_APB2Periph_GPIOA#define BUSER2GPIO_Pin_3#define BUSER2PORTGPIOD#define BUSER2PORTCLKRCC_APB2Periph_GPIOD

Next step is button initialization. In buttons.c we simply create function:

void ButtonsInit(void){//GPIO structure used to initialize Button pinsGPIO_InitTypeDef GPIO_InitStructure;//Enable clock on APB2 pripheral bus where buttons are connectedRCC_APB2PeriphClockCmd(BWAKEUPPORTCLK|BTAMPERPORTCLK|BUSER1PORTCLK|BUSER2PORTCLK,ENABLE);//select pins to initialize WAKEUP and USER1 buttonsGPIO_InitStructure.GPIO_Pin = BWAKEUP|BUSER1;//select floating input modeGPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;//select GPIO speedGPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz;GPIO_Init(BWAKEUPPORT, &GPIO_InitStructure);//select pins to initialize TAMPER buttonGPIO_InitStructure.GPIO_Pin = BTAMPER;GPIO_Init(BTAMPERPORT, &GPIO_InitStructure);//select pins to initialize USER2 buttonGPIO_InitStructure.GPIO_Pin = BUSER2;GPIO_Init(BUSER2PORT, &GPIO_InitStructure);}

Here we have to enable peripheral clocks for used ports with RCC_APB2PeriphClockCmd command. Then we can start setting up port pins. According to peripheral library we need to do this through structure variable where each field caries particular setting. These would be GPIO_Pins (all pins set for same port), then GPIO_Mode – we set it as input floating, and GPIO_Speed as max. Buttons aren’t connected to same port so we have to do initialization separately for each port with GPIO_Init function.

Lastly we write simple function that reads button state.

uint32_t ButtonRead(GPIO_TypeDef* Button_Port, uint16_t Button){return !GPIO_ReadInputDataBit(Button_Port, Button);}

When pressed, buttons pulls port pin low so GPIO_ReadInputDataBit() function returns ’0′. Probably it is more logical to have ’1′ on click so we used NOT ‘!’operator to do so. And here is a simple demo code which lights different LED on each button press. Code still runs in super loop so don’t judge code efficiency its only for demonstration.

//Buttons Test#include "stm32f10x.h"#include "leds.h"#include "buttons.h"//delay functionvoid Delay(__IO uint32_t nCount){for(; nCount != 0; nCount--);}int main(void){//init ledsLEDsInit();ButtonsInit();while (1){//read Wakeup buttonif (ButtonRead(BWAKEUPPORT, BWAKEUP)){//led 1 onLEDOn(1);//delayDelay(500000);//led 1 offLEDOff(1);}//read TAMPER buttonif (ButtonRead(BTAMPERPORT, BTAMPER)){//led 2 onLEDOn(2);//delayDelay(500000);//led 2 offLEDOff(2);}//read User1 buttonif (ButtonRead(BUSER1PORT, BUSER1)){//led 3 onLEDOn(3);//delayDelay(500000);//led 3 offLEDOff(3);}//read User2 buttonif (ButtonRead(BUSER2PORT, BUSER2)){//led 4 onLEDOn(4);//delayDelay(500000);//led 4 offLEDOff(4);}}}

In real applications buttons check should be interrupt based. But we will get to that soon.

Project source code[420KB]

[via: http://www.scienceprog.com/]

 Leave a Reply

(required)

(required)

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 
   
© 2011 Geko Geek This is a news aggregator website. Articles and images are copyrighted to their original source authors. Gekogeek takes no responsibility about the articles content Suffusion theme by Sayontan Sinha