Saturday, April 19, 2014

Chapter 5 : Controlling DC Motor

Example of Circuit

//Exercise1Motor.c : Controlling Direction of MotorA and MotorB
#include <18f4550.h> // Header file
#fuses HS, NOWDT, NOPROTECT, NOLVP
#use delay (clock = 20M)
// Pin Definition
#define MOTORA_1     PIN_B0 //IN1
#define MOTORA_2     PIN_B1 //IN2
#define MOTORB_1     PIN_B2 //IN3
#define MOTORB_2     PIN_B3 //IN4
void main(){
     //Set PORTB as output
     set_tris_b(0x00);

     //Configure PWM Pinout
     setup_timer_2(T2_DIV_BY_4,254,1);//PWM OUTPUT CONFIGURATION
setup_ccp1(ccp_pwm); //PWM 1 DUTY CYCLE CONFIGURATION
setup_ccp2(ccp_pwm); //PWM 2 DUTY CYCLE CONFIGURATION
     while(true){
           //Turn On Motor A CW
           output_high(MOTORA_1);
           output_low(MOTORA_2);

           //Turn On Motor B CCW
           output_low(MOTORB_1);
           output_high(MOTORB_2);
     }
}


//*************************************************************

//Exercise2Motor.c : Controlling Speed of MotorA and MotorB
#include <18f4550.h7gt; // Header file
#fuses HS, NOWDT, NOPROTECT, NOLVP
#use delay (clock = 20M)
// Pin Definition
#define MOTORA_1     PIN_B0 //IN1
#define MOTORA_2     PIN_B1 //IN2
#define MOTORB_1     PIN_B2 //IN3
#define MOTORB_2     PIN_B3 //IN4
void main(){
     //Set PORTB as output
     set_tris_b(0x00);

     //Configure PWM Pinout
     setup_timer_2(T2_DIV_BY_4,254,1);//PWM OUTPUT CONFIGURATION
setup_ccp1(ccp_pwm); //PWM 1 DUTY CYCLE CONFIGURATION
setup_ccp2(ccp_pwm); //PWM 2 DUTY CYCLE CONFIGURATION
     while(true){
           //Turn On Motor A CW
           output_high(MOTORA_1);
           output_low(MOTORA_2);
           set_pwm1_duty(200); //Speed of MotorA

           //Turn On Motor B CCW
           output_low(MOTORB_1);
           output_high(MOTORB_2);
           set_pwm2_duty(150); //Speed of MotorB
     }
}


//*************************************************************

//Exercise3Motor.c : Controlling Motor using Buttons
#include <18f4550.h> // Header file
#fuses HS, NOWDT, NOPROTECT, NOLVP
#use delay (clock = 20M)
#include <lcd.c>
//Pin Definition for LCD
#define LCD_ENABLE_PIN     PIN_D0
#define LCD_RS_PIN         PIN_D1
#define LCD_RW_PIN         PIN_D2
#define LCD_DATA4          PIN_D4
#define LCD_DATA5          PIN_D5
#define LCD_DATA6          PIN_D6
#define LCD_DATA7          PIN_D7

//Pin Definition for Buttons
#define    BUTTON1    PIN_B4
#define    BUTTON2    PIN_B5

// Pin Definition For Motor
#define MOTORA_1     PIN_B0 //IN1
#define MOTORA_2     PIN_B1 //IN2
#define MOTORB_1     PIN_B2 //IN3
#define MOTORB_2     PIN_B3 //IN4
void main(){
     //Set PINB0-PINB3 as output PINB4-PINB7 as input
     set_tris_b(0xF0);

     lcd_init();

     //Configure PWM Pinout
     setup_timer_2(T2_DIV_BY_4,254,1);//PWM OUTPUT CONFIGURATION
setup_ccp1(ccp_pwm); //PWM 1 DUTY CYCLE CONFIGURATION
setup_ccp2(ccp_pwm); //PWM 2 DUTY CYCLE CONFIGURATION
     while(true){

           if(!input(BUTTON1)){
                //Turn On Motor A MotorB turn Off
                output_high(MOTORA_1);
                output_low(MOTORA_2);
                output_high(MOTORB_1);
                output_high(MOTORB_2);
                set_pwm1_duty(100); //Speed of MotorA
                printf(lcd_putc,”\fMOTORA Move\nMOTORB Stop”);
                delay_ms(100);
           }
           else if(!input(BUTTON2)){
                output_high(MOTORA_1);
                output_high(MOTORA_2);
                output_high(MOTORB_1);
                output_low(MOTORB_2);
                set_pwm2_duty(100);
                printf(lcd_putc,”\fMOTORA Stop\nMOTORB Move”);
                delay_ms(100);
           }
           else {
                output_high(MOTORA_1);
                output_high(MOTORA_2);
                output_high(MOTORB_1);
                output_high(MOTORB_2);
                printf(lcd_putc,”\fMOTORA Stop\nMOTORB Stop”);
                delay_ms(100);
           }
     }
}

