第一発目のソース
自分用。まだ満足に動きません。
[コードを表示する]
/** -----------------------------------------------------------------
 RCDI12F
 build:   2013/06/09 version 0.2
 author:  Rilassaru
------------------------------------------------------------------ */

#include 
#include 
#define _XTAL_FREQ 8000000 // 8MHz

#define TRUE 1
#define FALSE 0
#define UINT8 unsigned char
#define UINT16 unsigned int
#define UINT32 unsigned long

__CONFIG(
 FOSC_INTOSCIO & // 発振機 内臓利用
 WDTE_OFF &  // ウォッチドッグ 無効
 PWRTE_ON &  // パワーアップタイマー 有効
 BOREN_ON &  // ブラウンアウト検出 有効
 MCLRE_OFF &  // ~MCLR 無効
 CP_OFF &   // コードプロテクション 無効
 IESO_OFF &  // IEスイッチオーバー 無効
 FCMEN_OFF   // フェイルセーフクロック 無効
 );


UINT16 gRPM = 0;
UINT8  gPICStop = FALSE;
signed char  gAnGreen = 0;

#define MAX_RPM 15
#define MIN_RPM 0

#define DEG_MIN 0
#define DEG_MAX 29

// 進角させたい角度(BTDC) ただし、最小10度〜最大39度の範囲で設定
// 39-gtblBTDC[x]でgtblT2CONの横軸になる
const unsigned int gtblBTDC[16] = {
// 0    1    2    3    4    5    6    7    8    9   10   11   12   13   14   15 (Krpm)
  10,  10,  20,  20,  20,  19,  18,  17,  16,  14,  14,  10,  10,  10,  10,  10 };
//23,  23,  20,  15,  15,  15,  20,  22,  24,  26,  28,  30,  30,  30,  30,  30 };
const UINT8 gtblT2CON[16][30] = {
//        0          1          2          3          4          5          6          7          8          9         10         11         12         13         14         15         16         17         18         19         20         21         22         23         24         25         26         27         28         29
 {0b0011110, 0b0011110, 0b0011110, 0b0011110, 0b0011110, 0b0011110, 0b0011110, 0b0011110, 0b0011110, 0b0100110, 0b0100110, 0b0100110, 0b0100110, 0b0100110, 0b0100110, 0b0100110, 0b0100110, 0b0100110, 0b0100110, 0b0100110, 0b0100110, 0b0100110, 0b0101110, 0b0101110, 0b0101110, 0b0101110, 0b0101110, 0b0101110, 0b0101110, 0b0101110 }, 
 {0b0011110, 0b0011110, 0b0011110, 0b0011110, 0b0011110, 0b0011110, 0b0011110, 0b0011110, 0b0011110, 0b0100110, 0b0100110, 0b0100110, 0b0100110, 0b0100110, 0b0100110, 0b0100110, 0b0100110, 0b0100110, 0b0100110, 0b0100110, 0b0100110, 0b0100110, 0b0101110, 0b0101110, 0b0101110, 0b0101110, 0b0101110, 0b0101110, 0b0101110, 0b0101110 }, 
 {0b0101101, 0b0101101, 0b0101101, 0b0101101, 0b0101101, 0b0101101, 0b0110101, 0b0110101, 0b0110101, 0b0110101, 0b0110101, 0b0110101, 0b0111101, 0b0111101, 0b0111101, 0b0111101, 0b0111101, 0b0111101, 0b0111101, 0b1000101, 0b1000101, 0b1000101, 0b1000101, 0b1000101, 0b1000101, 0b1001101, 0b1001101, 0b1001101, 0b1001101, 0b1001101 }, 
 {0b0011101, 0b0011101, 0b0011101, 0b0011101, 0b0011101, 0b0011101, 0b0100101, 0b0100101, 0b0100101, 0b0100101, 0b0100101, 0b0100101, 0b0100101, 0b0100101, 0b0100101, 0b0101101, 0b0101101, 0b0101101, 0b0101101, 0b0101101, 0b0101101, 0b0101101, 0b0101101, 0b0101101, 0b0101101, 0b0110101, 0b0110101, 0b0110101, 0b0110101, 0b0110101 }, 
 {0b0010101, 0b0010101, 0b0010101, 0b0010101, 0b0010101, 0b0010101, 0b0011101, 0b0011101, 0b0011101, 0b0011101, 0b0011101, 0b0011101, 0b0011101, 0b0011101, 0b0011101, 0b0011101, 0b0011101, 0b0011101, 0b0100101, 0b0100101, 0b0100101, 0b0100101, 0b0100101, 0b0100101, 0b0100101, 0b0100101, 0b0100101, 0b0100101, 0b0100101, 0b0100101 }, 
 {0b0101100, 0b0101100, 0b0101100, 0b0101100, 0b0110100, 0b0110100, 0b0110100, 0b0110100, 0b0111100, 0b0111100, 0b0111100, 0b1000100, 0b1000100, 0b1000100, 0b1000100, 0b1001100, 0b1001100, 0b1001100, 0b1001100, 0b1010100, 0b1010100, 0b1010100, 0b1010100, 0b1011100, 0b1011100, 0b1011100, 0b1011100, 0b1100100, 0b1100100, 0b1100100 }, 
 {0b0011100, 0b0100100, 0b0100100, 0b0100100, 0b0100100, 0b0100100, 0b0101100, 0b0101100, 0b0101100, 0b0101100, 0b0101100, 0b0110100, 0b0110100, 0b0110100, 0b0110100, 0b0111100, 0b0111100, 0b0111100, 0b0111100, 0b0111100, 0b1000100, 0b1000100, 0b1000100, 0b1000100, 0b1001100, 0b1001100, 0b1001100, 0b1001100, 0b1001100, 0b1010100 }, 
 {0b0010100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0100100, 0b0100100, 0b0100100, 0b0100100, 0b0100100, 0b0101100, 0b0101100, 0b0101100, 0b0101100, 0b0101100, 0b0110100, 0b0110100, 0b0110100, 0b0110100, 0b0110100, 0b0110100, 0b0111100, 0b0111100, 0b0111100, 0b0111100, 0b0111100, 0b1000100, 0b1000100 }, 
 {0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0100100, 0b0100100, 0b0100100, 0b0100100, 0b0100100, 0b0100100, 0b0101100, 0b0101100, 0b0101100, 0b0101100, 0b0101100, 0b0101100, 0b0110100, 0b0110100, 0b0110100, 0b0110100, 0b0110100, 0b0110100 }, 
 {0b0001100, 0b0001100, 0b0001100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0100100, 0b0100100, 0b0100100, 0b0100100, 0b0100100, 0b0100100, 0b0100100, 0b0101100, 0b0101100, 0b0101100, 0b0101100, 0b0101100, 0b0101100 }, 
 {0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0100100, 0b0100100, 0b0100100, 0b0100100, 0b0100100, 0b0100100, 0b0100100 }, 
 {0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0100100, 0b0100100, 0b0100100, 0b0100100, 0b0100100 }, 
 {0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0100100, 0b0100100, 0b0100100 }, 
 {0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0100100 }, 
 {0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100 }, 
 {0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0001100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0010100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100, 0b0011100 }}; 
                              
