• 追加された行はこの色です。
  • 削除された行はこの色です。
 閲覧総計:&counter();  (本日:&counter(today);  昨日:&counter(yesterday);)~
 ~
 *自作my_PWM()関数の紹介 [#s4da44ff]
 
 左右2輪駆動の自動車をPWM制御しようとする際、PWM出力が4つ必要
 となる。PIC16F62Aはハード組み込みのPWM出力を1つしま持っておらず
 このニーズを満足しない。
 
 その為、モータをPWM制御する関数を作成し利用している。
 
 
 
 |&attachref(,zoom,150x150,button){新しい写真添付};|&attachref(,zoom,150x150,button){新しい写真添付};|
 
  /**
  * my_PWM関数
  *
  * PIC 16F628A
  * 発信器 4MHz
  * 言語 MikroC v2.1
  * Device Flags: _BODEN_OFF _BOREN_OFF _CP_OFF _PWRTE_ON
  *     _WDT_OFF _LVP_OFF _MCLRE_OFF _INTRC_OSC_NOCLKOUT
  *
  * モータ mabuchi FA-130RA(1.5V-3.0V)
  * モータドライバIC 東芝TA7291P
  * http://www.semicon.toshiba.co.jp/td/ja/Linear_ICs/Motor_Driver_ICs/20060307_TA7291SG_datasheet.pdf
  * PortB 2,3:モータ右
  * PortB 5,4:モータ左
  * 電源:PIC:電池4本 モータ:電池3本
  */
  
  //関数プロトタイプ宣言
  void my_PWM_RB2(unsigned short int duty);
  void my_PWM_RB3(unsigned short int duty);
  void my_PWM_RB4(unsigned short int duty);
  void my_PWM_RB5(unsigned short int duty);
  
  void main(){
      unsigned int i;
      unsigned int pause = 1000; //待機時間1秒
  
      //ポートの初期化
      PORTB = 0b00000000;
      //入出力の設定 1:input  0:output
      TRISB = 0b00000000; //PORTB 8ヶ全て出力に設定
  
      //LED点灯
      PORTB = 0b11111111;
      Delay_ms(1000);
      PORTB = 0b00000000;
  
      do {
          for(i = 0; i < 500; i++) { //5秒継続
              my_PWM_RB2(99); //99%右前進
              my_PWM_RB5(97); //97%左前進
          }
  
          for(i = 0; i < 500; i++) { //5秒継続
              my_PWM_RB3(99); //99%右後進
              my_PWM_RB4(97); //97%左後進
          }
  
          for(i = 0; i < 500; i++) { //5秒継続
              my_PWM_RB2(99); //99%右前進
              my_PWM_RB5(60); //60%左前進
          }
  
          for(i = 0; i < 500; i++) { //5秒継続
              my_PWM_RB3(70); //70%右後進
              my_PWM_RB4(90); //90%左後進・
          }
  
          PORTB = 0b00000000; //停止
          Vdelay_ms(pause); //待機
      } while(1);
  }
  
  
  /**
  * my_PWM_RB2()  機能:RB2の出力を0%-100%で制御する
  *
  * duty:範囲 0-100
  * 100ループで約1秒 @4MHz
  */
  void my_PWM_RB2(unsigned short int duty) {
      unsigned short int period = 100; //PWM分解能
      unsigned short int t_count;
  
      PORTB.F2 = 1; //ON
      for(t_count = 0; t_count < period; t_count++){
          if(t_count > duty - 1){
              PORTB.F2 = 0; //OFF
          }
          delay_us(10);
      }
  }
  
  void my_PWM_RB3(unsigned short int duty) {
      unsigned short int period = 100; //PWM分解能
      unsigned short int t_count;
  
      PORTB.F3 = 1; //ON
      for(t_count = 0; t_count < period; t_count++){
          if(t_count > duty - 1){
              PORTB.F3 = 0; //OFF
          }
          delay_us(10);
      }
  }
  
  void my_PWM_RB4(unsigned short int duty) {
      unsigned short int period = 100; //PWM分解能
      unsigned short int t_count;
  
      PORTB.F4 = 1; //ON
      for(t_count = 0; t_count < period; t_count++){
          if(t_count > duty - 1){
              PORTB.F4 = 0; //OFF
          }
          delay_us(10);
      }
  }
  
  void my_PWM_RB5(unsigned short int duty) {
      unsigned short int period = 100; //PWM分解能
      unsigned short int t_count;
  
      PORTB.F5 = 1; //ON
      for(t_count = 0; t_count < period; t_count++){
          if(t_count > duty - 1){
              PORTB.F5 = 0; //OFF
          }
          delay_us(10);
      }
  }
 
 #comment