当前位置:网站首页>STM32学习记录:LED灯闪烁(寄存器版)
STM32学习记录:LED灯闪烁(寄存器版)
2022-07-06 09:25:00 【Bitter tea seeds】
系列文章目录
STM32F103ZE:正点原子精英板,使用寄存器点亮LED
文章目录
前言
主要就是会使用STM3210X的开发手册,会查看板子的原理图,理解总线的概念,会找寄存器。
只是单纯的会调用库是不行的,因为那都是别人封装好的,想成为一名优秀的工程师,一定要会面对底层进行编程。
一、参考开发手册
1.打开正点原子精英板原理图,寻找LED的引脚
可以看到,LED0和LED1分别在GPIOB5和GPIOE5引脚上。
2.打开STM32中文参考手册,进行资料查询
先看看STM32的系统结构
可以发现RCC为控制时钟,且GPIOB和GPIOE都由APB2控制
3.接着看一下RCC寄存器外设APB2时钟使能寄存器
看一下APB2的地址偏置
查看一下GPIOB和GPIOE的位
可以看到,GPIOB是第3位,GPIOE是第6位;
4.查看寄存器映像,寻找地址
5.查看端口输出数据寄存器
6.查看端口配置低寄存器
好啦!查看资料就到这里了,没啥难的,就是会找寄存器就行了。
二、开发步骤
1.点亮LED
代码如下(所示):
RCC时钟地址
#include "stm32f10x.h"
int main(void)
{
*(unsigned int *)0x40021018 |=(1<<3);//RCC地址是0x40021000,APB2偏置是18,所以,打开GPIOB的使能时钟。向←3位.
*(unsigned int *)0X40010C00 |=((1)<<(4*5));//‘|=’ 置1;配置低电平,*5因为在5端口上
*(unsigned int *)0X40010C0C &=~(1<<5);//‘&=~’ 清零;配置端口数据输出
*(unsigned int *)0x40021018 |=(1<<6);//打开GPIOE的使能时钟。向←6位.
*(unsigned int *)0x40011800 |=((1)<<(4*5));
*(unsigned int *)0x4001180C &=~(1<<5);
}
已验证,成功将正点原子精英板LED0、LED1灯点亮
2.让LED灯闪烁
LED.h
代码如下(所示):
#ifndef __led_H
#define __led_H
void LED_Init(void);
#endif
LED.c
代码如下(所示):
#include "stm32f10x.h"
#include "led.h"
void LED_Init(void)
{
RCC->APB2ENR|=1<<3;//->结构体指针;使能B
RCC->APB2ENR|=1<<6;//->结构体指针;使能E
/*GPIOB5*/
GPIOB->CRL&=0xFF0FFFFF;
GPIOB->CRL|=0x00300000;
GPIOB->ODR|=1<<5;
/*GPIOE5*/
GPIOE->CRL&=0xFF0FFFFF;
GPIOE->CRL|=0x00300000;
GPIOE->ODR|=1<<5;
}
main.c
代码如下(所示):
#include "stm32f10x.h"
#include "led.h"
#include "delay.h"
int main(void)
{
delay_init(72);
LED_Init();
while(1)
{
GPIOB->ODR|=1<<5;
delay_ms(500);
GPIOB->ODR=~(1<<5);
delay_ms(500);
GPIOE->ODR|=1<<5;
delay_ms(500);
// GPIOB->ODR=~(1<<5);
GPIOE->ODR=~(1<<5);
delay_ms(500);
}
}
边栏推荐
- UCORE lab5 user process management experiment report
- Nest and merge new videos, and preset new video titles
- How to build a nail robot that can automatically reply
- MATLAB实例:阶跃函数的两种表达方式
- Interview answering skills for software testing
- 软件测试Bug报告怎么写?
- 如何成为一个好的软件测试员?绝大多数人都不知道的秘密
- ArrayList set
- CSAPP Shell Lab 实验报告
- FSM and I2C experiment report
猜你喜欢
Maximum nesting depth of parentheses in leetcode simple questions
STM32学习记录:输入捕获应用
C4D quick start tutorial - Introduction to software interface
软件测试方法有哪些?带你看点不一样的东西
The most detailed postman interface test tutorial in the whole network. An article meets your needs
What to do when programmers don't modify bugs? I teach you
Sleep quality today 81 points
安全测试入门介绍
Crawler series (9): item+pipeline data storage
Servlet
随机推荐
How to build a nail robot that can automatically reply
LeetCode#118. Yanghui triangle
In Oracle, start with connect by prior recursive query is used to query multi-level subordinate employees.
ucorelab3
Word macro operation: convert the automatic number in the document into editable text type
[C language] twenty two steps to understand the function stack frame (pressing the stack, passing parameters, returning, bouncing the stack)
自动化测试你必须要弄懂的问题,精品总结
Capitalize the title of leetcode simple question
ucorelab4
学习记录:理解 SysTick系统定时器,编写延时函数
C4D quick start tutorial - Introduction to software interface
MATLAB实例:阶跃函数的两种表达方式
学习记录:串口通信和遇到的错误解决方法
Emqtt distribution cluster and node bridge construction
Programmers, how to avoid invalid meetings?
学习记录:USART—串口通讯
线程及线程池
Video scrolling subtitle addition, easy to make with this technique
Sorting odd and even subscripts respectively for leetcode simple problem
Leetcode simple question: check whether two strings are almost equal