Notes03:使用寄存器点亮LED

S32K312 MCAL SPI配置实战:从硬件抽象到稳定通信 SPI(串行外设接口)是一种广泛应用的同步串行通信协议,以其全双工、高速和简单的硬件连接特性,成为微控制器与传感器、存储器等外设通信的核心技术。其工作原理基于主从架构,通过时钟、数据线和片选信号实现同步数据传输,时钟极性与相位的正确配置是确保数据采样准确的关键。在汽车电子等对可靠性和可移植性要求极高的领域,AUTOSAR架构下的MCAL(微控制器抽象层)技术价值凸显,它通过硬件抽象和标准化API,将SPI等外设的底层操作封装,实现了应用代码与具体硬件的解耦,极大提升了软件的可维护性和跨平台移植能力。本文聚焦 阅读详情

使用寄存器点亮LED

目标

使用寄存器点亮LED_G

原理图

LED原理图

由原理图可知, LED_G阳极接3V3, 阴极接PB0, 故只需将PB0输出低电平即可点亮LED_G

打开时钟

使用RCC_APB2ENR寄存器

偏移地址
基地址

RCC的起始地址为0x4002 1000, RCC_APB2ENR的偏移地址为0x18, 故RCC_APB2ENR的绝对地址为0x4002 1000 + 0x18 = 0x4002 1018

由于我们需使用PB0所以需要打开IO端口B时钟, 由于GPIOB在APB2上所以需要使用RCC_APB2ENR

位说明

由图可知, 需将RCC_APB2ENR的位3置1

代码如下:

*(unsigned int *)0x40021018 |= (1 << 3); // 打开时钟, 控制RCC_APB2ENR寄存器, 实现IO端口B时钟开启

/*
 * Notes:
 * |= (1 << 3); // 1左移3位后相或, 即可把Bit3置位
 */

配置GPIO的模式

使用GPIOx_CRL寄存器, 如下图

位说明

由于使用PB0, 所以应配置GPIOB, GPIOB的起始地址为0x4001 0C00, 如下图

基地址

CRL的偏移地址为0x00, 故GPIOB_CRL的绝对地址为0x4001 0C00 + 0x00 = 0x4001 0C00

由于使用PB0, 所以需要配置端口0, 故使用CRL寄存器而非CRH寄存器, 并配置CNF0和MODE0

将PB0口配置成输出模式, 最大速度10MHz, 通用推挽输出模式, (关于GPIO原理的讲解在下节)

故MODE0 = 01, CNF0 = 00

代码如下:

*(unsigned int *)0x40010C00 |= ((1) << (4*0)); // 配置IO口为输出, 控制CRL寄存器, 实现GPIOB端口0为10MHz的推挽输出模式
/*
 * Notes:
 * 分成n个一组, 对清零后的第i组设置成某二进制数, 该数对应的十进制为b
 * a |= (b << n * i);
 * 此时 |= ((1) << (4 * 0)); // 分成4个一组, 将第0组(即bit3~0)设置为1对应的二进制(即0001B)
 */

PB0输出低电平

使用ODR寄存器, 如下图

位说明

由于需要使PB0输出故基地址应为GPIOB的起始地址**(即0x4001 0C00)**, 如图

基地址

故GPIOB_ODR的绝对地址为0x4001 0C00 + 0x0C = 0x4001 0C0C

因要使端口0输出低电平, 故应向ODR0 写入 0

代码如下:

*(unsigned int *)0x40010C0C &= ~(1 << 0); // 控制ODR寄存器, 实现GPIOB端口0输出低电平

/*
 * Notes:
 * 将变量a的Bit n位清零, a &= ~(1 << n)
 */

源码

main.c文件
#include "stm32f10x.h"

