2.第一个点灯函数

3 阅读2分钟

点灯函数(使用标准库开发)

课程:www.bilibili.com/video/BV1th…

准备工作

  • 下载安装 keil5(mdk534a),以及 Keil.STM32F1xx_DFP.2.2.0.pack
  • 下载 smt32f103c8t6 标准库文件(STM32F10x_StdPeriph_Lib_V3.5.0)
  • 买一套 smt32f103c8t6 开发板
  • 其他资料:STM32中文参考手册.pdf

创建项目

  • 在keil新建project,并选择 stm32f103c8

  • 在新建的项目所属的文件夹里新建 Start, User,Library 文件夹,并在项目里建同名组。

    • 并将内核库复制进入文件夹 Start,并导入到项目同名组
    • 在User新建main.c 文件,并保存到User下
    • 将标准库放入 Library文件夹下,并导入到项目同名组里(使用寄存器开发不需要此步骤,使用标准库开发需要)
    • 设置 Complier Include Path: ./Start; ./User; ./Library
    • 定义 definle: USE_STDPERIPH_DRIVER,用以启用标准库
  • 使用 ST-LINK 连接开发板(并在debugger里设置st-link debugger 和 对应的 flash download里设置 download function -> reset and run),连接开发板时注意引脚要对应

运行流程

在 main.c 里面写代码后,点击build,没错误,就点击 download 下载到硬件,自动运行(上面配置了 reset and run)

代码结构:

+---Library
|       misc.c
|       misc.h
|       stm32f10x_adc.c
|       stm32f10x_adc.h
|       stm32f10x_bkp.c
|       stm32f10x_bkp.h
|       stm32f10x_can.c
|       stm32f10x_can.h
|       stm32f10x_cec.c
|       stm32f10x_cec.h
|       stm32f10x_crc.c
|       stm32f10x_crc.h
|       stm32f10x_dac.c
|       stm32f10x_dac.h
|       stm32f10x_dbgmcu.c
|       stm32f10x_dbgmcu.h
|       stm32f10x_dma.c
|       stm32f10x_dma.h
|       stm32f10x_exti.c
|       stm32f10x_exti.h
|       stm32f10x_flash.c
|       stm32f10x_flash.h
|       stm32f10x_fsmc.c
|       stm32f10x_fsmc.h
|       stm32f10x_gpio.c
|       stm32f10x_gpio.h
|       stm32f10x_i2c.c
|       stm32f10x_i2c.h
|       stm32f10x_iwdg.c
|       stm32f10x_iwdg.h
|       stm32f10x_pwr.c
|       stm32f10x_pwr.h
|       stm32f10x_rcc.c
|       stm32f10x_rcc.h
|       stm32f10x_rtc.c
|       stm32f10x_rtc.h
|       stm32f10x_sdio.c
|       stm32f10x_sdio.h
|       stm32f10x_spi.c
|       stm32f10x_spi.h
|       stm32f10x_tim.c
|       stm32f10x_tim.h
|       stm32f10x_usart.c
|       stm32f10x_usart.h
|       stm32f10x_wwdg.c
|       stm32f10x_wwdg.h
|
+---Start
|       core_cm3.c
|       core_cm3.h
|       startup_stm32f10x_cl.s
|       startup_stm32f10x_hd.s
|       startup_stm32f10x_hd_vl.s
|       startup_stm32f10x_ld.s
|       startup_stm32f10x_ld_vl.s
|       startup_stm32f10x_md.s
|       startup_stm32f10x_md_vl.s
|       startup_stm32f10x_xl.s
|       stm32f10x.h
|       system_stm32f10x.c
|       system_stm32f10x.h
|
\---User
        main.c
        stm32f10x_conf.h
        stm32f10x_it.c
        stm32f10x_it.h

代码:

#include <stm32f10x.h>

int main(void)
{
	/*
	// 直接操作寄存器
	RCC->APB2ENR = 0x00000010;
	GPIOC->CRH = 0x00300000;
	// GPIOC->ODR = 0x00002000;
	GPIOC->ODR = 0x00000000;
	*/
	
	// 使用标准库
	RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOC, ENABLE);
	
	GPIO_InitTypeDef GPIO_InitStruct;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_Out_PP;  // 通用推挽输出
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_13;
	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
	GPIO_Init(GPIOC, &GPIO_InitStruct);
	
	// GPIO_SetBits(GPIOC, GPIO_Pin_13);  // 设置高电平(灭灯)
	GPIO_ResetBits(GPIOC, GPIO_Pin_13);  // 设置低电平(亮灯)
	
	while(1)
	{
		
	}
}