当前位置:网站首页>0基础自学STM32(野火)——寄存器点亮LED
0基础自学STM32(野火)——寄存器点亮LED
2022-06-29 16:58:00 【Fecter11】
野火寄存器点亮LED
#if 0
# include<stdio.h>
sbit LED = P0^0;
void main (void)
{
//P0 = 0xFE; //总线操作
LED = 0; //位操作
}
#endif
# include "stm32f10x.h"
void SystemInit(void);
int main (void)
{
//时钟控制
*(unsigned int*)0x40021018 |=(1<<(3)); //打开GPIOB端口的时钟
//输出模式
*(unsigned int*)0x40010C00 |=(1<<(4*0)); //CRL端口配置地寄存器,推挽输出
//数据设置
*(unsigned int*)0x40010C0C &=~(unsigned int)(1<<0); //ODR数据输出寄存器。寄存器操作 结合原理图这一步是点亮
//*(unsigned int*)0x40010C0C |=(1<<0); //结合原理图这一步是熄灭
//*(unsigned int*)0x40010C0C &=~(1<<0);//视频中代码没有强制转化为(unsigned int)(1<<0)这样写,我这样写是因为视频原代码编译后产生警告,提示我们当前是将int传值给unsigned int类型的
}
//|=
//&=~
void SystemInit(void)
{
}
// 用来存放STM32寄存器映射的代码
// 外设 perirhral
#define PERIPH_BASE ((unsigned int)0x40000000) //外设基地址
#define APB1PERIPH_BASE PERIPH_BASE //APB1总线基地址
#define APB2PERIPH_BASE (PERIPH_BASE + 0x10000) //APB2总线地址
#define AHBPERIPH_BASE (PERIPH_BASE + 0x20000) //AHB总线地址
#define RCC_BASE (AHBPERIPH_BASE + 0x1000) //时钟总线
#define GPIOB_BASE (APB2PERIPH_BASE + 0x0C00) //GPIOB总线
#define RCC_APB2ENR *(unsigned int*)(RCC_BASE + 0x18) //时钟使能寄存器
#define GPIOB_CRL *(unsigned int*)(GPIOB_BASE + 0x00) //端口配置低寄存器
#define GPIOB_CRH *(unsigned int*)(GPIOB_BASE + 0x04) //端口配置高寄存器
#define GPIOB_ODR *(unsigned int*)(GPIOB_BASE + 0x0C) //端口输出数据寄存器
我们操作的是PB0
注意这里有一个BUG,我们的ODR寄存器在复位后的初值为0x0000 0000
这样一来等于说ODR0也被复位为0。即便是屏蔽掉我们上面写的ODR&=~(1<<0)这行代码,LED还是被点亮。所以尽量要避免这种复位操作带来的影响。
这里还有一个重点。那就是寄存器配置的顺序,先配置时钟,再配置输入/输出,然后再给数据。否则有可能GPIO没有任何输出。
边栏推荐
- Picture and text show you how to thoroughly understand the atomicity of MySQL transaction undolog
- 微博评论高性能高可用架构设计(架构实战营 模块五作业)
- 深圳内推 | 深圳计算科学研究院招聘机器学习助理工程师(校招)
- 机器学习8-人工神经网络
- Is it safe to open a compass account and speculate in stocks? How individuals open accounts for stock trading
- 为防止被00后整顿,一公司招聘要求员工不能起诉公司
- Advanced MySQL - storage engine
- Redolog and binlog
- In depth analysis of Monai (I) data and transforms
- Why is informatization ≠ digitalization? Finally someone made it clear
猜你喜欢
What is the strength of a software testing engineer who can get a salary increase twice a year?
“授权同意”落地压力大?隐私计算提供一种可能的合规“技术解”
In depth analysis of Monai (I) data and transforms
一个简单但是能上分的特征标准化方法
数学知识:求组合数 II—求组合数
windows平台下的mysql启动等基本操作
【 OpenGL 】 Random Talk 1. The camera rotates around a point in the space by dragging the mouse
基于汇编实现的流载体的LSB隐藏项目
iNFTnews | Meta在元宇宙中的后续计划会是什么?
ICML 2022 | 基于解耦梯度优化的可迁移模仿学习方法
随机推荐
Which parameter is the partition information adjusted? The MySQL source stream API is used, not the table API
PHP删除目录
自己实现一个ThreadLocal
Redolog and binlog
垃圾收集器
美国芯片再遭重击,Intel或将被台积电击败而沦落至全球第三
又拍云 Redis 的改进之路
Why does selenium become the first choice for web automated testing? (source code attached)
Actual combat | magical conic gradient
XAMPP Apache安装时问题总结
微博评论高性能高可用架构设计(架构实战营 模块五作业)
使用kalibr标定工具进行单目相机和双目相机的标定
Us chips are hit hard again, and Intel may be defeated by TSMC and reduced to the third place in the world
Advanced MySQL - storage engine
为防止被00后整顿,一公司招聘要求员工不能起诉公司
Solid state storage manufacturer Yilian joins dragon dragon community to build a new open source ecosystem
『计组』CPU 如何区分指令和数据
深圳内推 | 深圳计算科学研究院招聘机器学习助理工程师(校招)
卷妹带你学数据库---5天冲刺Day4
InheritableThreadLocal 在线程池中进行父子线程间消息传递出现消息丢失的解析