ndfweb.cn

Stm32-Arduino计时器中断示例程序


2020-02-23 02:24:37 (7518)


#define LED_PIN 13

#define BUTTON_PIN 38

#define LED_RATE 500000    // in microseconds; should give 0.5Hz toggles


void handler_led(void);

void handler_count1(void);

void handler_count2(void);


int toggle = 0;


int count1 = 0;

int count2 = 0;


void setup()

{

Serial.begin(115200); // 被Maple忽略.但使用硬件串行的主板需要通过 USB 到串行适配器

    // Set up the LED to blink 

    pinMode(LED_PIN, OUTPUT);


    // Set up BUT for input

    pinMode(BUTTON_PIN, INPUT_PULLUP);


    // Setup LED Timer

    Timer2.setMode(TIMER_CH1, TIMER_OUTPUTCOMPARE);

    Timer2.setPeriod(LED_RATE); // in microseconds

    Timer2.setCompare(TIMER_CH1, 1);      // overflow might be small

    Timer2.attachInterrupt(TIMER_CH1, handler_led);


    // Setup Counting Timers

    Timer3.setMode(TIMER_CH1, TIMER_OUTPUTCOMPARE);

    Timer4.setMode(TIMER_CH1, TIMER_OUTPUTCOMPARE);

    Timer3.pause();

    Timer4.pause();

    Timer3.setCount(0);

    Timer4.setCount(0);

    Timer3.setOverflow(30000);

    Timer4.setOverflow(30000);

    Timer3.setCompare(TIMER_CH1, 1000);   // somewhere in the middle

    Timer4.setCompare(TIMER_CH1, 1000);   

    Timer3.attachInterrupt(TIMER_CH1, handler1);

    Timer4.attachInterrupt(TIMER_CH1, handler2);

    Timer3.resume();

    Timer4.resume();


}


void loop() {


    // Display the running counts

    Serial.print("Count 1: "); 

    Serial.print(count1);

    Serial.print("\t\tCount 2: "); 

    Serial.println(count2);


    // Run... while BUT is held, pause Count2

    for(int i = 0; i<1000; i++) {

        if(digitalRead(BUTTON_PIN)) {

            Timer4.pause();

        } else {

            Timer4.resume();

        }

        delay(1);

    }

}


void handler_led(void) {

    toggle ^= 1;

    digitalWrite(LED_PIN, toggle);


void handler1(void) {

    count1++;

void handler2(void) {

    count2++;


了解更多请访问:http://www.ndfweb.cn/news-779.html
  NDF俱乐部
  国际域名注册
  建站咨询
合作伙伴:万网 | 新网 | 新网互联 NDF网站建设淘宝店 | 实用工具 | 外貿網站建設 | 联系我们
鲁公网安备 37110202000336号 鲁ICP备2021027697号-1 Sitemap - RSSRSS订阅