STM32F103RCT6开发板M3单片机教程01--点亮LED
学习单片机开发,最主要的就是动手写代码,操作调试,改代码。
话不多话,开我们的单片机奇妙之旅吧!!!
本教程使用是(光明谷SUN_STM32mini开发板)

首先了解LED硬件连接,查看原理图

得知PA8连接LED阴极,低电平点亮。
接下来开发写代码点亮他吧
- 新建工程
打开keil软件,找到project,下拉菜单有一项new uvision project。点击这个项目,会出现一个项目路径。


自定义项目名称和保存位置。
选择需要烧代码的单片机芯片的生产公司(ST)相应的芯片型号STM32F103RCT6


点“OK”,新建好了工程。 - 添加STM32F10x库
- 新建源文件main.c 并添加到工程中
- /**********************************************************************************
- * Sun STM32 mini Demo
-
- * Description
-
- LED Flash
-
- * Version Date Auther Reversed History
- ----------------------------------------------------------------------------
- V1.0.0 2021-10-20 Lojam Fan Fisrt Created
-
- * (C) Sunshine Silicon Corporation
- * Website: http://www.sunsili.com http://sunsili.taobao.com http://bbs.sunsili.com
- * E-Mail : fan@sunsili.com
- **********************************************************************************/
- #include "stm32f10x.h"
- #include "Led.h"
- void Delay(u32 dly);
- /*******************************************************************************
- * 函数名 : main
- * 描述 : 主函数,用户程序从main函数开始运行
- * 输入 : 无
- * 输出 : 无
- * 返回值 : int:返回值为一个16位整形数
- * 说明 : 无
- *******************************************************************************/
- int main(void)
- {
- LED_GPIO_Config(); //LED IO配置
-
- while (1)
- {
- LED_ON(); //点亮LED.PA8
- Delay(1000000); //延时
- LED_OFF(); //关闭LED.PA8
- Delay(1000000); //延时
- }
- }
- /*******************************************************************************
- * 函数名 : Delay
- * 描述 : 软件延时函数
- * 输入 : dly:32位的整形数
- * 输出 : 无
- * 返回值 : 无
- * 说明 : 无
- *******************************************************************************/
- void Delay(u32 dly)
- {
- for(; dly > 0; dly--);
- }
复制代码
- 新建源文件led.c,建代码Grounp-〉BSP,添加led.c到BSP(Grounp)
- /**********************************************************************************
- * 文件名 :led.c
- * 描述 :led 应用函数库
- * 实验平台:SunMINI STM32F103RCT6开发板
- * 硬件连接: PB5 -> LED1
- * PB6 -> LED2
- * PB7 -> LED3
- * PB8 -> LED3
- * 库版本 :ST_v3.5
- **********************************************************************************/
- #include "Led.h"
-
- /*******************************************************************************
- * 函数名 : LED_GPIO_Config
- * 描述 : LED IO配置
- * 输入 : 无
- * 输出 : 无
- * 返回 : 无
- * 说明 : LED(1~4)的IO口分别是:PB5,PB6,PB7,PB8
- *******************************************************************************/
- void LED_GPIO_Config(void)
- {
- GPIO_InitTypeDef GPIO_InitStructure; //定义一个GPIO_InitTypeDef类型的GPIO初始化结构体
-
- RCC_APB2PeriphClockCmd(LED_RCC, ENABLE); //使能GPIOB的外设时钟
-
- GPIO_InitStructure.GPIO_Pin = LED; //选择要初始化的GPIOB引脚(PB5,PB6,PB7,PB8)
- GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //设置引脚工作模式为通用推挽输出
- GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //设置引脚输出最大速率为50MHz
- GPIO_Init(LED_PORT, &GPIO_InitStructure); //调用库函数中的GPIO初始化函数,初始化GPIOB中的PB5,PB6,PB7,PB8引脚
- LED_OFF(); //关闭LED
- }
- /*点亮LED.PA8*/
- void LED_ON(void)
- {
- // GPIO_SetBits(LED_PORT,LED);
- GPIO_SetBits(GPIO_LED);
- }
- /*关闭LED.PA8*/
- void LED_OFF(void)
- {
- // GPIO_ResetBits(LED_PORT,LED);
- GPIO_ResetBits(GPIO_LED);
- }
复制代码
- 新源头文件led.h
- #ifndef __LED_H__
- #define __LED_H__
-
- #include "stm32f10x.h"
- //LED端口定义
- #define LED_RCC RCC_APB2Periph_GPIOA
- #define LED_PORT GPIOA
- #define LED GPIO_Pin_8
- #define GPIO_LED GPIOA, GPIO_Pin_8
- void LED_GPIO_Config(void);
- void LED_ON(void);
- void LED_OFF(void);
- #endif
复制代码
- 编译工程
编辑好源代码后,保存全部,点"build", 稍等出现如下提示,编译成功,不成话,Keil会给提示,哪里不对,按提示修改代码,保存再编译,直到编译成功。
 - 配置调试Debug(参考阅: keil5使用JLink仿真调试STM32F103C8T6)
我使用用是(ARM仿真器下载器JLINK V9)
[url=] [/url]
ARM仿真器下载器JLINK V9 STM调试器 编程器 不掉固件工程设置(Option)

添加烧录算法

8. 在开发板上调试
启动调试((点Debug或按ctrl+F5)

在开发板上运行(点Run或按F5)
课后作业:
- 改来闪灯频率,改到快到一定时,变成常高
- 增加闪灯数亮
- 变换闪灯效果,如流水、交换、慢增长、减少等
工程源码:
[attach]275[/attach]
|