Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* USER CODE BEGIN Header */
- /**
- ******************************************************************************
- * @file : main.c
- * @brief : Main program body
- ******************************************************************************
- */
- /* USER CODE END Header */
- #include "main.h"
- #include <string.h>
- /* Private variables */
- volatile uint16_t virtualSecDelay = 1000;
- volatile uint32_t fastTicks = 0;
- volatile uint16_t virtSecCnt = 0;
- volatile uint8_t hh = 10, mm = 43, ss = 0;
- volatile uint8_t digitToShow = 0;
- static uint32_t lastBtn1Press = 0;
- static uint32_t lastBtn2Press = 0;
- /* Private function prototypes */
- void SystemClock_Config(void);
- static void MX_GPIO_Init(void);
- static void shiftOut_8bit_noLatch(uint8_t data);
- static void latchData(void);
- static void beep(uint16_t ms);
- /* USER CODE BEGIN PFP */
- /* USER CODE END PFP */
- /* Private user code ---------------------------------------------------------*/
- static const uint8_t seg7_CA[10] = {
- ~0x3F, ~0x06, ~0x5B, ~0x4F, ~0x66,
- ~0x6D, ~0x7D, ~0x07, ~0x7F, ~0x6F
- };
- static void shiftOut_8bit_noLatch(uint8_t data)
- {
- for(int i = 0; i < 8; i++){
- HAL_GPIO_WritePin(DATA_GPIO_Port, DATA_Pin, (data & 0x80) ? GPIO_PIN_SET : GPIO_PIN_RESET);
- HAL_GPIO_WritePin(CLOCK_GPIO_Port, CLOCK_Pin, GPIO_PIN_SET);
- HAL_GPIO_WritePin(CLOCK_GPIO_Port, CLOCK_Pin, GPIO_PIN_RESET);
- data <<= 1;
- }
- }
- static void latchData(void)
- {
- HAL_GPIO_WritePin(LATCH_GPIO_Port, LATCH_Pin, GPIO_PIN_SET);
- HAL_GPIO_WritePin(LATCH_GPIO_Port, LATCH_Pin, GPIO_PIN_RESET);
- }
- static void beep(uint16_t ms)
- {
- HAL_GPIO_WritePin(BUZZER_GPIO_Port, BUZZER_Pin, GPIO_PIN_RESET);
- for (volatile uint32_t i = 0; i < ms * 1200; i++);
- HAL_GPIO_WritePin(BUZZER_GPIO_Port, BUZZER_Pin, GPIO_PIN_SET);
- }
- /**
- * @brief The application entry point.
- */
- int main(void)
- {
- HAL_Init();
- SystemClock_Config();
- MX_GPIO_Init();
- while (1)
- {
- HAL_GPIO_WritePin(BUZZER_GPIO_Port, BUZZER_Pin, GPIO_PIN_SET);
- if (fastTicks >= virtualSecDelay) {
- fastTicks = 0;
- virtSecCnt++;
- ss++;
- if (ss >= 60) { ss = 0; mm++; }
- if (mm >= 60) { mm = 0; hh++; }
- if (hh >= 24) hh = 0;
- if (virtSecCnt >= 3600) {
- virtSecCnt = 0;
- beep(100);
- }
- }
- uint32_t now = HAL_GetTick();
- if (HAL_GPIO_ReadPin(BUTTON1_GPIO_Port, BUTTON1_Pin) == GPIO_PIN_RESET) {
- if (now - lastBtn1Press > 200) {
- lastBtn1Press = now;
- if (virtualSecDelay > 120)
- virtualSecDelay -= 100;
- else
- virtualSecDelay = 20;
- }
- }
- if (HAL_GPIO_ReadPin(BUTTON2_GPIO_Port, BUTTON2_Pin) == GPIO_PIN_RESET) {
- if (now - lastBtn2Press > 200) {
- lastBtn2Press = now;
- if (virtualSecDelay < 1900)
- virtualSecDelay += 100;
- else
- virtualSecDelay = 2000;
- }
- }
- }
- }
- /**
- * @brief System Clock Configuration
- */
- void SystemClock_Config(void)
- {
- RCC_OscInitTypeDef RCC_OscInitStruct = {0};
- RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
- RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI;
- RCC_OscInitStruct.HSIState = RCC_HSI_ON;
- RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT;
- RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE;
- 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_HSI;
- RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1;
- RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1;
- RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1;
- if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK)
- {
- Error_Handler();
- }
- }
- /**
- * @brief GPIO Initialization Function
- */
- static void MX_GPIO_Init(void)
- {
- GPIO_InitTypeDef GPIO_InitStruct = {0};
- __HAL_RCC_GPIOA_CLK_ENABLE();
- __HAL_RCC_GPIOB_CLK_ENABLE();
- HAL_GPIO_WritePin(GPIOA, CLOCK_Pin|DATA_Pin, GPIO_PIN_RESET);
- HAL_GPIO_WritePin(BUZZER_GPIO_Port, BUZZER_Pin, GPIO_PIN_SET);
- HAL_GPIO_WritePin(LATCH_GPIO_Port, LATCH_Pin, GPIO_PIN_RESET);
- GPIO_InitStruct.Pin = BUTTON1_Pin|BUTTON2_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_INPUT;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- GPIO_InitStruct.Pin = BUTTON3_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_IT_FALLING;
- GPIO_InitStruct.Pull = GPIO_PULLUP;
- HAL_GPIO_Init(BUTTON3_GPIO_Port, &GPIO_InitStruct);
- GPIO_InitStruct.Pin = CLOCK_Pin|DATA_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(GPIOA, &GPIO_InitStruct);
- GPIO_InitStruct.Pin = BUZZER_Pin|LATCH_Pin;
- GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
- GPIO_InitStruct.Pull = GPIO_NOPULL;
- GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
- HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
- }
- /* USER CODE BEGIN 4 */
- void SysTick_Handler(void)
- {
- HAL_IncTick();
- fastTicks++;
- uint8_t d[4] = { hh/10, hh%10, mm/10, mm%10 };
- uint8_t code = seg7_CA[ d[digitToShow] ];
- if (digitToShow == 1) code &= ~0x80;
- uint8_t digitSelect = (1 << digitToShow);
- shiftOut_8bit_noLatch(code);
- shiftOut_8bit_noLatch(digitSelect);
- latchData();
- digitToShow = (digitToShow + 1) % 4;
- }
- /* USER CODE END 4 */
- void Error_Handler(void)
- {
- __disable_irq();
- while (1)
- {
- }
- }
- #ifdef USE_FULL_ASSERT
- void assert_failed(uint8_t *file, uint32_t line)
- {
- while (1)
- {
- }
- }
- #endif
- ```
Advertisement
Add Comment
Please, Sign In to add comment