1. 新建工程
2. 给工程添加相关文件,相关文件的网址为STSW-STM32054 - STM32F10x标准外设库 - STMicroelectronics
直接点击“get latest”下载,需要的文件以及如何添加请参考以下博客(8条消息) stm32f103c8t6工程板的建立_你努力的样子,优秀至极-CSDN博客_stm32f103c8t6工程模板https://blog.csdn.net/weixin_42827999/article/details/1016996743. C语言编程实现流水灯
主要代码如下:
#include "stm32f10x.h"
#define RCC_APB2ENR *((unsigned volatile int*)0x40021018)
#define GPIOA_CRL *((unsigned volatile int*)0x40010800)
#define GPIOA_ODR *((unsigned volatile int*)0x4001080C)
#define DELAY Delay(void);
void LED_Init(void)
{
RCC_APB2ENR |= 1<<3;//设置时钟
GPIOA_CRL &=~(0x0F<<(4*1));
GPIOA_CRL &=~(0x0F<<(4*2));
GPIOA_CRL &=~(0x0F<<(4*3));//清零
GPIOA_CRL |=(1<<(4*1));
GPIOA_CRL |=(1<<(4*2));
GPIOA_CRL |=(1<<(4*3));//推挽输出,10M
}
void Delay(void)//延时函数
{
uint16_t i,j;
for(i=0;i<19601;i++)//延时1s
{
for(j=5;j>0;j--);
}
}
int main(void)
{
LED_Init();//初始化准备工作
while (1)
{
GPIOA_ODR &= ~(1<<1);//A1开
DELAY;
DELAY;
DELAY;
GPIOA_ODR |= 1<<1;//A1关
GPIOA_ODR &= ~(1<<2);//A2开
DELAY;
DELAY;
DELAY;
GPIOA_ODR |= 1<<2;//A2关
GPIOA_ODR &= ~(1<<3);//A3开
DELAY;
DELAY;
DELAY;
GPIOA_ODR |= 1<<3;//A3关
}
}
程序的主要思路为:在《STM中文参考手册上》找到寄存器的位置,然后配置时钟使能,设置输入输出模式,设置最大速率,以实现三个LED灯的循环亮灯。
编译生成hex文件
4、文件的烧录
STM串口的下载以及文件烧录请参考以下博客