女人自慰AV免费观看内涵网,日韩国产剧情在线观看网址,神马电影网特片网,最新一级电影欧美,在线观看亚洲欧美日韩,黄色视频在线播放免费观看,ABO涨奶期羡澄,第一导航fulione,美女主播操b

0
  • 聊天消息
  • 系統(tǒng)消息
  • 評(píng)論與回復(fù)
登錄后你可以
  • 下載海量資料
  • 學(xué)習(xí)在線課程
  • 觀看技術(shù)視頻
  • 寫文章/發(fā)帖/加入社區(qū)
會(huì)員中心
創(chuàng)作中心

完善資料讓更多小伙伴認(rèn)識(shí)你,還能領(lǐng)取20積分哦,立即完善>

3天內(nèi)不再提示

如何利用計(jì)時(shí)器中斷來保持一切正常運(yùn)轉(zhuǎn)

454398 ? 來源:網(wǎng)絡(luò)整理 ? 作者:網(wǎng)絡(luò)整理 ? 2019-11-27 16:45 ? 次閱讀

設(shè)置

對(duì)于本指南中的所有示例,將使用以下接線:

如何利用計(jì)時(shí)器中斷來保持一切正常運(yùn)轉(zhuǎn)

什么是中斷?

中斷是一個(gè)信號(hào),告訴處理器立即停止其正在執(zhí)行的操作并處理一些高優(yōu)先級(jí)的處理。這種高優(yōu)先級(jí)處理稱為中斷處理程序。

中斷處理程序與任何其他void函數(shù)一樣。如果編寫一個(gè)并將其附加到中斷,則在觸發(fā)該中斷信號(hào)時(shí)將調(diào)用該中斷。當(dāng)您從中斷處理程序返回時(shí),處理器將返回以繼續(xù)之前的操作。

它們來自何處?

可以生成中斷

來自一個(gè)Arduino定時(shí)器的定時(shí)器中斷。

外部中斷由于外部中斷引腳之一的狀態(tài)改變而引起的外部中斷。

Pin-change中斷是由于一組引腳中任何一個(gè)引腳的狀態(tài)改變。

它們有什么用?

使用中斷,您無需編寫循環(huán)代碼即可連續(xù)檢查高優(yōu)先級(jí)中斷條件。您不必?fù)?dān)心由于長(zhǎng)時(shí)間運(yùn)行的子例程而反應(yīng)遲鈍或錯(cuò)過了按鍵操作。

當(dāng)中斷發(fā)生時(shí),處理器將自動(dòng)停止正在執(zhí)行的操作并調(diào)用中斷處理程序。您只需編寫代碼以響應(yīng)中斷。

定時(shí)器中斷

請(qǐng)勿致電給我們,我們會(huì)打電話給您

在本系列的第1部分中,我們學(xué)習(xí)了如何使用millis()進(jìn)行計(jì)時(shí)。但是為了進(jìn)行這項(xiàng)工作,我們每次都要在循環(huán)中調(diào)用millis()來查看是否該做某事了。一毫秒多次調(diào)用millis()只是發(fā)現(xiàn)時(shí)間沒有改變,這是一種浪費(fèi)。如果只需要每毫秒檢查一次,那會(huì)不會(huì)很好?

計(jì)時(shí)器和計(jì)時(shí)器中斷讓我們可以做到這一點(diǎn)。我們可以設(shè)置一個(gè)計(jì)時(shí)器來每毫秒中斷一次。計(jì)時(shí)器實(shí)際上會(huì)打電話給我們,讓我們知道現(xiàn)在該檢查時(shí)鐘了!

Arduino計(jì)時(shí)器

Arduino Uno有3個(gè)計(jì)時(shí)器:Timer0,Timer1和Timer2。已經(jīng)將Timer0設(shè)置為生成毫秒中斷,以更新millis()報(bào)告的毫秒計(jì)數(shù)器。既然這就是我們要尋找的東西,我們也將得到Timer0來為我們生成一個(gè)中斷!

頻率和計(jì)數(shù)

定時(shí)器是簡(jiǎn)單的計(jì)數(shù)器,它們以從16MHz系統(tǒng)時(shí)鐘。您可以配置時(shí)鐘分頻器以更改頻率和各種不同的計(jì)數(shù)模式。您還可以將它們配置為在計(jì)時(shí)器達(dá)到特定計(jì)數(shù)時(shí)生成中斷。

