I am unable to drive this motor driver controller using pic its not to switch the motor simulation attached code is given below,

thunaivendan

Member level 3
Joined
Apr 19, 2013
Messages
61
Helped
0
Reputation
0
Reaction score
1
Trophy points
1,288
Location
Chennai, Tamil Nadu, India
Activity points
1,723


Code:
/* Main.c file generated by New Project wizard
 *
 * Created:   Tue Jun 4 2024
 * Processor: PIC16F877A
 * Compiler:  HI-TECH C for PIC10/12/16
 */

#include <htc.h>
#include <stdio.h>
#include <stdlib.h>
#define QA1 RA0
#define QA2 RA1
#define QA3 RA2
#define QA4 RA3
#define SW RE0
#define SW1 RE1
#define _XTAL_FREQ 20000000
//__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_OFF & LVP_ON & CPD_OFF & WRT_OFF & CP_OFF);
__CONFIG(0x3F72);
//void forwarddirection();
//void reversedirection();

void main()
{
    TRISA = 0x00;
    TRISE = 0x00;
//   TRISE = 0x00;
 while(1)
    {
     if (SW == 1)
        {
          QA1 = 1;
          __delay_ms(1); 
          QA2 = 0;
          __delay_ms(1);
          QA1 = 1;
          __delay_ms(1); 
          QA2 = 0;
          __delay_ms(1); 
          
        }
        if (SW1 == 1)
        {
             QA2 = 1;
          __delay_ms(1); 
          QA1 = 0;
          __delay_ms(1);
          QA2 = 1;
          __delay_ms(1); 
          QA1 = 0;
          __delay_ms(1); 
          
           }      
     }
 }
--- Updated ---

Please provide the simulation solution
 
Last edited by a moderator:

It isn't at all clear what you are trying to do. Your "while" loop just makes RA1 high and RA2 low, regardless of SW1. The bootstrap configuration of the IR2112 relies on it being driven at frequency high enough that C3/C5 can maintain enough voltage to fully turn on the top MOSFETS on but you drive it with DC and there seems to be no supply to the IR2112s anyway.

Brian.
 

I gave the D.C voltage 15 v to VC pin
--- Updated ---

I gave the D.C voltage 15 v to VC pin
My intention if switch pressed dc motor should forward,if second switch dc motor reverse biased,please tell me is this correct.what are all motor should connect to this circuit
 

There are several problems in your electrical design and your software.
Hardware first:

The IR2112 is designed to be driven from an alternating logic level, usually a PWM signal. The VB pin has the supply to the top gate driver on it. They way it turns the top MOSFET on is to generate the VB voltage by charging C5 through D1 when the negative end of C5 is near ground voltage (bottom MOSFET turned on) then when the bottom MOSFET turns off, VS rises and 'pushes' the charge on C5 to the VB pin. Note that when this happens, D1 is reverse biased so it doesn't conduct back to the supply line. If you don't drive the IR2112 with a high frequency, C5 will never charge enough to produce VB.

I would also suggest increasing R5 and R6 to at least 10K, otherwise you waste 5mA through them when the switches are closed. Also add a pull-up resistor and a capacitor to ground on the MCLR pin (as shown in the data sheet) so the reset is delays slightly after the supply is switched on. It also lets you reset the PIC by pulling MCLR low without shorting out the supply.

Software:
Your while{} loop tests SW1 twice for being high (switch not pressed), I think you intended to test the other switch (SW) instead. Even then, you should not toggle both QA1 and QA2 twice, just set them to the required state once. It would be wise to add code to check both switches though, think what happens if both are pressed at the same time.

Brian.
 

Hi Brian,
i slightly modified and circuit and Software:
in circuit i added the supply voltage as VCC whereever applicable,and as you requested change the PIN MCLR as per data sheet,but SW(RE0) is not changed to high see the simulation and while pressing SW1(RE1) switching is not happen.motor is not running.
other Than anything i need to modify,

in Code i made below:
Could you please ensure the its correct



/* Main.c file generated by New Project wizard
*
* Created: Tue Jun 4 2024
* Processor: PIC16F877A
* Compiler: HI-TECH C for PIC10/12/16
*/

