Oliver Keller
/
Nucleo_timer_test
timer tests with the STM32F303K8 nucleo board
main.cpp@0:e40146aa84f7, 2016-06-10 (annotated)
- Committer:
- OliverKeller
- Date:
- Fri Jun 10 17:52:10 2016 +0000
- Revision:
- 0:e40146aa84f7
test to read pulse width in us on D1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
OliverKeller | 0:e40146aa84f7 | 1 | #include "mbed.h" |
OliverKeller | 0:e40146aa84f7 | 2 | |
OliverKeller | 0:e40146aa84f7 | 3 | Timer t; |
OliverKeller | 0:e40146aa84f7 | 4 | int rise_time = 0; |
OliverKeller | 0:e40146aa84f7 | 5 | int fall_time = 0; |
OliverKeller | 0:e40146aa84f7 | 6 | bool new_rise = false; |
OliverKeller | 0:e40146aa84f7 | 7 | bool new_fall = false; |
OliverKeller | 0:e40146aa84f7 | 8 | |
OliverKeller | 0:e40146aa84f7 | 9 | void pin_up(){ |
OliverKeller | 0:e40146aa84f7 | 10 | rise_time=t.read_us(); |
OliverKeller | 0:e40146aa84f7 | 11 | new_rise=true; |
OliverKeller | 0:e40146aa84f7 | 12 | } |
OliverKeller | 0:e40146aa84f7 | 13 | |
OliverKeller | 0:e40146aa84f7 | 14 | void pin_down(){ |
OliverKeller | 0:e40146aa84f7 | 15 | fall_time=t.read_us(); |
OliverKeller | 0:e40146aa84f7 | 16 | new_fall=true; |
OliverKeller | 0:e40146aa84f7 | 17 | } |
OliverKeller | 0:e40146aa84f7 | 18 | |
OliverKeller | 0:e40146aa84f7 | 19 | Serial pc(USBTX, USBRX); |
OliverKeller | 0:e40146aa84f7 | 20 | |
OliverKeller | 0:e40146aa84f7 | 21 | int main() { |
OliverKeller | 0:e40146aa84f7 | 22 | pc.baud(230400); |
OliverKeller | 0:e40146aa84f7 | 23 | t.start(); |
OliverKeller | 0:e40146aa84f7 | 24 | InterruptIn _interrupt(D1); |
OliverKeller | 0:e40146aa84f7 | 25 | _interrupt.rise(&pin_up); |
OliverKeller | 0:e40146aa84f7 | 26 | _interrupt.fall(&pin_down); |
OliverKeller | 0:e40146aa84f7 | 27 | pc.printf("Hello World!"); |
OliverKeller | 0:e40146aa84f7 | 28 | while(1) { |
OliverKeller | 0:e40146aa84f7 | 29 | if(new_rise && new_fall) { |
OliverKeller | 0:e40146aa84f7 | 30 | pc.printf("%d\n\r", fall_time - rise_time); |
OliverKeller | 0:e40146aa84f7 | 31 | new_rise=false; |
OliverKeller | 0:e40146aa84f7 | 32 | new_fall=false; |
OliverKeller | 0:e40146aa84f7 | 33 | } |
OliverKeller | 0:e40146aa84f7 | 34 | } |
OliverKeller | 0:e40146aa84f7 | 35 | } |