Timer0是8位,從0到255計(jì)數(shù),并在溢出時(shí)生成中斷。默認(rèn)情況下,它使用64的時(shí)鐘分頻比為我們提供976.5625 Hz的中斷率(就我們的目的而言,足夠接近1KHz)。我們不會(huì)弄亂Timer0的頻率,因?yàn)槟菢訒?huì)破壞breakmillis()!

比較寄存器

Arduino定時(shí)器具有許多配置寄存器??梢允褂肁rduino IDE中定義的特殊符號(hào)來讀取或?qū)懭脒@些符號(hào)。 有關(guān)所有這些寄存器及其功能的詳細(xì)說明,請(qǐng)參見下面的“ 進(jìn)一步閱讀”中的鏈接。

我們將建立一個(gè)《計(jì)時(shí)器0的strong》比較寄存器(該寄存器稱為OCR0A),在該計(jì)數(shù)中間的某個(gè)位置產(chǎn)生另一個(gè)中斷。在每次滴答時(shí),計(jì)時(shí)器計(jì)數(shù)器都會(huì)與比較寄存器進(jìn)行比較,并且當(dāng)它們相等時(shí),將產(chǎn)生一個(gè)中斷。

只要計(jì)數(shù)器值超過0xAF,下面的代碼就會(huì)產(chǎn)生一個(gè)“ TIMER0_COMPA”中斷。

下載:文件

復(fù)制代碼

// Timer0 is already used for millis() - we‘ll just interrupt somewhere

// in the middle and call the “Compare A” function below

OCR0A = 0xAF;

TIMSK0 |= _BV(OCIE0A); // Timer0 is already used for millis() - we’ll just interrupt somewhere

// in the middle and call the “Compare A” function below

OCR0A = 0xAF;

TIMSK0 |= _BV(OCIE0A);

然后,我們將為定時(shí)器中斷向量(稱為“ TIMER0_COMPA_vect”)定義一個(gè)中斷處理程序。在此中斷處理程序中,我們將完成循環(huán)中所有的工作。

下載:文件

復(fù)制代碼

// Interrupt is called once a millisecond,

SIGNAL(TIMER0_COMPA_vect)

{

unsigned long currentMillis = millis();

sweeper1.Update(currentMillis);

//if(digitalRead(2) == HIGH)

{

sweeper2.Update(currentMillis);

led1.Update(currentMillis);

}

led2.Update(currentMillis);

led3.Update(currentMillis);

} // Interrupt is called once a millisecond,

SIGNAL(TIMER0_COMPA_vect)

{

unsigned long currentMillis = millis();

sweeper1.Update(currentMillis);

//if(digitalRead(2) == HIGH)

{

sweeper2.Update(currentMillis);

led1.Update(currentMillis);

}

led2.Update(currentMillis);

led3.Update(currentMillis);

}

這給我們留下了一個(gè)完全空的循環(huán)。

下載:文件

復(fù)制代碼

void loop()

{

} void loop()

{

}

您現(xiàn)在可以在循環(huán)中做任何您想做的事情。您甚至可以decade廢并使用delay()!閃光燈和掃地機(jī)將不在乎。無論如何,它們?nèi)匀粫?huì)每毫秒被調(diào)用一次!

進(jìn)一步閱讀:

這只是計(jì)時(shí)器可以執(zhí)行的簡(jiǎn)單示例。有關(guān)不同類型的計(jì)時(shí)器及其配置方式的更多詳細(xì)信息,請(qǐng)查看“庫(kù)和鏈接”頁(yè)面。

源代碼:

以下是整個(gè)代碼,包括閃光燈和掃地機(jī):

下載:文件

復(fù)制代碼

#include

class Flasher

{

// Class Member Variables

// These are initialized at startup

int ledPin; // the number of the LED pin

long OnTime; // milliseconds of on-time

long OffTime; // milliseconds of off-time

// These maintain the current state

int ledState; // ledState used to set the LED

unsigned long previousMillis; // will store last time LED was updated

// Constructor - creates a Flasher

// and initializes the member variables and state

public:

Flasher(int pin, long on, long off)

{

ledPin = pin;

pinMode(ledPin, OUTPUT);

OnTime = on;

OffTime = off;

ledState = LOW;

previousMillis = 0;

}

void Update(unsigned long currentMillis)

{

if((ledState == HIGH) && (currentMillis - previousMillis 》= OnTime))

{

ledState = LOW; // Turn it off

previousMillis = currentMillis; // Remember the time

digitalWrite(ledPin, ledState); // Update the actual LED

}

else if ((ledState == LOW) && (currentMillis - previousMillis 》= OffTime))

{

ledState = HIGH; // turn it on

previousMillis = currentMillis; // Remember the time

digitalWrite(ledPin, ledState); // Update the actual LED

}

}

};

