test timer roboval 2018

Dependencies:   mbed

Committer:
fdalforno
Date:
Wed Dec 27 21:25:41 2017 +0000
Revision:
0:183e37532b4d
test nuovo timer

Who changed what in which revision?

UserRevisionLine numberNew contents of line
fdalforno 0:183e37532b4d 1 #include "mbed.h"
fdalforno 0:183e37532b4d 2
fdalforno 0:183e37532b4d 3 #include "mbed.h"
fdalforno 0:183e37532b4d 4
fdalforno 0:183e37532b4d 5 DigitalOut led(LED1);
fdalforno 0:183e37532b4d 6 InterruptIn timer(D11);
fdalforno 0:183e37532b4d 7 InterruptIn resetTimer(D7);
fdalforno 0:183e37532b4d 8
fdalforno 0:183e37532b4d 9 Timer t;
fdalforno 0:183e37532b4d 10
fdalforno 0:183e37532b4d 11 int last_read = 0;
fdalforno 0:183e37532b4d 12
fdalforno 0:183e37532b4d 13
fdalforno 0:183e37532b4d 14 typedef struct time_screen {
fdalforno 0:183e37532b4d 15 int cents;
fdalforno 0:183e37532b4d 16 int seconds;
fdalforno 0:183e37532b4d 17 int minutes;
fdalforno 0:183e37532b4d 18 } measured_time;
fdalforno 0:183e37532b4d 19
fdalforno 0:183e37532b4d 20 //Conversione da millisecondi a mm:ss:cc
fdalforno 0:183e37532b4d 21 measured_time human_read(int ms){
fdalforno 0:183e37532b4d 22 measured_time read;
fdalforno 0:183e37532b4d 23 div_t qr = div(ms,1000);
fdalforno 0:183e37532b4d 24
fdalforno 0:183e37532b4d 25 read.cents = qr.rem % 100;
fdalforno 0:183e37532b4d 26
fdalforno 0:183e37532b4d 27 qr = div(qr.quot,60);
fdalforno 0:183e37532b4d 28 read.seconds = qr.rem;
fdalforno 0:183e37532b4d 29
fdalforno 0:183e37532b4d 30 qr = div(qr.quot,60);
fdalforno 0:183e37532b4d 31 read.minutes = qr.rem;
fdalforno 0:183e37532b4d 32
fdalforno 0:183e37532b4d 33 return read;
fdalforno 0:183e37532b4d 34 }
fdalforno 0:183e37532b4d 35
fdalforno 0:183e37532b4d 36
fdalforno 0:183e37532b4d 37 void accendiled(){
fdalforno 0:183e37532b4d 38 int read = t.read_ms();
fdalforno 0:183e37532b4d 39 if(read - last_read > 5000){
fdalforno 0:183e37532b4d 40 int lap_time = read - last_read;
fdalforno 0:183e37532b4d 41 measured_time time = human_read(lap_time);
fdalforno 0:183e37532b4d 42 printf("Giro %02d:%02d:%02d \r\n",time.minutes,time.seconds,time.cents);
fdalforno 0:183e37532b4d 43 last_read = read;
fdalforno 0:183e37532b4d 44 }
fdalforno 0:183e37532b4d 45
fdalforno 0:183e37532b4d 46
fdalforno 0:183e37532b4d 47 }
fdalforno 0:183e37532b4d 48
fdalforno 0:183e37532b4d 49 void stopTimer(){
fdalforno 0:183e37532b4d 50 t.stop();
fdalforno 0:183e37532b4d 51
fdalforno 0:183e37532b4d 52 int read = t.read_ms();
fdalforno 0:183e37532b4d 53 measured_time time = human_read(read);
fdalforno 0:183e37532b4d 54 printf("Totale %02d:%02d:%02d \r\n",time.minutes,time.seconds,time.cents);
fdalforno 0:183e37532b4d 55
fdalforno 0:183e37532b4d 56 t.reset();
fdalforno 0:183e37532b4d 57 t.start();
fdalforno 0:183e37532b4d 58 }
fdalforno 0:183e37532b4d 59
fdalforno 0:183e37532b4d 60 int main() {
fdalforno 0:183e37532b4d 61 timer.mode(PullUp);
fdalforno 0:183e37532b4d 62 timer.rise(&accendiled);
fdalforno 0:183e37532b4d 63
fdalforno 0:183e37532b4d 64 resetTimer.mode(PullDown);
fdalforno 0:183e37532b4d 65 resetTimer.rise(&stopTimer);
fdalforno 0:183e37532b4d 66
fdalforno 0:183e37532b4d 67
fdalforno 0:183e37532b4d 68 t.start();
fdalforno 0:183e37532b4d 69
fdalforno 0:183e37532b4d 70 while(true) {
fdalforno 0:183e37532b4d 71 led = !led;
fdalforno 0:183e37532b4d 72 wait(0.3f);
fdalforno 0:183e37532b4d 73 }
fdalforno 0:183e37532b4d 74
fdalforno 0:183e37532b4d 75
fdalforno 0:183e37532b4d 76 }