STM32F4 Discovery – Bài 2: Làm việc với GPIO


Giới thiệu chung

Vi điều khiển STM32F407VG có 5 bộ chân, mỗi bộ – gồm tối đa 16 chân – ta gọi là một cổng, ký hiệu A, B, C, D, E… Mỗi chân của cổng có thể được cấu hình làm đầu vào hoặc đầu ra cho tín hiệu số với các tham số phụ được thiết lập phù hợp với mục đích sử dụng (GPIO – General-purpose Input/Output). Bên cạnh đó, mỗi chân cũng có thể đảm trách một trong số 16 chức năng thay thế định trước (AFIO – Alternate Function Input/Output). Các tham số thiết lập cũng như các chức năng của chân có thể được cấu hình bằng chương trình người dùng thông qua việc ghi các giá trị phù hợp vào các thanh ghi bên trong vi điều khiển. Việc thay đổi giá trị các thanh ghi này cho phép xác định các tính chất của các chân và các cổng.

Mỗi cổng GPIO có:

  • 4 thanh ghi cấu hình 32-bit: GPIOx_MODER, GPIOx_OTYPER, GPIOx_OSPEEDR và GPIOx_PUPDR
  • 2 thanh ghi dữ liệu 32-bit: GPIOx_IDR and GPIOx_ODR
  • 1 thanh ghi 32-bit set/reset: GPIOx_BSRR
  • 1 thanh ghi khóa 32-bit: GPIOx_LCKR
  • 1 thanh ghi 32-bit lựa chọn chức năng thay thế (alternate function): GPIOx_AFRH và GPIOx_AFRL

Các cổng GPIO có thể làm việc ở nhiều chế độ khác nhau, việc cấu hình lựa chọn chế độ làm việc cho mỗi chân của cổng được thực hiện thông qua chương trình. Dưới đây là các chế độ làm việc của cổng:

  • Input floating
  • Input pull-up
  • Input pull-down
  • Analog
  • Output open-drain with pull-up or pull-down capability
  • Output push-pull with pull-up or pull-down capability
  • Alternate function push-pull with pull-up or pull-down capability
  • Alternate function open-drain with pull-up or pull-down capability
VDD_FT là chân vào/ra chịu được hiệu điện thế 5V, khác với chân VDD

Bảng các bit cấu hình của cổng:

GP=general-purpose, PP=push-pull, PU=pull-up, PD=pull-down, OD=open-drain, AF=alternate function

Để hiểu nguyên tắc làm việc với các cổng, chúng ta xem xét ví dụ lập trình điều khiển đèn LED. Trên board STM32F4Discovery có sẵn 8 đèn LED, trong đó có 4 đèn dành cho người dùng với các màu orange, green, red, blue. Chúng ta sẽ lập trình điều khiển 4 đèn này. 

Schematic khối LED đơn trên KIT STM32F4 Discovery

Ví dụ 1: Chớp tắt LED trên chân PD12

#include "stm32f4xx.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_gpio.h"

/* Ham giu cham */
void Delay(__IO uint32_t nCount)

int main(void) 
/* Khai bao struct de lam viec voi cong GPIO */
	GPIO_InitTypeDef CongD;

/* Ket noi GPIOD voi clock va bat clock */
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

/* Khoi tao cong D la output, chuan bi xuat du lieu ra chan 12 */
	CongD.GPIO_Pin = GPIO_Pin_12;
	CongD.GPIO_Mode = GPIO_Mode_OUT;
	CongD.GPIO_OType = GPIO_OType_PP;
	CongD.GPIO_Speed = GPIO_Speed_100MHz;
/* Goi ham khoi tao cong */
	GPIO_Init(GPIOD, &CongD);

	while (1) 
	/* Dao bit chan 12 cua GPIOD */
		GPIO_ToggleBits(GPIOD, GPIO_Pin_12);

	/* Doi mot khoang thoi gian */

Ví dụ 2:

#include "stm32f4xx.h"
#include "stm32f4xx_rcc.h"
#include "stm32f4xx_gpio.h"

int main(void) {
	GPIO_InitTypeDef CongD, CongA;

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);

	CongD.GPIO_Pin = GPIO_Pin_13 | GPIO_Pin_14;
	CongD.GPIO_Mode = GPIO_Mode_OUT;
	CongD.GPIO_OType = GPIO_OType_PP;
	CongD.GPIO_Speed = GPIO_Speed_100MHz;