#include <htc.h>
#include <stdio.h>
#include <stdlib.h>
#define QA1 RA0
#define QA2 RA1
//#define QA3 RA2
//#define QA4 RA3
#define SW RE0
#define SW1 RE1
#define _XTAL_FREQ 20000000
//__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_OFF & LVP_ON & CPD_OFF & WRT_OFF & CP_OFF);
__CONFIG(0x3F72);
//void forwarddirection();
//void reversedirection();
void main()
{
TRISA = 0x00;
TRISE = 0x00;
// TRISE = 0x00;
while(1)
{
SW=0;SW1=0;
if (SW == 1)
{
QA1 = 1;
__delay_ms(1);
QA1= 0;
__delay_ms(1);
QA2= 1;
__delay_ms(1);
QA2 = 0;
__delay_ms(1);

}
if (SW1 == 1)
{
QA2 = 1;
__delay_ms(1);
QA2 = 0;
__delay_ms(1);
QA1= 1;
__delay_ms(1);
QA1 = 0;
__delay_ms(1);

}
}
}
--- Updated ---

please see this switch(SW) is highlighted as low is like SW1,clarify this too as well brian
 
Last edited:

Hi,

when posting code....
* please use the [Insert Code] button marked with [ </> ]
* please document your code (if you are too lazy to write what the code is supposed to do ... why should we spend time to find out the same?)
* use meaningful directives ...

like:
Code:
#define released 0
#define pressed 1
#define SW_Fwd RE0
#define SW_Rev RE1

then the lines:
***
Code:
if (SW == 1)
   {
   ...
   }
if (SW1 == 1)
   {
   ...
   }

become meaningful:
Code:
if (SW_Fwd == pressed)
   {
   ...
   }
if (SW_Rev == pressed)
   {
   ...
   }

Please explain line:
"SW=0;SW1=0;"

BTW:
The generation of the bridge control signal in a while loop is the worst yo can do. There should be PWM, or at least timer interrupts in your PIC)
It changes behaviour (timing) with every line of code you add. It seems you don´t know how it works (otherwise you should give a timing diagram of how you expect the signals to look like ... so we can validate it)
Do you uderstand the meaning of slow_decay and fast_decay ... and how it influences RPM, torque and braking (current). If not: read about it.

Klaus
 
Last edited:


C:
#include <htc.h>
#include <stdio.h>
#include <stdlib.h>
#define release 0
#define pressed 1
#define QA1 RA0
#define QA2 RA1
//#define QA3 RA2
//#define QA4 RA3
#define SW_Fwd RE0
#define SW_Rev RE1
#define _XTAL_FREQ 20000000
//__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_OFF & LVP_ON & CPD_OFF & WRT_OFF & CP_OFF);
__CONFIG(0x3F72);
//void forwarddirection();
//void reversedirection();
void main()
{
    TRISA = 0x00;
    TRISE = 0x00;
//   TRISE = 0x00;
  //   SW=0;SW1=0;
 while(1)
    {
    
     if (SW_Fwd== pressed)
        {
          QA1 = 1;
          __delay_ms(1);
          QA2= 0;
          __delay_ms(1);
        QA1= 1;
        __delay_ms(1);
              QA2 = 0;
         __delay_ms(1);
          
        }
      if (SW_Rev  == pressed)
      {
             QA2 = 1;
          __delay_ms(1);
          QA1 = 0;
          __delay_ms(1);
                    QA2= 1;
               __delay_ms(1);
          QA1 = 0;
          __delay_ms(1);
        
           }     
     }
 }





--- Updated ---

check the line code now its changed I am not using any timer to generate the PWM
 
Last edited by a moderator:

I added CODE tags to make it clearer.
There are still problems, try to understand how the IR2112 works and you will see your present design can not turn the top MOSFET on at all.
You still have not dealt with the situation where both switches are pressed at the same time.
Why do you repeat the commands to change QA1 and QA2 state with delays between them?

As far as i understand, what you are trying to do is have forward or reverse control of a DC motor by reversing the polarity of voltage fed to it and the polarity is set by pressing one of the switches. If I am correct, you can use the IR2112 but you must drive it with much faster pulses, at the moment it is only bursts of 500Hz, something at least 10 times faster would be more suitable. You also have to 'remember' the last direction so you can tell whether it is changing when the other switch is pressed and not just detect when it is pressed.

A much better solution would be to DC drive the H-Bridge. You might be able to drive the lower two MOSFETS directly from the PIC pins and use a level translator (a single transistor driver) to drive the top ones. The circuit would be much simpler, especially if you use "logic level" MOSFETs.

