收藏官网首页
查看: 8207|回复: 1

【转】结合st官方库文件开发STM32的详细教程2

57

主题

156

帖子

716

积分

高级会员

Rank: 4

积分
716
发表于 2016-5-31 09:12:57 | 显示全部楼层 |阅读模式
免费使用STM32、APP自动代码生成工具

5.最后一步:在STM32f10x_conf.h文件的末尾有一个函数:void assert_failed(uint8_t* file, uint32_t line)。这个函数为用于您刚刚打开的函数参数错误检查的,如果检查出了错误,就执行这个函数。但是库文件中不知道你要做什么,所以这个函数的函数体需要你自己在应用代码中定义。所以你只需要在你的main函数前面添加一个函数即可;void assert_failed(uint8_t* file, uint32_t line) (while(1);)一般情况下,大家都是让这个函数内容为空。

到这里就完成了库文件的配置。接下来开始点亮led灯吧。

用库函数配置外设的步骤:

1.打开外设的时钟,使用函数:

RCC_AHBPeriphClockCmd(RCC_AHBPeriph_PPPx, ENABLE);

RCC_APB2PeriphClockCmd(RCC_APB2Periph_PPPx, ENABLE);

RCC_APB1PeriphClockCmd(RCC_APB1Periph_PPPx, ENABLE);

因为你的外设的时钟不同,所以有三个函数,您只需要选择其中的一个就行了,当然是对应的选择,不是随意的。

2.创建一个外设的初始化配置结构体:

GPIO_InitTypeDef GPIO_InitStructure;

对结构体中的内容结合你的需要修改:

GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 + GPIO_Pin_7 + GPIO_Pin_8 + GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;

最后初始化外设:

GPIO_Init(GPIOF,&GPIO_InitStructure);

到这里配置结束。您可以使用库文件中一大坨函数进行芯片的开发了。

接下来给大家我写的按键选择四个led原函数代码:

#include "stm32f10x.h"
#include "stm32f10x_conf.h"


void assert_failed(uint8_t* file, uint32_t line)
{
while(1);
}





int main()
{
    uint8_t a,b,c,d;
    GPIO_InitTypeDef GPIO_InitStructure;
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOF, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOA, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
    RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOD, ENABLE);
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_OD;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6 + GPIO_Pin_7 + GPIO_Pin_8 + GPIO_Pin_9;
    GPIO_InitStructure.GPIO_Speed = GPIO_Speed_10MHz;
    GPIO_Init(GPIOF,&GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Mode = GPIO_Mode_IN_FLOATING;
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_0;
    GPIO_Init(GPIOA,&GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_13;
    GPIO_Init(GPIOC,&GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_8;
    GPIO_Init(GPIOA,&GPIO_InitStructure);
    GPIO_InitStructure.GPIO_Pin = GPIO_Pin_3;
    GPIO_Init(GPIOD,&GPIO_InitStructure);
    while(1)
    {
        a = GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_0);
        b = GPIO_ReadInputDataBit(GPIOC,GPIO_Pin_13);
        c = GPIO_ReadInputDataBit(GPIOA,GPIO_Pin_8);
        d = GPIO_ReadInputDataBit(GPIOD,GPIO_Pin_3);
        if (a == 1)
            GPIO_SetBits(GPIOF,GPIO_Pin_6);
        else
            GPIO_ResetBits(GPIOF,GPIO_Pin_6);
        if (b == 1)
            GPIO_SetBits(GPIOF,GPIO_Pin_7);
        else
            GPIO_ResetBits(GPIOF,GPIO_Pin_7);
        if (c == 1)
            GPIO_SetBits(GPIOF,GPIO_Pin_8);
        else
            GPIO_ResetBits(GPIOF,GPIO_Pin_8);
        if (d == 1)
            GPIO_SetBits(GPIOF,GPIO_Pin_9);
        else
            GPIO_ResetBits(GPIOF,GPIO_Pin_9);
    }
}

转自:http://home.eeworld.com.cn/my/space-uid-585991-blogid-242120.html


3

主题

34

帖子

159

积分

注册会员

Rank: 2

积分
159
发表于 2016-7-2 17:28:17 | 显示全部楼层
这么好的帖子,帮顶一下
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

加入Q群 返回顶部

版权与免责声明 © 2006-2024 Gizwits IoT Technology Co., Ltd. ( 粤ICP备11090211号 )

快速回复 返回顶部 返回列表