
stm32l010 pwm 1r
main.cpp@0:2758e5a0fd2c, 2021-05-15 (annotated)
- Committer:
- caa45040
- Date:
- Sat May 15 10:28:05 2021 +0000
- Revision:
- 0:2758e5a0fd2c
stm32l010 pwm 1
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
caa45040 | 0:2758e5a0fd2c | 1 | #include "mbed.h" |
caa45040 | 0:2758e5a0fd2c | 2 | |
caa45040 | 0:2758e5a0fd2c | 3 | //Serial pc(USBTX, USBRX); // tx, rx |
caa45040 | 0:2758e5a0fd2c | 4 | //Serial pc(SERIAL_TX, SERIAL_RX); //767 |
caa45040 | 0:2758e5a0fd2c | 5 | //Serial pc(PA_2, PA_3); //010 |
caa45040 | 0:2758e5a0fd2c | 6 | //Serial pc(PA_9, PA_10); //010 |
caa45040 | 0:2758e5a0fd2c | 7 | |
caa45040 | 0:2758e5a0fd2c | 8 | //data SG90 |
caa45040 | 0:2758e5a0fd2c | 9 | // 2400us 2.4ms |
caa45040 | 0:2758e5a0fd2c | 10 | #define s_max 2400 |
caa45040 | 0:2758e5a0fd2c | 11 | // 500us 0.5ms |
caa45040 | 0:2758e5a0fd2c | 12 | #define s_min 500 |
caa45040 | 0:2758e5a0fd2c | 13 | #define s_r (2400-500) |
caa45040 | 0:2758e5a0fd2c | 14 | #define s_c ((2400-500)/256) |
caa45040 | 0:2758e5a0fd2c | 15 | #define SN(n6) (500+(((2400-500)/256)*n6)) |
caa45040 | 0:2758e5a0fd2c | 16 | |
caa45040 | 0:2758e5a0fd2c | 17 | |
caa45040 | 0:2758e5a0fd2c | 18 | //PWMのピンの設定 |
caa45040 | 0:2758e5a0fd2c | 19 | //PwmOut servo(A0); // //767 |
caa45040 | 0:2758e5a0fd2c | 20 | PwmOut servo(A4); //PA_5 //010 |
caa45040 | 0:2758e5a0fd2c | 21 | |
caa45040 | 0:2758e5a0fd2c | 22 | int main() { |
caa45040 | 0:2758e5a0fd2c | 23 | |
caa45040 | 0:2758e5a0fd2c | 24 | //周期の設定 |
caa45040 | 0:2758e5a0fd2c | 25 | //servo.period(0.020); // servo requires a 20ms period |
caa45040 | 0:2758e5a0fd2c | 26 | servo.period_ms (20); |
caa45040 | 0:2758e5a0fd2c | 27 | |
caa45040 | 0:2758e5a0fd2c | 28 | //無限ループ |
caa45040 | 0:2758e5a0fd2c | 29 | while (1) { |
caa45040 | 0:2758e5a0fd2c | 30 | |
caa45040 | 0:2758e5a0fd2c | 31 | //0から256までループ |
caa45040 | 0:2758e5a0fd2c | 32 | //for(float offset=0.0; offset<0.001; offset+=0.0001) { |
caa45040 | 0:2758e5a0fd2c | 33 | for(int offset=0; offset<256; offset++) { |
caa45040 | 0:2758e5a0fd2c | 34 | |
caa45040 | 0:2758e5a0fd2c | 35 | //pc.printf(" -S=%d\t%d\r\n", offset , SN(offset) ); //767 |
caa45040 | 0:2758e5a0fd2c | 36 | |
caa45040 | 0:2758e5a0fd2c | 37 | //オン時間の設定 |
caa45040 | 0:2758e5a0fd2c | 38 | //servo.pulsewidth(0.001 + offset); // servo position determined by a pulsewidth between 1-2ms |
caa45040 | 0:2758e5a0fd2c | 39 | servo.pulsewidth_us( SN(offset) ); |
caa45040 | 0:2758e5a0fd2c | 40 | |
caa45040 | 0:2758e5a0fd2c | 41 | //250ミリ秒まつ |
caa45040 | 0:2758e5a0fd2c | 42 | //wait_ms(250); |
caa45040 | 0:2758e5a0fd2c | 43 | wait_ms(4); |
caa45040 | 0:2758e5a0fd2c | 44 | }//for |
caa45040 | 0:2758e5a0fd2c | 45 | }//while |
caa45040 | 0:2758e5a0fd2c | 46 | }//main |
caa45040 | 0:2758e5a0fd2c | 47 | |
caa45040 | 0:2758e5a0fd2c | 48 | //容量削減 |
caa45040 | 0:2758e5a0fd2c | 49 | void error(const char* format, ...){} |