attachInterrupt(interrupt, function, mode)

本文详细介绍了Arduino外部中断的原理、语法及其注意事项,并通过示例代码展示了如何利用外部中断处理突发事件,如读取旋转编码器、监视用户输入等。通过中断机制,Arduino能够更高效地响应外部事件,释放CPU资源进行其他任务。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

====== attachInterrupt(interrupt, function, mode) ======


===== description =====


当发生外部中断时,调用一个指定函数。当中断发生时,该函数会取代正在执行的程序。大多数的Arduino板有两个外部中断:0(数字引脚2)和1(数字引脚3)。


arduino Mege有四个外部中断:数字2(引脚21),3(20针),4(引脚19),5(引脚18)。
===== 语法 =====


interrupt:中断引脚数
\\ function:中断发生时调用的函数,此函数必须不带参数和不返回任何值。该函数有时被称为中断服务程序。
\\ mode:定义何时发生中断以下四个contstants预定有效值:
  * LOW 当引脚为低电平时,触发中断
  * CHANGE 当引脚电平发生改变时,触发中断
  * RISING 当引脚由低电平变为高电平时,触发中断
  * FALLING 当引脚由高电平变为低电平时,触发中断.
===== 返回 =====



===== 注意事项 =====


当中断函数发生时,delay()和millis()的数值将不会继续变化。当中断发生时,串口收到的数据可能会丢失。你应该声明一个变量来在未发生中断时储存变量。
===== 使用中断 =====


在单片机自动化程序中当突发事件发生时,中断是非常有用的,它可以帮助解决时序问题。一个使用中断的任务可能会读一个旋转编码器,监视用户的输入。


如果你想以确保程序始终抓住一个旋转编码器的脉冲,从来不缺少一个脉冲,它将使写一个程序做任何事情都要非常棘手,因为该计划将需要不断轮询的传感器线编码器,为了赶上脉冲发生时。其他传感器也是如此,如试图读取一个声音传感器正试图赶上一按,或红外线槽传感器(照片灭弧室),试图抓住一个硬币下降。在所有这些情况下,使用一个中断可以释放的微控制器来完成其他一些工作。
===== 程序示例 =====
<code cpp>
int pin = 13;
volatile int state = LOW;


void setup()
{
  pinMode(pin, OUTPUT);
  attachInterrupt(0, blink, CHANGE);
}


void loop()
{
  digitalWrite(pin, state);
}


void blink()
{
  state = !state;
}
</code>




[[arduino:arduino_language_reference|返回主菜单]] 


