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.
Fork of mbed-os-example-mbed5-blinky by
main.cpp@21:967504024346, 2018-10-04 (annotated)
- Committer:
- ykuroda
- Date:
- Thu Oct 04 02:05:55 2018 +0000
- Revision:
- 21:967504024346
- Parent:
- 20:13fcc82f4cab
- Child:
- 22:f0164a2a09b0
use RtosTimer
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ykuroda | 20:13fcc82f4cab | 1 | // mbed-os-io-control |
ykuroda | 20:13fcc82f4cab | 2 | // I/O制御プログラム(機械工学実験1) |
ykuroda | 19:9381d775bfc5 | 3 | // |
ykuroda | 20:13fcc82f4cab | 4 | // 20161027 ... v1.0 ... originally written by Y.Kuroda for Mechatronics |
ykuroda | 20:13fcc82f4cab | 5 | // 20180917 ... v2.0 ... customised for Jikken1(mbed LPC1768専用) |
ykuroda | 21:967504024346 | 6 | // 20181004 ... v2.1 ... RtosTimer |
ykuroda | 19:9381d775bfc5 | 7 | // |
Jonathan Austin |
0:2757d7abb7d9 | 8 | #include "mbed.h" |
ykuroda | 20:13fcc82f4cab | 9 | |
ykuroda | 20:13fcc82f4cab | 10 | const int DELTA_T = 1; // mainスレッドの時間間隔の設定[ms](デフォルトは1ms) |
ykuroda | 20:13fcc82f4cab | 11 | int delta_t = DELTA_T; |
ykuroda | 20:13fcc82f4cab | 12 | int qbits = 6; // 量子化シフト値 |
ykuroda | 20:13fcc82f4cab | 13 | const int SIG_MYT = 0x1; // signal番号 |
ykuroda | 20:13fcc82f4cab | 14 | DigitalOut led1(LED1); |
ykuroda | 20:13fcc82f4cab | 15 | DigitalOut led2(LED2); |
ykuroda | 20:13fcc82f4cab | 16 | DigitalOut led3(LED3); |
ykuroda | 20:13fcc82f4cab | 17 | DigitalOut led4(LED4); |
ykuroda | 20:13fcc82f4cab | 18 | |
ykuroda | 20:13fcc82f4cab | 19 | // p11,12 サンプリング周期設定ピン(デフォルトは0) |
ykuroda | 20:13fcc82f4cab | 20 | // p11上位ビット,p12下位ビット.0:1kHz, 1: 100Hz, 2: 10Hz, 3: 1Hz |
ykuroda | 20:13fcc82f4cab | 21 | InterruptIn samplingtime_MSB(p11); |
ykuroda | 20:13fcc82f4cab | 22 | InterruptIn samplingtime_LSB(p12); |
Jonathan Austin |
0:2757d7abb7d9 | 23 | |
ykuroda | 20:13fcc82f4cab | 24 | // p13,14 量子化粒度設定ピン(デフォルトは0) |
ykuroda | 20:13fcc82f4cab | 25 | // p13上位ビット,p14下位ビット.0:10bit, 1:8bit, 2:6bit, 3:4bit |
ykuroda | 20:13fcc82f4cab | 26 | InterruptIn quantization_MSB(p13); |
ykuroda | 20:13fcc82f4cab | 27 | InterruptIn quantization_LSB(p14); |
ykuroda | 20:13fcc82f4cab | 28 | |
ykuroda | 20:13fcc82f4cab | 29 | // アナログ入出力ピン(入出力共に0 - 3.3V) |
ykuroda | 20:13fcc82f4cab | 30 | AnalogOut aout(p18); |
ykuroda | 20:13fcc82f4cab | 31 | AnalogIn ain(p20); |
Jonathan Austin |
0:2757d7abb7d9 | 32 | |
ykuroda | 20:13fcc82f4cab | 33 | // デジタル入力ピン(割り込みにより実現) |
ykuroda | 20:13fcc82f4cab | 34 | InterruptIn din1(p21); |
ykuroda | 20:13fcc82f4cab | 35 | InterruptIn din2(p22); |
ykuroda | 20:13fcc82f4cab | 36 | |
ykuroda | 20:13fcc82f4cab | 37 | // 方形波,パルス波出力ピン |
ykuroda | 20:13fcc82f4cab | 38 | PwmOut squarewave(p23); |
ykuroda | 20:13fcc82f4cab | 39 | PwmOut pulsewave(p24); |
ykuroda | 20:13fcc82f4cab | 40 | |
ykuroda | 21:967504024346 | 41 | RtosTimer* rt; |
ykuroda | 21:967504024346 | 42 | Thread* thread_dt; // サンプリング処理スレッドを起動 |
ykuroda | 21:967504024346 | 43 | |
ykuroda | 20:13fcc82f4cab | 44 | // サンプリング処理をするスレッド(時間管理された処理) |
ykuroda | 20:13fcc82f4cab | 45 | void sampling_thread(const void* arg) { |
ykuroda | 19:9381d775bfc5 | 46 | while(true){ |
ykuroda | 19:9381d775bfc5 | 47 | Thread::signal_wait(SIG_MYT); // シグナルを待つ |
ykuroda | 20:13fcc82f4cab | 48 | unsigned short a_data = ain.read_u16(); // AD入力(ADは12ビット) |
ykuroda | 20:13fcc82f4cab | 49 | aout.write_u16((a_data>>qbits)<<qbits);// DA出力(量子化粒度はピンにより設定) |
ykuroda | 21:967504024346 | 50 | led4=0; |
Jonathan Austin |
0:2757d7abb7d9 | 51 | } |
Jonathan Austin |
0:2757d7abb7d9 | 52 | } |
Jonathan Austin |
1:846c97078558 | 53 | |
ykuroda | 20:13fcc82f4cab | 54 | // 量子化粒度設定ハンドラ |
ykuroda | 20:13fcc82f4cab | 55 | void qsize_handler(void) { |
ykuroda | 20:13fcc82f4cab | 56 | // 量子化設定ピン(p13,14)の設定を読んで量子化粒度を決定する |
ykuroda | 20:13fcc82f4cab | 57 | // 設定ピンの状態を読み取り,2ビット値に変換 |
ykuroda | 20:13fcc82f4cab | 58 | int quantization = (quantization_MSB<<1)&0x2|quantization_LSB&0x1; |
ykuroda | 20:13fcc82f4cab | 59 | qbits = 6+quantization*4; // 量子化サイズの決定 |
ykuroda | 20:13fcc82f4cab | 60 | // (デジタル出力時の量子化サイズは 0:1024, 1:256, 2:64, 3:16 steps) |
ykuroda | 20:13fcc82f4cab | 61 | } |
ykuroda | 20:13fcc82f4cab | 62 | |
ykuroda | 20:13fcc82f4cab | 63 | // サンプリングタイム設定割り込みハンドラ...時間設定ピンの状態が変化した時に呼び出される |
ykuroda | 20:13fcc82f4cab | 64 | void pinstate_handler(void) { |
ykuroda | 20:13fcc82f4cab | 65 | // ピンの状態を読み取り,2ビット値に変換 |
ykuroda | 20:13fcc82f4cab | 66 | int samplingtime = (samplingtime_MSB<<1)&0x2|samplingtime_LSB&0x1; |
ykuroda | 20:13fcc82f4cab | 67 | // 制御周期を10^nとする(n=0:1s, n=1:0.1s, n=2:0.01s, n=3:0.001s) |
ykuroda | 20:13fcc82f4cab | 68 | delta_t = (int)pow(10.0,(double)samplingtime); |
ykuroda | 20:13fcc82f4cab | 69 | squarewave.period_ms(delta_t); // 方形波の周期の設定 |
ykuroda | 20:13fcc82f4cab | 70 | pulsewave.period_ms(delta_t); // パルス波の周期の設定 |
ykuroda | 20:13fcc82f4cab | 71 | squarewave.write(0.5F); // duty比 |
ykuroda | 20:13fcc82f4cab | 72 | pulsewave.write(0.1F); // duty比 |
ykuroda | 21:967504024346 | 73 | rt->stop(); |
ykuroda | 21:967504024346 | 74 | rt->start(delta_t); |
ykuroda | 20:13fcc82f4cab | 75 | } |
ykuroda | 20:13fcc82f4cab | 76 | |
ykuroda | 20:13fcc82f4cab | 77 | // デジタル入力割り込みハンドラ...デジタル入力信号の状態が変化した時に呼び出される |
ykuroda | 20:13fcc82f4cab | 78 | void din_handler(void) { |
ykuroda | 20:13fcc82f4cab | 79 | led1 = din1; // ピンの状態をそのままLEDの点灯状態にする |
ykuroda | 20:13fcc82f4cab | 80 | led2 = din2; |
ykuroda | 20:13fcc82f4cab | 81 | } |
ykuroda | 19:9381d775bfc5 | 82 | |
ykuroda | 21:967504024346 | 83 | void rtos_catcher(void const* n) { |
ykuroda | 21:967504024346 | 84 | led4=1; |
ykuroda | 21:967504024346 | 85 | thread_dt->signal_set(SIG_MYT); // スレッドへ再開シグナルを送る |
ykuroda | 21:967504024346 | 86 | } |
ykuroda | 21:967504024346 | 87 | |
ykuroda | 19:9381d775bfc5 | 88 | int main() { |
ykuroda | 20:13fcc82f4cab | 89 | squarewave.period_ms(delta_t); // 初期周期の設定 |
ykuroda | 20:13fcc82f4cab | 90 | pulsewave.period_ms(delta_t); // 初期周期の設定 |
ykuroda | 20:13fcc82f4cab | 91 | squarewave.write(0.5F); // 初期duty比(方形波=50%) |
ykuroda | 20:13fcc82f4cab | 92 | pulsewave.write(0.1F); // 初期duty比(パルス波=10%) |
ykuroda | 20:13fcc82f4cab | 93 | |
ykuroda | 21:967504024346 | 94 | rt = new RtosTimer(rtos_catcher, osTimerPeriodic, (void*)0); |
ykuroda | 21:967504024346 | 95 | rt->start(delta_t); |
ykuroda | 21:967504024346 | 96 | |
ykuroda | 20:13fcc82f4cab | 97 | samplingtime_MSB.rise(pinstate_handler);// 周期設定ハンドラの設定. |
ykuroda | 20:13fcc82f4cab | 98 | samplingtime_MSB.fall(pinstate_handler);// 設定ピンの状態変化で |
ykuroda | 20:13fcc82f4cab | 99 | samplingtime_LSB.rise(pinstate_handler);// ハンドラが呼び出される |
ykuroda | 20:13fcc82f4cab | 100 | samplingtime_LSB.fall(pinstate_handler);// ようにする |
ykuroda | 20:13fcc82f4cab | 101 | quantization_MSB.rise(qsize_handler); // 量子化粒度設定ハンドラの設定 |
ykuroda | 20:13fcc82f4cab | 102 | quantization_MSB.fall(qsize_handler); |
ykuroda | 20:13fcc82f4cab | 103 | quantization_LSB.rise(qsize_handler); |
ykuroda | 20:13fcc82f4cab | 104 | quantization_LSB.fall(qsize_handler); |
ykuroda | 20:13fcc82f4cab | 105 | din1.rise(&din_handler); // デジタル入力ハンドラの設定 |
ykuroda | 20:13fcc82f4cab | 106 | din1.fall(&din_handler); // din1,2の状態変化でハンドラ |
ykuroda | 20:13fcc82f4cab | 107 | din2.rise(&din_handler); // が呼ばれる |
ykuroda | 20:13fcc82f4cab | 108 | din2.fall(&din_handler); |
ykuroda | 20:13fcc82f4cab | 109 | |
ykuroda | 21:967504024346 | 110 | thread_dt = new Thread(sampling_thread); // サンプリング処理スレッドを起動 |
ykuroda | 19:9381d775bfc5 | 111 | |
ykuroda | 19:9381d775bfc5 | 112 | while(true){ |
ykuroda | 20:13fcc82f4cab | 113 | Thread::wait(delta_t); // mainスレッドの時間間隔の設定[ms] |
ykuroda | 21:967504024346 | 114 | // thread_dt.signal_set(SIG_MYT); // スレッドへ再開シグナルを送る |
ykuroda | 21:967504024346 | 115 | // led4 = !led4; // 処理の先頭でLEDをを点滅 |
ykuroda | 19:9381d775bfc5 | 116 | } |
ykuroda | 20:13fcc82f4cab | 117 | } |