当前位置:网站首页>arduino UNO R3的寄存器写法(1)-----引脚电平状态变化
arduino UNO R3的寄存器写法(1)-----引脚电平状态变化
2022-07-06 09:16:00 【一入极客深似海】
学习过51或STM32的,尤其是会STM32的开发者对寄存器有非常深的了解和痛苦,因为arduino的封装好的函数,使我们在使用中,不用去找相应的寄存器,我们只需要使用对应的函数,配置好就可以使用了。这一点真是小白的福音。arduino UNO使用的是ATMEGA328P是AVR单片机的一个型号,常用的Ardunio UNO和Arduino Pro Mini都是基于这款芯片,我研究了一下arduino的寄存器写法,记录一下。省的以后忘记了。
声明:笔者从未接触过AVR单片机的写法,所有研究资料来自于度娘。语法错误或用法错误,欢迎饱学之士指点迷津
在官方中有对arduino的相关寄存器和引脚的的详细标注

PORTx -控制输出数据0、1; //改变寄存器引脚电平状态
PINx —控制(读取)输入数据; //读取寄存器引脚的电平状态
指令中加粗的x表示的要使用的寄存器,
比如要使用D0—D7的中的某个引脚,我们就用到了PD寄存器,那么就是
DDRD
PORTD
PIND
基础介绍已经说完了,直接上程序对比,大家感受一下,假如要点亮D2这个引脚
先看一下通常写法
在看一下寄存器写法
//#include <avr/io.h> //如果编译错误,则引入此函数
void setup() {
DDRD=0B00000100; // 二进制顺序为D7--D0;1为输出,0为输入
PORTD=0B00000100; // 二进制顺序为D7--D0;1为高电平,0为低电平
}
void loop() {
}
仿真结果如下
从一个引脚对比,我们可以看到,使用寄存器用法,可以使用到非常小的程序存储空间。当然,这样直接对比,没有意义,和耍流氓一样。函数是对寄存器的封装,要调用的很多,相当于中间商。用起来简单当然也要付出一定的成本。
下面我们看一下读取引脚状态,这次我们使用A0-A5来查看数据
//#include <avr/io.h> //如果编译错误,则引入此函数
void setup() {
DDRC=0B000100; // 二进制顺序为A5--A0;1为输出,0为输入
PORTC=0B000100; // 二进制顺序为A5--A0;1为高电平,0为低电平
Serial.begin(9600);
Serial.print(PINC,BIN); // 读取PC寄存器的引脚状态
}
void loop() {
}

仿真结果
后面我会进行持续介绍,不定时更新
边栏推荐
- Using LinkedHashMap to realize the caching of an LRU algorithm
- [Kerberos] deeply understand the Kerberos ticket life cycle
- [BSidesCF_2020]Had_a_bad_day
- 4、安装部署Spark(Spark on Yarn模式)
- 分布式節點免密登錄
- RT-Thread 线程的时间片轮询调度
- encoderMapReduce 随手记
- nodejs 详解
- Reading notes of difficult career creation
- Wangeditor rich text component - copy available
猜你喜欢
随机推荐
互联网协议详解
Contiki source code + principle + function + programming + transplantation + drive + network (turn)
分布式節點免密登錄
Encodermappreduce notes
分布式事务的实现方案
Some concepts often asked in database interview
Machine learning -- linear regression (sklearn)
Using LinkedHashMap to realize the caching of an LRU algorithm
[CDH] cdh5.16 configuring the setting of yarn task centralized allocation does not take effect
【yarn】Yarn container 日志清理
Those commonly used tool classes and methods in hutool
数据分析之缺失值填充(重点讲解多重插值法Miceforest)
小L的试卷
mysql实现读写分离
express框架详解
【CDH】CDH5.16 配置 yarn 任务集中分配设置不生效问题
STM32型号与Contex m对应关系
L2-007 family real estate (25 points)
B tree and b+ tree of MySQL index implementation
Gallery's image browsing and component learning










