dc motor help

LINIX Brushless, VEXTA Brushless, RC Servo, DC Geared, Linear, Stepper, Tamiya.....

dc motor help

Postby kwaidaangurl » Sat Oct 16, 2010 1:11 pm

hi there, i have problem on my dc.. i don't know whether it is my hardware or software.. i use md30b and sk40c from cytron.. when i run, the motor only moving forward direction only and ccw didnt move at all.. i attached my coding here.. hopefully someone can guide me.. thanks

CODE: SELECT_ALL_CODE
#include <p18f4550.h>
#include <delays.h>

//define RUN pins
#define RUN1   PORTDbits.RD1
#define RUN2   PORTDbits.RD2

//define CCW pins
#define CCW1   PORTDbits.RD0
#define CCW2   PORTDbits.RD3

//define PWM pins
#define PWM1   CCPR1L   //PIN RC2
#define PWM2   CCPR2L   //PIN RB3

// motor subroutine
void setup(void);
void setup_pwm (void);
void motors (unsigned char, unsigned char, char, char);   
void brake (void);
void turnleft (void);
void turnright (void);

//setup motors subroutine
void setup (void)
{
   TRISBbits.TRISB3 = 0;   //PWM2
   TRISCbits.TRISC2 = 0;   //PWM1
   TRISDbits.TRISD0 = 0;   //CCW1
   TRISDbits.TRISD1 = 0;   //RUN1
   TRISDbits.TRISD2 = 0;   //RUN2
   TRISDbits.TRISD3 = 0;   //CCW2
   
   RUN1   =   1;      //break motor 1
   CCW1   =   1;      //motor1 rotate in countclockwise
   PWM1   =   0;      //clear motor 1 PWM
   
   RUN2   =   1;      //break motor 2
   CCW2   =   0;      //motor2 rotate in clockwise
   PWM2   =   0;      //clear motor 2 PWM
}

//setup pwm (pulse width modulation) for motors
void setup_pwm (void)
{   //find 100 PR2, decimal = 100: hex = 0x64
   PR2      =   0x64;      //20.2 usec (20MHz osc) 49.5kHz (1 int period(4 cycle of clock) > calc 256 period > freq of 256 period)
   CCP1CON   =   0x0C;      //b'00001100' PWM1 activated
   CCP2CON   =   0x0C;      //PWM2 activated
   T2CON   =   0x04;      //b'00000100; TMR2 ON, Postscale 1:1, Prescaler = 1
}

// motors control subroutine
void motors (unsigned char speed1, unsigned char speed2, char direction1, char direction2)
{   //motor 1 = left motor :: motor 2 = right motor
   //examples : motors (200,200,1,1);      move robot forward
   //          motors (200,200,0,0);      move robot backward

   //declare motor speed
   PWM1 = speed1;
   PWM2 = speed2;
   
   //declare motors rotate direction
   if (direction1 == 0)   //setup motor1 direction
      CCW1 = 0;
   else
      CCW1 = 1;

   if (direction2 == 0)   //setup motor2 direction
      CCW2 = 1;
   else
      CCW2 = 0;
   
   RUN1 = 0;      //run motor1
   RUN2 = 0;      //run motor2
}

//brake  motors subroutine
void brake (void)         //brake both  motors
{
   //brake motors instatenously
   //example : brake();
   RUN1 = 1;
   RUN2 = 1;
   Delay10KTCYx(9);   //delay 10 ms for next command
}


void turnleft (void)
{
   motors (100,100,0,1);

}


void turnright (void)
{
   motors (100,100,1,0);
   }
kwaidaangurl
Greenhorn
 
Posts: 3
Joined: Sat Oct 16, 2010 1:01 pm

Re: dc motor help

Postby ober » Sun Oct 17, 2010 9:38 am

Please use proper method to share source code. Thanks.

Referring to the User's Manual of MD30B, the control method is different from what you are writing. I assume RUN1 and CCW1 is connected to one MD30B. MD30B uses 2 pin to control the state of motor. To activate the motor RUN1 and CCW1 must be different:

RUN1 = 0 ; CCW1 = 1; //motor activate in 1 direction
RUN1 = 1, CCW1 = 0; //motor activate in another direction

Of course the PWM must provide some duty cycle inorder for the motor to move. In MD30B User's Manual the pin is labeled as CW and CCW, there is no RUN pin.

To stop motor, both RUN1 and CCW1 should be in same logic state.
RUN1 = 0; CCW1 = 0; //motor brake to GND
RUN1 = 1; CCW1= 1; //motor brake to Vin

Simple way to test the MD30B is working or not, is to supply power and test the motor with 2 push button on board.
Ober Choo
Cytron Technologies Sdn Bhd
www.cytron.com.my
User avatar
ober
Moderator
 
Posts: 1486
Joined: Wed Apr 15, 2009 1:03 pm

Re: dc motor help

Postby kwaidaangurl » Sun Oct 17, 2010 12:29 pm

thanks for the reply ober...
i did some changes on my program.... and it succeed..and i used ccw and cw instead of run .

when i used md30b with adc, just to try on and off the motor.. the motor just move in ccw.. is it because my adc, it disabled the portd??? my led is working on and off...

md30b must give logic 0 to on and 1 to off it..right

i attached my sample code below.. hopefully u can help me..

CODE: SELECT_ALL_CODE
#include <p18f4550.h>
#include <delays.h>
#include <adc.h>

