アプリケーションノートミニマイコン評価カード CT-298 3 軸加速度センサーモジュール MM-2860 書込み済みマイコンプログラム通信コマンド概要 1. 概要 CT-298 DIP SF9S08C 3 MM-2860 HC9S08QG8-XYZ2_v1.1 PC PC PC HC9S08QG8-XYZ2_v1.1 CodeWorrior http://www.freescale.co.jp/products/8bit/9s08qg.html RD3112MMA7260Q_26JUL05.exe CT-2983 MM-2860 MM2860_SG043226.pdf 2. デモプログラムの通信フォーマットとコマンド 表 1 デモプログラムの通信フォーマット ボーレート 38,400 スタートビット 1 ビット データ長 8 ビット ストップビット 1 ビット パリティ なし CT-298 LED1 SW2 表 2 SW2 とデモプログラムの動作 リセット解除時の SW2 の状態押されている 押されていない デモプログラムの動作 FLASH の内容を読み取って前に実行されたアプリケーション ( Shock Detection Anti-Theft Battery Saver のいずれか) を実行する PC からキャラクタ 'R' を受信すると 'N' を送る 'N' キャラクタの送信後に 1 回 50ms 間ブザーを鳴らし LED2 を点滅する後は受信待ちとなり 送られてくるコマンド ( キャラクタ ) の内容に従って動作する PC PC PC 表 3 PC からの主なコマンドとデモプログラムの処理内容 コマンドデモプログラムの内容 V X Y Z 軸の AD 値を送る K キャリブレーションデータを FLASH から読み出し 送る k コマンド受信のあと さらにキャリブレーションデータを PC から受信し FLASH に書込む
表 4 送受信フォーマット コマンド受信データのフォーマット送信データのフォーマット ('' 内はキャラクタを示す ) バイト数 X 軸 Y 軸 Z 軸 V - 'x' 'y' 'z' - 6 AD 値 AD 値 AD 値 K - 'X' Xoff Xmax 'Y' Yoff Ymax 'Z' Zoff Zmax 9 k Xoff Xmax Yoff Ymax Zoff Zmax - 6 RD3112MMA7260Q_26JUL05.exe XoffYoff Zoff 0g XmaxYmaxZmax 1g 3. サンプルアプリケーション PC CT-298 + MM-2860 PC VKk 表 5 サンプルアプリケーション開発環境 OS Windows アプリケーション開発環境 Windows XP Professional Service Pack2 Microsoft Visual Basic 2005 ExpressEdition Version8.0.50727.762 Microsoft.NET Framework Version2.0.50727 図 1 サンプルアプリケーション実行画面
アプリケーションノート MM-2860 デモプログラムの評価方法 Form1 COM reflesh COM COM Public Class Form1 Dim portname As String = "com1" Dim baudrate As Integer = 38400 Dim parity As IO.Ports.Parity = IO.Ports.Parity.None Dim databits As Integer = 8 Dim stopbits As IO.Ports.StopBits = IO.Ports.StopBits.One Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load ComboBoxCom.Items.Clear() GetPortName() Private Sub reflesh_click(byval sender As System.Object, ByVal e As System.EventArgs) Handles reflesh.click ComboBoxCom.Items.Clear() GetPortName() Private Sub GetPortName() For Each sp As String In My.Computer.Ports.SerialPortNames ComboBoxCom.Items.Add(sp) Next Private Sub ComboBoxCom_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles ComboBoxCom.SelectedIndexChanged portname = ComboBoxCom.Text port_info.text = "COM : " & portname & vbnewline _ & "Boudrate : " & baudrate & vbnewline _ & "Parity : " & parity & vbnewline _ & "Bit Length : " & databits & vbnewline _ & "Stopbits : " & stopbits & vbnewline リスト 1 Form1.vb (1) 図 2 COM ポートの選択
R COM R NN Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click Dim Command As String = "R" Dim N As Integer Try Using com As IO.Ports.SerialPort = _ My.Computer.Ports.OpenSerialPort(portname, baudrate, parity, databits, stopbits) com.readtimeout = 500 com.write(command) N = com.readchar If (Convert.ToChar(N) = "N") Then Rcommand.Text = "N " Else Rcommand.Text = "N " End If End Using Catch ex As Exception MessageBox.Show(ex.Message, " ", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try リスト 2 Form1.vb (2) 図 3 R コマンド実行画面
アプリケーションノート MM-2860 デモプログラムの評価方法 V COM xyz 1 Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button4.Click Dim Command As String = "V" Dim buf As Byte() = {&H0} Dim bufx As Byte() = {&H0} Dim bufy As Byte() = {&H0} Dim bufz As Byte() = {&H0} Dim offset As Integer = 0 Dim count As Integer = 1 Try Using com As IO.Ports.SerialPort = _ My.Computer.Ports.OpenSerialPort(portname, baudrate, parity, databits, stopbits) com.readtimeout = 500 com.write(command) Loop While Convert.ToChar(buf(0)) <> "x" com.read(bufx, offset, count) Loop While Convert.ToChar(buf(0)) <> "y" com.read(bufy, offset, count) Loop While Convert.ToChar(buf(0)) <> "z" com.read(bufz, offset, count) x.text = bufx(0) y.text = bufy(0) z.text = bufz(0) End Using Catch ex As Exception MessageBox.Show(ex.Message, " ", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try リスト 3 Form1.vb (3) 図 4 V コマンド実行画面
k COM Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click Dim Command As String = "k" Dim sendbyte As Byte() = {&H0, &H0, &H0, &H0, &H0, &H0} Try sendbyte(0) = Convert.ToByte(xoff_box.Text) sendbyte(1) = Convert.ToByte(xmax_box.Text) sendbyte(2) = Convert.ToByte(yoff_box.Text) sendbyte(3) = Convert.ToByte(ymax_box.Text) sendbyte(4) = Convert.ToByte(zoff_box.Text) sendbyte(5) = Convert.ToByte(zmax_box.Text) Using com As IO.Ports.SerialPort = _ My.Computer.Ports.OpenSerialPort(portname, baudrate, parity, databits, stopbits) com.write(command) com.write(sendbyte, 0, 6) End Using Catch ex As Exception MessageBox.Show(ex.Message, " ", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try リスト 4 Form1.vb (4) 図 5 k コマンド実行画面
アプリケーションノート MM-2860 デモプログラムの評価方法 K V 2 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim Command As String = "K" Dim buf As Byte() = {&H0} Dim bufxo As Byte() = {&H0} Dim bufyo As Byte() = {&H0} Dim bufzo As Byte() = {&H0} Dim bufxm As Byte() = {&H0} Dim bufym As Byte() = {&H0} Dim bufzm As Byte() = {&H0} Dim offset As Integer = 0 Dim count As Integer = 1 Try Using com As IO.Ports.SerialPort = _ My.Computer.Ports.OpenSerialPort(portname, baudrate, parity, databits, stopbits) com.readtimeout = 500 com.write(command) Loop While Convert.ToChar(buf(0)) <> "X" com.read(bufxo, offset, count) com.read(bufxm, offset, count) Loop While Convert.ToChar(buf(0)) <> "Y" com.read(bufyo, offset, count) com.read(bufym, offset, count) Loop While Convert.ToChar(buf(0)) <> "Z" com.read(bufzo, offset, count) com.read(bufzm, offset, count) xoff_label.text = bufxo(0) xmax_label.text = bufxm(0) yoff_label.text = bufyo(0) ymax_label.text = bufym(0) zoff_label.text = bufzo(0) zmax_label.text = bufzm(0) End Using Catch ex As Exception MessageBox.Show(ex.Message, " ", MessageBoxButtons.OK, MessageBoxIcon.Error) End Try End Class リスト 5 Form1.vb (5)
図 6 K コマンド実行画面 HC08 Microsoft Windows Microsoft https:// www.sunhayato.co.jp/inquiry/ http://www.sunhayato.co.jp/ Copyright 2007 Sunhayato Corp. 2007 年 7 月 1 日発行 REV.1.10 SG043253 ホームページ :www.sunhayato.co.jp