Stm32f103zetx实验二

T2

#include "main.h"

#include "i2c.h"

#include "tim.h"

#include "usart.h"

#include "gpio.h"

#include "fsmc.h"

 

/* Private includes ----------------------------------------------------------*/

/* USER CODE BEGIN Includes */

#include "lcd.h"

#include <string.h>

#include <stdio.h> // 标准输入输出库,用于printf函数

/* USER CODE END Includes */

 

/* Private typedef -----------------------------------------------------------*/

/* USER CODE BEGIN PTD */

// 重定向printf函数到UART1

int fputc(int ch, FILE *f)

{

  HAL_UART_Transmit(&huart1, (uint8_t *)&ch, 1, 0xffff);

  return ch;

}

/* USER CODE END PTD */

 

/* Private define ------------------------------------------------------------*/

/* USER CODE BEGIN PD */

/* USER CODE END PD */

 

/* Private macro -------------------------------------------------------------*/

/* USER CODE BEGIN PM */

// 全局变量

float time = 0; // 计时器时间变量

int time_status = 0; // 计时器状态标志

int key_button = 0; // 按键标志变量

int KEY2_STATUS = 0; // 按键2状态

int open_satatus = 1; // PWM开关状态

/* USER CODE END PM */

 

/* Private variables ---------------------------------------------------------*/

/* USER CODE BEGIN PV */

/* USER CODE END PV */

 

/* Private function prototypes -----------------------------------------------*/

void SystemClock_Config(void);

/* USER CODE BEGIN PFP */

/* USER CODE END PFP */

 

/* Private user code ---------------------------------------------------------*/

/* USER CODE BEGIN 0 */

/* USER CODE END 0 */

 

/**

  * @brief The application entry point.

  * @retval int

  */

int main(void)

{

  /* USER CODE BEGIN 1 */

  /* USER CODE END 1 */

  

 

  /* MCU Configuration--------------------------------------------------------*/

 

  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */

  HAL_Init();

 

  /* USER CODE BEGIN Init */

  /* USER CODE END Init */

 

  /* Configure the system clock */

  SystemClock_Config();

 

  /* USER CODE BEGIN SysInit */

  /* USER CODE END SysInit */

 

  /* Initialize all configured peripherals */

  MX_GPIO_Init();

  MX_FSMC_Init();

  MX_I2C1_Init();

  MX_TIM3_Init();

  MX_USART1_UART_Init();

  MX_TIM2_Init();

 

  /* USER CODE BEGIN 2 */

  lcd_init(); // 初始化LCD屏幕

  lcd_clear(WHITE); // 清除屏幕内容,背景设为白色

  char text_copy[100]; // 文本缓冲区

  int pwmVal = 0; // PWM占空比值

  int pwmStatus = 0; // PWM状态标志

  /* USER CODE END 2 */

 

  /* Infinite loop */

  /* USER CODE BEGIN WHILE */

  while (1)

  {

    /* USER CODE END WHILE */

 

    /* USER CODE BEGIN 3 */

    // 显示学号

    sprintf(text_copy, "202105505123");

    lcd_show_string(30, 90, 400, 16, 16, text_copy, BLUE);

 

    // 根据按键状态执行不同的操作

    switch (key_button)

    {

    case 1:

    {

      // 按键1按下,蜂鸣器响1秒

      HAL_GPIO_WritePin(BEEP_GPIO_Port, BEEP_Pin, GPIO_PIN_SET);

      HAL_Delay(1000);

      HAL_GPIO_WritePin(BEEP_GPIO_Port, BEEP_Pin, GPIO_PIN_RESET);

      key_button = 0; // 重置按键标志

    }

    break;

    case 2:

    {

      // 按键2按下,启动PWM

      HAL_TIM_PWM_Start(&htim3, TIM_CHANNEL_2);

      key_button = 0; // 重置按键标志

    }

    break;

    case 3:

    {

      // 按键3按下,处理计时器逻辑

      if (KEY2_STATUS == 0)

      {

        // 停止计时器并显示时间

        memset(text_copy, '\0', sizeof(text_copy));

        HAL_TIM_Base_Stop_IT(&htim2);

        sprintf(text_copy, "pulse: %6.3fms", time / 38);

        printf("time: %6.3fms", time / 38);

        lcd_show_string(30, 110, 400, 16, 16, text_copy, BLUE);

      }

      else

      {

        // 重置计时器并开始计时

        time = 0;

        HAL_TIM_Base_Start_IT(&htim2);

      }

      key_button = 0; // 重置按键标志

    }

    break;

    }

 

    // 根据open_status的值调整PWM占空比

    if (open_satatus == 1)

    {

      if (pwmStatus == 0)

      {

        pwmVal++;

        if (pwmVal >= 100)

        {

          pwmStatus = 1;

        }

      }

      else if (pwmStatus == 1)

      {

        pwmVal--;

        if (pwmVal <= 0)

        {

          pwmStatus = 0;

        }

      }

      // 设置PWM占空比

      __HAL_TIM_SetCompare(&htim3, TIM_CHANNEL_2, pwmVal);

      HAL_Delay(20); // 延时20ms

    }

  }

  /* USER CODE END 3 */

}

 

/**

  * @brief System Clock Configuration

  * @retval None

  */

void SystemClock_Config(void)

{

  RCC_OscInitTypeDef RCC_OscInitStruct = {0};

  RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};

 

  // 初始化CPU, AHB和APB总线时钟

  RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;

  RCC_OscInitStruct.HSEState = RCC_HSE_ON;

  RCC_OscInitStruct.HSEPredivValue = RCC_HSE_PREDIV_DIV1;

  RCC_OscInitStruct.HSIState = RCC_HSI_ON;

  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();

  }

 

  // 初始化CPU, AHB和APB总线时钟

  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();

  }

}

 

/* USER CODE BEGIN 4 */

// 定时器周期结束回调函数

void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim)

{

  if (htim->Instance == TIM2)

  {

    // 每次定时器2溢出增加time变量

    time = time + 1;

  }

}

 

// GPIO中断回调函数

void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)

{

  if (key_button == 0)

  {

    if (GPIO_Pin == KEY1_Pin)

    {

      // 按键1按下

      key_button = 1;

    }

    else if (GPIO_Pin == KEY3_Pin)

    {

      // 按键3按下

      key_button = 3;

      if (HAL_GPIO_ReadPin(KEY3_GPIO_Port, KEY3_Pin) == GPIO_PIN_SET)

      {

        KEY2_STATUS = 0;

      }

      else

      {

        KEY2_STATUS = 1;

      }

    }

    else if (GPIO_Pin == KEY2_Pin)

    {

      // 按键2按下

      key_button = 2;

    }

  }

}

/* USER CODE END 4 */

 

/**

  * @brief This function is executed in case of error occurrence.

  * @retval None

  */

void Error_Handler(void)

{

  /* USER CODE BEGIN Error_Handler_Debug */

  /* 用户可以在这里添加自己的错误处理代码 */

  __disable_irq();

  while (1)

  {

  }

  /* USER CODE END Error_Handler_Debug */

}

 

#ifdef USE_FULL_ASSERT

/**

  * @brief Reports the name of the source file and the source line number

  * where the assert_param error has occurred.

  * @param file: pointer to the source file name

  * @param line: assert_param error line source number

  * @retval None

  */

void assert_failed(uint8_t *file, uint32_t line)

  /* USER CODE BEGIN 6 */

  /* 用户可以在这里添加自己的断言失败处理代码 */

  /* ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */

  /* USER CODE END 6 */

}

#endif /* USE_FULL_ASSERT

 

  • 8
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值