メインプログラム:PWMCNT.c

2010.12 割込み処理をEvent.cで行うように修正しました。
プロセッサーエキスパートで設定した結果、下図の様なソースが生成されます。
初期設定は全て完了していますので割込み処理はEvent.c、MAIN関数の含まれているPWMCNT.cの記述すれば終わりです。

ロータリーエンコーダ割込関数

この関数はロータリーエンコーダーのB相が接続されているキャプチャ入力が変化したことによる割り込みで、ベクターテーブルにより指定されています。割り込み処理終了後、割り込み前の位置に戻ります。割込みにが発生すると以下の手順でイベント関数(Event.c)にプログラムポインターが移動するので割込み処理を記述する事になります。

  1. インプットキャプチャーで割込みが発生すると割込みベクタテーブル Vector.cにプログラムポインタが移動し、割込み原因Vtpm1ch0に割り当てられたRE_B_Interruptとなります。
  2. 割込みベクタからRE_B.cのISR(RE_B_Interrupt)に移動し、ここで割込みフラグがクリアされEvent.cのRE_B_OnCapture()関数へ移動します。
  3. Event.cのRE_B_OnCapture()関数の/*Write your code ... */以降に割込み処理を記述します。今回は外部変数 FLG をセットし、再び割込みを許可します。

【Event.c】
/* Write your code here ... */ 以降の3行だけが記述したコードで、それ以外は自動生成されたコードです。
extern char FLG;は外部変数宣言でFLGという変数はmain関数のあるPWMCNT.cで宣言された物を使用する事を意味します。
割込み処理はEvent.cで全て処理します。

/** ###################################################################
** Filename : Events.C
** Project : PWMCNT
** Processor : MC9S08SH8CPJ
** Beantype : Events
** Version : Driver 01.02
** Compiler : CodeWarrior HCS08 C Compiler
** Date/Time : 2009/08/28, 14:21
** Abstract :
** This is user's event module.
** Put your event handler code here.
** Settings :
** Contents :
** No public methods
**
** ###################################################################*/
/* MODULE Events */


#include "Cpu.h"
#include "Events.h"

/* User includes (#include below this line is not maintained by Processor Expert) */

/*
** ===================================================================
** Event : RE_B_OnCapture (module Events)
**
** Component : RE_B [Capture]
** Description :
** This event is called on capturing of Timer/Counter actual
** value (only when the bean is enabled - <Enable> and the
** events are enabled - <EnableEvent>.This event is available
** only if a <interrupt service/event> is enabled.
** Parameters : None
** Returns : Nothing
** ===================================================================
*/
void RE_B_OnCapture(void)
{
/* Write your code here ... */

extern char FLG;     // 外部変数宣言
FLG = 1;          // 変化フラグをセット
TPM1C0SC_CH0IE = 1; // 割り込み許可
}

/* END Events */

/*
** ###################################################################
**
** This file was created by Processor Expert 3.06 [04.26]
** for the Freescale HCS08 series of microcontrollers.
**
** ###################################################################
*/

【PWMCNT.c】

RE_Bが変化した事でFLGがセットされ、メインループでRE_AがHiかLowかで回転方向に合わせて、PWMの幅を増減します。インクリメントで一つづつ加減する事も出来ますが、サーボの変化量が小さいので100にしてあります。

/** ###################################################################
** Filename : PWMCNT.C
** Project : PWMCNT
** Processor : MC9S08SH8CPJ
** Version : Driver 01.11
** Compiler : CodeWarrior HCS08 C Compiler
** Date/Time : 2009/08/28, 14:21
** Abstract :
** Main module.
** This module contains user's application code.
** Settings :
** Contents :
** No public methods
**
** ###################################################################*/
/* MODULE PWMCNT */


/* Including needed modules to compile this module/procedure */
#include "Cpu.h"
#include "Events.h"
#include "RE_B.h"
#include "RE_A.h"
#include "PWM_OUT.h"
/* Include shared modules, which are used for whole project */
#include "PE_Types.h"
#include "PE_Error.h"
#include "PE_Const.h"
#include "IO_Map.h"

/* User includes (#include below this line is not maintained by Processor Expert) */

/****************************************************
* *
* Filename : PWMCNT.c *
* Version : 01.00 *
* Compiler : CodeWarrior HCS08 C Compiler *
* Date/Time : 2009/08/030, 0:00 *
* *
* PWM制御プログラム *
* *
* Copyright : n.Tanaka *
* *
****************************************************/

/***** 宣言部 *****/
char TRM = 0xA4;        // Memory address 00A0
char FTRM = 1;          // Memory address 00A1
unsigned int DATA = 6000;  // サーボ制御値(0.25uS*6000=1.5mS)
char FLG = 0;          // 変化フラグ

/***********
* MAIN関数 *
***********/
void main(void)
{
/* Write your local variable definition here */

/*** Processor Expert internal initialization. DON'T REMOVE THIS CODE!!! ***/
  PE_low_level_init();
/*** End of Processor Expert internal initialization. ***/

/* Write your code here */
/* For example: for(;;) { } */


/***** 初期設定 *****/
  ICSTRM = TRM;      // 内部基準クロックのトリム設定
  ICSSC_FTRIM = FTRM;  // 内部基準クロックのファイントリム設定
  TPM1C0SC_CH0IE = 1;  // タイマー1 割り込み許可

/***** メインループ *****/
  for(;;)
  {
    if(FLG)
    {
      if(PTCD_PTCD1 == PTCD_PTCD0)  // 回転方向判断 A=B 時計方向
      {
        if (TPM2C0V < 0xFAAD)      // 最大値制限
        {
          DATA = DATA + 100;
          TPM2C0V = DATA;       // Rch VR値をインクリメント
        }
      }
      else                    // A=!B 反時計方向
      {
        if (TPM2C0V > 0)         // 最小値制限
        {
          DATA = DATA - 100;
          TPM2C0V = DATA;      // Rch VR値をデクリメント
        }
      }
      FLG = 0;                // 変化フラグのクリア
    }
  }

/*** Don't write any code pass this line, or it will be deleted during code generation. ***/
/*** Processor Expert end of main routine. DON'T MODIFY THIS CODE!!! ***/
for(;;){}
/*** Processor Expert end of main routine. DON'T WRITE CODE BELOW!!! ***/
} /*** End of main routine. DO NOT MODIFY THIS TEXT!!! ***/

/* END PWMCNT */
/*
** ###################################################################
**
** This file was created by Processor Expert 3.06 [04.26]
** for the Freescale HCS08 series of microcontrollers.
**
** ###################################################################
*/

TOP BACK NEXT