#define ccw PORTDbits.RD1
#define cw PORTDbits.RD0
#define pwm PORTCbits.RC2
#define led LATBbits.LATB7

#pragma config WDT = OFF
#pragma config LVP=OFF
#pragma config FOSC=HS

#define led   LATBbits.LATB7 // Define aliases for the output pins

int read_adc_channel( unsigned char channel){
      SetChanADC(channel);  // Connect the A/D converver to the chosen channel
      ConvertADC();        // Start the conversion
      while( BusyADC() )     // Wait for the conversion to finish.
      {                 // With the settings used in OpenADC() this takes
                       // Aquisition + conversion = 20 + 11 TAD
                       // 1/48 MHz * 64 * (20+11) = 41.3 microseconds                        
               ;// Doing nothing, if you have something usefull instead of
                // waiting, you can do it in this loop or before the while.
      }
      return ReadADC(); // Read the ADC result from the ADRESH and ADRESL
                    // registers and return the 10 bit result as an integer.
                    // The range is 0-1000 with 5 mV per bit.
}

//#pragma config CCP2MX = OFF         //select PWM2 at RB3

void maju(void);
void belakang (void);

int adcresult0;

void main ()
{   
   PORTD=1;
   TRISDbits.TRISD0=0; //ccw
   TRISDbits.TRISD1=0; //cw
   TRISCbits.TRISC2 = 0;   //PWM1


   TRISBbits.TRISB7 = 0;  // Set these pins as outputs
   
   TRISAbits.TRISA0 = 1;  // Set these pins as inputs
   //find 100 PR2, decimal = 100: hex = 0x64
   PR2      =   0x64;      //20.2 usec (20MHz osc) 49.5kHz (1 int period(4 cycle of clock) > calc 256 period > freq of 256 period)
   CCP1CON   =   0x0C;      //b'00001100' PWM1 activated
//   CCP2CON   =   0x0C;      //PWM2 activated
   T2CON   =   0x04;      //b'00000100; TMR2 ON, Postscale 1:1, Prescaler = 1

      OpenADC(ADC_FOSC_64 & ADC_RIGHT_JUST & ADC_20_TAD,
         ADC_CH0 & ADC_INT_OFF & ADC_VREFPLUS_VDD &                          ADC_VREFMINUS_VSS, 0b1100);
   // Open the ADC with 48 MHz/64 as clock, the result is right justified
   // The aquisition takes 20 periods
   // The initial channel is channel 0, no interrupt on completion of the conversion,
   // The reference voltages are VDD and VSS (5V and GND)
   // The last argument is the setting for the number of AD channels
   // See the PIC18F4550 datasheet page 260, PCFG3:PCFG0
   
   Delay10TCYx(5); //Necessary delay after opening the ADC

   while (1)

   {adcresult0 = read_adc_channel( ADC_CH0 );
      if(adcresult0>=512)
      // && = AND, so 300 < result < 600
      {   
         led = 1;
   maju();
         
      }   
      else
   {
   led = 0;
   belakang();
   }   
   }
}
void maju (void)
{   pwm=100;
   PORTDbits.RD0=0;
   Delay10KTCYx(200);
   PORTDbits.RD0=1;
   Delay10KTCYx(200);
}

void belakang (void)
{
   pwm=25;
   PORTDbits.RD1=0;
   Delay10KTCYx(200);
   PORTDbits.RD1=1;
   Delay10KTCYx(200);
   


}
kwaidaangurl
Greenhorn
 
Posts: 3
Joined: Sat Oct 16, 2010 1:01 pm

Re: dc motor help

Postby robosang » Mon Oct 18, 2010 9:12 pm

Ya, I can help you to check, but might need to wait until next month or even next year :lol:
robosang
Expert
 
Posts: 1239
Joined: Wed Jun 10, 2009 5:37 pm

Re: dc motor help

Postby kwaidaangurl » Mon Oct 18, 2010 11:19 pm

ok thanks.. :)
kwaidaangurl
Greenhorn
 
Posts: 3
Joined: Sat Oct 16, 2010 1:01 pm

Re: dc motor help

Postby ober » Tue Oct 19, 2010 9:05 am

Hi, din go through the code truly. Normally we need hardware to verify the workable of the code. Anyway from the routine that control motor, it seem that you have define PWM to RC2 pin. In other words, PWM is RC2, and RC2 is a just a bit that point to RC2 hardware pin, not a 8-bit register that can store large value. It can only be 1 or 0 for RC2. Another problem is, since you have activate the PWM, changing the logic value in RC2 will not change anything at PWM output.

To change the value of PWM output, you need to change the value in CCPR1L.

Try to debug slowly, do a simple function instead of complex function. and from a working function, expand it, add another function and slowly debug it.
Ober Choo
Cytron Technologies Sdn Bhd
www.cytron.com.my
User avatar
ober
Moderator
 
Posts: 1486
Joined: Wed Apr 15, 2009 1:03 pm

Re: dc motor help

Postby shahrul » Tue Oct 19, 2010 10:22 am

It's hard checking coding without hardware, that why no anybody will see the overall code. Other people just can tell the concept.
User avatar
shahrul
Professional
 
Posts: 812
Joined: Sat May 16, 2009 9:54 pm
Location: Selangor


Return to DC Motor

Who is online

Users browsing this forum: No registered users and 20 guests