BASIC4MCU | 질문게시판 | 안녕하세요 세그먼트 질문입니다
페이지 정보
작성자 stm32 작성일2024-03-21 21:03 조회1,851회 댓글4건본문
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
/* USER CODE END Includes */
/* Private typedef -----------------------------------------------------------*/
/* USER CODE BEGIN PTD */
/* USER CODE END PTD */
/* Private define ------------------------------------------------------------*/
/* USER CODE BEGIN PD */
/* USER CODE END PD */
/* Private macro -------------------------------------------------------------*/
/* USER CODE BEGIN PM */
/* USER CODE END PM */
/* Private variables ---------------------------------------------------------*/
/* USER CODE BEGIN PV */
unsigned char Font[18] = {0x3F, 0x06, 0x5B, 0x4F,
0X66, 0x6D, 0x7D, 0x07,
0x7F, 0x6F, 0x77, 0x7c,
0x39, 0x5E, 0x79, 0x71,
0x40, 0x80
};
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
void DisplayNumber(int number);
/* 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();
/* USER CODE BEGIN 2 */
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
int number = 1;
while (1)
{
DisplayNumber(number); // 현재 숫자를 표시
HAL_Delay(10); // 1초 동안 대기
number++; // 숫자 증가
if (number > 9999) number = 0; // 숫자가 9를 넘어가면 다시 0으로 초기화
}
/* USER CODE END 3 */
}
/**
* @brief System Clock Configuration
* @retval None
*/
void SystemClock_Config(void)
{
RCC_OscInitTypeDef RCC_OscInitStruct = {0};
RCC_ClkInitTypeDef RCC_ClkInitStruct = {0};
/** Configure the main internal regulator output voltage
*/
__HAL_RCC_PWR_CLK_ENABLE();
__HAL_PWR_VOLTAGESCALING_CONFIG(PWR_REGULATOR_VOLTAGE_SCALE1);
/** Initializes the RCC Oscillators according to the specified parameters
* in the RCC_OscInitTypeDef structure.
*/
RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSE;
RCC_OscInitStruct.HSEState = RCC_HSE_ON;
RCC_OscInitStruct.PLL.PLLState = RCC_PLL_ON;
RCC_OscInitStruct.PLL.PLLSource = RCC_PLLSOURCE_HSE;
RCC_OscInitStruct.PLL.PLLM = 8;
RCC_OscInitStruct.PLL.PLLN = 168;
RCC_OscInitStruct.PLL.PLLP = RCC_PLLP_DIV2;
RCC_OscInitStruct.PLL.PLLQ = 4;
if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK)
{
Error_Handler();
}
/** Initializes the CPU, AHB and APB buses clocks
*/
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_DIV2;
if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_2) != HAL_OK)
{
Error_Handler();
}
}
/**
* @brief GPIO Initialization Function
* @param None
* @retval None
*/
static void MX_GPIO_Init(void)
{
GPIO_InitTypeDef GPIO_InitStruct = {0};
/* GPIO Ports Clock Enable */
__HAL_RCC_GPIOH_CLK_ENABLE();
__HAL_RCC_GPIOC_CLK_ENABLE();
__HAL_RCC_GPIOA_CLK_ENABLE();
/*Configure GPIO pin Output Level */
HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7, GPIO_PIN_RESET);
HAL_GPIO_WritePin(GPIOA, GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3, GPIO_PIN_RESET);
/*Configure GPIO pins : PC0 PC1 PC2 PC3 PC4 PC5 PC6 PC7 */
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3|GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW;
HAL_GPIO_Init(GPIOC, &GPIO_InitStruct);
/*Configure GPIO pins : PA0 PA1 PA2 PA3 */
GPIO_InitStruct.Pin = GPIO_PIN_0|GPIO_PIN_1|GPIO_PIN_2|GPIO_PIN_3;
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);
}
/* USER CODE BEGIN 4 */
void DisplayNumber(int number) {
int i;
int digit;
int temp = number;
for (i = 0; i < 30; i++) {
digit = temp % 10; // Extract the last digit
HAL_GPIO_WritePin(GPIOC, Font[digit], GPIO_PIN_SET); // Set the corresponding segments
HAL_Delay(1000); // Delay for stability
HAL_GPIO_WritePin(GPIOC, Font[digit], GPIO_PIN_RESET); // Reset the segments
temp /= 10; // Move to the next digit
}
}
/* 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 */
/* User can add his own implementation to report the HAL error return state */
__disable_irq();
while (1)
{
}
/* USER CODE END Error_Handler_Debug */
}
숫자 +1씩 증가하는 코드를 만들고 있습니다 그런데 함수를 넣었는데 계속 하나씩 올라가는게 아니라 4자리 전부다 1씩 올라갑니다
0000부터 +1식 증가할려면 어떤부분을 수정해야되나요?
댓글 4
조회수 1,851master님의 댓글
master 작성일
while(1){
DisplayNumber(number);
HAL_Delay(10); // 1초 동안 대기
if(++number>9999)number=0;
}
메인루프의 딜레이는 주석에 1초로 적혀있지만 실제로는 10ms밖에 안되고요
딜레이를 디스플레이에서 표시하는 것으로 1초를 할당 하려면 루프 딜레이를 삭제해야겠죠
while(1){
DisplayNumber(number);
if(++number>9999)number=0;
}
세그먼트와 (디지트)콤몬 구동이 제대로 되어 있어서 발생하는 문제입니다.
master님의 댓글
master 작성일
void DisplayNumber(int number){
int i,k,digit,n,f[4];
n=number;
for(k=0;k<4;k++){ f[k]=n%10; n/=10; }
//
for(i=0;i<250;i++){ // 4ms*250=1000ms
for(k=0;k<4;k++){ // 4ms
GPIOC->ODR=Font[f[k]];
GPIOA->ODR=~(1<<k); HAL_Delay(1); GPIOA->ODR=0xF;
}
}
}
stm32님의 댓글
stm32 작성일
알려준시대로 사용했더니 카운터는 나오는데 세그먼트가 1xxx부터 올라서
oid DisplayNumber(int number) {
int i, k, digit, n, f[4];
n = number;
for (k = 0; k < 4; k++) {
f[k] = n % 10;
n /= 10;
}
//
for (i = 0; i < 250; i++) { // 4ms * 250 = 1000ms
for (k = 3; k >= 0; k--) { // 끝에서부터 시작하여 첫 번째 세그먼트까지
GPIOC->ODR = Font[f[k]];
GPIOA->ODR = ~(1 << (3 - k));
HAL_Delay(1);
GPIOA->ODR = 0xF;
}
}
}
참고해서 수정했더니 되었습니다 항상 감사합니다!!
master님의 댓글
master 작성일
GPIOA->ODR = ~(1 << (3 - k));
GPIOA->ODR = ~(8 >> k);
아래 코드가 더 간단하겠죠?