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@0:cc171456477d, 2019-08-05 (annotated)
- Committer:
- QuangAnhLe
- Date:
- Mon Aug 05 17:49:57 2019 +0000
- Revision:
- 0:cc171456477d
1.commit
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| QuangAnhLe | 0:cc171456477d | 1 | #include "mbed.h" |
| QuangAnhLe | 0:cc171456477d | 2 | |
| QuangAnhLe | 0:cc171456477d | 3 | #define sqr(x) (x)*(x) |
| QuangAnhLe | 0:cc171456477d | 4 | #define abs(x) (x)<0 ? -(x):(x) |
| QuangAnhLe | 0:cc171456477d | 5 | Serial pc(PA_2,PA_3); |
| QuangAnhLe | 0:cc171456477d | 6 | |
| QuangAnhLe | 0:cc171456477d | 7 | // sin(x)0 x- (x^3/3!)+(x^5/5!)-(x^7/7!)+ (x^9/9!)- (x^11/!)+... |
| QuangAnhLe | 0:cc171456477d | 8 | float mysin(float x) |
| QuangAnhLe | 0:cc171456477d | 9 | |
| QuangAnhLe | 0:cc171456477d | 10 | { |
| QuangAnhLe | 0:cc171456477d | 11 | float sinx=x; |
| QuangAnhLe | 0:cc171456477d | 12 | float term=x; |
| QuangAnhLe | 0:cc171456477d | 13 | int step=2; |
| QuangAnhLe | 0:cc171456477d | 14 | int sign=-1; |
| QuangAnhLe | 0:cc171456477d | 15 | |
| QuangAnhLe | 0:cc171456477d | 16 | do |
| QuangAnhLe | 0:cc171456477d | 17 | { |
| QuangAnhLe | 0:cc171456477d | 18 | term *= sqr(x)/(step*(step+1)); |
| QuangAnhLe | 0:cc171456477d | 19 | sinx += term * sign; |
| QuangAnhLe | 0:cc171456477d | 20 | step += 2; |
| QuangAnhLe | 0:cc171456477d | 21 | sign *= -1; |
| QuangAnhLe | 0:cc171456477d | 22 | } |
| QuangAnhLe | 0:cc171456477d | 23 | while (abs (term) >0.00001F); |
| QuangAnhLe | 0:cc171456477d | 24 | |
| QuangAnhLe | 0:cc171456477d | 25 | return sinx; |
| QuangAnhLe | 0:cc171456477d | 26 | } |
| QuangAnhLe | 0:cc171456477d | 27 | |
| QuangAnhLe | 0:cc171456477d | 28 | |
| QuangAnhLe | 0:cc171456477d | 29 | int main() { |
| QuangAnhLe | 0:cc171456477d | 30 | |
| QuangAnhLe | 0:cc171456477d | 31 | |
| QuangAnhLe | 0:cc171456477d | 32 | float x= 0; |
| QuangAnhLe | 0:cc171456477d | 33 | |
| QuangAnhLe | 0:cc171456477d | 34 | pc.baud(115200); |
| QuangAnhLe | 0:cc171456477d | 35 | while(1) |
| QuangAnhLe | 0:cc171456477d | 36 | { |
| QuangAnhLe | 0:cc171456477d | 37 | |
| QuangAnhLe | 0:cc171456477d | 38 | pc.printf("#A%f\r\n",10*mysin(x)); |
| QuangAnhLe | 0:cc171456477d | 39 | |
| QuangAnhLe | 0:cc171456477d | 40 | x=x+0.01F; |
| QuangAnhLe | 0:cc171456477d | 41 | if (x > 6.28F) |
| QuangAnhLe | 0:cc171456477d | 42 | x=0; |
| QuangAnhLe | 0:cc171456477d | 43 | |
| QuangAnhLe | 0:cc171456477d | 44 | wait(0.1); |
| QuangAnhLe | 0:cc171456477d | 45 | } |
| QuangAnhLe | 0:cc171456477d | 46 | |
| QuangAnhLe | 0:cc171456477d | 47 | } |