Tuesday, 8 April 2014

STM32F100RBT6 (ARM Cortex M3) Programming Tutorial - Reading the Input

Reading the button
After blinking the LED, described in lesson 1, we can do another exercise - reading the button state. On the STM32VLDISCOVERY board we have two buttons - one for resetting board (RST, black) and one for user purpose (USER, blue). The USER button is connected to PA0 pin and active states are high.
Creating project is similar to described in lesson 1. First thing to do to use this button is enable clock for GPIO peripheral:
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPCEN;
Remember that we use also GPIOC, so we need turn on clock for both GPIOA and GPIOC. Next, let's do some defines for USER button:
#define SW_USER_GPIO GPIOA
#define SW_USER_PIN  0
and now we can configure GPIO to work as input:
#if (SW_USER_PIN > 7)
  SW_USER_GPIO->CRH = (SW_USER_GPIO->CRH & CONFMASKH(SW_USER_PIN)) |
 GPIOPINCONFH(SW_USER_PIN,     GPIOCONF(GPIO_MODE_INPUT,
 GPIO_CNF_INPUT_FLOATING));
#else
  SW_USER_GPIO->CRL = (SW_USER_GPIO->CRH & CONFMASKL(SW_USER_PIN)) |
 GPIOPINCONFL(SW_USER_PIN,     GPIOCONF(GPIO_MODE_INPUT,
 GPIO_CNF_INPUT_FLOATING));
#endif
Now we can check in endless loop state of the button and turn LED on if is pressed:
while (1) {
if(SW_USER_GPIO->IDR & (1 << SW_USER_PIN))
  LED_BLUE_GPIO->BSRR = (1 << LED_BLUE_PIN);
else
  LED_BLUE_GPIO->BRR = (1 << LED_BLUE_PIN);
}
Complete code of example:
//=============================================================================
// STM32VLDISCOVERY tutorial
// Lesson 2. Reading from the switch.
// Copyright : Gaurav Verma, Assistant Professor, ECE Department.
// Jaypee Institute of Information Technology, Sector-62, Noida.
// e-mail : gaurav.iitkg@gmail.com
// Mobile No.: 9811506739
//=============================================================================
#include "stm32f10x.h"
#include "antilib_gpio.h"
//=============================================================================
// Defines
//=============================================================================
#define LED_BLUE_GPIO GPIOC
#define LED_BLUE_PIN 8
#define SW_USER_GPIO GPIOA
#define SW_USER_PIN 0
//=============================================================================
// main function
//=============================================================================
int main(void)
{
RCC->APB2ENR |= RCC_APB2ENR_IOPAEN | RCC_APB2ENR_IOPCEN;
#if (LED_BLUE_PIN > 7)
  LED_BLUE_GPIO->CRH = (LED_BLUE_GPIO->CRH & CONFMASKH(LED_BLUE_PIN)) |
 GPIOPINCONFH(LED_BLUE_PIN,     GPIOCONF(GPIO_MODE_OUTPUT2MHz,
 GPIO_CNF_OUTPUT_PUSHPULL));
#else
  LED_BLUE_GPIO->CRL = (LED_BLUE_GPIO->CRL & CONFMASKL(LED_BLUE_PIN)) |
 GPIOPINCONFL(LED_BLUE_PIN,   GPIOCONF(GPIO_MODE_OUTPUT2MHz,
 GPIO_CNF_OUTPUT_PUSHPULL));
#endif
#if (SW_USER_PIN > 7)

  SW_USER_GPIO->CRH = (SW_USER_GPIO->CRH & CONFMASKH(SW_USER_PIN)) |
 GPIOPINCONFH(SW_USER_PIN,     GPIOCONF(GPIO_MODE_INPUT,
 GPIO_CNF_INPUT_FLOATING));
#else
  SW_USER_GPIO->CRL = (SW_USER_GPIO->CRH & CONFMASKL(SW_USER_PIN)) |
 GPIOPINCONFL(SW_USER_PIN,     GPIOCONF(GPIO_MODE_INPUT,
 GPIO_CNF_INPUT_FLOATING));
#endif
while (1) {
if(SW_USER_GPIO->IDR & (1 << SW_USER_PIN))
  LED_BLUE_GPIO->BSRR = (1 << LED_BLUE_PIN);
else
  LED_BLUE_GPIO->BRR = (1 << LED_BLUE_PIN);
}
}
//=============================================================================
// End of file
//=============================================================================

After flashing MCU and pressing down the USER button blue LED will be turned on, after release the button LED will be turned off.

No comments:

Post a Comment