• stm32f1xx单片机拦截中断源代码


    这个是实现后的效果,可以看到已经没有中断的效果了

    这个是拦截前的效果可以看到电平是在变化的

    实现原理非常简单:一句话搞定:

    		if(TIM2->CNT==TIM2->ARR-5)TIM2->CNT-=5;

    以下是完整的代码:是用来补充说明和筹字数的

    1. /* USER CODE BEGIN Header */
    2. /**
    3. ******************************************************************************
    4. * @file : main.c
    5. * @brief : Main program body
    6. ******************************************************************************
    7. * @attention
    8. *
    9. * Copyright (c) 2023 STMicroelectronics.
    10. * All rights reserved.
    11. *
    12. * This software is licensed under terms that can be found in the LICENSE file
    13. * in the root directory of this software component.
    14. * If no LICENSE file comes with this software, it is provided AS-IS.
    15. *
    16. ******************************************************************************
    17. */
    18. /* USER CODE END Header */
    19. /* Includes ------------------------------------------------------------------*/
    20. #include "main.h"
    21. #include "tim.h"
    22. #include "gpio.h"
    23. /* Private includes ----------------------------------------------------------*/
    24. /* USER CODE BEGIN Includes */
    25. /* USER CODE END Includes */
    26. /* Private typedef -----------------------------------------------------------*/
    27. /* USER CODE BEGIN PTD */
    28. /* USER CODE END PTD */
    29. /* Private define ------------------------------------------------------------*/
    30. /* USER CODE BEGIN PD */
    31. /* USER CODE END PD */
    32. /* Private macro -------------------------------------------------------------*/
    33. /* USER CODE BEGIN PM */
    34. /* USER CODE END PM */
    35. /* Private variables ---------------------------------------------------------*/
    36. /* USER CODE BEGIN PV */
    37. /* USER CODE END PV */
    38. /* Private function prototypes -----------------------------------------------*/
    39. void SystemClock_Config(void);
    40. /* USER CODE BEGIN PFP */
    41. /* USER CODE END PFP */
    42. /* Private user code ---------------------------------------------------------*/
    43. /* USER CODE BEGIN 0 */
    44. /* USER CODE END 0 */
    45. /**
    46. * @brief The application entry point.
    47. * @retval int
    48. */
    49. int main(void)
    50. {
    51. /* USER CODE BEGIN 1 */
    52. uint8_t sz[]="hello nice to meet you";
    53. /* USER CODE END 1 */
    54. /* MCU Configuration--------------------------------------------------------*/
    55. /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
    56. HAL_Init();
    57. /* USER CODE BEGIN Init */
    58. /* USER CODE END Init */
    59. /* Configure the system clock */
    60. SystemClock_Config();
    61. /* USER CODE BEGIN SysInit */
    62. /* USER CODE END SysInit */
    63. /* Initialize all configured peripherals */
    64. MX_GPIO_Init();
    65. MX_TIM2_Init();
    66. /* USER CODE BEGIN 2 */
    67. HAL_TIM_Base_Start_IT(&htim2);
    68. /* USER CODE END 2 */
    69. /* Infinite loop */
    70. /* USER CODE BEGIN WHILE */
    71. while (1)
    72. {
    73. /* USER CODE END WHILE */
    74. if(TIM2->CNT==TIM2->ARR-5)TIM2->CNT-=5;
    75. /* USER CODE BEGIN 3 */
    76. }
    77. /* USER CODE END 3 */
    78. }
    79. /**
    80. * @brief System Clock Configuration
    81. * @retval None
    82. */
    83. void SystemClock_Config(void)
    84. {
    85. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
    86. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
    87. /** Initializes the RCC Oscillators according to the specified parameters
    88. * in the RCC_OscInitTypeDef structure.
    89. */
    90. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
    91. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
    92. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
    93. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
    94. RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSI_DIV2;
    95. RCC_OscInitStruct.PLL.PLLMUL = RCC_PLL_MUL2;
    96. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
    97. {
    98. Error_Handler();
    99. }
    100. /** Initializes the CPU, AHB and APB buses clocks
    101. */
    102. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
    103. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
    104. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_PLLCLK;
    105. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
    106. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV2;
    107. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
    108. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
    109. {
    110. Error_Handler();
    111. }
    112. }
    113. /* USER CODE BEGIN 4 */
    114. /* USER CODE END 4 */
    115. /**
    116. * @brief This function is executed in case of error occurrence.
    117. * @retval None
    118. */
    119. void Error_Handler(void)
    120. {
    121. /* USER CODE BEGIN Error_Handler_Debug */
    122. /* User can add his own implementation to report the HAL error return state */
    123. __disable_irq();
    124. while (1)
    125. {
    126. }
    127. /* USER CODE END Error_Handler_Debug */
    128. }
    129. #ifdef USE_FULL_ASSERT
    130. /**
    131. * @brief Reports the name of the source file and the source line number
    132. * where the assert_param error has occurred.
    133. * @param file: pointer to the source file name
    134. * @param line: assert_param error line source number
    135. * @retval None
    136. */
    137. void assert_failed(uint8_t *file, uint32_t line)
    138. {
    139. /* USER CODE BEGIN 6 */
    140. /* User can add his own implementation to report the file name and line number,
    141. ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */
    142. /* USER CODE END 6 */
    143. }
    144. #endif /* USE_FULL_ASSERT */

    这就表明了,单片机里面是可以植入病毒的,或者不算病毒,里面的代码也是具有修改破坏的可能性,假设在一个中断里面插入这么一句,理论上除了异常不能被屏蔽,所有的中断都可以屏蔽,导致的结果就是:插入外设例如USB没有反应,识别不出来,因为外设是通过中断来识别的,那样那个单片机就没有用处了,好像就废了可以扔掉了,不过这个东西可以用来保护单片机不被修改,

    假设稍微修改一下,可以输入密码,用来加密

  • 相关阅读:
    【Azure Developer】在App Service上放置一个JS页面并引用msal.min.js成功获取AAD用户名示例
    WMI使用学习笔记
    5G网络整体架构
    飞天使-学以致用-devops知识点4-SpringBoot项目CICD实现(实验失败,了解大概流程)
    Vue3.0项目——打造企业级音乐App(二)图片懒加载、v-loading指令的开发和优化
    npx expo start -c 屏幕白屏
    R语言ggplot2可视化:使用ggpubr包的ggscatter函数可视化散点图、使用get_palette函数基于自定义调色板生成一个k色调色板
    通过profibus PA转Modbus rtu协议网关把RTU数据传到pa设备上
    2022“杭电杯”中国大学生算法设计超级联赛(3)杭电多校第三场
    【图像分割】基于混洗Shuffled Complex Evolution实现图像分割附matlab代码
  • 原文地址:https://blog.csdn.net/geniusChinaHN/article/details/132651310