//*************************************************************

//Exercise4Motor.c : Controlling Speed of Motor using Buttons
#include <18f4550.h> // Header file
#fuses HS, NOWDT, NOPROTECT, NOLVP
#use delay (clock = 20M)
#include <lcd.c>
//Pin Definition for LCD
#define LCD_ENABLE_PIN     PIN_D0
#define LCD_RS_PIN         PIN_D1
#define LCD_RW_PIN         PIN_D2
#define LCD_DATA4          PIN_D4
#define LCD_DATA5          PIN_D5
#define LCD_DATA6          PIN_D6
#define LCD_DATA7          PIN_D7

//Pin Definition for Buttons
#define    BUTTON1    PIN_B4
#define    BUTTON2    PIN_B5

// Pin Definition For Motor
#define MOTORA_1     PIN_B0 //IN1
#define MOTORA_2     PIN_B1 //IN2
#define MOTORB_1     PIN_B2 //IN3
#define MOTORB_2     PIN_B3 //IN4
void main(){
     int32 speed=50; //Speed of motor
     //Set PINB0-PINB3 as output PINB4-PINB7 as input
     set_tris_b(0xF0);

     lcd_init();

     //Configure PWM Pinout
     setup_timer_2(T2_DIV_BY_4,254,1);//PWM OUTPUT CONFIGURATION
setup_ccp1(ccp_pwm); //PWM 1 DUTY CYCLE CONFIGURATION
setup_ccp2(ccp_pwm); //PWM 2 DUTY CYCLE CONFIGURATION
     while(true){

           if(!input(BUTTON1)){
                speed++;
                output_high(MOTORA_1);
                output_low(MOTORA_2);
                output_high(MOTORB_1);
                output_low(MOTORB_2);
                set_pwm1_duty(speed); //Speed of MotorA
                set_pwm2_duty(speed);
                printf(lcd_putc,”\fSpeed = %ld”,speed);
                delay_ms(100);
           }
           else if(!input(BUTTON2)){
                speed--;
                output_high(MOTORA_1);
                output_low(MOTORA_2);
                output_high(MOTORB_1);
                output_low(MOTORB_2);
                set_pwm1_duty(speed); //Speed of MotorA
                set_pwm2_duty(speed);
                printf(lcd_putc,”\fSpeed = %ld”,speed);
                delay_ms(100);
           }
           if(speed>=200){
                speed=200;
           }
           else if(speed<=20){
                speed=20;
           }
     }
}


//*************************************************************
//Exercise5Motor.c : Controlling Speed of Motor using Potentiometer
#include <18f4550.h> // Header file
#fuses HS, NOWDT, NOPROTECT, NOLVP
#device ADC=10
#use delay (clock = 20M)
#include <lcd.c>
//Pin Definition for LCD
#define LCD_ENABLE_PIN     PIN_D0
#define LCD_RS_PIN         PIN_D1
#define LCD_RW_PIN         PIN_D2
#define LCD_DATA4          PIN_D4
#define LCD_DATA5          PIN_D5
#define LCD_DATA6          PIN_D6
#define LCD_DATA7          PIN_D7

//Pin Definition for Buttons
#define    BUTTON1    PIN_B4
#define    BUTTON2    PIN_B5

// Pin Definition For Motor
#define MOTORA_1     PIN_B0 //IN1
#define MOTORA_2     PIN_B1 //IN2
#define MOTORB_1     PIN_B2 //IN3
#define MOTORB_2     PIN_B3 //IN4
void main(){
     int32 speed=250; //Speed of motor
     int32 value;
     //Set PINB0-PINB3 as output PINB4-PINB7 as input
     set_tris_b(0xF0);
     lcd_init();

     //Configure Analog Input
     setup_adc_ports (ALL_ANALOG);
     setup_adc (ADC_CLOCK_INTERNAL);

     //Configure PWM Pinout
     setup_timer_2(T2_DIV_BY_4,254,1);//PWM OUTPUT CONFIGURATION
setup_ccp1(ccp_pwm); //PWM 1 DUTY CYCLE CONFIGURATION
setup_ccp2(ccp_pwm); //PWM 2 DUTY CYCLE CONFIGURATION
     while(true){

        set_adc_channel(2);
         value=read_adc();
         value=value*100/1023;
        
         if(value>=80)
         {           
            speed = speed+10;
            delay_ms(100);
         }
         else if (value<=30)   
         {
            speed = speed-10;
            delay_ms(100);
         }
        
         if (speed >= 100)
         {
            speed = 100;
         }
         else if(speed<=2)
         {
            speed=2;        
         }
        
         output_high(MOTORA_1);
         output_low(MOTORA_2);
         output_high(MOTORB_1);
         output_low(MOTORB_2);
         set_pwm1_duty(speed);
         set_pwm2_duty(speed);
         printf(lcd_putc,"\fSPEED %ld",speed);
         printf(lcd_putc,"\nPercentage=%d%%",(int)value);        
         delay_ms(10); 
   }
}