class Sweeper

{

Servo servo; // the servo

int pos; // current servo position

int increment; // increment to move for each interval

int updateInterval; // interval between updates

unsigned long lastUpdate; // last update of position

public:

Sweeper(int interval)

{

updateInterval = interval;

increment = 1;

}

void Attach(int pin)

{

servo.attach(pin);

}

void Detach()

{

servo.detach();

}

void Update(unsigned long currentMillis)

{

if((currentMillis - lastUpdate) 》 updateInterval) // time to update

{

lastUpdate = millis();

pos += increment;

servo.write(pos);

if ((pos 》= 180) || (pos 《= 0)) // end of sweep

{

// reverse direction

increment = -increment;

}

}

}

};

Flasher led1(11, 123, 400);

Flasher led2(12, 350, 350);

Flasher led3(13, 200, 222);

Sweeper sweeper1(25);

Sweeper sweeper2(35);

void setup()

{

sweeper1.Attach(9);

sweeper2.Attach(10);

// Timer0 is already used for millis() - we‘ll just interrupt somewhere

// in the middle and call the “Compare A” function below

OCR0A = 0xAF;

TIMSK0 |= _BV(OCIE0A);

}

// Interrupt is called once a millisecond, to update the LEDs

// Sweeper2 s not updated if the button on digital 2 is pressed.

SIGNAL(TIMER0_COMPA_vect)

{

unsigned long currentMillis = millis();

sweeper1.Update(currentMillis);

if(digitalRead(2) == HIGH)

{

sweeper2.Update(currentMillis);

led1.Update(currentMillis);

}

led2.Update(currentMillis);

led3.Update(currentMillis);

}

void loop()

{

} #include

class Flasher

{

// Class Member Variables

// These are initialized at startup

int ledPin; // the number of the LED pin

long OnTime; // milliseconds of on-time

long OffTime; // milliseconds of off-time

// These maintain the current state

int ledState; // ledState used to set the LED

unsigned long previousMillis; // will store last time LED was updated

// Constructor - creates a Flasher

// and initializes the member variables and state

public:

Flasher(int pin, long on, long off)

{

ledPin = pin;

pinMode(ledPin, OUTPUT);

OnTime = on;

OffTime = off;

ledState = LOW;

previousMillis = 0;

}

void Update(unsigned long currentMillis)

{

if((ledState == HIGH) && (currentMillis - previousMillis 》= OnTime))

{

ledState = LOW; // Turn it off

previousMillis = currentMillis; // Remember the time

digitalWrite(ledPin, ledState); // Update the actual LED

}

else if ((ledState == LOW) && (currentMillis - previousMillis 》= OffTime))

{

ledState = HIGH; // turn it on

previousMillis = currentMillis; // Remember the time

digitalWrite(ledPin, ledState); // Update the actual LED

}

}

};

class Sweeper

{

Servo servo; // the servo

int pos; // current servo position

int increment; // increment to move for each interval

int updateInterval; // interval between updates

unsigned long lastUpdate; // last update of position

public:

Sweeper(int interval)

{

updateInterval = interval;

increment = 1;

}

void Attach(int pin)

{

servo.attach(pin);

}

void Detach()

{

servo.detach();

}

void Update(unsigned long currentMillis)

{

if((currentMillis - lastUpdate) 》 updateInterval) // time to update

{

lastUpdate = millis();

pos += increment;

servo.write(pos);

if ((pos 》= 180) || (pos 《= 0)) // end of sweep

{

// reverse direction

increment = -increment;

}

}

}

};

Flasher led1(11, 123, 400);

Flasher led2(12, 350, 350);

Flasher led3(13, 200, 222);

Sweeper sweeper1(25);

Sweeper sweeper2(35);

void setup()

{

sweeper1.Attach(9);

sweeper2.Attach(10);

// Timer0 is already used for millis() - we’ll just interrupt somewhere

// in the middle and call the “Compare A” function below

OCR0A = 0xAF;

TIMSK0 |= _BV(OCIE0A);

}

// Interrupt is called once a millisecond, to update the LEDs

// Sweeper2 s not updated if the button on digital 2 is pressed.

SIGNAL(TIMER0_COMPA_vect)

