GPIOB Pin 13 Initialization and Data Reading in STM32 Microcontroller
The first code snippet initializes the GPIOB peripheral clock and configures GPIOB Pin 13 as an input with a pull-up resistor. This is achieved by using the RCC_APB2PeriphClockCmd() function to enable the clock for the GPIOB peripheral and then defining a GPIO_InitTypeDef structure to specify the pin's mode, speed, and other attributes. The GPIO_Init() function then applies these settings to the GPIOB pin.
The second code snippet reads the value of GPIOB Pin 13 using the GPIO_ReadInputDataBit() function and returns the value as a variable. This allows you to access the current state of the pin, which can be used for various applications such as reading a switch or sensor.
Code Breakdown:
void GM_Init(void)
{
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB, ENABLE); // Enable clock for GPIOB peripheral
GPIO_InitTypeDef GPIO_InitStructure; // Create a structure for GPIO initialization
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IPU; // Set GPIO mode as Input with Pull-up resistor
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13; // Set the pin number as 13
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; // Set the GPIO speed as 50MHz
GPIO_Init(GPIOB, &GPIO_InitStructure); // Initialize GPIOB with the specified settings
}
uint8_t GM_GetNum(void)
{
return GPIO_ReadInputDataBit(GPIOB, GPIO_Pin_13); // Read and return the value of GPIOB Pin 13
}
原文地址: https://www.cveoy.top/t/topic/oAZL 著作权归作者所有。请勿转载和采集!