Lesson 2: Part 2: Controlling GPIO - On-Board Input - Input Reading

 Recite from Part 1, 3 configurations is require as below:

  1. Enable GPIO section for respective pin
    • SFR: RCC_AHBENR , bit: IOPAEN, value: 1
  2. Configure pin to input function
    • SFR: GPIOA_MODER, bit: MODER0, value: 00
  3. Reading input status
    • SFR: GPIOA_IDR, bit: IDR0
The corresponding C code for 3 cases above are:
  1. Enable GPIO section for respective pin
    • RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
  2. Configure pin to input function
    • GPIOA->MODER &= ~GPIO_MODER_MODER0;  //mask the bit to 00
    • GPIOA->MODER |= MODE_INPUT;         //set the bit
  3. Reading pin status into a variable
    • InputState = GPIOA->IDR & GPIO_IDR_0;
The main.c source code is shown as below, or retrieve from git:
#include "stm32f0xx.h"

void MCUInit(void)
{
    /* Input config */
    //Enable GPIO_A block
    RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
    //Configure PA0 as Input
    GPIOA->MODER &= ~GPIO_MODER_MODER0;//mask the bit to 00
}

int main(void)
{
    unsigned char InputState;
   
    MCUInit();
   
    while(1)
    {
        //read input state
        InputState = GPIOA->IDR & GPIO_IDR_0;
    }
}

Real Life Application

  1. Pressing play button in DVD to start video playback process.
  2. Pressing the power button at hand phone to lit up screen for phone unlock process

Exercise

  1. Since reset setting on GPIOA->MODER is already as input, try remove the configurations and see if input detection still works.
  2. Base on USER button state, change the LD3 LED state, for example:
    • when USER button press, turn on LD3 LED
    • when USER button release, turn off LD3 LED

No comments:

Post a Comment