You could also consider using one of the many ICs designed as motor drivers, for example L293 or SN754410, they connect directly to the PIC pins and motor with no other components needed.

Brian.
 

PR2= 0xFF;
CCP1CON=0x3F;
CCP2CON=0x3F;
T2CON = 0x7F;

based these values i can able to generate the PWM but its included in the code with in IF condition as CCP1CON = 0x3f;its switiching will not be happen and motor is not running,those two register is specified outside while its switching will be happen eventhough the motor is not running biran
CCP1CON=0x3F;
CCP2CON=0x3F;

--- Updated ---

PR2= 0xFF;
CCP1CON=0x3F;
CCP2CON=0x3F;
T2CON = 0x7F;

based these values i can able to generate the PWM but its included in the code with in IF condition as CCP1CON = 0x3f;its switiching will happen but motor is not running.
C:
void main()
{
    TRISA = 0x00;
    TRISE = 0xFF;
     TRISC = 0x00;
      PR2= 0xFF;
    //  CCP1CON=0x3F;
   //      CCP2CON=0x3F;
      T2CON = 0x7F;
 
 
//   TRISE = 0x00;
  //   SW=0;SW1=0;
 while(1)
    {
 
     if (SW_Fwd== pressed)
        {
                    CCP1CON=0x3F;
        //  QA1 = 1;
         // __delay_ms(1);
        //  QA2= 0;
        //  __delay_ms(1);
        //QA1= 1;
        //__delay_ms(1);
           //   QA2 = 0;
        // __delay_ms(1);
       
        }
      if (SW_Rev  == pressed)
      {
                  CCP2CON=0x3F;
            // QA2 = 1;
        //  __delay_ms(1);
        //  QA1 = 0;
          //__delay_ms(1);
                   // QA2= 1;
             //  __delay_ms(1);
        //  QA1 = 0;
        //  __delay_ms(1);
     
           }  
     }
 }
--- Updated ---

I added CODE tags to make it clearer.
There are still problems, try to understand how the IR2112 works and you will see your present design can not turn the top MOSFET on at all.
You still have not dealt with the situation where both switches are pressed at the same time.
Why do you repeat the commands to change QA1 and QA2 state with delays between them?

As far as i understand, what you are trying to do is have forward or reverse control of a DC motor by reversing the polarity of voltage fed to it and the polarity is set by pressing one of the switches. If I am correct, you can use the IR2112 but you must drive it with much faster pulses, at the moment it is only bursts of 500Hz, something at least 10 times faster would be more suitable. You also have to 'remember' the last direction so you can tell whether it is changing when the other switch is pressed and not just detect when it is pressed.

A much better solution would be to DC drive the H-Bridge. You might be able to drive the lower two MOSFETS directly from the PIC pins and use a level translator (a single transistor driver) to drive the top ones. The circuit would be much simpler, especially if you use "logic level" MOSFETs.

You could also consider using one of the many ICs designed as motor drivers, for example L293 or SN754410, they connect directly to the PIC pins and motor with no other components needed.

Brian.

based on PWM code i want controll the Speed of the motor below code is sufficient
--- Updated ---

Code:
#include <htc.h>
#include <stdio.h>
#include <stdlib.h>
//#define release 0
//#define pressed 1
#define QA1 RA0
#define QA2 RA1
//#define QA3 RA2
//#define QA4 RA3
//#define SW_Fwd RE0
//#define SW_Rev RE1
#define _XTAL_FREQ 20000000
//__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_OFF & LVP_ON & CPD_OFF & WRT_OFF & CP_OFF);
__CONFIG(0x3F72);
//void forwarddirection();
//void reversedirection();
void main()
{
    TRISA = 0x00;
    TRISE = 0xFF;
     TRISC = 0x00;
      PR2= 0xFF;
    CCP1CON=0x3F;
  //     CCP2CON=0x3F;
      T2CON = 0x7F;
 
   
//   TRISE = 0x00;
  //   SW=0;SW1=0;
 while(1)
    {
      CCPR1L =5;//CCPR2L = 50;
//     if (SW_Fwd== pressed)
//        {
                ///    CCP1CON=0x3F;
       
        //  QA1 = 1;
         // __delay_ms(1);
        //  QA2= 0;
        //  __delay_ms(1);
        //QA1= 1;
        //__delay_ms(1);
           //   QA2 = 0;
        // __delay_ms(1);
       
//        }
//      if (SW_Rev  == pressed)
//      {
    //              CCP2CON=0x3F;
            // QA2 = 1;
        //  __delay_ms(1);
        //  QA1 = 0;
          //__delay_ms(1);
                   // QA2= 1;
             //  __delay_ms(1);
        //  QA1 = 0;
        //  __delay_ms(1);
     
         //  }    
     }
 }