{

unsigned long currentMillis = millis();

sweeper1.Update(currentMillis);

if(digitalRead(2) == HIGH)

{

sweeper2.Update(currentMillis);

led1.Update(currentMillis);

}

led2.Update(currentMillis);

led3.Update(currentMillis);

}

void loop()

{

}

外部中斷

最好是退出循環(huán)

與定時(shí)器中斷不同的是,外部事件會(huì)觸發(fā)外部中斷。例如,當(dāng)按下按鈕或從旋轉(zhuǎn)編碼器接收到脈沖時(shí)。但是,就像計(jì)時(shí)器中斷一樣,您不需要繼續(xù)輪詢GPIO引腳以進(jìn)行更改。

Arduino UNO有2個(gè)外部中斷引腳。在此示例中,我們將按鈕附加到其中一個(gè)按鈕,并使用它來重置我們的清掃器。首先,在我們的清除程序類中添加一個(gè)“ reset()”函數(shù)。 reset()函數(shù)將位置設(shè)置為0,并立即將伺服器放置在此處:

下載:文件

復(fù)制代碼

void reset()

{

pos = 0;

servo.write(pos);

increment = abs(increment);

} void reset()

{

pos = 0;

servo.write(pos);

increment = abs(increment);

}

接下來,我們將添加對(duì)AttachInterrupt()的調(diào)用以連接外部中斷

在UNO上,中斷0與數(shù)字引腳2相關(guān)聯(lián)。我們告訴它在該引腳上尋找信號(hào)的“ FALLING”沿。當(dāng)按下按鈕時(shí),信號(hào)從HIGH降到LOW,并調(diào)用“重置”中斷處理程序。

下載:文件

復(fù)制代碼

pinMode(2, INPUT_PULLUP);

attachInterrupt(0, Reset, FALLING); pinMode(2, INPUT_PULLUP);

attachInterrupt(0, Reset, FALLING);

這是“重置”中斷處理程序。它僅調(diào)用清除程序重置功能:

下載:文件

復(fù)制代碼

void Reset()

{

sweeper1.reset();

sweeper2.reset();

} void Reset()

{

sweeper1.reset();

sweeper2.reset();

}

現(xiàn)在,每當(dāng)您按下按鈕時(shí),伺服器就會(huì)停止其正在執(zhí)行的操作,并立即尋找到零位置。

源代碼:

這是帶有計(jì)時(shí)器和外部中斷的完整草圖:

下載:文件

復(fù)制代碼

#include

class Flasher

{

// Class Member Variables

// These are initialized at startup

int ledPin; // the number of the LED pin

long OnTime; // milliseconds of on-time

long OffTime; // milliseconds of off-time

// These maintain the current state

volatile int ledState; // ledState used to set the LED

volatile unsigned long previousMillis; // will store last time LED was updated

// Constructor - creates a Flasher

// and initializes the member variables and state

public:

Flasher(int pin, long on, long off)

{

ledPin = pin;

pinMode(ledPin, OUTPUT);

OnTime = on;

OffTime = off;

ledState = LOW;

previousMillis = 0;

}

void Update(unsigned long currentMillis)

{

if((ledState == HIGH) && (currentMillis - previousMillis 》= OnTime))

{

ledState = LOW; // Turn it off

previousMillis = currentMillis; // Remember the time

digitalWrite(ledPin, ledState); // Update the actual LED

}

else if ((ledState == LOW) && (currentMillis - previousMillis 》= OffTime))

{

ledState = HIGH; // turn it on

previousMillis = currentMillis; // Remember the time

digitalWrite(ledPin, ledState); // Update the actual LED

}

}

};

class Sweeper

{

Servo servo; // the servo

int updateInterval; // interval between updates

volatile int pos; // current servo position

volatile unsigned long lastUpdate; // last update of position

volatile int increment; // increment to move for each interval

public:

Sweeper(int interval)

{

updateInterval = interval;

increment = 1;

}

void Attach(int pin)

{

servo.attach(pin);

}

void Detach()

{

servo.detach();

}

void reset()

{

pos = 0;

servo.write(pos);

increment = abs(increment);

}

void Update(unsigned long currentMillis)

{

if((currentMillis - lastUpdate) 》 updateInterval) // time to update

{

lastUpdate = currentMillis;

pos += increment;

servo.write(pos);

if ((pos 》= 180) || (pos 《= 0)) // end of sweep

{

// reverse direction

increment = -increment;

}

}

}

};

Flasher led1(11, 123, 400);

