CuriosityでmTouchを試してみる。その3

その2ではmTouchの設定とピンアサインをMCCを使って行いました。

その3ではLEDの処理などを追加していきます。

まずはその2で生成されたファイルを眺めてみます。

結構たくさんあるみたいです。ファイル名のあとに "[Ignored]" とあるのはソースコード管理のための Mercurial でのステータスでしょう。

main.c を開いてみると、実際のコード部分はとてもシンプルです。
#include "mcc_generated_files/mcc.h"
/*
                         Main application
 */
void main(void)
{
    // initialize the device
    SYSTEM_Initialize();

    // When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits
    // Use the following macros to:

    // Enable the Global Interrupts
    //INTERRUPT_GlobalInterruptEnable();

    // Enable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptEnable();

    // Disable the Global Interrupts
    //INTERRUPT_GlobalInterruptDisable();

    // Disable the Peripheral Interrupts
    //INTERRUPT_PeripheralInterruptDisable();

    while (1)
    {
        // Add your application code
    }
}

まず、main()の while(1)の中にMOUTCH_Service_Mainloop();を追加します。
while (1)
      {
          // Add your application code
+        MOUTCH_Service_Mainloop();
      }

次に、main()の外にコールバック関数を書きます。
#include "mcc_generated_files/mcc.h"
  
+ void processButtonTouch(enum mtouch_button_names button)
+ {
+     switch(button)
+     {
+         case Button0: LED_SetLow();break;
+         default: break;
+     }
+ }
+  
+ void processButtonRelease(enum mtouch_button_names button)
+ {
+     switch(button)
+     {
+         case Button0: LED_SetHigh();break;
+         default: break;
+     }
+ }
ここで、"Button0" はもしもボタンの名称を変えていたらそれに合わせます。"LED" も同様です。LED_SetLow() / LED_SetHigh() などはMCCによって生成されていますので、そのまま使えます。

最後に、while(1)ループの前にコールバック関数へのフックを書きます。また、globalインタラプトとperipheralインタラプトを有効にします。
/*
                           Main application
   */
  void main(void)
  {
      // initialize the device
      SYSTEM_Initialize();
  
      // When using interrupts, you need to set the Global and Peripheral Interrupt Enable bits
      // Use the following macros to:
  
      // Enable the Global Interrupts
-     // INTERRUPT_GlobalInterruptEnable();
+     INTERRUPT_GlobalInterruptEnable();
  
      // Enable the Peripheral Interrupts
-     //INTERRUPT_PeripheralInterruptEnable();
+     INTERRUPT_PeripheralInterruptEnable();
  
      // Disable the Global Interrupts
      //INTERRUPT_GlobalInterruptDisable();
  
      // Disable the Peripheral Interrupts
      //INTERRUPT_PeripheralInterruptDisable();
  
+     MTOUCH_Button_SetPressedCallback(processButtonTouch);
+     MTOUCH_Button_SetNotPressedCallback(processButtonRelease);
  
      while (1)
      {
          // Add your application code
          MTOUCH_Service_Mainloop();
      }
  }
ソースの変更はここまでです。
早速ビルドしてみます。F11キーまたは "Production"→"Build Main Project" を実行します。エラーがなければ書き込みボタンを押します。最初にボード上のPIC24にファームウェアをダウンロードしたあとでPIC16F1619への書き込みが始まります。

その4へ続く

0 件のコメント:

コメントを投稿

WesternDigitalこわれた。

たぶん寿命を過ぎてるのでクレームとまではいかないのだけれど。 WesternDigitalのWD60EFRXが壊れました。モノは2014年11月に購入。ところが3年で壊れ、RMAにより2017年11月に交換したものです。BlackBlazeでも故障率がそれなりに高かったので、そう...