[Shanghai Hangxin ACM32F070 development board + touch function evaluation board evaluation] + use of GPIO port
[Copy link]
This post was last edited by jinglixixi on 2022-10-3 01:31
Learning about the GPIO port is the best way to master the use of the development board, and analyzing and learning from routines is a quick way to get started.
1) Use of output mode
The ACM32F070 development board is equipped with a green LED for output testing. The circuit is shown in Figure 1.
Figure 1 LED circuit
As shown in the figure, the control pin of the LED is PD3. Therefore, the following function can be used to configure the pin.
void GPIO_Test(void)
{
GPIOD_Handle.Pin = GPIO_PIN_3;
GPIOD_Handle.Mode = GPIO_MODE_OUTPUT_PP;
GPIOD_Handle.Pull = GPIO_PULLUP;
GPIOD_Handle.Alternate = GPIO_FUNCTION_0;
HAL_GPIO_Init(GPIOD, &GPIOD_Handle);
}
To control the LED to flash, the main program is as follows:
int main(void)
{
System_Init();
APP_GPIO_Test(GPIO_INT);
while (1)
{
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, GPIO_PIN_SET);
System_Delay_MS(500);
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, GPIO_PIN_CLEAR);
System_Delay_MS(500);
}
}
After compiling and downloading the program, you can see the LED display effect.
2) Use of input mode
The ACM32F070 development board is equipped with a black button for testing purposes, and its circuit is shown in Figure 2.
Figure 2 Key circuit
As shown in the figure, the control pin of this button is PC13. Therefore, a routine can be used to perform interrupt-based testing.
At this time, the main program needs to be changed to the following form:
int main(void)
{
System_Init();
Uart_Init(115200);
APP_GPIO_Test(GPIO_PC13);
while(1);
}
After downloading and testing, the output information of the serial port is shown in Figure 3.
According to the program analysis, when the user key is pressed, there should be a prompt of "Get PC13 interrupt flag!!!", but the prompt content is not seen.
According to relevant information, the usage of pins PC13, PC14, and PC15 is quite special.
Figure 3 Key test
If you need to change the main program to the following form:
int main(void)
{
System_Init();
Uart_Init(115200);
APP_GPIO_Test(GPIO_INT);
while(1);
}
However, when PA0 is grounded, the changes shown in Figure 4 can be seen.
Figure 4 PA0 trigger changes
So can we use buttons to control the LED?
Of course it is possible, but an external button module is required, and the control effect is shown in Figures 5 and 6.
Figure 5: No key pressed
Figure 6 Button status
The corresponding pin configuration function is:
void GPIO_Test(void)
{
GPIOD_Handle.Pin = GPIO_PIN_3;
GPIOD_Handle.Mode = GPIO_MODE_OUTPUT_PP;
GPIOD_Handle.Pull = GPIO_PULLUP;
GPIOD_Handle.Alternate = GPIO_FUNCTION_0;
HAL_GPIO_Init(GPIOD, &GPIOD_Handle);
GPIOC_Handle.Pin = GPIO_PIN_0;
GPIOC_Handle.Mode = GPIO_MODE_INPUT;
GPIOD_Handle.Pull = GPIO_ GPIO_PULLUP;
GPIOC_Handle.Alternate = GPIO_FUNCTION_0;
HAL_GPIO_Init(GPIOA, &GPIOA_Handle);
}
The main program to realize the control function is:
int main(void)
{
System_Init();
GPIO_Test();
while (1)
{
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0)==RESET)
{
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, GPIO_PIN_CLEAR);
}
else
{
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_3, GPIO_PIN_SET);
}
System_Delay_MS(200);
}
}
3) Buzzer control
The ACM32F070 development board is equipped with a buzzer, and its circuit is shown in Figure 7.
Figure 7 Buzzer circuit
The control pin of the buzzer is PD1, and its pin configuration statement is as follows:
GPIOD_Handle.Pin = GPIO_PIN_1;
GPIOD_Handle.Mode = GPIO_MODE_OUTPUT_PP;
GPIOD_Handle.Pull = GPIO_PULLUP;
GPIOD_Handle.Alternate = GPIO_FUNCTION_0;
HAL_GPIO_Init(GPIOD, &GPIOD_Handle);
The main program for controlling the buzzer with buttons is:
int main(void)
{
System_Init();
GPIO_Test();
while (1)
{
if(HAL_GPIO_ReadPin(GPIOA, GPIO_PIN_0)==RESET)
{
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_1, GPIO_PIN_CLEAR);
}
else
{
HAL_GPIO_WritePin(GPIOD, GPIO_PIN_1, GPIO_PIN_SET);
}
System_Delay_MS(200);
}
}
With the basic usage of GPIO port, you can use GPIO port to simulate I2C to drive OLED or corresponding sensors.
|