stm32学习记录(一)

来源:互联网 发布:php读取本地json文件 编辑:程序博客网 时间:2024/06/05 11:18

前言:最近又重拾stm32,希望借助博客记录自己的所学并有所提高
介绍stm32什么的就不说了,直接从点亮led开始吧


例子一(点亮led)

在进行编程前,需要知道:
1.如果要使用stm32的io口,需要先对io口进行初始化。这个初始化函数包括以下几个部分:

GPIO_InitTypeDef GPIO_InitStructure;//定义一个结构体,必须放在开头                        RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);//对f端口的时钟进行初始化,必须要有这一步,否则不能用io口            GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;//io口的模式为输出模式            GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;//指定管脚            GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;//速度            GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;//推挽输出            GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;//默认为上落,即默认io口开始为高电平            GPIO_Init(GPIOF,&GPIO_InitStructure);//在声明一下,别忘了取地址符

第一步:建立led.h led.c,在led.h里面声明初始化函数:
void LED_Init(void);

在led.c中定义这个初始化函数。

#include "led.h"void LED_Init(){            GPIO_InitTypeDef GPIO_InitStructure;//¶¨Òå½á¹¹Ì壬±ØÐë·ÅÔÚ¿ªÍ·            RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOF,ENABLE);            GPIO_InitStructure.GPIO_Mode=GPIO_Mode_OUT;            GPIO_InitStructure.GPIO_Pin=GPIO_Pin_10;            GPIO_InitStructure.GPIO_Speed=GPIO_Speed_100MHz;//ËÙ¶È            GPIO_InitStructure.GPIO_OType=GPIO_OType_PP;//ÍÆÍÆÍìÊä³ö            GPIO_InitStructure.GPIO_PuPd=GPIO_PuPd_UP;//ÉÏÀ­            GPIO_Init(GPIOF,&GPIO_InitStructure);//ÔÚÉùÃ÷Ò»ÏÂ

第二步
写主函数:

#include "stm32f4xx.h"#include "led.h"void delay(){   int a,b;    for(a=5000;a>=0;a--)    for(b=1000;b>=0;b--);}int main(){    LED_Init();    while(1)    {        GPIO_ResetBits(GPIOF,GPIO_Pin_10);        delay();        GPIO_SetBits(GPIOF,GPIO_Pin_10);        delay();    }}

说明:
1.
GPIO_ResetBits(GPIOF,GPIO_Pin_10)
是用来让某一管脚输出0
2.

    GPIO_SetBits(GPIOF,GPIO_Pin_10);

是用来让某一管脚输出1

3.
这里的delay是用来粗略延迟,但是往后学我们知道stm32里面有更精确的延迟的方法,即systick,以后再说。

4.最后要说,以后再添加头文件时,一定要放进app中。并且还要在设置中添加头文件路径

原创粉丝点击