目次
前
次
方向指示器実現
方向指示器としては、次のようにバイクや自動車の左右に
ランプが2つある仕様で考えます。
2つのLEDを利用すれば、次の回路で充分でしょう。
海上での船舶は、右舷に緑、左舷に赤の灯火を
用意する規定があります。船舶に使えるように
と考えておきます。
カウンタータイマーは、CT16B0を利用するとして
PIO0_8、PIO0_9にLEDを接続して、コンペアマッチ
による点滅を実現します。
状態値定義
左右のいずれか一方にクロックを出力すればよいので
ワード「flash」に出力する値で、制御すると考えて
おきます。
0 : 消灯
1 : 右点滅
2 : 左点滅
I/O操作
消灯では、PIO0_8、PIO0_9をGPIOに指定して
出力論理値を'0'にすればよいでしょう。
PIO0_8、PIO0_9をGPIOに設定するには、2つのSFRに
対しての値設定が必要になります。
それぞれのピンのGPIO選択は、以下。
\ define LABEL
$40044060 constant PIO0_8
$40044064 constant PIO0_9
\ Set P0.8 as GPIO
0 PIO0_8 !
\ Set P0.9 as GPIO
0 PIO0_9 !
ポート0に対しての論理値出力設定は、次のコードで
対応できます。
\ define LABEL
$50008000 constant GPIO0_DDR
\ Set P0.8 and P0.9 as Output
$300 GPIO0_DDR !
論理値の'0'を設定するには、全ピンに対しての
アクセスを考えればよいでしょう。
\ define LABEL
$50003FFC constant GPIO0_DAT
\ clear
0 GPIO0_DAT !
消灯に必要なコードが揃ったのでまとめます。
\ define LABEL
$40044060 constant PIO0_8
$40044064 constant PIO0_9
$50003FFC constant GPIO0_DAT
$50008000 constant GPIO0_DDR
: clear_led
0 PIO0_8 ! \ Set P0.8 as GPIO
0 PIO0_9 ! \ Set P0.9 as GPIO
$300 GPIO0_DDR ! \ Set P0.8 and P0.9 as Output
0 GPIO0_DAT ! \ clear
;
右点滅
点滅はコンペアマッチで実現します。
右はPIO0_8に接続されているので、コンペアマッチ
出力に設定しておかなければなりません。
\ define LABEL
$40044060 constant PIO0_8
\ Set P0.8 as Compare Match output
1 1 lsfhit PIO0_8 bis!
点滅には'1'と'0'を交互に出力すればよいので
MR0にDUTY比に相当する値を設定し、さらに
MCR、EMRにパラメータを設定します。
\ define LABEL
$4000C014 constant B0MCR
$4000C018 constant B0MR0
$4000C014 constant B0EMR
\ set MR0
500 B0MR0 !
\ set compare mactch output with MR0
1 7 lshift B0MCR bis!
\ set toggle mode
3 0 lshift B0EMR !
点滅を指定するワードを定義します。
\ define LABEL
$40044060 constant PIO0_8
$4000C014 constant B0MCR
$4000C018 constant B0MR0
$4000C014 constant B0EMR
: flash_right
\ Set P0.8 as Compare Match output
1 1 lsfhit PIO0_8 bis!
\ set MR0
500 B0MR0 !
\ set compare mactch output with MR0
1 7 lshift B0MCR bis!
\ set toggle mode
3 0 lshift B0EMR !
;
タイマーに関係する処理は、後で定義します。
左点滅
点滅はコンペアマッチで実現します。
左はPIO0_9に接続されているので、コンペアマッチ
出力に設定しておかなければなりません。それ以外
は、右の点滅とほぼ同じにするとよいでしょう。
\ define LABEL
$40044064 constant PIO0_9
$4000C014 constant B0MCR
$4000C018 constant B0MR0
$4000C014 constant B0EMR
: flash_left
\ Set P0.9 as Compare Match output
1 1 lsfhit PIO0_9 bis!
\ set MR1
500 B0MR1 !
\ set compare mactch output with MR1
1 7 lshift B0MCR bis!
\ set toggle mode
3 2 lshift B0EMR !
;
タイマーに関係する処理を考えます。
タイマー関係設定
ForthインタプリタをのせているLPC1114は、デフォルトで
12MHzを利用しているので、この12MHzをタイマーカウンタ
に供給しなければなりません。
CT16B0にクロックと電力を供給するので、次のコードを
実行すればよいでしょう。
\ SFR
$40048080 constant CLKCTRL
\ CT16B0 enable
1 7 lshift CLKCTRL bis!
プリスケーラとMR2にパラメータを設定して、点滅周期を
決めます。1Hzにすれば、トグル指定しているので
点滅としては、充分でしょう。
\ SFR
$40000004 constant B0TCR
$40000008 constant B0TC
$4000000C constant B0PR
$40000010 constant B0PC
$40000014 constant B0MCR
$40000018 constant B0MR0
$4000001C constant B0MR1
$40000020 constant B0MR2
\ Time Control Register disable
2 B0TCR !
\ MR2 -> Reset TC
1 7 lshift B0MCR bis!
\ Set Prescale Register
11999 B0PR !
\ 999 -> MR2
999 B0MR2 !
\ if TC = MR2 , send 'H' or 'L' to pin
3 7 lshift B0EMR !
\ start
1 B0TCR !
タイマーカウンタを動かすか、停止するかを指定するワードと
初期化をそれぞれ定義します。
\ SFR
$40048080 constant CLKCTRL
$40000004 constant B0TCR
$40000008 constant B0TC
$4000000C constant B0PR
$40000010 constant B0PC
$40000014 constant B0MCR
$40000018 constant B0MR0
$4000001C constant B0MR1
$40000020 constant B0MR2
: init_ct16b0
\ CT16B0 enable
1 7 lshift CLKCTRL bis!
\ MR2 -> Reset TC
1 7 lshift B0MCR bis!
\ Set Prescale Register
11999 B0PR !
\ 999 -> MR2
999 B0MR2 !
\ if TC = MR2 , send 'H' or 'L' to pin
3 7 lshift B0EMR !
;
: peform
0 > if 1 else 2 then B0TCR !
;
使うコードがすべて揃ったので、まとめます。
\ define LABEL
$40048080 constant CLKCTRL
$40044060 constant PIO0_8
$40044064 constant PIO0_9
$50003FFC constant GPIO0_DAT
$50008000 constant GPIO0_DDR
$40000004 constant B0TCR
$40000008 constant B0TC
$4000000C constant B0PR
$40000010 constant B0PC
$40000014 constant B0MCR
$4000C018 constant B0MR0
$4000001C constant B0MR1
$40000020 constant B0MR2
$4000C014 constant B0EMR
: clear_led
0 PIO0_8 ! \ Set P0.8 as GPIO
0 PIO0_9 ! \ Set P0.9 as GPIO
$300 GPIO0_DDR ! \ Set P0.8 and P0.9 as Output
0 GPIO0_DAT ! \ clear
;
: flash_right
\ Set P0.8 as Compare Match output
1 1 lsfhit PIO0_8 bis!
\ set MR0
500 B0MR0 !
\ set compare mactch output with MR0
1 7 lshift B0MCR bis!
\ set toggle mode
3 0 lshift B0EMR !
\ enable
1 perform
;
: flash_left
\ Set P0.9 as Compare Match output
1 1 lsfhit PIO0_9 bis!
\ set MR1
500 B0MR1 !
\ set compare mactch output with MR1
1 7 lshift B0MCR bis!
\ set toggle mode
3 2 lshift B0EMR !
\ enable
1 perform
;
: init_ct16b0
\ CT16B0 enable
1 7 lshift CLKCTRL bis!
\ MR2 -> Reset TC
1 7 lshift B0MCR bis!
\ Set Prescale Register
11999 B0PR !
\ 999 -> MR2
999 B0MR2 !
\ if TC = MR2 , send 'H' or 'L' to pin
3 7 lshift B0EMR !
;
: peform
0 > if 1 else 2 then B0TCR !
;
: init_flash
init_ct16b0
;
: flash
init_ct16b0
dup dup
0 = if 0 perform clear_led then
1 = if flash_right then
2 = if flash_left then
;
使い方は、次のようにタイプします。
\ initialize
init_flash {enter}
\ left blink
2 flash {enter}
\ stop blink
0 flash {enter}
\ right blink
1 flash {enter}
\ stop blink
0 flash {enter}
目次
前
次