当前位置:网站首页>[Punctuality Atom] Simple application of sys.c, sys.h bit-band operations
[Punctuality Atom] Simple application of sys.c, sys.h bit-band operations
2022-07-30 07:13:00 【Clockwisee】
前言
Because I follow the Jiangke University Self-Chemistry Association to studystm32c8t6最小系统板,when browsing other people's code,It is found that the big guys like to use the bit band operation,After learning through the official video of Punctual Atom,It is found that this is no different from the use of library functions.
个人理解
Bit-band operations are mostly rightGPIO口的操作,It is to encapsulate some register addresses、映射,to form with51MCU similar register operation
代码呈现
This code is intercepted from the battleship of the punctual atomv3(Also the elite version)第十五讲-Write the marquee experiment by hand-位操作
sys.c This can be ignored first
#include "sys.h"
//
//本程序只供学习使用,未经作者许可,不得用于其它任何用途
//ALIENTEK Mini STM32开发板
//系统中断分组设置化
//正点原子@ALIENTEK
//技术论坛:www.openedv.com
//修改日期:2012/9/10
//版本:V1.4
//版权所有,盗版必究.
//Copyright(C) 正点原子 2009-2019
//All rights reserved
//********************************************************************************
//THUMB指令不支持汇编内联
//采用如下方法实现执行汇编指令WFI
void WFI_SET(void)
{
__ASM volatile("wfi");
}
//关闭所有中断
void INTX_DISABLE(void)
{
__ASM volatile("cpsid i");
}
//开启所有中断
void INTX_ENABLE(void)
{
__ASM volatile("cpsie i");
}
//设置栈顶地址
//addr:栈顶地址
__asm void MSR_MSP(u32 addr)
{
MSR MSP, r0 //set Main Stack value
BX r14
}
sys.h 这个就重要了
#ifndef __SYS_H
#define __SYS_H
#include "stm32f10x.h"
//
//本程序只供学习使用,未经作者许可,不得用于其它任何用途
//ALIENTEK STM32开发板
//正点原子@ALIENTEK
//技术论坛:www.openedv.com
//修改日期:2012/8/18
//版本:V1.7
//版权所有,盗版必究.
//Copyright(C) 广州市星翼电子科技有限公司 2009-2019
//All rights reserved
//
//0,不支持ucos
//1,支持ucos
#define SYSTEM_SUPPORT_OS 0 //定义系统文件夹是否支持UCOS
//位带操作,实现51类似的GPIO控制功能
//具体实现思想,参考<<CM3权威指南>>第五章(87页~92页).
//IO口操作宏定义
#define BITBAND(addr, bitnum) ((addr & 0xF0000000)+0x2000000+((addr &0xFFFFF)<<5)+(bitnum<<2)) //addr 地址 bit 比特
#define MEM_ADDR(addr) *((volatile unsigned long *)(addr))
#define BIT_ADDR(addr, bitnum) MEM_ADDR(BITBAND(addr, bitnum))
//IO口地址映射
#define GPIOA_ODR_Addr (GPIOA_BASE+12) //0x4001080C
#define GPIOB_ODR_Addr (GPIOB_BASE+12) //0x40010C0C
#define GPIOC_ODR_Addr (GPIOC_BASE+12) //0x4001100C
#define GPIOD_ODR_Addr (GPIOD_BASE+12) //0x4001140C
#define GPIOE_ODR_Addr (GPIOE_BASE+12) //0x4001180C
#define GPIOF_ODR_Addr (GPIOF_BASE+12) //0x40011A0C
#define GPIOG_ODR_Addr (GPIOG_BASE+12) //0x40011E0C
#define GPIOA_IDR_Addr (GPIOA_BASE+8) //0x40010808
#define GPIOB_IDR_Addr (GPIOB_BASE+8) //0x40010C08
#define GPIOC_IDR_Addr (GPIOC_BASE+8) //0x40011008
#define GPIOD_IDR_Addr (GPIOD_BASE+8) //0x40011408
#define GPIOE_IDR_Addr (GPIOE_BASE+8) //0x40011808
#define GPIOF_IDR_Addr (GPIOF_BASE+8) //0x40011A08
#define GPIOG_IDR_Addr (GPIOG_BASE+8) //0x40011E08
//IO口操作,只对单一的IO口!
//确保n的值小于16!
#define PAout(n) BIT_ADDR(GPIOA_ODR_Addr,n) //输出
#define PAin(n) BIT_ADDR(GPIOA_IDR_Addr,n) //输入
#define PBout(n) BIT_ADDR(GPIOB_ODR_Addr,n) //输出
#define PBin(n) BIT_ADDR(GPIOB_IDR_Addr,n) //输入
#define PCout(n) BIT_ADDR(GPIOC_ODR_Addr,n) //输出
#define PCin(n) BIT_ADDR(GPIOC_IDR_Addr,n) //输入
#define PDout(n) BIT_ADDR(GPIOD_ODR_Addr,n) //输出
#define PDin(n) BIT_ADDR(GPIOD_IDR_Addr,n) //输入
#define PEout(n) BIT_ADDR(GPIOE_ODR_Addr,n) //输出
#define PEin(n) BIT_ADDR(GPIOE_IDR_Addr,n) //输入
#define PFout(n) BIT_ADDR(GPIOF_ODR_Addr,n) //输出
#define PFin(n) BIT_ADDR(GPIOF_IDR_Addr,n) //输入
#define PGout(n) BIT_ADDR(GPIOG_ODR_Addr,n) //输出
#define PGin(n) BIT_ADDR(GPIOG_IDR_Addr,n) //输入
//以下为汇编函数
void WFI_SET(void); //执行WFI指令
void INTX_DISABLE(void);//关闭所有中断
void INTX_ENABLE(void); //开启所有中断
void MSR_MSP(u32 addr); //设置堆栈地址
#endif
Even bit manipulation,也少不了IOPort call clock and initialization,说白了,It's about library functionsGPIO_SetBits、GPIO_ResetBits分别用0、1替换
Submit the code below:
main.c
#include "stm32f10x.h"
#include "led.h"
#include "delay.h" //没有加#include"sys.h"是因为delay.hThe header files are already included
/************************************************ ALIENTEK 战舰STM32F103开发板实验0 工程模板 注意,这是手册中的新建工程章节使用的main文件 技术支持:www.openedv.com 淘宝店铺:http://eboard.taobao.com 关注微信公众平台微信号:"正点原子",免费获取STM32资料. 广州市星翼电子科技有限公司 作者:正点原子 @ALIENTEK ************************************************/
int main(void)
{
delay_init();
LED_Init();
while(1){
PBout(5)=1;
PEout(5)=1;
delay_ms(500);
PBout(5)=0;
PEout(5)=0;
delay_ms(500);
}
}
这是在led.c里面的初始化
#include "led.h"
//
//本程序只供学习使用,未经作者许可,不得用于其它任何用途
//ALIENTEK战舰STM32开发板
//LED驱动代码
//正点原子@ALIENTEK
//技术论坛:www.openedv.com
//修改日期:2012/9/2
//版本:V1.0
//版权所有,盗版必究.
//Copyright(C) 广州市星翼电子科技有限公司 2009-2019
//All rights reserved
//
//初始化PB5和PE5为输出口.并使能这两个口的时钟
//LED IO初始化
void LED_Init(void)
{
GPIO_InitTypeDef GPIO_InitStructure;
RCC_APB2PeriphClockCmd(RCC_APB2Periph_GPIOB|RCC_APB2Periph_GPIOE, ENABLE); //使能PB,PE端口时钟
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //LED0-->PB.5 端口配置
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_Out_PP; //推挽输出
GPIO_InitStructure.GPIO_Speed = GPIO_Speed_50MHz; //IO口速度为50MHz
GPIO_Init(GPIOB, &GPIO_InitStructure); //根据设定参数初始化GPIOB.5
GPIO_SetBits(GPIOB,GPIO_Pin_5); //PB.5 输出高
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_5; //LED1-->PE.5 端口配置, 推挽输出
GPIO_Init(GPIOE, &GPIO_InitStructure); //推挽输出 ,IO口速度为50MHz
GPIO_SetBits(GPIOE,GPIO_Pin_5); //PE.5 输出高
}
边栏推荐
- Generalized Focal Loss 论文阅读笔记
- 思谋面试准备
- 目标检测中的知识蒸馏方法
- 点云统计滤波理解
- vs编译boost库脚本
- 【青岛站】SWAT模型高阶应用暨无资料地区建模、不确定分析与气候变化、土地利用对面源污染影响模型改进及案例分析研讨
- QT每周技巧(3)~~~~~~~~~串口添加
- FPGA解析B码----连载1
- Flood Control Assessment Report Compilation Method and Flood Modelling under the New Guidelines (HEC-RAS)
- "R Language + Remote Sensing" Comprehensive Evaluation Method of Water Environment
猜你喜欢
海量遥感数据处理与GEE云计算技术应用【基础、进阶】
边境的悍匪—机器学习实战:第五章 支持向量机
十一、Kotlin进阶学习:1、集合;2、List操作;3、可变集合——MutableList;4、Set;5、Map;6、MutableMap;
Nodejs PM2 monitoring and alarm email (2)
边境的悍匪—Kaggle—泰坦尼克号生还预测详细教程
十五、Kotlin进阶学习:一、子类与子类型;二、协变;三、逆变;
十、Kotlin基础学习:1、延迟加载;2、异常处理;3、使用 throw 主动抛出异常;4、自定义异常;
无人机生态环境监测、图像处理与GIS数据分析
边境的悍匪—机器学习实战:第十二章 使用TensorFlow自定义模型和训练
R语言 生态环境领域应用
随机推荐
Generalized Focal Loss paper reading notes
边境的悍匪—机器学习实战:第七章 集成学习和随机森林
【总结】工业检测项目中如何选择合适的损失函数
边境的悍匪—机器学习实战:第五章 支持向量机
Based on R language geographic weighted regression, principal component analysis, discriminant analysis and other spatial heterogeneity data analysis
写在公众号之前——QT,ARM,DSP,单片机,电力电子与传动!
FPGA解析B码----连载2
R language application in the field of ecological environment
QT连载3:基于QT和STM32H750的LORA试验平台(2)
标准化(Normalization)知识点总结
Knowledge distillation method of target detection
原创 Acegi 1.03 安全机制
DeepLearing4j深度学习之Yolo Tiny实现目标检测
R语言 生态环境领域应用
为什么会出现梯度爆炸和梯度消失现象?怎么缓解这种现象的发生?
Xcode 绑定按钮点击事件
C语言实战小项目(传统卡牌游戏)
QT连载1:readyRead()函数,数据分包不完整解决办法
[Getting C language from zero basis - navigation summary]
Meta分析在生态环境领域里的应用