const UINT8 gtblPR2[16][30] = {
 {194, 201, 208, 215, 222, 229, 236, 243, 250, 192, 197, 203, 208, 213, 218, 223, 229, 234, 239, 244, 250, 255, 208, 212, 216, 220, 225, 229, 233, 237 },
 {194, 201, 208, 215, 222, 229, 236, 243, 250, 192, 197, 203, 208, 213, 218, 223, 229, 234, 239, 244, 250, 255, 208, 212, 216, 220, 225, 229, 233, 237 },
 {208, 216, 225, 233, 241, 250, 215, 222, 229, 236, 243, 250, 220, 226, 232, 238, 244, 250, 255, 229, 234, 239, 244, 250, 255, 231, 236, 240, 245, 250 },
 {203, 212, 222, 231, 240, 250, 194, 201, 208, 215, 222, 229, 236, 243, 250, 205, 211, 216, 222, 227, 233, 238, 244, 250, 255, 217, 222, 226, 231, 236 },
 {197, 208, 218, 229, 239, 250, 173, 180, 187, 194, 201, 208, 215, 222, 229, 236, 243, 250, 192, 197, 203, 208, 213, 218, 223, 229, 234, 239, 244, 250 },
 {213, 226, 240, 253, 222, 233, 244, 255, 228, 238, 247, 225, 233, 241, 250, 229, 237, 244, 251, 233, 240, 246, 253, 236, 242, 248, 254, 238, 244, 250 },
 {240, 194, 208, 222, 236, 250, 211, 222, 233, 244, 255, 222, 231, 240, 250, 222, 230, 238, 246, 253, 229, 236, 243, 250, 228, 234, 240, 246, 253, 233 },
 {238, 174, 190, 206, 222, 238, 253, 202, 214, 226, 238, 250, 209, 219, 228, 238, 247, 214, 222, 230, 238, 246, 253, 224, 231, 238, 244, 251, 226, 232 },
 {145, 166, 187, 208, 229, 250, 180, 194, 208, 222, 236, 250, 197, 208, 218, 229, 239, 250, 208, 216, 225, 233, 241, 250, 215, 222, 229, 236, 243, 250 },
 {148, 185, 222, 129, 148, 166, 185, 203, 222, 240, 172, 185, 197, 209, 222, 234, 246, 194, 203, 212, 222, 231, 240, 250, 207, 214, 222, 229, 237, 244 },
 {33, 66, 100, 133, 166, 200, 233, 133, 150, 166, 183, 200, 216, 233, 250, 177, 188, 200, 211, 222, 233, 244, 255, 200, 208, 216, 225, 233, 241, 250 },
 {30, 60, 90, 121, 151, 181, 212, 242, 136, 151, 166, 181, 196, 212, 227, 242, 171, 181, 191, 202, 212, 222, 232, 242, 252, 196, 204, 212, 219, 227 },
 {27, 55, 83, 111, 138, 166, 194, 222, 250, 138, 152, 166, 180, 194, 208, 222, 236, 250, 175, 185, 194, 203, 212, 222, 231, 240, 250, 194, 201, 208 },
 {25, 51, 76, 102, 128, 153, 179, 205, 230, 128, 141, 153, 166, 179, 192, 205, 217, 230, 243, 170, 179, 188, 196, 205, 213, 222, 230, 239, 247, 192 },
 {23, 47, 71, 95, 119, 142, 166, 190, 214, 238, 130, 142, 154, 166, 178, 190, 202, 214, 226, 238, 250, 174, 182, 190, 198, 206, 214, 222, 230, 238 },
 {22, 44, 66, 88, 111, 133, 155, 177, 200, 222, 244, 133, 144, 155, 166, 177, 188, 200, 211, 222, 233, 244, 255, 177, 185, 192, 200, 207, 214, 222 }};

