SWITCH INTERFACING WITH ARM PROCESSOR
OBJECTIVE:
The main objective of this program is to read input from a switch connected to Port G on
the STM32F4 microcontroller and controls an LED connected to Port F. When the switch is
pressed, the program inverts the input signal and making LED glow based on the switch's
position.
ALGORITHM:
Step 1: Start the program.
Step 2: Include Required Libraries:
Include the necessary STM32F4 library files to access the microcontroller's features:
stm32f4xx.h for basic definitions.
stm32f4xx_rcc.h for controlling the clock settings.
Step 3: Define a Configuration Function ( configuration_port() ):
Purpose: Set up the microcontroller's GPIO ports for input and output operations.
Step 4: Enable Clocks for GPIO Ports:
Enable the clock for Port F using the Advanced High Performance Bus (AHB1).
Enable the clock for Port G using the AHB1.
Step 5: Configure GPIO Port F for Output:
Set pins 0, 1, 2, and 3 of Port F to output mode.
Explanation: This allows these pins to send signals out.
Step 6: Configure GPIO Port G for Input:
Set all pins of Port G to input mode.
Explanation: This allows these pins to receive signals.
Step 7: Define the Main Function (main ()):
Purpose: Execute the primary logic of the program.
Step 8: Initialize GPIO Configuration:
Call the configuration_ports () function to apply the GPIO settings.
Step 9: Read Input from GPIO Port G:
Access the input data from Port G using the Input Data Register (IDR).
Step 10: Invert the Input Data:
Due to the internal pull resistor, the port G works in the active low state. In order to
attain proper output, we need to invert the state of Port G, which is given as input to
the Port F.
Invert the bits of the data read from Port G.
Step 11: Send the inverted Data to GPIO Port F:
Output the inverted data to Port F using the Output Data Register (ODR).
Explanation: The state of Port G is now reflected in Port F, but with each bit flipped.
Step 12: End of the program.
The program continues to run, with Port F always reflecting the state of Port G.
PROGRAM:
#include "stm32f4xx.h" // Header file for the STM32F4 microcontroller
#include "stm32f4xx_rcc.h" // Header file for clock control
void configuration_ports () // Function to configure GPIO ports
{
// Enable the clock for GPIO Port F (using AHB1 bus)
RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOF, ENABLE);
// Enable the clock for GPIO Port G (using AHB1 bus)
RCC_AHB1PeriphClockCmd (RCC_AHB1Periph_GPIOG, ENABLE);
// Set pins 0, 1, 2, 3 of Port F to output mode to control four leds
// 0x00000055 corresponds to setting bits for pins 0, 1, 2, and 3 to '01' (output mode)
GPIOF->MODER = 0x00000055;
// Set all pins of Port G to input mode
// 0x00000000 sets all bits to '00' (input mode)
GPIOG->MODER = 0x00000000;
}
// Main function
void main ()
{
// Configure the GPIO ports
configuration_ports ();
// Read the input data from Port G, invert the bits and output the result to Port F
GPIOF->ODR = ~(GPIOG->IDR);
}