int main(void)
{
   
   
    *(unsigned int *)0x40021018 |= (1 << 3); // 打开时钟, 控制RCC_APB2ENR寄存器, 实现IO端口B时钟开启
    /*
     * RCC_APB2ENR寄存器(Address : 0x40021000(RCC) + 0x18)
     * Bit3 : IOPBN : IO端口B时钟使能(I/O port B clock enable)
     * 由软件置1或请0
     * 0 : IO端口B时钟关闭
     * 1 : IO端口B时钟开启
     */
    *(unsigned int *)0x40010C00 |= ((1) << (4*0)); // 配置IO口为输出, 控制CRL寄存器, 实现GPIOB端口0为10MHz的推挽输出模式
    /*
     * GPIOx_CRL 端口配置低寄存器(Address : 0x40010C00(GPIO端口B)  + 0x00)
     * Bit32 CNF0 : 00 通用推挽输出模式
     * Bit10 MODE0 : 01 输出模式, 最大速度10MHz
	 * 
	 * Notes:
	 * 分成n个一组, 对清零后的第i组设置成某二进制数, 该数对应的十进制为b
	 * a |= (b << n * i);
	 * 此时 |= ((1) << (4 * 0)); // 分成4个一组, 将第0组(即bit3~0)设置为1对应的二进制(即0001B)
     */
    *(unsigned int *)0x40010C0C &= ~(1 << 0); // 控制ODR寄存器, 实现GPIOB端口0输出低电平
    /*
     * GPIOx_ODR 端口输出数据寄存器(Address : 0x40010C00(GPIO端口B)+ 0x0C)
     * Bit0 ODR0 : 端口输出数据
	 * 
	 * Notes:
	 * 将变量a的Bit n清零, a &= ~(1 << n);
     * 
     * 注 : 
     * 根据原理图LED_G的阳极为3V3, 阴极接PB0
     */

    return 0;
}

/*
 * Notes:
 *
 * 置位 : |= 
 * |= (1 << 3);  // 1左移3位后相或, 把Bit3置位
 * 
 * 复位 : &= ~ 
 * &= ~(1 << 0); 1左移0位取反后相与, 把Bit0复位
 */

void SystemInit(void)
{
   
   
    return; // 编译需要
}

stm32f10x.h文件

// 用来存放STM32寄存器映射的代码
// 暂时不写

启动文件startup_stm32f10x_hd.s

;*                        SRAM mounted on STM3210E-EVAL board to be used as data 
;*                        memory (optional, to be enabled by user)
;*                      - Branches to __main in the C library (which eventually
;*                        calls main()).
;*                      After Reset the CortexM3 processor is in Thread mode
STM32——跑马灯(寄存器) 学习STM32的一些笔记 阅读详情

相关推荐

STM32_基础入门(一)_寄存器跑马灯实验

STM32_基础入门(一)_寄存器跑马灯实验

weixin_50546241的博客 1693

STM32F103+ESP32-S3+LVGL嵌入式人机交互系统设计

嵌入式人机交互系统是物联网终端落地的核心能力,其本质是在资源受限MCU上实现稳定、低延迟、高可靠性的图形渲染与设备协同。LVGL作为轻量级GUI框架,需深度适配硬件资源约束与实时性要求;而多MCU架构(如STM32+ESP32)则通过职责分离解决性能瓶颈——本地控制与UI交由Cortex-M3主控闭环处理,联网通信由专用Wi-Fi SoC承担,避免资源争抢。这种‘物理隔离+契约通信’模式显著提升工业级鲁棒性,广泛应用于智能家居面板、工控HMI、边缘网关等场景。本文聚焦STM32F103与LVGL深度优化实践

452

使用寄存器控制LED灯点亮的嵌入式实现

首先,我们需要准备一块开发板或者嵌入式系统,其中包含一个可用的GPIO(通用输入输出)口用于连接LED灯。通过以上的代码和说明,我们可以实现使用寄存器来控制LED灯点亮的嵌入式功能。这种方法可以应用于不同的嵌入式系统中,具有一定的灵活性和可移植性。在主程序的前面,需要包含相关的头文件和定义所需的宏。在嵌入式系统中,使用寄存器来控制外设是一种常见的方法。根据具体的硬件平台和开发环境,GPIO配置的方法可能略有不同。是一个8位寄存器,用来控制P1口的数据输入和输出。是一个宏定义,用来指定特定的引脚和端口。

CyberBytezZ的博客 368

SLAM轨迹评估神器evo实战:从GPS到TUM格式的完整转换指南(附Python代码)

本文详细介绍了如何使用evo工具将GPS数据转换为TUM格式,以便进行SLAM轨迹评估。通过Python代码实现GPS到笛卡尔坐标的精确转换,并提供了完整的处理流程和实战案例,帮助开发者轻松完成从原始GPS数据到精确轨迹评估的全过程。特别适用于ORB-SLAM等视觉SLAM算法的性能验证。

weixin_29074295的博客 294

【FPGA】Verilog代码实现温湿度传感器DHT11

#今天也是咸鱼的一天~ 因为参加Robei 杯 是要做一个机器人,先不说这个机器人具体怎么机器法,但是和外界互通的传感器肯定少不了,通过获取外界环境数据,进行处理,然后做出各种各样的功能。 先来讲个简单的模块,温湿度传感器DHT11。 DHT11介绍 DHT11是一款价格便宜,易于使用的温度湿度测量二合一传感器。它具有超小体积、极低功耗的特点。它使用单根总线与单片机进行双向的串行数据传输,信号传...