/** -----------------------------------------------------------------
           1|VDD    PIC12F683    VSS|8
  TO SCR<- 2|GP5(MC2)        GP0/AN0|7 <-Green analog
      X <- 3|GP4(MC1)        GP1/AN1|6 <-Yellow analog
      X <- 4|GP3/~MCLR  GP2/AN2/CCP1|5 <-PULSE (Input capture)
------------------------------------------------------------------ */

#define SCRGATE GP5

/** -----------------------------------------------------------------
 void main(void)
 メイン関数
------------------------------------------------------------------ */
void main(void)
{
 SCRGATE = 0;
 
 OPTION_REG = 0b10000010; // PUしない
 WPU        = 0b00000000; // PUしない
 OSCCON     = 0b01110000; // 8MHz
 TRISIO     = 0b00000111; // IN: GP0/GP1/GP2/ OUT:GP3/GP4/GP5
 ANSEL      = 0b01010011; // AN0/1選択、Tad(Fosc/16)=2.0us
 CMCON0     = 0x7;   // コンパレータ無効
 ADCON0     = 0b00000001; // 左詰、VDD、AN0利用
 //ADCON0     = 0b10000001; // 右詰、VDD、AN0利用
 //ADCON0     = 0b10000101; // 右詰、VDD、AN1利用

 // Input capture初期化(*1)
 CCP1CON = 0;   // 一度リセット
 CCP1CON = 0b00000101; // Every rising edge
 CCP1IF  = 0;   // 設定後は必ずIFをリセット

 // タイマー1初期化
 T1CON  = 0b00010100; // T1CKPS 1:2
 TMR1IF = 0;
 TMR1L  = 0;
 TMR1H  = 0;
 
 // タイマー2初期化
 T2CON = 0;
 TMR2IF = 0;
 PR2 = 0;
 
 // 割り込み初期化
 GIE = 1;
 PEIE = 1;
 TMR1IE = 1;  // Timer1 intterupt on
 TMR2IE = 1;
 TMR1ON = 1;  // Timer1 start
 CCP1IE = 1;  // Capture interrupt on
 
 // メインループ
 while(1) {
  GO_DONE = 1;  // 変換開始
  while(GO_DONE); // 変換待ち Tad*11=22us
  // 上位4ビット=16を取り出す
  gAnGreen = (signed char)(ADRESH >>4)-8; // Analog input
  if( ADRESH == 0 )
   gPICStop = TRUE;
  else
   gPICStop = FALSE;
 }
}



/** -----------------------------------------------------------------
  void interrupt ISR(void)
  割り込み制御関数
  Timer1、Captureを処理する

Piston -70-------------------00
PULSE   (-)               (+)
        00-10-20-30-40-50-60-70-80-90-...-180--270--360
TMR1     +------------------->|
TMR0     |------>| 30deg.(-40deg.)
------------------------------------------------------------------ */
void interrupt ISR(void)
{

 if(CCP1IF) {
  signed char deg;
  UINT16 ccpr1;
  // タイマーリセット
  TMR1H=0;
  TMR1L=0;
  
  if( FALSE == gPICStop ) {

   // 回転数算出
   ccpr1 = ((CCPR1H*0x100)|CCPR1L) + 1;
 
   gRPM = (unsigned)60000/ccpr1;
   if(gRPM > MAX_RPM) {
    gRPM = MAX_RPM;
   }
   deg = 40-gtblBTDC[gRPM] + gAnGreen;
 
   if( deg < DEG_MIN ) deg = DEG_MIN;
   if( deg > DEG_MAX ) deg = DEG_MAX;
 
   PR2   = gtblPR2[gRPM][deg];
   T2CON = gtblT2CON[gRPM][deg];
  } else {
   SCRGATE = FALSE;
  } 
  CCP1IF = 0;
 }

 // Timer1 タイムアウト→エンジン停止と判断
 if(TMR1IF) {
  gRPM = 0;
  TMR1IF = 0;
 }

 // Timer2 タイムアウト
 if(TMR2IF) {
  // TMR2を止める
  TMR2ON = 0;
  SCRGATE = 1;
  __delay_us(100);
  SCRGATE = 0;

  TMR2IF = 0;
 }

}