--- Updated ---

PR2= 0xFF;
CCP1CON=0x3F;
CCP2CON=0x3F;
T2CON = 0x7F;

based these values i can able to generate the PWM but its included in the code with in IF condition as CCP1CON = 0x3f;its switiching will not be happen and motor is not running,those two register is specified outside while its switching will be happen eventhough the motor is not running biran
CCP1CON=0x3F;
CCP2CON=0x3F;

--- Updated ---

Code:
void main()
{
    TRISA = 0x00;
    TRISE = 0xFF;
     TRISC = 0x00;
      PR2= 0xFF;
    //  CCP1CON=0x3F;
   //      CCP2CON=0x3F;
      T2CON = 0x7F;
 
 
//   TRISE = 0x00;
  //   SW=0;SW1=0;
 while(1)
    {
 
     if (SW_Fwd== pressed)
        {
                    CCP1CON=0x3F;
        //  QA1 = 1;
         // __delay_ms(1);
        //  QA2= 0;
        //  __delay_ms(1);
        //QA1= 1;
        //__delay_ms(1);
           //   QA2 = 0;
        // __delay_ms(1);
     
        }
      if (SW_Rev  == pressed)
      {
                  CCP2CON=0x3F;
            // QA2 = 1;
        //  __delay_ms(1);
        //  QA1 = 0;
          //__delay_ms(1);
                   // QA2= 1;
             //  __delay_ms(1);
        //  QA1 = 0;
        //  __delay_ms(1);
   
           }  
     }
 }
--- Updated ---


based on PWM code i want controll the Speed of the motor below code is sufficient
--- Updated ---

Code:
#include <htc.h>
#include <stdio.h>
#include <stdlib.h>
//#define release 0
//#define pressed 1
#define QA1 RA0
#define QA2 RA1
//#define QA3 RA2
//#define QA4 RA3
//#define SW_Fwd RE0
//#define SW_Rev RE1
#define _XTAL_FREQ 20000000
//__CONFIG(FOSC_HS & WDTE_OFF & PWRTE_OFF & BOREN_OFF & LVP_ON & CPD_OFF & WRT_OFF & CP_OFF);
__CONFIG(0x3F72);
//void forwarddirection();
//void reversedirection();
void main()
{
    TRISA = 0x00;
    TRISE = 0xFF;
     TRISC = 0x00;
      PR2= 0xFF;
    CCP1CON=0x3F;
  //     CCP2CON=0x3F;
      T2CON = 0x7F;
 
 
//   TRISE = 0x00;
  //   SW=0;SW1=0;
 while(1)
    {
      CCPR1L =5;//CCPR2L = 50;
//     if (SW_Fwd== pressed)
//        {
                ///    CCP1CON=0x3F;
       
        //  QA1 = 1;
         // __delay_ms(1);
        //  QA2= 0;
        //  __delay_ms(1);
        //QA1= 1;
        //__delay_ms(1);
           //   QA2 = 0;
        // __delay_ms(1);
       
//        }
//      if (SW_Rev  == pressed)
//      {
    //              CCP2CON=0x3F;
            // QA2 = 1;
        //  __delay_ms(1);
        //  QA1 = 0;
          //__delay_ms(1);
                   // QA2= 1;
             //  __delay_ms(1);
        //  QA1 = 0;
        //  __delay_ms(1);
     
         //  }  
     }
 }
in the above code,how we can send it to HIN as high pulse alone and LIN in as Low pulse alone from the PWM output
 
Last edited by a moderator:

I am not using any timer to generate the PWM
yes, unfortunately...

and
* you don´t press the [ CODE ] button
* and you don´t explain why you repeat the port setting
* and you don´t comment your code
* and you don´t change the 100% duty cycle on HIN
* and you don´t change the FWD/REV problem
* and so on..

So we tell you what to do .. but you ignore it. I call our effort as a waste of time.

We say: you have to turn right .. next post you write: I did not turn right.

I have to leave...

Klaus
 

I will agree your comments
 

Cookies are required to use this site. You must accept them to continue using the site. Learn more…