Ninquelote的博客 9163

5G核心技术:N3与N4接口深度解析

5G网络中,N3和N4接口是实现用户数据传输的核心。N3接口是gNB与UPF之间的用户面通道,采用GTP-U协议封装数据,通过UDP/IP传输,确保低时延和业务隔离。N4接口是SMF与UPF之间的控制面链路,基于PFCP协议下发转发规则(FAR/TDR/QER/URR),实现精细化流量控制。二者协同工作:N4下发策略(如QoS、计费规则),N3执行数据转发,共同支持5G三大场景(eMBB/uRLLC/mMTC),体现控制与转发分离(CUPS)架构优势。

Qinti_mm的博客 1637

Yolov26 在 Rk3588 上的 Rknn Onnx 部署实践(c++)

本文介绍了YOLOv26在RK3588平台的C++推理部署方案。重点包括:1)扩展Ultralytics导出格式,新增RKNN_ONNX专用导出选项;2)重构Detect Head结构,使其更适合RKNN推理;3)提供C++侧后处理实现建议。该方案保留了Ultralytics训练生态,通过优化模型结构输出,将解码等操作移至C++端,充分发挥Rockchip NPU性能。适用于RK3588/RK356x等平台,需配合rknn-toolkit2使用,为YOLOv26的嵌入式部署提供完整解决方案。

qq_64366203的博客 2178

三角洲行动交易行自动化脚本

DFMarketBot - 三角洲行动自动交易助手 DFMarketBot是一款专为三角洲行动游戏设计的交易行自动化工具,通过OCR识别和鼠标模拟实现自动补卡、补子弹功能。项目采用Python开发,支持Windows平台,主要功能包括: 自动检测并购买指定物品(房卡/子弹) 价格监控与智能购买(低于设定价格自动下单) 自定义购买数量与次数限制 支持窗口/无窗口两种游戏模式 使用前需安装Tesseract OCR引擎并配置游戏为1920*1080分辨率。通过JSON文件可灵活配置监控物品清单、理想价格和购买策

愤怒的阿木木的博客 3万+

Java调用OpenDDS(2)-理解OpenDDS自带的Messager示例

理解Messenger

zhangzl419 1660

Unity 面试篇|(六)数据结构和算法篇 【全面总结 | 持续更新】

先拿10000个数建堆,然后一次添加剩余元素,如果大于堆顶的数(10000中最小的),将这个数替换堆顶,并调整结构使之仍然是一个最小堆,这样遍历完后,堆中的10000个数就是所需的最大的10000个。桶排序是计数排序的升级版。它的工作原理:首先在未排序序列中找到最小(大)元素,存放到排序序列的起始位置,然后,再从剩余未排序元素中继续寻找最小(大)元素,然后放到已排序序列的末尾。这样处理就可以分别在每个文件的10^6个数据中找出最大的10000个数,合并到一起在再找出1000*10000中的最终的结果。

游戏开发小Y的博客 12万+

Python航空航天设计神器!从入门到实操,小白也能搞定飞机优化

AeroSandbox的核心价值的是:​降低航空工程优化的门槛,用Python实现高效、简洁的工程设计​。无论是科研、学习还是实际项目,它都能帮你节省大量时间,不用再手动推导导数、编写复杂的优化逻辑。对于新手来说,建议先从案例1(Rosenbrock函数)入手,熟悉优化流程,再尝试案例2(翼型优化),逐步掌握核心用法;对于有航空相关需求的小伙伴,可以深入研究内置的物理模型,结合自己的项目进行定制化开发。最后,互动时间到!👉 你是用AeroSandbox做科研、练手还是实际项目?

源代码杀手的博客 125

数学建模模型总结

这是本人在两年内准备参加数学建模竞赛搜集到的模型资料,已经经过了本人的筛选和整理,希望能够对准备参加数学建模竞赛的学弟学妹们提供一些帮助!

老版QQ原生表情gif动态

这是老版的QQ表情,不过全是动态的的,Windows照片查看器是不会动的,可以右击用浏览器打开查看,再说一遍是老版的,只是分享给宁愿用老版也要动态的人。

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

上一篇: Notes02:常用位操作方法
下一篇: Notes04:GPIO功能框图详解
普通网友
博客等级 码龄6年 121粉丝 72原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值