311
Dependencies: mbed
main.cpp@0:3be825a27f88, 2016-03-10 (annotated)
- Committer:
- HPWang
- Date:
- Thu Mar 10 19:53:25 2016 +0000
- Revision:
- 0:3be825a27f88
lab1 part1 test
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
HPWang | 0:3be825a27f88 | 1 | #include "mbed.h" |
HPWang | 0:3be825a27f88 | 2 | |
HPWang | 0:3be825a27f88 | 3 | PwmOut mypwm(PB_10); |
HPWang | 0:3be825a27f88 | 4 | DigitalOut led1(LED1);//LED1 = D13 = PA_5 (LED on Nucleo board) |
HPWang | 0:3be825a27f88 | 5 | Ticker timer1; |
HPWang | 0:3be825a27f88 | 6 | Ticker timer2; |
HPWang | 0:3be825a27f88 | 7 | |
HPWang | 0:3be825a27f88 | 8 | int a; |
HPWang | 0:3be825a27f88 | 9 | int b; |
HPWang | 0:3be825a27f88 | 10 | int c; |
HPWang | 0:3be825a27f88 | 11 | float pwm_duty ; |
HPWang | 0:3be825a27f88 | 12 | double angle ; |
HPWang | 0:3be825a27f88 | 13 | |
HPWang | 0:3be825a27f88 | 14 | void init_IO(void) |
HPWang | 0:3be825a27f88 | 15 | { |
HPWang | 0:3be825a27f88 | 16 | a = 0; |
HPWang | 0:3be825a27f88 | 17 | b = 0; |
HPWang | 0:3be825a27f88 | 18 | c = 0; |
HPWang | 0:3be825a27f88 | 19 | led1 = 0; |
HPWang | 0:3be825a27f88 | 20 | |
HPWang | 0:3be825a27f88 | 21 | } |
HPWang | 0:3be825a27f88 | 22 | |
HPWang | 0:3be825a27f88 | 23 | |
HPWang | 0:3be825a27f88 | 24 | void init_servo(void) |
HPWang | 0:3be825a27f88 | 25 | { |
HPWang | 0:3be825a27f88 | 26 | angle = 0; |
HPWang | 0:3be825a27f88 | 27 | pwm_duty = 0.079; |
HPWang | 0:3be825a27f88 | 28 | mypwm.period_ms(20); |
HPWang | 0:3be825a27f88 | 29 | mypwm.write(pwm_duty); |
HPWang | 0:3be825a27f88 | 30 | } |
HPWang | 0:3be825a27f88 | 31 | |
HPWang | 0:3be825a27f88 | 32 | |
HPWang | 0:3be825a27f88 | 33 | void timer1_interrupt(void) |
HPWang | 0:3be825a27f88 | 34 | { |
HPWang | 0:3be825a27f88 | 35 | a ++ ; |
HPWang | 0:3be825a27f88 | 36 | if (a == 500)led1 = 1; |
HPWang | 0:3be825a27f88 | 37 | |
HPWang | 0:3be825a27f88 | 38 | else if (a == 1000) |
HPWang | 0:3be825a27f88 | 39 | { |
HPWang | 0:3be825a27f88 | 40 | led1 = 0; |
HPWang | 0:3be825a27f88 | 41 | a = 0; |
HPWang | 0:3be825a27f88 | 42 | b = b+1; |
HPWang | 0:3be825a27f88 | 43 | } |
HPWang | 0:3be825a27f88 | 44 | } |
HPWang | 0:3be825a27f88 | 45 | |
HPWang | 0:3be825a27f88 | 46 | |
HPWang | 0:3be825a27f88 | 47 | void init_TIMER(void) |
HPWang | 0:3be825a27f88 | 48 | { |
HPWang | 0:3be825a27f88 | 49 | timer1.attach_us(&timer1_interrupt, 1000.0);//1ms interrupt period (1 KHz) |
HPWang | 0:3be825a27f88 | 50 | } |
HPWang | 0:3be825a27f88 | 51 | |
HPWang | 0:3be825a27f88 | 52 | |
HPWang | 0:3be825a27f88 | 53 | |
HPWang | 0:3be825a27f88 | 54 | |
HPWang | 0:3be825a27f88 | 55 | |
HPWang | 0:3be825a27f88 | 56 | |
HPWang | 0:3be825a27f88 | 57 | void run_servo(void) |
HPWang | 0:3be825a27f88 | 58 | { |
HPWang | 0:3be825a27f88 | 59 | angle = angle + c * 0.015; |
HPWang | 0:3be825a27f88 | 60 | pwm_duty = 0.079 +(0.084/180)*angle; |
HPWang | 0:3be825a27f88 | 61 | mypwm.write(pwm_duty); |
HPWang | 0:3be825a27f88 | 62 | c++; |
HPWang | 0:3be825a27f88 | 63 | if (c >6000) |
HPWang | 0:3be825a27f88 | 64 | { |
HPWang | 0:3be825a27f88 | 65 | timer2.detach(); |
HPWang | 0:3be825a27f88 | 66 | } |
HPWang | 0:3be825a27f88 | 67 | } |
HPWang | 0:3be825a27f88 | 68 | |
HPWang | 0:3be825a27f88 | 69 | void run_servo_counter(void) |
HPWang | 0:3be825a27f88 | 70 | { |
HPWang | 0:3be825a27f88 | 71 | timer2.attach_us(&run_servo, 1000.0);//1ms interrupt period (1 KHz) |
HPWang | 0:3be825a27f88 | 72 | } |
HPWang | 0:3be825a27f88 | 73 | |
HPWang | 0:3be825a27f88 | 74 | int main() |
HPWang | 0:3be825a27f88 | 75 | { |
HPWang | 0:3be825a27f88 | 76 | init_IO(); |
HPWang | 0:3be825a27f88 | 77 | init_servo(); |
HPWang | 0:3be825a27f88 | 78 | init_TIMER(); |
HPWang | 0:3be825a27f88 | 79 | |
HPWang | 0:3be825a27f88 | 80 | while(1) |
HPWang | 0:3be825a27f88 | 81 | { |
HPWang | 0:3be825a27f88 | 82 | |
HPWang | 0:3be825a27f88 | 83 | if (b >= 3) |
HPWang | 0:3be825a27f88 | 84 | { |
HPWang | 0:3be825a27f88 | 85 | run_servo_counter(); |
HPWang | 0:3be825a27f88 | 86 | } |
HPWang | 0:3be825a27f88 | 87 | |
HPWang | 0:3be825a27f88 | 88 | else if(b > 10) |
HPWang | 0:3be825a27f88 | 89 | { |
HPWang | 0:3be825a27f88 | 90 | timer1.detach(); |
HPWang | 0:3be825a27f88 | 91 | timer2.detach(); |
HPWang | 0:3be825a27f88 | 92 | wait(1); |
HPWang | 0:3be825a27f88 | 93 | init_IO(); |
HPWang | 0:3be825a27f88 | 94 | init_servo(); |
HPWang | 0:3be825a27f88 | 95 | wait(3); |
HPWang | 0:3be825a27f88 | 96 | init_TIMER() |
HPWang | 0:3be825a27f88 | 97 | |
HPWang | 0:3be825a27f88 | 98 | } |
HPWang | 0:3be825a27f88 | 99 | } |
HPWang | 0:3be825a27f88 | 100 | |
HPWang | 0:3be825a27f88 | 101 | } |
HPWang | 0:3be825a27f88 | 102 |