Flasher led2(12, 350, 350);

Flasher led3(13, 200, 222);

Sweeper sweeper1(25);

Sweeper sweeper2(35);

void setup()

{

sweeper1.Attach(9);

sweeper2.Attach(10);

// Timer0 is already used for millis() - we‘ll just interrupt somewhere

// in the middle and call the “Compare A” function below

OCR0A = 0xAF;

TIMSK0 |= _BV(OCIE0A);

pinMode(2, INPUT_PULLUP);

attachInterrupt(0, Reset, FALLING);

}

void Reset()

{

sweeper1.reset();

sweeper2.reset();

}

// Interrupt is called once a millisecond,

SIGNAL(TIMER0_COMPA_vect)

{

unsigned long currentMillis = millis();

sweeper1.Update(currentMillis);

//if(digitalRead(2) == HIGH)

{

sweeper2.Update(currentMillis);

led1.Update(currentMillis);

}

led2.Update(currentMillis);

led3.Update(currentMillis);

}

void loop()

{

} #include

class Flasher

{

// Class Member Variables

// These are initialized at startup

int ledPin; // the number of the LED pin

long OnTime; // milliseconds of on-time

long OffTime; // milliseconds of off-time

// These maintain the current state

volatile int ledState; // ledState used to set the LED

volatile unsigned long previousMillis; // will store last time LED was updated

// Constructor - creates a Flasher

// and initializes the member variables and state

public:

Flasher(int pin, long on, long off)

{

ledPin = pin;

pinMode(ledPin, OUTPUT);

OnTime = on;

OffTime = off;

ledState = LOW;

previousMillis = 0;

}

void Update(unsigned long currentMillis)

{

if((ledState == HIGH) && (currentMillis - previousMillis 》= OnTime))

{

ledState = LOW; // Turn it off

previousMillis = currentMillis; // Remember the time

digitalWrite(ledPin, ledState); // Update the actual LED

}

else if ((ledState == LOW) && (currentMillis - previousMillis 》= OffTime))

{

ledState = HIGH; // turn it on

previousMillis = currentMillis; // Remember the time

digitalWrite(ledPin, ledState); // Update the actual LED

}

}

};

class Sweeper

{

Servo servo; // the servo

int updateInterval; // interval between updates

volatile int pos; // current servo position

volatile unsigned long lastUpdate; // last update of position

volatile int increment; // increment to move for each interval

public:

Sweeper(int interval)

{

updateInterval = interval;

increment = 1;

}

void Attach(int pin)

{

servo.attach(pin);

}

void Detach()

{

servo.detach();

}

void reset()

{

pos = 0;

servo.write(pos);

increment = abs(increment);

}

void Update(unsigned long currentMillis)

{

if((currentMillis - lastUpdate) 》 updateInterval) // time to update

{

lastUpdate = currentMillis;

pos += increment;

servo.write(pos);

if ((pos 》= 180) || (pos 《= 0)) // end of sweep

{

// reverse direction

increment = -increment;

}

}

}

};

Flasher led1(11, 123, 400);

Flasher led2(12, 350, 350);

Flasher led3(13, 200, 222);

Sweeper sweeper1(25);

Sweeper sweeper2(35);

void setup()

{

sweeper1.Attach(9);

sweeper2.Attach(10);

// Timer0 is already used for millis() - we’ll just interrupt somewhere

// in the middle and call the “Compare A” function below

OCR0A = 0xAF;

TIMSK0 |= _BV(OCIE0A);

pinMode(2, INPUT_PULLUP);

attachInterrupt(0, Reset, FALLING);

}

void Reset()

{

sweeper1.reset();

sweeper2.reset();

}

// Interrupt is called once a millisecond,

SIGNAL(TIMER0_COMPA_vect)

{

unsigned long currentMillis = millis();

sweeper1.Update(currentMillis);

//if(digitalRead(2) == HIGH)

{

sweeper2.Update(currentMillis);

led1.Update(currentMillis);

}

led2.Update(currentMillis);

led3.Update(currentMillis);

}

void loop()

{

}

庫(kù)和鏈接

有關(guān)計(jì)時(shí)器的更多信息

可以將計(jì)時(shí)器配置為以各種頻率運(yùn)行并以不同的模式運(yùn)行。除了產(chǎn)生中斷,它們還用于控制PWM引腳。以下鏈接是了解如何配置和使用計(jì)時(shí)器的出色資源:

Arduino PWM的機(jī)密

