Friday, June 7, 2013

Microcontroller & DC Motor using L298

In this project we will control the speed and the direction of a DC motor using L298 (H-bridge)




Code:
void main() {
          unsigned char dutyCyle ;
          TRISB = 0 ; PORTB = 0 ;

          PWM1_Init(1000) ;
          PWM1_Set_Duty(0) ;
          PWM1_Start() ;

          while(1)
          {
              PORTB.F0 = 1 ; PORTB.F1 = 0 ;   // Direction

              for(dutyCyle = 0 ; dutyCyle < 254 ; dutyCyle++)   // Speed
              {
                           PWM1_Set_Duty(dutyCyle) ;
                           Delay_ms(20) ;

              }

              Delay_ms(500) ;

              PORTB.F0 = 0 ; PORTB.F1 = 1 ;   // Direction
              for(dutyCyle = 0 ; dutyCyle < 255 ; dutyCyle++)   // Speed
              {
                           PWM1_Set_Duty(dutyCyle) ;
                           Delay_ms(20) ;

              }
              Delay_ms(500) ;
          }
}

No comments:

Post a Comment

Related Posts Plugin for WordPress, Blogger...