解等例3-5

ex3-5.c

#include <t_services.h>
#include "kernel_id.h"
#include "ex3-5.h"

#define PB_DDR (*(volatile unsigned char *)0xFFFFD4) // アドレス0xFFFFD4へのポインタを定義
#define PB_DR (*(volatile unsigned char *)0xFFFFD6) // アドレス0xFFFFD6へのポインタを定義
#define PA_DDR (*(volatile unsigned char *)0xFFFFD1) // アドレス0xFFFFD1へのポインタを定義
#define PA_DR (*(volatile unsigned char *)0xFFFFD3) // アドレス0xFFFFD3へのポインタを定義

#define PTN (FLGPTN)0x01


#define S0 0
#define S1 1
#define S2 2
#define S3 3

// スイッチの指定
#define SW_PA0   0
#define SW_PA1   1
#define SW_PA2   2
#define SW_PA3   3

// スイッチON/OFF 判定
#define SW_ON(bit) (!(PA_DR & (0x1 << bit)))
#define SW_OFF(bit) (PA_DR & (0x1 << bit))

int detect_switch_on(int sw_no)
{
    static int status[4] = {S0};
    long int w;
 
    switch(status[sw_no])
 {
  case S0 :
            if (SW_ON(sw_no)) { // bitが0だったら、スイッチPA0が押されたと判断
    status[sw_no] = S1;
   }
   break;

  case S1 :
         for (w=0; w<9999; w++); // チャタリング対策のwaitを追加
   status[sw_no] = S2;
   break;
   
  case S2 :
      if (SW_OFF(sw_no))
      {
    status[sw_no] = S3;
   }   
   break;
   
  case S3 :
         for (w=0; w<9999; w++); // チャタリング対策のwaitを追加
   status[sw_no] = S0;
   return TRUE;
   break;
   
  default :
   status[sw_no] = S0;
   break;
 }
   
 return FALSE;
}


void main_task(VP_INT exinf) // メインタスク
{
 int sw = 0;
 
 PA_DDR = 0x00;  // Aポートを入力に設定
 PB_DDR = 0xFF;  // Bポートを出力に設定
 PB_DR = 0xFF; // LEDを全消灯
 
 act_tsk(TASK1); // タスクの起動
 act_tsk(TASK2); // タスクの起動
 sta_cyc(CYCHDR1);
 
 while (1) {
  if (detect_switch_on(SW_PA0)) {
   switch (sw & 0x1) {
    case 0x0 :  // PA1 pushed
     stp_cyc(CYCHDR1);
     break;
    case 0x1 :
     sta_cyc(CYCHDR1);
     break;
   }
   sw++;
  }
 }
}

void task1(VP_INT exinf)  // LED点滅タスク1
{
 int i;
 FLGPTN flgptn;
 
 while(1)
 {
  for(i=0; i<3; i++)
  {
   PB_DR &= ~0x1; // LED PB0 点灯
   dly_tsk(100); // 0.1秒待ち
   PB_DR |= 0x1; // LED PB0 消灯
   dly_tsk(100); // 0.1秒待ち
  }
  wai_flg(FLAG_ID1, PTN, TWF_ANDW, &flgptn); // イベントフラグPTN2待ち
 }
}


void task2(VP_INT exinf)  // LED点滅タスク1
{
 int i;
 FLGPTN flgptn;
 
 while(1)
 {
  for(i=0; i<3; i++)
  {
   PB_DR &= ~0x2; // LED PB3 点灯
   dly_tsk(270); // 0.27秒待ち
   PB_DR |= 0x2; // LED PB3 消灯
   dly_tsk(270); // 0.27秒待ち
  }
  wai_flg(FLAG_ID2, PTN, TWF_ANDW, &flgptn); // イベントフラグPTN2待ち
 }
}

void cyclic_handler(VP_INT exinf) // 周期ハンドラ
{
 static int c = 0x8;

 if (c==0x8) {
  iset_flg(FLAG_ID1, PTN);
  PB_DR |= c; // LEDの点滅
 } else {
  iset_flg(FLAG_ID2, PTN);
  PB_DR &= c;
 }
 c = ~c;

// PB_DR = ~PB_DR;
}

ex3-5.h

#include <t_services.h>

/* 各タスクの優先度の定義*/
#define MAIN_PRIORITY 5  /* メインタスクの優先度 */
        /* HIGH_PRIORITY より高くすること */
#define HIGH_PRIORITY 9  /* 並列に実行されるタスクの優先度 */
#define MID_PRIORITY 10
#define LOW_PRIORITY 11

#undef CPUEXC1    /* CPU例外ハンドラをサポートしない */

#ifndef TASK_PORTID
#define TASK_PORTID 1  /* 文字入力するシリアルポートID */
#endif /* TASK_PORTID */

#ifndef STACK_SIZE
#define STACK_SIZE 8192  /* タスクのスタックサイズ */
#endif /* STACK_SIZE */

#ifndef LOOP_REF
#define LOOP_REF 1000000  /* 速度計測用のループ回数 */
#endif /* LOOP_REF */

/* 関数のプロトタイプ宣言 */
#ifndef _MACRO_ONLY

extern void task1(VP_INT tskno);
extern void task2(VP_INT tskno);
extern void main_task(VP_INT exinf);
void cyclic_handler(VP_INT exinf);


#endif /* _MACRO_ONLY */

ex3-5.cfg

#define _MACRO_ONLY
#include "ex3-5.h"

INCLUDE("\"ex3-5.h\"");
CRE_TSK(MAIN_TASK, {TA_HLNG|TA_ACT, (VP_INT)0, main_task, LOW_PRIORITY, STACK_SIZE, NULL});
CRE_TSK(TASK1, {TA_HLNG, (VP_INT)1, task1, HIGH_PRIORITY, STACK_SIZE, NULL});
CRE_TSK(TASK2, {TA_HLNG, (VP_INT)2, task2, MID_PRIORITY, STACK_SIZE, NULL});
CRE_CYC(CYCHDR1, { TA_HLNG, (VP_INT)0, cyclic_handler, 1000, 0 });


#define FLAG_INIT (FLGPTN)0
CRE_FLG(FLAG_ID1, {TA_CLR, FLAG_INIT});
CRE_FLG(FLAG_ID2, {TA_CLR, FLAG_INIT});

#include "../../systask/timer.cfg"
#include "../../systask/serial.cfg"
#include "../../systask/logtask.cfg"

 

最終更新:2014年12月02日 13:54