計(jì)時(shí)器/PWM速查表

計(jì)時(shí)器庫(kù)

網(wǎng)上有許多Arduino``計(jì)時(shí)器‘’庫(kù)可用與本系列第1部分中所做的一樣,許多人僅監(jiān)視millis()并要求進(jìn)行持續(xù)輪詢。但是實(shí)際上有一些配置可以讓您配置定時(shí)器以生成中斷。

Paul Stoffregan出色的TimerOne和TimerThree庫(kù)處理了許多定時(shí)器中斷配置的低級(jí)細(xì)節(jié)。 (請(qǐng)注意,TimerThree不適用于UNO。它可以與Leonardo,Mega和某些Teensy板一起使用)

TimerOne和Timer 3庫(kù)

引腳更改中斷

對(duì)于2個(gè)不足的情況

Arduino UNO只有2個(gè)外部中斷引腳。但是,如果您需要兩個(gè)以上的中斷,該怎么辦?幸運(yùn)的是,Arduino UNO在所有引腳上都支持“引腳更改”中斷。

引腳更改中斷類似于外部中斷。區(qū)別在于,在8個(gè)相關(guān)引腳中的任何一個(gè)引腳上都會(huì)因狀態(tài)變化而產(chǎn)生一個(gè)中斷。這些操作要稍微復(fù)雜一點(diǎn),因?yàn)槟仨毟櫵?個(gè)引腳的最后一個(gè)已知狀態(tài),以找出8個(gè)引腳中的哪個(gè)導(dǎo)致了中斷。

Arduino Playground的PinChangeInt庫(kù)實(shí)現(xiàn)了一個(gè)方便的引腳更改中斷接口:http://playground.arduino.cc/Main/PinChangeInt

PinChangeInt庫(kù)

計(jì)時(shí)器和中斷禮儀

中斷就像超市的快速通道??紤]周全,將其保持在10件以下,一切都會(huì)順利進(jìn)行。

如果一切都是高水平的,那么沒有什么是高度優(yōu)先的。

中斷處理程序應(yīng)僅用于處理高優(yōu)先級(jí),對(duì)時(shí)間敏感的事件。請(qǐng)記住,在中斷處理程序中時(shí),禁用了中斷。如果您嘗試在中斷級(jí)別執(zhí)行過多操作,則會(huì)降低對(duì)其他中斷的響應(yīng)。

一次僅一個(gè)中斷。

ISR,中斷被禁用。這有兩個(gè)非常重要的含義:

在ISR中完成的工作應(yīng)保持簡(jiǎn)短,以免丟失任何中斷。

在ISR中的代碼不應(yīng)調(diào)用任何需要激活中斷(例如delay()或使用i2c總線的任何中斷)。這將導(dǎo)致程序掛起。

將冗長(zhǎng)的處理推遲到循環(huán)中。

如果您需要進(jìn)行大量處理以響應(yīng)中斷,請(qǐng)使用中斷處理程序僅執(zhí)行必要的操作,然后設(shè)置易失性狀態(tài)變量(參見下文)以指示需要進(jìn)一步處理。當(dāng)您從循環(huán)中調(diào)用更新功能時(shí),請(qǐng)檢查狀態(tài)變量以查看是否需要任何后續(xù)處理。

重新配置計(jì)時(shí)器之前需要檢查 《計(jì)時(shí)器是一種有限的資源。 UNO上只有3個(gè),它們用于許多用途。如果您弄亂了計(jì)時(shí)器配置,則其他某些功能可能不再起作用。例如,在Arduino UNO上:

Timer0 -用于引腳5和6上的millis(),micros(),delay()和PWM

Timer1 -用于Servos,WaveHC庫(kù)和引腳9和10上的PWM

Timer2 -由Tone和PWM引腳上使用11&13

安全共享數(shù)據(jù)

因?yàn)橹袛鄬和#瑹o論處理器正在處理該中斷如何,我們必須小心在中斷處理程序和循環(huán)中的代碼之間共享數(shù)據(jù)。

易失性變量

有時(shí),編譯器會(huì)嘗試優(yōu)化代碼以提高速度。有時(shí),這些優(yōu)化會(huì)將常用變量的副本保留在寄存器中以便快速訪問。問題是,如果這些變量之一在中斷處理程序和循環(huán)代碼之間共享,則其中一個(gè)變量最終可能會(huì)查看陳舊的副本而不是真實(shí)的副本。將變量標(biāo)記為易失性會(huì)告訴編譯器不要對(duì)優(yōu)化進(jìn)行潛在的危險(xiǎn)操作。