**相关讨论请访问[[http://www.geek-workshop.com|极客工坊]]**




版权说明:本词条由极客工坊团队发布维护,转载请说明出处。

/********************************************************************* ** Device: nRF24L01+ ** ** File: EF_nRF24L01_TX.c ** ** ** ** ** ** Copyright (C) 2011 ElecFraks. ** ** This example code is in the public domain. ** ** ** ** Description: ** ** This file is a sample code for your reference. ** ** It's the v1.1 nRF24L01+ by arduino ** ** Created by ElecFreaks. Robi.W,24 July 2011 ** ** ** ** http://www.elecfreaks.com ** ** ** ** SPI-compatible ** ** CS - to digital pin 8 ** ** CSN - to digital pin 9 (SS pin) ** ** SCK - to digital pin 10 (SCK pin) ** ** MOSI - to digital pin 11 (MOSI pin) ** ** MISO - to digital pin 12 (MISO pin) ** ** IRQ - to digital pin 13 (MISO pin) ** *********************************************************************/ #include "NRF24L01.h" #include "SPI.h" //*************************************************** #define TX_ADR_WIDTH 5 // 5 unsigned chars TX(RX) address width #define TX_PLOAD_WIDTH 32 // 32 unsigned chars TX payload unsigned char TX_ADDRESS[TX_ADR_WIDTH] = { 0x34,0x43,0x10,0x10,0x01 }; // Define a static TX address unsigned char rx_buf[TX_PLOAD_WIDTH]; unsigned char tx_buf[TX_PLOAD_WIDTH]; //*************************************************** void setup() { pinMode(CE, OUTPUT); pinMode(SCK, OUTPUT); pinMode(CSN, OUTPUT); pinMode(MOSI, OUTPUT); pinMode(MISO, INPUT); pinMode(IRQ, INPUT); // attachInterrupt(1, _ISR, LOW); // interrupt enable Serial.begin(9600); init_io(); // Initialize IO port unsigned char status=SPI_Read(STATUS); Serial.print("status = "); Serial.println(status,HEX); // There is read the mode鈥檚 status register, the default value should be 鈥楨鈥? Serial.println("*****************RX_Mode start******************************R"); RX_Mode(); // set RX mode } void loop() { for(;;) { unsigned char status = SPI_Read(STATUS); // read register STATUS's value if(status&RX_DR) // if receive data ready (TX_DS) interrupt { SPI_Read_Buf(RD_RX_PLOAD, rx_buf, TX_PLOAD_WIDTH); // read playload to rx_buf SPI_RW_Reg(FLUSH_RX,0); // clear RX_FIFO for(int i=0; i<32; i++) { Serial.print(" "); Serial.print(rx_buf[i],HEX); // print rx_buf } Serial.println(" "); } SPI_RW_Reg(WRITE_REG+STATUS,status); // clear RX_DR or TX_DS or MAX_RT interrupt flag delay(1000); } } //************************************************** // Function: init_io(); // Description: // flash led one time,chip enable(ready to TX or RX Mode), // Spi disable,Spi clock line init high //************************************************** void init_io(void) { digitalWrite(IRQ, 0); digitalWrite(CE, 0); // chip enable digitalWrite(CSN, 1); // Spi disable } /************************************************** * Function: SPI_RW(); * * Description: * Writes one unsigned char to nRF24L01, and return the unsigned char read * from nRF24L01 during write, according to SPI protocol **************************************************/ unsigned char SPI_RW(unsigned char Byte) { unsigned char i; for(i=0;i<8;i++) // output 8-bit { if(Byte&0x80) { digitalWrite(MOSI, 1); // output 'unsigned char', MSB to MOSI } else { digitalWrite(MOSI, 0); } digitalWrite(SCK, 1); // Set SCK high.. Byte <<= 1; // shift next bit into MSB.. if(digitalRead(MISO) == 1) { Byte |= 1; // capture current MISO bit } digitalWrite(SCK, 0); // ..then set SCK low again } return(Byte); // return read unsigned char } /**************************************************/ /************************************************** * Function: SPI_RW_Reg(); * * Description: * Writes value 'value' to register 'reg' /**************************************************/ unsigned char SPI_RW_Reg(unsigned char reg, unsigned char value) { unsigned char status; digitalWrite(CSN, 0); // CSN low, init SPI transaction status = SPI_RW(reg); // select register SPI_RW(value); // ..and write value to it.. digitalWrite(CSN, 1); // CSN high again return(status); // return nRF24L01 status unsigned char } /**************************************************/ /************************************************** * Function: SPI_Read(); * * Description: * Read one unsigned char from nRF24L01 register, 'reg' /**************************************************/ unsigned char SPI_Read(unsigned char reg) { unsigned char reg_val; digitalWrite(CSN, 0); // CSN low, initialize SPI communication... SPI_RW(reg); // Select register to read from.. reg_val = SPI_RW(0); // ..then read register value digitalWrite(CSN, 1); // CSN high, terminate SPI communication return(reg_val); // return register value } /**************************************************/ /************************************************** * Function: SPI_Read_Buf(); * * Description: * Reads 'unsigned chars' #of unsigned chars from register 'reg' * Typically used to read RX payload, Rx/Tx address /**************************************************/ unsigned char SPI_Read_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes) { unsigned char status,i; digitalWrite(CSN, 0); // Set CSN low, init SPI tranaction status = SPI_RW(reg); // Select register to write to and read status unsigned char for(i=0;i<bytes;i++) { pBuf[i] = SPI_RW(0); // Perform SPI_RW to read unsigned char from nRF24L01 } digitalWrite(CSN, 1); // Set CSN high again return(status); // return nRF24L01 status unsigned char } /**************************************************/ /************************************************** * Function: SPI_Write_Buf(); * * Description: * Writes contents of buffer '*pBuf' to nRF24L01 * Typically used to write TX payload, Rx/Tx address /**************************************************/ unsigned char SPI_Write_Buf(unsigned char reg, unsigned char *pBuf, unsigned char bytes) { unsigned char status,i; digitalWrite(CSN, 0); // Set CSN low, init SPI tranaction status = SPI_RW(reg); // Select register to write to and read status unsigned char for(i=0;i<bytes; i++) // then write all unsigned char in buffer(*pBuf) { SPI_RW(*pBuf++); } digitalWrite(CSN, 1); // Set CSN high again return(status); // return nRF24L01 status unsigned char } /**************************************************/ /************************************************** * Function: RX_Mode(); * * Description: * This function initializes one nRF24L01 device to * RX Mode, set RX address, writes RX payload width, * select RF channel, datarate & LNA HCURR. * After init, CE is toggled high, which means that * this device is now ready to receive a datapacket. /**************************************************/ void RX_Mode(void) { digitalWrite(CE, 0); SPI_Write_Buf(WRITE_REG + RX_ADDR_P0, TX_ADDRESS, TX_ADR_WIDTH); // Use the same address on the RX device as the TX device SPI_RW_Reg(WRITE_REG + EN_AA, 0x01); // Enable Auto.Ack:Pipe0 SPI_RW_Reg(WRITE_REG + EN_RXADDR, 0x01); // Enable Pipe0 SPI_RW_Reg(WRITE_REG + RF_CH, 40); // Select RF channel 40 SPI_RW_Reg(WRITE_REG + RX_PW_P0, TX_PLOAD_WIDTH); // Select same RX payload width as TX Payload width SPI_RW_Reg(WRITE_REG + RF_SETUP, 0x07); // TX_PWR:0dBm, Datarate:2Mbps, LNA:HCURR SPI_RW_Reg(WRITE_REG + CONFIG, 0x0f); // Set PWR_UP bit, enable CRC(2 unsigned chars) & Prim:RX. RX_DR enabled.. digitalWrite(CE, 1); // Set CE pin high to enable RX device // This device is now ready to receive one packet of 16 unsigned chars payload from a TX device sending to address // '3443101001', with auto acknowledgment, retransmit count of 10, RF channel 40 and datarate = 2Mbps. } /**************************************************/
最新发布
05-25
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值