// ストットルハイ側 // th_h_pwm.c // PIC12F683PWM機能使用 #include<12f683.h> #fuses INTRC_IO,NOWDT,PUT,NOPROTECT,NOMCLR,NOBROWNOUT //ウオッチドックタイマ無し、パワーアップタイマ使用 //プロテクト無し、MCLRなし #use delay(CLOCK=8000000) //8MHz駆動 #byte GP=5 #bit IRSIG=GP.3 #bit out2=GP.0 //out1 GP.2 pwm #define ON 1 #define OFF 0 #define HI 1 #define LOW 0 signed long th_count; //パルスカウント用 //メインルーチン void main() { int gcount; setup_comparator(NC_NC); //デジタル入力切り替え setup_ccp1(CCP_PWM); //PWMモード使用宣言 setup_timer_2(T2_DIV_BY_4,255,1); //CCP1 PWMモードセット //PWM周期=(255+1)*4*(1/8E6)*4=0.512ms=1.95KHz enable_interrupts(GLOBAL); //割り込み全体許可 set_tris_a(0x08);//GP3入力、他は出力 th_count=0; while(TRUE){//ギャップ検出ルーチン gcount=0; while(gcount < 30){ if(IRSIG==LOW){ delay_us(100); gcount++; } else{ gcount=0;} } While(IRSIG==LOW){} // 2ch(スロットル)パルス幅測定 th_count=0; while(IRSIG==HI){//Low、Hiループは1回10μSに調整済み delay_us(6); th_count=th_count+1; } //スロットルの処理 if(th_count<159){//最スロー センター1.56mssec max1.94,min1.16 th_count=0; out2=LOW; } else if(th_count>190){//フルスロットル th_count=1023; out2=HI; } else{ th_count=(th_count-158)*25;//PWM10段階(100 *10) out2=HI; } set_pwm1_duty(th_count); } }