保護(hù)較大的變量

Evan標(biāo)記變量 volatile 是不夠的如果它的變量大于整數(shù)(例如字符串,數(shù)組,結(jié)構(gòu)等)。較大的變量需要幾個(gè)指令周期來更新,并且如果在更新的中間發(fā)生中斷,則數(shù)據(jù)可能會(huì)被破壞。如果您具有與中斷處理程序共享的較大變量或結(jié)構(gòu),則在從循環(huán)中更新中斷時(shí)應(yīng)禁用中斷。 (默認(rèn)情況下,已在中斷處理程序中禁用了中斷。)
責(zé)任編輯:wv

聲明:本文內(nèi)容及配圖由入駐作者撰寫或者入駐合作網(wǎng)站授權(quán)轉(zhuǎn)載。文章觀點(diǎn)僅代表作者本人,不代表電子發(fā)燒友網(wǎng)立場(chǎng)。文章及其配圖僅供工程師學(xué)習(xí)之用,如有內(nèi)容侵權(quán)或者其他違規(guī)問題,請(qǐng)聯(lián)系本站處理。 舉報(bào)投訴
  • 中斷
    +關(guān)注

    關(guān)注

    5

    文章

    904

    瀏覽量

    42494
  • 計(jì)時(shí)器
    +關(guān)注

    關(guān)注

    1

    文章

    428

    瀏覽量

    33439
  • Arduino
    +關(guān)注

    關(guān)注

    188

    文章

    6490

    瀏覽量

    190041
