altervisi0n

Untitled

Nov 4th, 2025
11
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.68 KB | None | 0 0
  1. /* USER CODE BEGIN Header */
  2. /**
  3. ******************************************************************************
  4. * @file : main.c
  5. * @brief : Main program body
  6. ******************************************************************************
  7. */
  8. /* USER CODE END Header */
  9. #include "main.h"
  10. #include <string.h>
  11.  
  12. /* Private variables */
  13. volatile uint16_t virtualSecDelay = 1000;
  14. volatile uint32_t fastTicks = 0;
  15. volatile uint16_t virtSecCnt = 0;
  16. volatile uint8_t hh = 10, mm = 43, ss = 0;
  17. volatile uint8_t digitToShow = 0;
  18.  
  19. static uint32_t lastBtn1Press = 0;
  20. static uint32_t lastBtn2Press = 0;
  21.  
  22. /* Private function prototypes */
  23. void SystemClock_Config(void);
  24. static void MX_GPIO_Init(void);
  25. static void shiftOut_8bit_noLatch(uint8_t data);
  26. static void latchData(void);
  27. static void beep(uint16_t ms);
  28.  
  29. /* USER CODE BEGIN PFP */
  30.  
  31. /* USER CODE END PFP */
  32.  
  33. /* Private user code ---------------------------------------------------------*/
  34. static const uint8_t seg7_CA[10] = {
  35. ~0x3F, ~0x06, ~0x5B, ~0x4F, ~0x66,
  36. ~0x6D, ~0x7D, ~0x07, ~0x7F, ~0x6F
  37. };
  38.  
  39. static void shiftOut_8bit_noLatch(uint8_t data)
  40. {
  41. for(int i = 0; i < 8; i++){
  42. HAL_GPIO_WritePin(DATA_GPIO_Port, DATA_Pin, (data & 0x80) ? GPIO_PIN_SET : GPIO_PIN_RESET);
  43. HAL_GPIO_WritePin(CLOCK_GPIO_Port, CLOCK_Pin, GPIO_PIN_SET);
  44. HAL_GPIO_WritePin(CLOCK_GPIO_Port, CLOCK_Pin, GPIO_PIN_RESET);
  45. data <<= 1;
  46. }
  47. }
  48.  
  49. static void latchData(void)
  50. {
  51. HAL_GPIO_WritePin(LATCH_GPIO_Port, LATCH_Pin, GPIO_PIN_SET);
  52. HAL_GPIO_WritePin(LATCH_GPIO_Port, LATCH_Pin, GPIO_PIN_RESET);
  53. }
  54.  
  55. static void beep(uint16_t ms)
  56. {
  57. HAL_GPIO_WritePin(BUZZER_GPIO_Port, BUZZER_Pin, GPIO_PIN_RESET);
  58. for (volatile uint32_t i = 0; i < ms * 1200; i++);
  59. HAL_GPIO_WritePin(BUZZER_GPIO_Port, BUZZER_Pin, GPIO_PIN_SET);
  60. }
  61.  
  62. /**
  63. * @brief The application entry point.
  64. */
  65. int main(void)
  66. {
  67. HAL_Init();
  68. SystemClock_Config();
  69. MX_GPIO_Init();
  70.  
  71. while (1)
  72. {
  73. HAL_GPIO_WritePin(BUZZER_GPIO_Port, BUZZER_Pin, GPIO_PIN_SET);
  74.  
  75. if (fastTicks >= virtualSecDelay) {
  76. fastTicks = 0;
  77. virtSecCnt++;
  78. ss++;
  79. if (ss >= 60) { ss = 0; mm++; }
  80. if (mm >= 60) { mm = 0; hh++; }
  81. if (hh >= 24) hh = 0;
  82.  
  83. if (virtSecCnt >= 3600) {
  84. virtSecCnt = 0;
  85. beep(100);
  86. }
  87. }
  88.  
  89. uint32_t now = HAL_GetTick();
  90.  
  91. if (HAL_GPIO_ReadPin(BUTTON1_GPIO_Port, BUTTON1_Pin) == GPIO_PIN_RESET) {
  92. if (now - lastBtn1Press > 200) {
  93. lastBtn1Press = now;
  94. if (virtualSecDelay > 120)
  95. virtualSecDelay -= 100;
  96. else
  97. virtualSecDelay = 20;
  98. }
  99. }
  100.  
  101. if (HAL_GPIO_ReadPin(BUTTON2_GPIO_Port, BUTTON2_Pin) == GPIO_PIN_RESET) {
  102. if (now - lastBtn2Press > 200) {
  103. lastBtn2Press = now;
  104. if (virtualSecDelay < 1900)
  105. virtualSecDelay += 100;
  106. else
  107. virtualSecDelay = 2000;
  108. }
  109. }
  110. }
  111. }
  112.  
  113. /**
  114. * @brief System Clock Configuration
  115. */
  116. void SystemClock_Config(void)
  117. {
  118. RCC_OscInitTypeDef RCC_OscInitStruct = {0};
  119. RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
  120.  
  121. RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
  122. RCC_OscInitStruct.HSIState = RCC_HSI_ON;
  123. RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
  124. RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
  125. if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
  126. {
  127. Error_Handler();
  128. }
  129.  
  130. RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK
  131. |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2;
  132. RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI;
  133. RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
  134. RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
  135. RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
  136.  
  137. if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
  138. {
  139. Error_Handler();
  140. }
  141. }
  142.  
  143. /**
  144. * @brief GPIO Initialization Function
  145. */
  146. static void MX_GPIO_Init(void)
  147. {
  148. GPIO_InitTypeDef GPIO_InitStruct = {0};
  149.  
  150. __HAL_RCC_GPIOA_CLK_ENABLE();
  151. __HAL_RCC_GPIOB_CLK_ENABLE();
  152.  
  153. HAL_GPIO_WritePin(GPIOA, CLOCK_Pin|DATA_Pin, GPIO_PIN_RESET);
  154. HAL_GPIO_WritePin(BUZZER_GPIO_Port, BUZZER_Pin, GPIO_PIN_SET);
  155. HAL_GPIO_WritePin(LATCH_GPIO_Port, LATCH_Pin, GPIO_PIN_RESET);
  156.  
  157. GPIO_InitStruct.Pin = BUTTON1_Pin|BUTTON2_Pin;
  158. GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
  159. GPIO_InitStruct.Pull = GPIO_NOPULL;
  160. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  161.  
  162. GPIO_InitStruct.Pin = BUTTON3_Pin;
  163. GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
  164. GPIO_InitStruct.Pull = GPIO_PULLUP;
  165. HAL_GPIO_Init(BUTTON3_GPIO_Port, &GPIO_InitStruct);
  166.  
  167. GPIO_InitStruct.Pin = CLOCK_Pin|DATA_Pin;
  168. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  169. GPIO_InitStruct.Pull = GPIO_NOPULL;
  170. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  171. HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
  172.  
  173. GPIO_InitStruct.Pin = BUZZER_Pin|LATCH_Pin;
  174. GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
  175. GPIO_InitStruct.Pull = GPIO_NOPULL;
  176. GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
  177. HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  178. }
  179.  
  180. /* USER CODE BEGIN 4 */
  181. void SysTick_Handler(void)
  182. {
  183. HAL_IncTick();
  184. fastTicks++;
  185.  
  186. uint8_t d[4] = { hh/10, hh%10, mm/10, mm%10 };
  187. uint8_t code = seg7_CA[ d[digitToShow] ];
  188.  
  189. if (digitToShow == 1) code &= ~0x80;
  190.  
  191. uint8_t digitSelect = (1 << digitToShow);
  192.  
  193. shiftOut_8bit_noLatch(code);
  194. shiftOut_8bit_noLatch(digitSelect);
  195. latchData();
  196.  
  197. digitToShow = (digitToShow + 1) % 4;
  198. }
  199. /* USER CODE END 4 */
  200.  
  201. void Error_Handler(void)
  202. {
  203. __disable_irq();
  204. while (1)
  205. {
  206. }
  207. }
  208.  
  209. #ifdef USE_FULL_ASSERT
  210. void assert_failed(uint8_t *file, uint32_t line)
  211. {
  212. while (1)
  213. {
  214. }
  215. }
  216. #endif
  217. ```
Advertisement
Add Comment
Please, Sign In to add comment