[WAT-STM32-2.8] 04번 예제 GPIOA,GPIOB로 LED 이동

[WAT-STM32-2.8] 04번 예제 GPIOA,GPIOB 로 LED 이동

WAT-STM32-2.8 예제 중 4번째로 GPIOA, GPIOB 로 LED 를 이동하는 예제입니다.
LED1, LED2, LED3 를 1초 간격으로 ON/OFF 합니다.

 

 

주요 코드

// WAT_STM3228_04 GPIOA, GPIOB 으로 LED 이동
// 1초 간격으로 LED1, LED2, LED3 하나씩 이동 
//
// 출처: https://docs.whiteat.com/?p=3426
// 

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

/********* GPIOA 메모리 지정 *******/
#define PERIPH_BASE           ((uint32_t)0x40000000) 
#define APB2PERIPH_BASE       (PERIPH_BASE + 0x10000)
#define GPIOA_BASE            (APB2PERIPH_BASE + 0x0800)
#define GPIOA               ((GPIO_TypeDef *) GPIOA_BASE)

/********* 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)


void Delay(vu32 nCount);

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

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

	GPIOB->CRL &=  0xFFFFF0FF;
	GPIOB->CRL |=  0x00000300;

  	while (1)
  	{	 

	 	GPIOA->BRR = GPIO_Pin_2;		/* LED1 on*/
		Delay(0x5FFFF); 				 

		GPIOB->BRR = GPIO_Pin_2;		 /* LED2 on*/
		Delay(0x5FFFF);  			  

		GPIOA->BRR = GPIO_Pin_3;	   /* LED3 on*/
		Delay(0x5FFFF);				   
  	}
}

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

결과화면

 

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

답글 남기기

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