Monday, February 8, 2016

DC Motor speed control with PIC16F84A microcontroller

Interfacing DC motor with PIC16F84A
DC motor speed can be easily controlled if the microcontroller has CCP module to generate PWM (Pulse Width Modulation) signal, the speed is controlled when the PWM signal duty cycle is varied and if the duty cycle is changed the power delivered to the motor will be changed also . The microcontroller PIC16F84A is a very old chip but it is good for beginners to start programming and electronics, it doesn't have CCP module and it has only 1 timer which is Timer0, this timer is an 8-bit timer. Using this timer we are going to see how to make a software PWM in order to control the DC motor.
DC motor speed control using PIC16F84A circuit schematic:
dc motor speed control software pwm technique pic16f84 pic microcontroller mikroc code 
In the circuit there are two buttons to increase and decrease the speed of the motor, these buttons are connected to RB0 and RB1 respectively.
There is an LED connected to RB2 used to indicate that the motor is at full speed.
The crystal frequency is 8MHz and the transistor is N-type mosfet.
DC motor speed control using PIC16F84A mikroC code:

 unsigned short duty = 0;
 void Interrupt(){
  if (TMR0IF_bit){
   if( PORTA.F0 ){
    TMR0 = duty;
     PORTA.F0 = 0;}
   else {
    TMR0 = (255 - duty) ;
    PORTA.F0 = 1; 
    }
   TMR0IF_bit         = 0;
   }
 }
 void main() {
  TRISA.F0 = 0;
  TRISB = 3;
  PORTA = 0;
  PORTB = 0;
  OPTION_REG         = 2;
  TMR0                 = 0;
  INTCON         = 0xA0;
  while(1){
   while(!PORTB.F0){
    if(duty > 254){
     duty = 254;
     PORTB.F2 =1;}
    duty++;
    delay_ms(100);
    }
   while(!PORTB.F1){
    if(duty < 1)
     duty = 1;
    duty--;
    PORTB.F2 =0;
    delay_ms(100);
    }
   }
  }

Project video: