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

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

Để 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.

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)
{
while(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_PuPd = GPIO_PuPd_NOPULL;
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 */
Delay(100000);
}
}
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_PuPd = GPIO_PuPd_NOPULL;
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_PuPd = GPIO_PuPd_DOWN;
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);
else
GPIO_ResetBits(GPIOD, GPIO_Pin_13 | GPIO_Pin_14);
}
}
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!!
Hello, everything is going well here and ofcourse every one
is sharing information, that’s actually good,
keep up writing.
Saved as a favorite, I love your website!
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
you!
I tһink the admin of this ѕіte is reazlly
working һard in support of his website, since herte every stuff is
quality ƅased material.
Тake a look at my page; Death – Obituary – shooting
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!
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!
Thanks!
Good post! We are linking to this particularly great
post on our site. Keep up the great writing.
Its not my first time to go to see this website, i am browsing this web site dailly
and take good facts from here everyday.
Here is my blog post … http://www.corprewfuneralhome.net
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.
I was suggested this website by my cousin. I’m not sure whether this post is written by him as no one else know such detailed about my trouble.
You are incredible! Thanks!
This post is genuinely a fastidious one it assists
new web visitors, who are wishing in favor of blogging.
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!
It’s really very complex in this full of activity life to listen news on TV, so I just use
the web for that purpose, and get the hottest information.
You should take part in a contest for one of the best websites online.
I am going to recommend this web site!
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.
Hey there! Do you know if they make any plugins to help with
Search Engine Optimization? I’m trying to get my blog to rank for some targeted keywords
but I’m not seeing very good gains. If you know of any please share.
Cheers!
Also visit my blog: wsgcparkinglot.com
Τhankfᥙⅼnesss tto my father who told mee regarding this
weblog, this web site is in fact amazing.
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.
It is in point of fact a great and useful piece of info.
I am satisfied that you shared this useful info with us. Please
keep us informed like this. Thank you for sharing.
Also visit my web page: freeholmes.com
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 fyfc.net
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 :: topads.fr
Really when someone doesn’t be aware of after that its up to other visitors that they will help, so here it happens.
My web blog: pathakwavecurecenter.com
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: sertified.org
If some one wants expert view about running a blog then i recommend him/her to pay a visit this website, Keep up the pleasant job.
Also visit my web page: railwayearth.com
I seriously love your website.. Very nice colors & theme. Did you make this website yourself?
Please reply back as I?m looking to create my own personal site and want to know where you
got this from or just what the theme is named.
Many thanks!
Have a look at my blog post; ydp4clinic.co.kr
Do you have a spam issue on this site; I also am a blogger, and I was curious about your situation;
many of us have developed some nice methods and we are looking to trade strategies with other folks,
why not shoot me an e-mail if interested.
Feel free to surf to my site: https://www.groovelineentertainment.com
I know this if off topic but I’m looking into starting
my own blog and was wondering what all is needed to get setup?
I’m assuming having a blog like yours would cost a pretty penny?
I’m not very web smart so I’m not 100% sure. Any tips or advice would be greatly appreciated.
Many thanks
I have read so many articles regarding the blogger lovers
except this piece of writing is actually a fastidious article, keep it up.
I’m impressed, I must say. Rarely do I encounter a blog that’s equally educative
and engaging, and without a doubt, you have hit the nail on the head.
The issue is something too few folks are speaking intelligently about.
I am very happy I came across this during my hunt for something regarding
this.
Good info. Lucky me I ran across your website by chance (stumbleupon).
I have book marked it for later!
Very good write-up. I absolutely love this website. Continue the good work!
Visit my website … https://kebe.top
Utterly composed articles, Really enjoyed looking at.
Here is my page: kebe.top
As I website possessor I conceive the content material here is rattling superb,
thanks for your efforts.
My blog … exterminatorsouthflorida.com
I am continually searching online for tips that can facilitate me.
Thx!
Feel free to surf to my web site http://www.adsyellowpages.com
This piece of writing will assist the internet people for building up new weblog or even a weblog from start to end.
Also visit my web-site :: http://railwayearth.com
I absolutely love your blog.. Very nice colors & theme.
Did you make this amazing site yourself? Please reply back as I’m wanting to create
my own personal blog and would like to find out where you got this from or just what the theme is named.
Cheers!
My web page … judi sabung ayam online
Good article. I certainly love this website. Keep it up!
Stop by my page :: slot deposit pulsa
I like looking through and I think this website got some really utilitarian stuff on it!
Here is my web site: nesscloud.com
If you desire to grow your know-how only keep visiting this website and be updated
with the newest gossip posted here.
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
updates.
Nice post. I was checking continuously this blog and I’m impressed!
Very helpful information particularly the last part 🙂 I care for such info a lot.
I was looking for this particular information for a long time.
Thank you and good luck.
Feel free to visit my page – 918kiss download pc
I pay a visit each day a few web pages and sites to read articles, but this blog offers feature
based content.
Thank you for the good writeup. It in fact was a amusement account it.
Look advanced to more added agreeable from you!
By the way, how can we communicate?
Here is my web-site; http://bbs.inhe365.com/home.php?mod=space&uid=276761&do=profile&from=space
Highly descriptive blog, I liked that bit. Will there be a part 2?
Also visit my web-site – kebe.top
Heya i’m for the first time here. I came across this board
and I find It truly useful & it helped me out a lot.
I hope to give something back and help others like you helped me.
my page – 롤대리 라이즈팀
Hello Dear, are you in fact visiting this website regularly, if so after that you will without doubt get
fastidious know-how.
What’s up, this weekend is good in support of me, since this
time i am reading this fantastic informative paragraph here at
my home.
Hi there, I enjoy reading all of your post. I like to write a little comment to support you.
Here is my web page – mega888 ios download
Good information. Lucky me I recently found your website by chance
(stumbleupon). I have saved it for later!
WOW just what I was looking for. Came here by searching for heal eczema
Also visit my blog post – o2o.jjfwpt.com
I like this blog it’s a master piece! Glad
I detected this on google.
Also visit my web-site: kebe.top
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.
Thank you for the auspicious writeup. It in truth was once a
leisure account it. Look advanced to far added agreeable from you!
By the way, how could we keep up a correspondence?
What’s up, the whole thing is going well here and ofcourse every one is sharing facts, that’s genuinely fine, keep up writing.
Here is my website: illinoiszone.com
Hello, you used to write wonderful, but the last few posts have been kinda boring?
I miss your tremendous writings. Past few posts are just a little
bit out of track! come on!
My page … railwayearth.com
That is a good tip especially to those new to the blogosphere.
Short but very precise information… Many thanks for sharing this one.
A must read post!
Hello There. I found your blog using msn. This is a very well written article.
I will make sure to bookmark it and return to read more of your useful information. Thanks for the post.
I’ll definitely return.