stm32两位秒表程序设计
时间: 2023-10-06 20:08:23 浏览: 111
以下是基于STM32的两位秒表程序设计,其中使用了HAL库。
首先,需要定义两个计数器变量和两个标志位,用于标记是否启动计时器:
```c
uint16_t counter1 = 0;
uint16_t counter2 = 0;
uint8_t start_flag1 = 0;
uint8_t start_flag2 = 0;
```
然后,在SysTick中断处理函数中,对计数器进行累加,并在达到1秒时将标志位设置为0,表示计时结束:
```c
void SysTick_Handler(void)
{
counter1++;
counter2++;
if (counter1 == 1000) {
counter1 = 0;
start_flag1 = 0;
}
if (counter2 == 1000) {
counter2 = 0;
start_flag2 = 0;
}
}
```
接下来,可以使用按键中断来启动和停止计时器。当按下按键时,将标志位设置为1,表示开始计时;当再次按下按键时,将标志位设置为0,表示停止计时:
```c
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if (GPIO_Pin == GPIO_PIN_0) {
if (start_flag1 == 0) {
start_flag1 = 1;
counter1 = 0;
} else {
start_flag1 = 0;
}
}
if (GPIO_Pin == GPIO_PIN_1) {
if (start_flag2 == 0) {
start_flag2 = 1;
counter2 = 0;
} else {
start_flag2 = 0;
}
}
}
```
最后,在主函数中,可以定时输出计数器的值,以显示秒表计时结果:
```c
while (1)
{
if (start_flag1 == 1) {
printf("Counter 1: %d\n", counter1);
}
if (start_flag2 == 1) {
printf("Counter 2: %d\n", counter2);
}
HAL_Delay(10);
}
```
完整的代码如下所示:
```c
#include "stm32f1xx_hal.h"
#include "stdio.h"
uint16_t counter1 = 0;
uint16_t counter2 = 0;
uint8_t start_flag1 = 0;
uint8_t start_flag2 = 0;
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
int main(void)
{
HAL_Init();
SystemClock_Config();
MX_GPIO_Init();
HAL_SYSTICK_Config(HAL_RCC_GetHCLKFreq()/1000);
HAL_NVIC_SetPriority(SysTick_IRQn, 0, 0);
while (1)
{
if (start_flag1 == 1) {
printf("Counter 1: %d\n", counter1);
}
if (start_flag2 == 1) {
printf("Counter 2: %d\n", counter2);
}
HAL_Delay(10);
}
}
void SysTick_Handler(void)
{
counter1++;
counter2++;
if (counter1 == 1000) {
counter1 = 0;
start_flag1 = 0;
}
if (counter2 == 1000) {
counter2 = 0;
start_flag2 = 0;
}
}
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
{
if (GPIO_Pin == GPIO_PIN_0) {
if (start_flag1 == 0) {
start_flag1 = 1;
counter1 = 0;
} else {
start_flag1 = 0;
}
}
if (GPIO_Pin == GPIO_PIN_1) {
if (start_flag2 == 0) {
start_flag2 = 1;
counter2 = 0;
} else {
start_flag2 = 0;
}
}
}
static void MX_GPIO_Init(void)
{
__HAL_RCC_GPIOA_CLK_ENABLE();
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1;
GPIO_InitStruct.Mode = GPIO_MODE_IT_RISING_FALLING;
GPIO_InitStruct.Pull = GPIO_PULLUP;
HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
HAL_NVIC_SetPriority(EXTI0_IRQn, 2, 0);
HAL_NVIC_EnableIRQ(EXTI0_IRQn);
HAL_NVIC_SetPriority(EXTI1_IRQn, 2, 0);
HAL_NVIC_EnableIRQ(EXTI1_IRQn);
}
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL9;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
|RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
```
阅读全文