收藏 人收藏

    評(píng)論

    相關(guān)推薦
    熱點(diǎn)推薦

    EE-109:ADSP2106x:使用2106x SPORT作為計(jì)時(shí)器

    電子發(fā)燒友網(wǎng)站提供《EE-109:ADSP2106x:使用2106x SPORT作為計(jì)時(shí)器.pdf》資料免費(fèi)下載
    發(fā)表于 01-07 14:11 ?0次下載
    EE-109:ADSP2106x:使用2106x SPORT作為<b class='flag-5'>計(jì)時(shí)器</b>

    單個(gè) MSP430? 計(jì)時(shí)器模塊的多時(shí)基應(yīng)用說明

    電子發(fā)燒友網(wǎng)站提供《單個(gè) MSP430? 計(jì)時(shí)器模塊的多時(shí)基應(yīng)用說明.pdf》資料免費(fèi)下載
    發(fā)表于 09-13 11:09 ?0次下載
    單個(gè) MSP430? <b class='flag-5'>計(jì)時(shí)器</b>模塊的多時(shí)基應(yīng)用說明

    MSPM0-高級(jí)控制計(jì)時(shí)器有助于實(shí)現(xiàn)更好的控制和更好的數(shù)字輸出

    電子發(fā)燒友網(wǎng)站提供《MSPM0-高級(jí)控制計(jì)時(shí)器有助于實(shí)現(xiàn)更好的控制和更好的數(shù)字輸出.pdf》資料免費(fèi)下載
    發(fā)表于 08-28 11:30 ?0次下載
    MSPM0-高級(jí)控制<b class='flag-5'>計(jì)時(shí)器</b>有助于實(shí)現(xiàn)更好的控制和更好的數(shù)字輸出

    TLC555-Q1 LinCMOS?計(jì)時(shí)器數(shù)據(jù)表

    電子發(fā)燒友網(wǎng)站提供《TLC555-Q1 LinCMOS?計(jì)時(shí)器數(shù)據(jù)表.pdf》資料免費(fèi)下載
    發(fā)表于 08-23 11:19 ?0次下載
    TLC555-Q1 LinCMOS?<b class='flag-5'>計(jì)時(shí)器</b>數(shù)據(jù)表

    TLC555 LinCMOS?技術(shù)計(jì)時(shí)器數(shù)據(jù)表

    電子發(fā)燒友網(wǎng)站提供《TLC555 LinCMOS?技術(shù)計(jì)時(shí)器數(shù)據(jù)表.pdf》資料免費(fèi)下載
    發(fā)表于 08-20 11:15 ?3次下載
    TLC555 LinCMOS?技術(shù)<b class='flag-5'>計(jì)時(shí)器</b>數(shù)據(jù)表

    LMC555 CMOS計(jì)時(shí)器數(shù)據(jù)表

    電子發(fā)燒友網(wǎng)站提供《LMC555 CMOS計(jì)時(shí)器數(shù)據(jù)表.pdf》資料免費(fèi)下載
    發(fā)表于 08-20 09:16 ?1次下載
    LMC555 CMOS<b class='flag-5'>計(jì)時(shí)器</b>數(shù)據(jù)表

    CW32L083 IAP跳轉(zhuǎn)后中斷無響應(yīng)是怎么回事?

    最近做個(gè)項(xiàng)目,需要IAP。按照官方的教程一切順利,軟件APP跳轉(zhuǎn)一切正常,但是跳轉(zhuǎn)后中斷沒有響應(yīng)。搜索了堆資料,APP在mian中找開了
    發(fā)表于 07-26 07:17

    ESP8266EX連接到WIFI AP時(shí),是否需要執(zhí)行些特殊程序?

    ESP8266EX處于 STATION 模式。我正在使用 0.9.2 SDK 和 lubuntu 構(gòu)建。 1.當(dāng)模塊上電后首次連接到WIFI AP時(shí),一切正常。 2. 當(dāng)我關(guān)閉我的 WIFI
    發(fā)表于 07-15 07:57

    為什么ESP8266EX再次從WIFI AP獲取IP時(shí),它會(huì)在幾秒鐘后斷開TCP客戶端連接?

    我制作了個(gè)自定義固件連接到 TCP 套接字服務(wù),一切正常,直到 WIFI 失去連接并再次重新連接。我將嘗試解釋: 1. ESP8266EX啟動(dòng),當(dāng)它連接到我的 AP 時(shí)(我在
    發(fā)表于 07-15 06:17

    spi_flash期間的計(jì)時(shí)器中斷導(dǎo)致崩潰怎么解決?

    解決。但是,如果我已經(jīng)在 user_init() 中啟動(dòng)定時(shí)中斷,我會(huì)遇到類似的問題:如果我這樣做,esp 將重新啟動(dòng)。只有當(dāng)我在 ESP 連接到 wifi 網(wǎng)絡(luò)后啟用計(jì)時(shí)器中斷時(shí),
    發(fā)表于 07-12 11:54

    TLE986x如何定期重新啟動(dòng)計(jì)時(shí)器?

    我在模式 0-13 位定時(shí)模式下運(yùn)行 T3。 達(dá)到溢出時(shí),計(jì)時(shí)器停止。 請(qǐng)問如何定期重新啟動(dòng)計(jì)時(shí)器
    發(fā)表于 07-03 07:13

    雙路精密計(jì)時(shí)器選購(gòu)指南:準(zhǔn)確選擇,高效工作

    在快節(jié)奏的現(xiàn)代生活中,準(zhǔn)確的時(shí)間管理對(duì)于個(gè)人和團(tuán)隊(duì)的成功至關(guān)重要。雙路精密計(jì)時(shí)器作為種高效的計(jì)時(shí)工具,受到了越來越多人的青睞。那么,如何選購(gòu)款適合自己的雙路精密
    的頭像 發(fā)表于 06-26 16:06 ?588次閱讀

    SNx5DPHY440SS CSI-2/DSI DPHY 重計(jì)時(shí)器數(shù)據(jù)表

    電子發(fā)燒友網(wǎng)站提供《SNx5DPHY440SS CSI-2/DSI DPHY 重計(jì)時(shí)器數(shù)據(jù)表.pdf》資料免費(fèi)下載
    發(fā)表于 06-25 11:07 ?1次下載
    SNx5DPHY440SS CSI-2/DSI DPHY 重<b class='flag-5'>計(jì)時(shí)器</b>數(shù)據(jù)表

    使用psoc 4100s控制和capsense csx測(cè)量方法實(shí)現(xiàn)傳感,如何從Capsense調(diào)諧讀取idac值?

    我使用 psoc 4100s 控制和 capsense csx 測(cè)量方法實(shí)現(xiàn)傳感。 到目前為止,代碼和其他一切正常。 為了計(jì)算 idac 電流, ,我需要 idac 值和 ida
    發(fā)表于 05-30 06:08

    求助,關(guān)于PSoC 6上的ADC中斷問題求解

    ,除 adc 外一切正常。 問題: 我把問題縮小到應(yīng)用程序 cm4 側(cè)的 cyhal_adc_read() 函數(shù)上。 Adc init 正常,但程序在執(zhí)行該功能時(shí)停止。 中斷沒有啟動(dòng),但我不確定
    發(fā)表于 05-29 07:37