当前位置:网站首页>电控学习 第二周
电控学习 第二周
2022-06-12 17:12:00 【魔法电子小Q】
目录
2)void (*userFunc):中断服务例程(ISR)的函数指针
中断:
1、过程和意义:
一般用由数字引脚上的状态改变硬件中断。中断触发时,控制器会暂停当前正在运行的主程序,而跳转去运行中断程序,中断程序运行完后,再回到主程序暂停的位置继续运行当前程序
2、分类:
1)硬件中断:
相应外部事件而发生。例如:外部中断引脚从高电平变为低电平时。
2)软件中断:
相应于软件中发送的指令而发生。例如:attachInterrupt( )函数(Arduino唯一支持的中断函数)。
3、中断函数:
attachInterrupt( ) 外部中断配置函数
void attachInterrupt(uint8_interruptNum,void(*userFunc)(void),int mode);
1)uint8_interruptNum:中断号
中断号不是pin号,是Arduino上每个可以注册的pin口都会被分配一个终中断号,这里传入的是中断号而不是pin口。在 Arduino Mega 2560 中:
| 中断号 | 0 | 1 | 2 | 3 | 4 | 5 |
|---|---|---|---|---|---|---|
| 引脚号 | PIN2 | PIN3 | PIN21 | PIN20 | PIN19 | PIN18 |
不同的pin口在不同的开发板上会有不同的中断号,幸运的是,Arduino还提供了另一个函数digitalPinToInterrupt(int),此函数能输入pin口并输出对应的中断号(前提是输入的pin口号必须为支持的),此时此函数可以注册中断号:
attachInterrupt(digitalPinToInterrupt(pin),ISR,mode);
2)void (*userFunc):中断服务例程(ISR)的函数指针
在C/C++中,直接写该函数的函数名即可,触发时,该函数会被调用。(此函数必须无参数也无返回值!)
3)mode:中断触发条件
LOW 低电平时触发
CHANGE 电平改变时触发
RISING 低电平变为高电平时触发
FALLING 高电平变为低电平时触发
int SensorLED = 13;
int SensorINPUT = 2;//连接震动开关到中断0,即数字引脚2
char state = LOW;
void setup()
{
pinMode(SensorLED, OUTPUT);
pinMode(SensorINPUT, INPUT_PULLUP);//震动开关 上拉输入模式
analogInterrupt(0, blink, FALLING);
//下降沿时触发(高电平->低电平时),触发中断号为0,运行中断处理函数blink
}
void loop()
{
if (state = HIGH) {
state = LOW;
digitalWrite(SensorLED, HIGH);//开灯
delay(500);
}
else {
digitalWrite(SensorLED, LOW);//关灯
}
}
void blink()
{
state = !state;//一旦触发中断,state状态反转
}此函数的实现:
把震动开关设置为上拉输入模式,加上一个下降沿时触发的中断函数,中断时调用函数blink()将state反转,具体实现见函数。
当按下并弹起震动开关时,点位变化从低电位->高电位->低电位,触发了中断函数,并调用中断实现函数blink(),将state反转为HIGH(刚开始为LOW),循环时进入循环函数loop(),开灯,延迟500ms,并将state置为LOW, 再次进入loop()循环时,灯熄灭。
表现的情况为:震动时,灯先亮一会,再熄灭。
数字IO口的外部电路(输入与输出):
例如:用按钮来控制LED灯的开关
int LEDpin=13;
int pin=2;
void setup()
{
pinMode(LEDpin,OUTPUT);
pinMode(pin,INPUT);
}
void loop()
{
if(digitalRead(pin)==HIGH){
digitalWrite(LEDpin,HIGH);
}else{
digitalWrite(LEDpin,LOW);
}
delay(1);
}按钮防抖控制:delay(ms)
例如:用按钮来控制LED灯的开关
int x=0;//用x来控制灯的状态,x=0时灯灭,x=1时灯亮
int deng1;//deng1表示按键当时的状态
int deng2;//deng2表示按键之前的状态
void setup()
{
pinMode(13,OUTPUT);
pinMode(2,INPUT);
}
void loop()
{
deng1=digitalRead(2);
if(deng1==1 and deng2==0){
delay(50);//按钮防抖!
if(x==0){
x=1;
}else{
x=0;
}
}
deng2=deng1;
if(x==1){
digitalWrite(13,HIGH);
}else{
digitalWrite(13,LOW);
}
}此函数的实现:
开始时,x、deng1、deng2都为0;
第一次按下按钮时,deng1=1,deng2=0;进入第一个if条件语句,此时deng1=1;deng2=1;并把x置为1;进入第二个if条件句,灯亮;
第一次放开按钮时,deng1=0,deng2=1;不进入第一个if条件语句,之后deng2=deng1=0;x值不变还是1,进入第二个if条件语句,灯还亮;
第二次按下按钮时,deng1=1,deng2=0;进入第一个if条件语句,此时deng1=1;deng2=1;并把x置为0;进入第二个if条件语句,灯灭;
第二次放开按钮时,deng1=0,deng2=1;不进入第一个if条件语句,之后deng2=deng1=0;x值不变还是0,进入第二个if条件语句,灯还灭;
综上,按下和放起一次开关,灯的状态改变一次。
模拟输入和输出:
map( )函数
模拟输入是返回一个0-1023的数值,用analogRead(pin)
模拟输出是输出一个0-255的数值,用analogWrite(pin,value)
map(value,fromLOW,fromHIGH,toLow,toHigh)
将value变量依照fromLOW-fromHIGH范围对等到toLow-toHigh范围
int moni=0;
int deng=0;
void setup()
{
Serial.begin(9600);
pinMode(13,OUTPUT);
}
void loop()
{
moni=analogRead(0);
Serial.println(moni);
deng=map(moni,0,1023,0,255);
analogWrite(13,deng);
delay(1);
}边栏推荐
- The R language uses the aggregate The plot function visualizes the summary statistical information of each subset (visualization is based on the probability value and its 95% confidence interval of th
- C# 业务流水号规则生成组件
- Guitar Pro tutorial how to set up a MIDI keyboard
- How to do a good job of testing in the company (do a good job of testing)
- 内核中断整体流程图
- Learn the mitmproxy packet capturing tool from scratch
- MIPS 通用寄存器 + 指令
- 两位新晋Committer的“升级攻略”
- ftrace
- Li Kou today's question 926 Flip string to monotonic increment
猜你喜欢

goland变成中文版了怎么修改回英文版

初识GO语言

Unit sshd.service could not be found

Gerrit+2触发Jenkins任务

5、Embedding

Su directly switches to super administrator mode, so that many error reports can be avoided

Yyds dry goods inventory leetcode question set 911 - 920

5、Embedding

快速入门scrapy爬虫框架

Download PHP source code of leaf sharing station
随机推荐
Swintransformer network architecture
1.5 什么是架构师(连载)
卖疯了的临期产品:超低价、大混战与新希望
R语言使用epiDisplay包的tableStack函数基于分组变量生成统计分析表(包含描述性统计分析、假设检验、不同数据使用不同的统计量和假设检验方法)、自定义配置是否显示统计检验内容
Pat class a 1139 first contact
R language uses the sum function of epidisplay package to calculate the descriptive statistical summary information of the specified variables in dataframe under different grouped variables and visual
错误记录:IllegalStateException: Optional int parameter ‘xxxx‘ is
R语言使用epiDisplay包的aggregate.plot函数可视化每个子集的汇总统计信息(可视化基于单个分组下的阳性指标的概率值及其95%置信区间、基于折线图、仅仅适用于目标类别为二分类)
1723. minimum time to complete all work
(7) Loop statement for
R语言使用ggplot2可视化dataframe数据中特定数据列的密度图(曲线)、并使用xlim参数指定X轴的范围
How to change Golan back to the English version when it becomes the Chinese version
PAT甲级 1139 第一次接触
Modify the configuration of the router connected to your computer. The website is 192.168.1.1
(6) Control statement if/else switch
rolabelImg的安装使用
Exclusive interview with oppo find X5 Product Manager: deeply cultivate self-developed chips to create the ultimate flagship experience with the highest standards
Learn the mitmproxy packet capturing tool from scratch
Use GCC's PGO (profile guided optimization) to optimize the entire system
AlibabaProtect.exe如何删除、卸载