当前位置:网站首页>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);
}
}
边栏推荐
- Nest and merge new videos, and preset new video titles
- How to become a good software tester? A secret that most people don't know
- How to build a nail robot that can automatically reply
- How to write the bug report of software test?
- FSM and I2C experiment report
- Thinking about three cups of tea
- Leetcode simple question: check whether two strings are almost equal
- MySQL数据库(二)DML数据操作语句和基本的DQL语句
- 学习记录:USART—串口通讯
- CSAPP shell lab experiment report
猜你喜欢
随机推荐
[C language] twenty two steps to understand the function stack frame (pressing the stack, passing parameters, returning, bouncing the stack)
What to do when programmers don't modify bugs? I teach you
Currently, mysql5.6 is used. Which version would you like to upgrade to?
学习记录:STM32F103 时钟系统概述工作原理
线程及线程池
Scoring system based on 485 bus
Stm32 dossiers d'apprentissage: saisie des applications
Heap, stack, queue
What is "test paper test" in software testing requirements analysis
Thinking about three cups of tea
Threads and thread pools
自动化测试你必须要弄懂的问题,精品总结
Which version of MySQL does php7 work best with?
51 lines of code, self-made TX to MySQL software!
Mysql database (III) advanced data query statement
The maximum number of words in the sentence of leetcode simple question
12306: mom, don't worry about me getting the ticket any more (1)
Learning record: Tim - capacitive key detection
FSM and I2C experiment report
[200 opencv routines] 98 Statistical sorting filter







