2016/03/06 servo Winston edit

Dependencies:   mbed

Committer:
winstonkuo
Date:
Sun Mar 06 13:38:44 2016 +0000
Revision:
0:428e046afc89
2016/03/06 servo by Winston

Who changed what in which revision?

UserRevisionLine numberNew contents of line
winstonkuo 0:428e046afc89 1 /*LAB_SERVO*/
winstonkuo 0:428e046afc89 2 #include "mbed.h"
winstonkuo 0:428e046afc89 3
winstonkuo 0:428e046afc89 4 PwmOut servo(A0);
winstonkuo 0:428e046afc89 5 PwmOut pwm1(D7);
winstonkuo 0:428e046afc89 6
winstonkuo 0:428e046afc89 7 DigitalOut myled(LED1);
winstonkuo 0:428e046afc89 8 DigitalOut led1(D13);
winstonkuo 0:428e046afc89 9 DigitalOut led2(D12);
winstonkuo 0:428e046afc89 10
winstonkuo 0:428e046afc89 11 Ticker timer1;
winstonkuo 0:428e046afc89 12 void timer1_interrupt(void);
winstonkuo 0:428e046afc89 13
winstonkuo 0:428e046afc89 14 void init_TIMER(void);
winstonkuo 0:428e046afc89 15 void init_IO(void);
winstonkuo 0:428e046afc89 16 void init_PWM(void);
winstonkuo 0:428e046afc89 17
winstonkuo 0:428e046afc89 18 float servo_duty = 0.058; //0.079 +(0.084/180)*angle, -90<angle<90
winstonkuo 0:428e046afc89 19 bool servo_flag = false;
winstonkuo 0:428e046afc89 20
winstonkuo 0:428e046afc89 21 int main()
winstonkuo 0:428e046afc89 22 {
winstonkuo 0:428e046afc89 23 init_TIMER();
winstonkuo 0:428e046afc89 24 init_IO();
winstonkuo 0:428e046afc89 25 init_PWM();
winstonkuo 0:428e046afc89 26
winstonkuo 0:428e046afc89 27 while(1)
winstonkuo 0:428e046afc89 28 {
winstonkuo 0:428e046afc89 29 servo_duty += 0.00007;
winstonkuo 0:428e046afc89 30
winstonkuo 0:428e046afc89 31 if(servo_flag == true)
winstonkuo 0:428e046afc89 32 {
winstonkuo 0:428e046afc89 33 servo.write(servo_duty);
winstonkuo 0:428e046afc89 34 servo_flag = false;
winstonkuo 0:428e046afc89 35 }
winstonkuo 0:428e046afc89 36
winstonkuo 0:428e046afc89 37 if(servo_duty > 0.1)
winstonkuo 0:428e046afc89 38 break;
winstonkuo 0:428e046afc89 39
winstonkuo 0:428e046afc89 40 }
winstonkuo 0:428e046afc89 41
winstonkuo 0:428e046afc89 42 }
winstonkuo 0:428e046afc89 43
winstonkuo 0:428e046afc89 44 void timer1_interrupt(void)
winstonkuo 0:428e046afc89 45 {
winstonkuo 0:428e046afc89 46 servo_flag = true;
winstonkuo 0:428e046afc89 47 }
winstonkuo 0:428e046afc89 48
winstonkuo 0:428e046afc89 49 void init_TIMER(void)
winstonkuo 0:428e046afc89 50 {
winstonkuo 0:428e046afc89 51 timer1.attach_us(&timer1_interrupt, 10000);//10ms interrupt period (100 Hz)
winstonkuo 0:428e046afc89 52 }
winstonkuo 0:428e046afc89 53
winstonkuo 0:428e046afc89 54 void init_IO(void)
winstonkuo 0:428e046afc89 55 {
winstonkuo 0:428e046afc89 56 led1 = 0;
winstonkuo 0:428e046afc89 57 led2 = 1;
winstonkuo 0:428e046afc89 58 }
winstonkuo 0:428e046afc89 59
winstonkuo 0:428e046afc89 60 void init_PWM(void)
winstonkuo 0:428e046afc89 61 {
winstonkuo 0:428e046afc89 62 servo.period_ms(20); //pwm period 20ms
winstonkuo 0:428e046afc89 63 servo.write(servo_duty);
winstonkuo 0:428e046afc89 64 //pwm1.period_us(50);
winstonkuo 0:428e046afc89 65 //pwm1.write(0.5);
winstonkuo 0:428e046afc89 66 //TIM1->CCER |= 0x4;
winstonkuo 0:428e046afc89 67 }