일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- 스텝모터
- AXI4
- AMBA
- ordering model
- Verilog
- tff
- QoS
- Low-power Interface
- 구조적모델링
- stepmotor
- 임베디드시스템
- single copy atomic size
- FPGA
- Interoperability
- T flip flop
- FGPA #반도체설계 #verilog #시프트레지스터 #uart
- 펌웨어
- SoC
- STM32
- APB3
- out-of-order
- ABMA
- 레지스터슬라이스
- cacheable
- Multiple transaction
- 카운터
- atomic access
- Multiple outstanding
- ERROR RESPONSE
- AXI3
Archives
- Today
- Total
CHIP KIDD
Queue 자료구조 구현 본문
ARM을 이용해
Circular Queue 를 구현하는 자료입니다.
위 그림에서 설명하는 단계대로 코드를 구현해보겠습니다.
중요한 점은 Full 과 Empty 상태를 알아내는 것.
+ Rear와 Front는 항상 붙어다니는 존재이기에 TypeDef Struct 를 통한 구조체로 만드는게 편리합니다.
+ 포인터를 이용해 직접 메모리에 접근하겠습니다. ( * / -> / &)
Queue.c
/*
* Queue.c
*
* Created on: May 6, 2021
* Author: kccistc
*/
#include "Queue.h"
void QueInit(QUEUE *que)
{
que->rear = 0;
que->front = 0;
}
uint8_t isQueFull(QUEUE *que)
{
return ((que->rear+1)%QUE_SIZE == que->front);
}
uint8_t isQueEmpty(QUEUE *que)
{
return (que->rear == que->front);
}
void EnQue(QUEUE *que, int data)
{
if(isQueFull(que)) return;
que->rear = (que->rear + 1) % QUE_SIZE;
que->aQueBuff[que->rear] = data;
}
uint8_t DeQue(QUEUE *que)
{
if(isQueEmpty(que)) {
return 0;
}
else{
que->front = (que->front +1) % QUE_SIZE;
return que->aQueBuff[que->front];
}
}
Main.c
/* USER CODE BEGIN Header */
/**
******************************************************************************
* @file : main.c
* @brief : Main program body
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2021 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* USER CODE END Header */
/* Includes ------------------------------------------------------------------*/
#include "main.h"
/* Private includes ----------------------------------------------------------*/
/* USER CODE BEGIN Includes */
#include "../UserCode/Queue.h"
#include <stdio.h>
/* 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 ---------------------------------------------------------*/
UART_HandleTypeDef huart2;
/* USER CODE BEGIN PV */
/* USER CODE END PV */
/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
/* USER CODE BEGIN PFP */
/* USER CODE END PFP */
/* Private user code ---------------------------------------------------------*/
/* USER CODE BEGIN 0 */
int __io_putchar(int ch)
{
HAL_UART_Transmit(&huart2, &ch, 1, 100);
return ch;
}
/* 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_USART2_UART_Init();
/* USER CODE BEGIN 2 */
QUEUE btn1QueBuf;
QueInit(&btn1QueBuf);
int count =10;
for (int i =0; i<14; i++){
count = count +i;
EnQue(&btn1QueBuf, count);
printf("index : %d, Enque : %d\n", i, count);
}
for (int i =0; i<15; i++){
if (isQueEmpty(&btn1QueBuf)){
printf("index : %d, QueBuff is Empty !\n", i );
}
else {
printf("index : %d, Deque : %d\n", i, DeQue(&btn1QueBuf));
}
}
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
}
/* USER CODE END 3 */
}
Queue.h
/*
* Queue.h
*
* Created on: May 6, 2021
* Author: kccistc
*/
#ifndef USERCODE_QUEUE_H_
#define USERCODE_QUEUE_H_
#include "stm32f4xx_hal.h"
#define QUE_SIZE 10
typedef struct _queue {
int rear;
int front;
int aQueBuff[QUE_SIZE];
}QUEUE;
void QueInit(QUEUE *que);
uint8_t isQueFull(QUEUE *que);
uint8_t isQueEmpty(QUEUE *que);
void EnQue(QUEUE *que, int data);
uint8_t DeQue(QUEUE *que);
#endif /* USERCODE_QUEUE_H_ */
결과값
'전기전자 > ARM' 카테고리의 다른 글
[ARM] I2C 통신을 이용한 DS3231 RTC(Real Time Clock) 구현 (2) | 2021.03.26 |
---|---|
[ARM] 엘레베이터 프로젝트 - SPI 통신을 이용한 OLED 구현 (0) | 2021.03.18 |
[ARM] 엘레베이터 프로젝트 - EXTI 외부인터럽터 / 포토 인터럽터 센서 이용 (0) | 2021.03.18 |
[ARM] 엘레베이터 프로젝트- Step Motor 회전속도 및 방향 제어 (0) | 2021.03.16 |