[WAT-STM32-2.8] 08번 예제 헤더파일 사용으로 코드 간소화

[WAT-STM32-2.8] 08번 예제 메인 코드 간단히 하기

WAT-STM32-2.8 예제 중 8번째로 main.c 파일을 간단히 하는 예제입니다.
7번 예제까지는 1개의 파일(main.c)에서 작성하였습니다.
이번 예제에서는 공통으로 사용되는 코드를 헤더파일(.h) 로 따로 작성하여
main.c 코드를 간소화해 보겠습니다.

그리고 다음 예제부터는 main.c 코드 설명만 진행하겠습니다.

 

공동으로 사용되는 코드

1 ~ 7번까지 예제를 보면 항상 사용되는 코드가 있었습니다.
아래와 같은 코드인데, 이것은 STM32F103 시리즈에서는 모두 동일하게 사용되며 절대 변경해서는 안되는 코드입니다.

이러한 코드는 별도의 파일(main.h)로 사용하는 것이 좋습니다.

#define     __IO    volatile             
typedef unsigned           int uint32_t;
typedef __IO uint32_t  vu32; 
typedef unsigned short     int uint16_t;

#define GPIO_Pin_0                 ((uint16_t)0x0001)  /*!< Pin 0 selected */
#define GPIO_Pin_1                 ((uint16_t)0x0002)  /*!< Pin 1 selected */
#define GPIO_Pin_2                 ((uint16_t)0x0004)  /*!< Pin 2 selected */
#define GPIO_Pin_3                 ((uint16_t)0x0008)  /*!< Pin 3 selected */
#define GPIO_Pin_4                 ((uint16_t)0x0010)  /*!< Pin 4 selected */
#define GPIO_Pin_5                 ((uint16_t)0x0020)  /*!< Pin 5 selected */
#define GPIO_Pin_6                 ((uint16_t)0x0040)  /*!< Pin 6 selected */
#define GPIO_Pin_7                 ((uint16_t)0x0080)  /*!< Pin 7 selected */
#define GPIO_Pin_8                 ((uint16_t)0x0100)  /*!< Pin 8 selected */
#define GPIO_Pin_9                 ((uint16_t)0x0200)  /*!< Pin 9 selected */
#define GPIO_Pin_10                ((uint16_t)0x0400)  /*!< Pin 10 selected */
#define GPIO_Pin_11                ((uint16_t)0x0800)  /*!< Pin 11 selected */
#define GPIO_Pin_12                ((uint16_t)0x1000)  /*!< Pin 12 selected */
#define GPIO_Pin_13                ((uint16_t)0x2000)  /*!< Pin 13 selected */
#define GPIO_Pin_14                ((uint16_t)0x4000)  /*!< Pin 14 selected */
#define GPIO_Pin_15                ((uint16_t)0x8000)  /*!< Pin 15 selected */
#define GPIO_Pin_All               ((uint16_t)0xFFFF)  /*!< All pins selected */

#define RCC_APB2Periph_AFIO              ((uint32_t)0x00000001)
#define RCC_APB2Periph_GPIOA             ((uint32_t)0x00000004)
#define RCC_APB2Periph_GPIOB             ((uint32_t)0x00000008)
#define RCC_APB2Periph_GPIOC             ((uint32_t)0x00000010)
#define RCC_APB2Periph_GPIOD             ((uint32_t)0x00000020)

/************ GPIOB <*************/
typedef struct
{
  __IO uint32_t CRL;
  __IO uint32_t CRH;
  __IO uint32_t IDR;
  __IO uint32_t ODR;
  __IO uint32_t BSRR;
  __IO uint32_t BRR;
  __IO uint32_t LCKR;
} GPIO_TypeDef;

typedef struct
{
  __IO uint32_t CR;
  __IO uint32_t CFGR;
  __IO uint32_t CIR;
  __IO uint32_t APB2RSTR;
  __IO uint32_t APB1RSTR;
  __IO uint32_t AHBENR;
  __IO uint32_t APB2ENR;
  __IO uint32_t APB1ENR;
  __IO uint32_t BDCR;
  __IO uint32_t CSR;
} RCC_TypeDef;

/********* GPIOB 메모리 지정 *******/
#define PERIPH_BASE           ((uint32_t)0x40000000) 
#define APB2PERIPH_BASE       (PERIPH_BASE + 0x10000)
#define GPIOB_BASE            (APB2PERIPH_BASE + 0x0c00)
#define GPIOB               ((GPIO_TypeDef *) GPIOB_BASE)

/************ RCC 메모리 지정   *************/
#define AHBPERIPH_BASE        (PERIPH_BASE + 0x20000)
#define RCC_BASE              (AHBPERIPH_BASE + 0x1000)
#define RCC                 ((RCC_TypeDef *) RCC_BASE)

 

주요 코드

아래처럼 main.c 코드 사이즈가 줄었습니다.

// WAT_STM3228_08 example GPIOB with header file
// 1초 간격으로 LED2 ON/OFF 
//
// 예제1과 달리 헤더 파일을 사용하여 main.c 가 간단해 졌습니다.
//
// 출처: https://docs.whiteat.com/?p=3654
// 

#include "main.h"

int main(void)
{
  /* GPIOB Port Enable   */
  RCC->APB2ENR |= RCC_APB2Periph_GPIOB;	

  /*-- GPIO Mode Configuration speed, input/output -----------------------*/
  /*-- GPIOB max speed: 50MHz , General purpose output push-pull  ---*/
  GPIOB->CRL &=  0xFFFFF0FF;
  GPIOB->CRL |=  0x00000300;

  while (1)
  {	
    GPIOB->BRR = GPIO_Pin_2; 
    Delay(0x17FFFF);
    GPIOB->BSRR = GPIO_Pin_2;
    Delay(0x17FFFF);
  }
}

결과화면

1번 예제와 동일하게 1초마다 ON/OFF 합니다.

WAT-STM32-2.8보드  전체 예제

[WAT-STM32-2.8] STM32F103RB Board + 2.8인치 TFT LCD 터치 세트

제품 구매

WAT-STM32-2.8 [STM32F103RB Board + 2.8인치 TFT LCD 터치 세트] 는 https://kit128.com/goods/view?no=221 에서 구매하실 수 있습니다.

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다