Skip to main content

3. GPIO dan System Clock



3.1. INISIALISASI DENGAN CMSIS

Penanganan konfigurasi GPIO dan System Clock menurut CMSIS untuk STM32 seri F1 (ARM Cortex-M3) berbeda dengan seri F0 (ARM Cortex-M0) , F3 dan F4 (ARM Cortex-M4). Cortex-M3 merupakan produk awal ARM. M0 dan M4 adalah produk lebih baru.

Berikut adalah struktur folder proyek Keil uvision :

STM32F1:

Lihat RM0008 STM32F103xx Reference Manual rev.20


 1. RCC_APB2ENR      > enable GPIOx clock APB2 peripheral.  
                       8.3.7  APB2 peripheral clock enable register (RCC_APB2ENR) 
                       IO PortA: bit2, PortB: bit3, port C : bit4, dst.
 2. GPIOx_CRL        > 9.2.1  Port configuration register low (GPIOx_CRL) 
 3. GPIOx_CRH        > 9.2.1  Port configuration register high (GPIOx_CRH)
 4. GPIOx_BSRR       > 9.2.5  Port bit set/reset register (GPIOx_BSRR)
    atau GPIOx_BRR     9.2.6 Port bit reset register (GPIOx_BRR)
    atau GPIOx_ODR     9.2.4  Port output data register (GPIOx_ODR) 
    atau GPIOx_IDR     9.2.3  Port input data register (GPIOx_IDR)

STM32F0, F3, F4:

jangan lupa lihat reference manual masing-masing.


 0. RCC_AHB1ENR
 1. GPIOx_MODER              
 2. GPIOx_OTYPER
 3. GPIOx_OSPEEDR
 4. GPIOx_ODR 
    atau GPIOx_IDR
    atau GPIOx_BSRR
    atau GPIOx_BRR

Contoh source-code blinky untuk stm32f4 :


#include "stm32f4xx.h"
int b;
void main()
{
  RCC->AHB1ENR |=0x09;
  GPIOD->MODER|=1<<2*15;
  GPIOD->OSPEEDR=(unsigned)(2<<2*15);
  GPIOD->OTYPER=0<<2*15;
  GPIOD->PUPDR=1<<2*15;

  while(1)
  {
    b=0x01&GPIOA->IDR;
    if (b==1)GPIOD->ODR|=(1<<15);
    else GPIOD->ODR&=~(1<<15);
  }
}     

3.2. INISIALISASI DENGAN HAL

STM32CubeMx merupakan code generator, artinya anda tentukan beberapa hal, selanjutnya CubeMx yang menyiapkan code bagi anda. Untuk setting GPIO dan System Clock, STM32CubeMx menyediakan fasilitas grafis yang sangat membantu. Perhatikan contoh-contoh proyek pada bab lalu.

Berikut adalah struktur folder yang di-generate (dicipta secara otomatis) oleh CubeMX :

Inisialisasi system clock dan GPIO setelah code di generate akan memunculkan SystemClock_Config(); dan MX_GPIO_Init(); seperti terlihat di bawah.
Source code main.c bisa dilihat di folder "Src".


/* Includes ------------------------------------------------------------------*/
#include "main.h"

/* Private function prototypes -----------------------------------------------*/
void SystemClock_Config(void);
static void MX_GPIO_Init(void);

int main(void)
{
  /* Reset of all peripherals, Initializes the Flash interface and the Systick. */
  HAL_Init();

  /* Configure the system clock */
  SystemClock_Config();

  /* Initialize all configured peripherals */
  MX_GPIO_Init();

  /* Infinite loop */
  while (1)
  {
    // codemu disini
  }
}


/* Error_Handler_Debug */
void Error_Handler(void)
{
}

#ifdef  USE_FULL_ASSERT
void assert_failed(uint8_t *file, uint32_t line)
{ 
}
#endif /* USE_FULL_ASSERT */

3.3. MANIPULASI GPIO

a. Manipulasi GPIO menggunakan CMSIS

GPIO paling mudah diatur dengan CMSIS, contoh2 banyak sekali di internet. Berikut beberapa yg menarik:

b. Manipulasi GPIO Dengan HAL

Berikut 3 buah GPIO functions yang harus anda kenali saat ini dari library HAL : (lihat disini untuk lebih rinci...)

  • Reads the specified input port pin.
      HAL_GPIO_ReadPin (GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)
  • Sets or clears the selected data port bit.
      HAL_GPIO_WritePin (GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin, GPIO_PinState PinState)
  • Toggles the specified GPIO pins.
      HAL_GPIO_TogglePin (GPIO_TypeDef *GPIOx, uint16_t GPIO_Pin)

c. Menjalankan beberapa GPIO sekaligus

It is possible to operate multiple GPIOs by manipulating the registers directly, but it is not general purpose. Lihat bbrp contoh berikut:


3.4. CONTOH PROYEK

Berikut beberapa contoh proyek yang menjelaskan penggunaan manipulasi GPIO untuk on-off LED:

Previous  |  ke atas  |  Home  |  Disclaimer  | 

Comments