Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
main.cpp@1:d43df9a7cef2, 2020-10-31 (annotated)
- Committer:
- yeongsookim
- Date:
- Sat Oct 31 07:12:28 2020 +0000
- Revision:
- 1:d43df9a7cef2
- Parent:
- 0:3ead6014ad51
2020 Hall Sensor
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| yeongsookim | 0:3ead6014ad51 | 1 | #include "mbed.h" |
| yeongsookim | 0:3ead6014ad51 | 2 | #include "Motor.h" |
| yeongsookim | 0:3ead6014ad51 | 3 | #include "Plotting.h" |
| yeongsookim | 0:3ead6014ad51 | 4 | #include "HallSensor.h" |
| yeongsookim | 0:3ead6014ad51 | 5 | |
| yeongsookim | 0:3ead6014ad51 | 6 | |
| yeongsookim | 0:3ead6014ad51 | 7 | //To plot with usb, set as below. |
| yeongsookim | 0:3ead6014ad51 | 8 | Serial pc(USBTX,USBRX); // Tx, Rx Pin |
| yeongsookim | 0:3ead6014ad51 | 9 | Plotting plot; |
| yeongsookim | 0:3ead6014ad51 | 10 | |
| yeongsookim | 0:3ead6014ad51 | 11 | //Set each gpio to see the output of the hall sensor as a led |
| yeongsookim | 0:3ead6014ad51 | 12 | DigitalOut myled1(LED1); |
| yeongsookim | 0:3ead6014ad51 | 13 | |
| yeongsookim | 0:3ead6014ad51 | 14 | HallSensor hall (p29); |
| yeongsookim | 0:3ead6014ad51 | 15 | Motor motor (p23, p24, p25, p26); |
| yeongsookim | 0:3ead6014ad51 | 16 | |
| yeongsookim | 0:3ead6014ad51 | 17 | //Interrupt is generated every 1ms and count is increased by 1 |
| yeongsookim | 0:3ead6014ad51 | 18 | unsigned int uiFlag_1ms = 0; |
| yeongsookim | 0:3ead6014ad51 | 19 | unsigned int uiFlag_50ms = 0; |
| yeongsookim | 0:3ead6014ad51 | 20 | |
| yeongsookim | 0:3ead6014ad51 | 21 | void counter_1ms () |
| yeongsookim | 0:3ead6014ad51 | 22 | { |
| yeongsookim | 0:3ead6014ad51 | 23 | uiFlag_1ms++; |
| yeongsookim | 0:3ead6014ad51 | 24 | uiFlag_50ms++; |
| yeongsookim | 0:3ead6014ad51 | 25 | } |
| yeongsookim | 0:3ead6014ad51 | 26 | |
| yeongsookim | 0:3ead6014ad51 | 27 | int main() |
| yeongsookim | 0:3ead6014ad51 | 28 | { |
| yeongsookim | 0:3ead6014ad51 | 29 | wait(1); |
| yeongsookim | 0:3ead6014ad51 | 30 | |
| yeongsookim | 1:d43df9a7cef2 | 31 | |
| yeongsookim | 1:d43df9a7cef2 | 32 | ////Set the 1ms thicker. |
| yeongsookim | 0:3ead6014ad51 | 33 | Ticker ticker_1ms; |
| yeongsookim | 1:d43df9a7cef2 | 34 | ticker_1ms.attach(&counter_1ms, 0.001); |
| yeongsookim | 1:d43df9a7cef2 | 35 | |
| yeongsookim | 1:d43df9a7cef2 | 36 | //Set the motor to 10%. |
| yeongsookim | 1:d43df9a7cef2 | 37 | motor.setSpeed_percent(10.0, FORWARD); |
| yeongsookim | 0:3ead6014ad51 | 38 | |
| yeongsookim | 0:3ead6014ad51 | 39 | while(1) { |
| yeongsookim | 0:3ead6014ad51 | 40 | // Every 1 ms, |
| yeongsookim | 0:3ead6014ad51 | 41 | if(uiFlag_1ms >= 1) { |
| yeongsookim | 0:3ead6014ad51 | 42 | uiFlag_1ms = 0; |
| yeongsookim | 0:3ead6014ad51 | 43 | |
| yeongsookim | 0:3ead6014ad51 | 44 | //If A is 0, turn on the LED. |
| yeongsookim | 0:3ead6014ad51 | 45 | myled1 = hall.getPinState(); |
| yeongsookim | 0:3ead6014ad51 | 46 | } |
| yeongsookim | 0:3ead6014ad51 | 47 | |
| yeongsookim | 0:3ead6014ad51 | 48 | // Every 50 ms, |
| yeongsookim | 0:3ead6014ad51 | 49 | if(uiFlag_50ms >= 50) { |
| yeongsookim | 0:3ead6014ad51 | 50 | uiFlag_50ms = 0; |
| yeongsookim | 0:3ead6014ad51 | 51 | |
| yeongsookim | 0:3ead6014ad51 | 52 | // clear plotting buffer |
| yeongsookim | 0:3ead6014ad51 | 53 | plot.reset (); |
| yeongsookim | 0:3ead6014ad51 | 54 | |
| yeongsookim | 0:3ead6014ad51 | 55 | // put data to buffer |
| yeongsookim | 1:d43df9a7cef2 | 56 | plot.put(hall.getSpeed_rps(), 0); |
| yeongsookim | 0:3ead6014ad51 | 57 | |
| yeongsookim | 0:3ead6014ad51 | 58 | // send buffer |
| yeongsookim | 1:d43df9a7cef2 | 59 | plot.send(&pc); |
| yeongsookim | 0:3ead6014ad51 | 60 | } |
| yeongsookim | 0:3ead6014ad51 | 61 | } |
| yeongsookim | 0:3ead6014ad51 | 62 | } |