TVZ Mechatronics Team


Zagreb University of Applied Sciences, Professional Study in Mechatronics

You are viewing an older revision! See the latest version

Tajmeri i prekidi

U sljedećim vježbama naučit ćete koristiti klase Timer, Timeout, Ticker i InterruptIn iz standardne mbed biblioteke. Programski kôdovi su dijelom preuzeti iz preporučene literature: Toulson, R. & Wilmshurst, T. (2012). Fast and Effective Embedded Systems Design - Applying the ARM mbed, Newnes, Oxford, ISBN: 9780080977683.

Korištenje mbed klase Timer

Proučite API dokumentaciju klase Timer.

Proučite sljedeći programski kôd i pokušajte zaključiti što će se događati s LED-icama. Nakon toga testirajte program na mbed-u i provjerite da li su vaša predviđanja bila točna. Ako nisu, vratite se natrag na programski kôd i zaključite zašto.

#include "mbed.h"
Timer timer_fast;
Timer timer_slow;
DigitalOut ledA(LED1);
DigitalOut ledB(LED4);

void task_fast(void);
void task_slow(void);

int main() {
  timer_fast.start();
  timer_slow.start(); 
  while(true){
    if (timer_fast.read() > 0.2) {
      task_fast();
      timer_fast.reset();
    }
    if (timer_slow.read() > 1) {
      task_slow();
      timer_slow.reset();
    }
  }
}

void task_fast(void) {
  ledA = !ledA;
}
void task_slow(void) {
  ledB = !ledB;
}

Korištenje mbed klase Timeout

Proučite API dokumentaciju klase Timeout.

Proučite sljedeći programski kôd i pokušajte zaključiti što će se događati s LED-icama. Nakon toga testirajte program na mbed-u i provjerite da li su vaša predviđanja bila točna. Ako nisu, vratite se natrag na programski kôd i zaključite zašto.

#include "mbed.h"
Timeout response;
DigitalIn button (p14);
DigitalOut led1(LED1);
DigitalOut led2(LED2);
DigitalOut led3(LED3);

void blink() {
  led2 = 1;
  wait(0.5);
  led2 = 0;
}

int main() {
  while(true) {
    if(button == 1){
      response.attach(&blink, 3.0);
      led3=1;
    } else {
      led3=0;
    }
    led1=!led1;     
    wait(0.2);
  }
}

Modificirajte gornji programski kôd tako da dodate drugo tipkalo, čijim pritiskom se poništava pridruživanje blink() funkcije s objekta response (izvršite detach()).

Exercise 3: Using the mbed Ticker

Study the API documentation of the Ticker class.

Examine the following code and try to figure out what will happen with the LED. Test the program on the mbed and see if your predictions were correct.

#include "mbed.h"
void led_switch(void);
Ticker time_up;
DigitalOut myled(LED1);

void led_switch() {
    myled=!myled;       
}

int main(){
    time_up.attach(&led_switch, 0.2);
    while(true) {
        wait(1);
    }
}

Modify the above code to flash another LED every 1 second.

Exercise 4: Using the mbed InterruptIn

Study the API documentation of the InterruptIn class.

Examine the following code and try to figure out what will happen with the LEDs. Test the program on the mbed and see if your predictions were correct.

#include "mbed.h"
InterruptIn button(p14);
DigitalOut led(LED1);       
DigitalOut flash(LED4);

void isr1() {
  led = !led;
}

int main() {
  button.rise(&isr1);                              
  while(true) {
    flash = !flash;
    wait(0.2);
  }
}

Press the button multiple times and carefully observe the behavior of the LED1 when the button is pressed. Did you remember the bouncing effect?

Exercise 5: Switch debouncing

Modify the program from Exercise 4 to remove the bouncing effect, e.g. to debounce the button. Possible solution is given in the following code:

#include "mbed.h"
InterruptIn button(p14);
DigitalOut led(LED1);       
DigitalOut flash(LED4);
Timer debounce;

void isr1() {
  if (debounce.read_ms() > 200) {
    led = !led;
    debounce.reset();
  }
}

int main() {
  debounce.start();
  button.rise(&isr1);                              
  while(true) {
    flash = !flash;
    wait(0.2);
  }
}

Also read about a DebounceIn and PinDetect classes, which are written for the purpose of switch debouncing. A very nice tutorial about pushbutton and switches can be found here.

Exercise 6: Measuring distance using ultrasonic sensor HC-SR04

Write the simple program for measuring distance using an ultrasonic sensor HC-SR04.

One possible solution is given in the program bellow. Try to write your own solution first.

Import programHC-SR04

A program that demonstrates the development of library, using as an example an ultrasonic distance sensor HC-SR04.

Čestitke!

Završili ste sve vježbe iz teme Tajmeri i prekidi.

Povratak na naslovnu stranicu TVZ Mechatronics Team-a.


All wikipages