[WAT-STM32-2.8] 09번 예제 헤더파일 사용으로 코드 간소화(72MHz)

[WAT-STM32-2.8] 09번 예제 메인 코드 간소화(72MHz)

 

WAT-STM32-2.8 예제 중 9번째로 main.c 파일(72MHz 용)을 간단히 하는 예제입니다.
이번 예제에서는 헤더파일(.h) 로 따로 작성하여 72MHz 로 동작합니다.

공동으로 사용되는 코드

예제8 처럼 별도의 파일(main.h)을 사용하며 RCC 부분이 추가되었습니다.

#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)

/************ GPIO <*************/
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;

typedef struct
{
  __IO uint32_t ACR;
} FLASH_INTERFACE; 



/********* GPIOB Adress 지정 *******/
#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 Adress 지정   *************/
#define AHBPERIPH_BASE        (PERIPH_BASE + 0x20000)
#define RCC_BASE              (AHBPERIPH_BASE + 0x1000)
#define RCC                 ((RCC_TypeDef *) RCC_BASE)

/************ FLASH Address 지정   *************/
#define FLASH_BASE   	((uint32_t)0x40022000) 
#define FLASH     ((FLASH_INTERFACE *) FLASH_BASE)

void Delay(vu32 nCount) // delay function
{
  for(; nCount != 0; nCount--);
}
주요 코드

아래처럼 RCC 를 추가하여 main.c 를 작성합니다.

// WAT_STM3228_09 72MHz로 동작
// 1초 간격으로 LED2 ON/OFF
//
// 출처: https://docs.whiteat.com/?p=3658
// 

#include "main.h"

int main(void)
{
  // Clock control register
  // Bit17 HSERDY: External high-speed clock ready flag
  // Bit16 HSEON: External high-speed clock enable
  unsigned char sws = 0;
  RCC->CR |= 0X00010000; // HSEON
  while(!(RCC->CR>>17));  //HSE 준비될때까지 기다림

  // Clock configuration register (RCC_CFGR)
  // Bits21:18 PLLMUL: PLL multiplication factor
  // Bits10:8 PPRE1: APB low-speed prescaler (APB1)
  /* Bit10-8 0x100    100: HCLK divided by 2  */
  RCC->CFGR = 0x00000400;   

  /* 
  0000:PLL input clock x 2	  1000:PLL input clock x 10
  0001:PLL input clock x 3	  1001:PLL input clock x 11
  0010:PLL input clock x 4	  1010:PLL input clock x 12
  0011:PLL input clock x 5	  1011:PLL input clock x 13
  0100:PLL input clock x 6	  1100:PLL input clock x 14
  0101:PLL input clock x 7	  1101:PLL input clock x 15
  0110:PLL input clock x 8	  1110:PLL input clock x 16
  0111:PLL input clock x 9	  1111:PLL input clock x 16	 
  */

  RCC->CFGR |= 7<<18; // PLL input clock x 9 -> 72MHz
  RCC->CFGR |= 1<<16; //1: HSE oscillator clock selected as PLL input clock
  FLASH->ACR|=0x32;
  RCC->CR |= 1<<24;   //1: PLL ON

  while(!(RCC->CR>>25)); //0: PLL unlocked 기다림 
  RCC->CFGR |= 1<<1;  //10: PLL selected as system clock

  while(sws != 0x2)	 // 10: PLL used as system clock
  {
    sws = RCC->CFGR>>2;	 // Bits3:2 10: PLL used as system clock
    sws &= 0x3;
  }

  /* 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*5);  // 72MHz 일때 약 1초
    GPIOB->BSRR = GPIO_Pin_2;
    Delay(0x17FFFF*5); // 72MHz 일때 약 1초
  }
}

결과화면

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 에서 구매하실 수 있습니다.

답글 남기기

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