Recite from Part 1, 3 configurations is require as below:
- Enable GPIO section for respective pin
- SFR: RCC_AHBENR , bit: IOPAEN, value: 1
- Configure pin to input function
- SFR: GPIOA_MODER, bit: MODER0, value: 00
- Reading input status
- SFR: GPIOA_IDR, bit: IDR0
The corresponding C code for 3 cases above are:
- Enable GPIO section for respective pin
- RCC->AHBENR |= RCC_AHBENR_GPIOAEN;
- Configure pin to input function
- GPIOA->MODER &= ~GPIO_MODER_MODER0; //mask the bit to 00
- GPIOA->MODER |= MODE_INPUT; //set the bit
- 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
- Pressing play button in DVD to start video playback process.
- Pressing the power button at hand phone to lit up screen for phone unlock process
Exercise
- Since reset setting on GPIOA->MODER is already as input, try remove the configurations and see if input detection still works.
- 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