//Khoi tao cong D
	GPIO_Init(GPIOD, & CongD);

	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA, ENABLE);

	CongA.GPIO_Pin = GPIO_Pin_0;
	CongA.GPIO_Mode = GPIO_Mode_IN;
	CongA.GPIO_OType = GPIO_OType_PP;
	CongA.GPIO_Speed = GPIO_Speed_100MHz;
	//Khoi tao cong A
	GPIO_Init(GPIOA, & CongA);

	while (1) 
		if (GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_0)) 
			GPIO_SetBits(GPIOD, GPIO_Pin_13 | GPIO_Pin_14);
			GPIO_ResetBits(GPIOD, GPIO_Pin_13 | GPIO_Pin_14);


  1. A motivating discussion is definitely worth comment.

    I believe that you ought to write more about this topic, it may not
    be a taboo subject but generally people don’t discuss such topics.
    To the next! Kind regards!!

  2. Hi there! Quick question that’s totally off topic.

    Do you know how to make your site mobile friendly? My site looks weird
    when browsing from my apple iphone. I’m trying to find a theme or
    plugin that might be able to resolve this problem.
    If you have any suggestions, please share. Thank

  3. Have you ever considered about adding a little bit more than just your articles?
    I mean, what you say is fundamental and
    all. Nevertheless imagine if you added some great visuals or video clips to
    give your posts more, “pop”! Your content is excellent but with pics and videos, this site could undeniably be one of the most beneficial in its field.
    Terrific blog!

  4. My brother recommended I may like this website.
    He used to be totally right. This submit actually made my
    day. You can not consider simply how much time I had spent for this info!

  5. Hi there I am so grateful I found your blog, I
    really found you by mistake, while I was searching on Bing for
    something else, Regardless I am here now and would just like to say thanks for a incredible post and a all round
    exciting blog (I also love the theme/design), I don’t have time to browse it all at the moment but
    I have book-marked it and also added in your RSS feeds,
    so when I have time I will be back to read more, Please do keep up the
    superb job.

  6. Howdy would you mind letting me know which web host you’re working with?
    I’ve loaded your blog in 3 different internet browsers and I must say this blog loads a lot
    quicker then most. Can you suggest a good internet hosting provider at a honest price?
    Many thanks, I appreciate it!

  7. Simply desire to say your article is as astonishing. The clearness in your submit is just cool and
    i can think you are an expert on this subject. Well with
    your permission allow me to grasp your feed to
    stay updated with forthcoming post. Thank you
    one million and please carry on the rewarding work.

  8. I blog qᥙite odten and I seriously apprecіate
    your informatіon. The аrtіclе has reaⅼly peaked mmy interest.
    I’m going tto bookmark your blog and keep checking fоr new details abⲟut once per week.

    I opted in f᧐r your RSS fewd as well.

  9. Hey would you mind stating which blog platform you’re working with?
    I’m looking to start my own blog soon but I’m having a tough time selecting between BlogEngine/Wordpress/B2evolution and Drupal.
    The reason I ask is because your layout seems different then most blogs and I’m
    looking for something unique. P.S Sorry for being off-topic but I had to ask!

    my website

  10. Have you ever considered publishing an e-book or guest authoring on other blogs?
    I have a blog based on the same ideas you discuss and would love to have you share some stories/information. I know my audience would
    appreciate your work. If you’re even remotely interested, feel
    free to send me an email.

    My web-site ::

  11. I enjoy you because of every one of your hard work on this web site.
    Gloria takes pleasure in getting into investigation and it is simple to grasp why.

    We know all about the compelling mode you convey functional guides through this web
    site and therefore increase response from others about this area of
    interest plus my child has been understanding a lot of things.
    Take pleasure in the remaining portion of the new year.
    You are always conducting a very good job.[X-N-E-W-L-I-N-S-P-I-N-X]I am really impressed along with your writing talents and also with the structure in your weblog.
    Is this a paid subject or did you modify it your self?
    Either way stay up the nice high quality writing, it’s rare to peer a nice blog like
    this one today.

    Here is my website:

  12. Hey I know this is off topic but I was wondering if you knew of any widgets I could add to my
    blog that automatically tweet my newest twitter updates.
    I’ve been looking for a plug-in like this for quite some time and was hoping maybe
    you would have some experience with something like this.
    Please let me know if you run into anything.
    I truly enjoy reading your blog and I look forward to your new

  13. Excellent goods from you, man. I have understand your stuff previous to and you are just too
    great. I actually like what you have acquired
    here, really like what you’re saying and the way in which you say it.
    You make it entertaining and you still care for to keep it smart.
    I can’t wait to read far more from you. This is really a terrific site.


Please enter your comment!
Please enter your name here